简体   繁体   English

如何触发激活 vscode markdown 扩展

[英]How to trigger activation of the vscode markdown extension

In my VS Code extension I have some code that uses the built in Markdown extension.在我的 VS Code 扩展中,我有一些使用内置 Markdown 扩展的代码。 I capture a reference to it by registering as a markdown plugin and putting the following code at the end of my extension's activate method.我通过注册为 markdown 插件并将以下代码放在我的扩展程序激活方法的末尾来捕获对它的引用。

  return {
    extendMarkdownIt(mdparam: any) {
      return md = mdparam;
    }
  };

Markdown calls this when it activates. Markdown 在激活时调用它。

Generally this is not a problem.一般来说,这不是问题。 Most of the use cases for my extension involve a markdown file already loaded into the active editor, and the loading of this file triggers activation of the markdown extension.我的扩展的大多数用例涉及一个 markdown 文件已经加载到活动编辑器中,并且该文件的加载触发了 markdown 扩展的激活。

However there are some legitimate use cases in which this is not so.但是,在某些合法用例中并非如此。

I need to programmatically trigger activation of the markdown extension.我需要以编程方式触发 markdown 扩展的激活。 Some of these cases involve having a different kind of file open in the active editor so loading a markdown file into it is not an acceptable option.其中一些情况涉及在活动编辑器中打开不同类型的文件,因此将 markdown 文件加载到其中不是可接受的选项。

Some potential strategies:一些潜在的策略:

  1. Change the language mode.更改语言模式。 There is a command workbench.action.editor.changeLanguageMode but no documentation.有一个命令workbench.action.editor.changeLanguageMode但没有文档。 I tried我试过了
vscode.commands.executeCommand('workbench.action.editor.changeLanguageMode', 'md');

but this triggers the UI但这会触发 UI
在此处输入图像描述
so I tried a pattern I've seen in the parameters of other commands and added , true .所以我尝试了一种我在其他命令的参数中看到的模式并添加了, true This suppressed the UI but doesn't seem to work.这抑制了 UI,但似乎不起作用。

  1. Load a markdown file into a new editor then close it again.将 markdown 文件加载到新编辑器中,然后再次关闭它。 This should work, but it's ugly .这应该可以,但是很难看。
  2. Put something in the contributions section of my extension that changes the activation trigger for the markdown extension so that it is triggered by the other file types on which my extension operates.在我的扩展程序的贡献部分中添加一些内容,以更改 markdown 扩展程序的激活触发器,以便它由我的扩展程序运行的其他文件类型触发。

Of these options my favourite would be 3 but I don't even know whether this is even possible.在这些选项中,我最喜欢的是 3,但我什至不知道这是否可能。 Option 1 is hampered by the crappy (in many cases non-existent) documentation for vscode internal commands.选项 1 受到 vscode 内部命令的蹩脚(在许多情况下不存在)文档的阻碍。

Option 1 it is.选项1是。 If anyone knows how to do option 3 please tell, the solution below is a ghastly hack.如果有人知道如何执行选项 3,请告诉,下面的解决方案是一个可怕的 hack。

It is possible to trigger activation of the Markdown extension by changing the document language of any open editor to markdown.通过将任何打开的编辑器的文档语言更改为 markdown,可以触发 Markdown 扩展的激活。 In the event that there are no open editors a document with the markdown language set can be created in memory and loaded into an editor.如果没有打开的编辑器,可以在 memory 中创建具有 markdown 语言集的文档并将其加载到编辑器中。

If VS Code is busy loading extensions activation can take several hundred milliseconds so the best thing to do is watch the variable into which markdown-it is captured.如果 VS Code 正忙于加载扩展,激活可能需要数百毫秒,所以最好的办法是观察 markdown-it 被捕获到的变量。

The variable md is a global (global to my extension, not the whole of VS Code) into which a reference is acquired as shown in the question.变量md是一个全局变量(对我的扩展来说是全局的,而不是整个 VS 代码),如问题所示,获取了一个引用。

  let ed = vscode.window.activeTextEditor;
  if (ed) {
    let lid = ed.document.languageId;
    if (lid !== "markdown") {
      vscode.languages.setTextDocumentLanguage(ed.document, "markdown").then(
        function waitForMd() {
          if (md) {
            vscode.languages.setTextDocumentLanguage(ed!.document, lid);
          } else {
            setTimeout(waitForMd, 100);
          }
        }
      );
    }
  } else {
    vscode.workspace.openTextDocument({ language: "markdown" }).then(doc => {
      vscode.window.showTextDocument(doc).then(
        function waitForMd() {
          if (md) {
            vscode.commands.executeCommand("workbench.action.closeActiveEditor");
          } else {
            setTimeout(waitForMd, 100);
          }
        });
    });
  }

Once the capture completes we can restore the true language or close the editor as appropriate.捕获完成后,我们可以恢复真实语言或酌情关闭编辑器。 To be realistic the second case (no active editor) is unlikely because my own extension won't activate until you load something.实际上,第二种情况(没有活动编辑器)不太可能,因为我自己的扩展在您加载某些内容之前不会激活。 At any rate it works stably now.无论如何,它现在可以稳定运行。 The larger project is progressing nicely.更大的项目进展顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM