简体   繁体   中英

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. 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.

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

Markdown calls this when it activates.

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.

However there are some legitimate use cases in which this is not so.

I need to programmatically trigger activation of the markdown extension. 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.

Some potential strategies:

  1. Change the language mode. There is a command workbench.action.editor.changeLanguageMode but no documentation. I tried
vscode.commands.executeCommand('workbench.action.editor.changeLanguageMode', 'md');

but this triggers the UI
在此处输入图像描述
so I tried a pattern I've seen in the parameters of other commands and added , true . This suppressed the UI but doesn't seem to work.

  1. Load a markdown file into a new editor then close it again. 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.

Of these options my favourite would be 3 but I don't even know whether this is even possible. Option 1 is hampered by the crappy (in many cases non-existent) documentation for vscode internal commands.

Option 1 it is. If anyone knows how to do option 3 please tell, the solution below is a ghastly hack.

It is possible to trigger activation of the Markdown extension by changing the document language of any open editor to 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.

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.

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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