【问题标题】:Set language of a text document in a VSCode extension在 VSCode 扩展中设置文本文档的语言
【发布时间】:2018-01-24 04:28:44
【问题描述】:

我有一个 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

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    打开命令面板(视图->命令面板)
    运行“配置语言特定设置”
    在 Select Language 下拉菜单中应该有一个 SQL 设置

    【讨论】:

    • 最简单的手动操作是使用底部栏中的开关。但我想自动完成
    【解决方案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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 2018-12-12
        相关资源
        最近更新 更多