How to open untitled text file and paste formatted text using VSCode API in typescript

How to open a new read-only text file on a tab and paste formatted text into it using the studio visual code API.

I did not find any example regarding this to add plain text

Below is my code that opens some untitled file.

var setting: vscode.Uri = vscode.Uri.parse("untitled:" + "C:\summary.txt");

    vscode.workspace.openTextDocument(setting).then((a: vscode.TextDocument) => {


        vscode.window.showTextDocument(a, 1, false);


    }, (error: any) => {
        console.error(error);
        debugger;
    });

Please provide a simple example that you can add to these lines to add text. Since official examples are a little complicated.

+4
source share
1 answer

The following should give you an idea

...
var setting: vscode.Uri = vscode.Uri.parse("untitled:" + "C:\summary.txt");
vscode.workspace.openTextDocument(setting).then((a: vscode.TextDocument) => {
    vscode.window.showTextDocument(a, 1, false).then(e => {
        e.edit(edit => {
            edit.insert(new vscode.Position(0, 0), "Your advertisement here");
        });
    });
}, (error: any) => {
    console.error(error);
    debugger;
});
...
+7
source

All Articles