Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open file and insert text using the VSCode API

How can I open a new read-only text file in tab and insert some formatted text in that file using the Visual Studio Code API?

I didnt find any example regarding this to add simple text

Following 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 give the simple example that can be added to these lines to add the text. As the official examples are little complex.

like image 817
Shan Khan Avatar asked Jul 09 '16 08:07

Shan Khan


1 Answers

The following should give you the 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;
});
...
like image 56
DAXaholic Avatar answered Sep 22 '22 17:09

DAXaholic