在VSCode扩展中设置文本文档的语言

时间:2017-08-16 06:38:17

标签: visual-studio-code vscode-extensions

我有一个Visual Studio Code扩展,我尝试打开一个 虚拟编辑器:

vscode.workspace.openTextDocument(vscode.Uri.parse(previewSchema + ":" + path))

context.subscriptions.push(extractHibernateLogCommand, vscode.Disposable.from(
    vscode.workspace.registerTextDocumentContentProvider(previewSchema, hibernateExtractorProvider)
));

这些文件总是语言:纯文本。是否可以通过编程方式将其更改为“SQL”以获得正确的突出显示?

Full code

3 个答案:

答案 0 :(得分:2)

我自己找到了解决方案:

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);
});

使用TextDocumentContentProvider时的解决方案似乎无法实现。

The commit with my change

答案 1 :(得分:1)

Since VSCode 1.28(2018年9月),还可以为使用languages.setTextDocumentLanguage()创建的文档 之后设置文档的语言模式:

  

设置(并更改)与给定文档关联的language

     

请注意,调用此函数将触发onDidCloseTextDocument事件,然后触发onDidOpenTextDocument事件。

下面是一个简单的示例,该示例打开包含{}的文档并将语言设置为JSON:

vscode.workspace.openTextDocument({content: "{}"}).then(document => {
    vscode.window.showTextDocument(document);
    vscode.languages.setTextDocumentLanguage(document, "json");
});

答案 2 :(得分:0)

打开命令调色板(View-> Command Palette)
运行"配置语言特定设置"
在“选择语言”下拉列表中应该有一个SQL

的设置
相关问题