Set language of a text document in a VSCode extension
Since VSCode 1.28 (September 2018), it's also possible to set the language mode for a document after it has been created using languages.setTextDocumentLanguage()
:
Set (and change) the language that is associated with the given document.
Note that calling this function will trigger the
onDidCloseTextDocument
event followed by theonDidOpenTextDocument
event.
Here's a simple example that opens a document containing {}
and sets the language to JSON:
vscode.workspace.openTextDocument({content: "{}"}).then(document => {
vscode.window.showTextDocument(document);
vscode.languages.setTextDocumentLanguage(document, "json");
});
I found a solution by myself:
let options: Object = {
content: string,
language: "sql"
};
vscode.workspace.openTextDocument(options).then(doc => {
vscode.window.showTextDocument(doc, vscode.ViewColumn.One);
}, err => {
vscode.window.showErrorMessage(err);
});
A solution when using TextDocumentContentProvider
seems not to be possible.
The commit with my change