在 vscode 扩展名的自定义编辑器中打开文本文档

Open text document in custom editor from vscode extension

我正在开发一个 Visual Studio 打开 webview 自定义编辑器的代码扩展。

其中一部分是提示用户输入文件名、创建文件然后打开它的命令。

看起来像这样:

window
  .showInputBox({
    prompt: "Enter name for file",
  })
  .then((title) => {
    if (!title) {
      return;
    }

    const fileContent = generateDefaultFileContent();
    const filePath = path.join(folder.uri.path, `${title}.custom_file_format`);

    workspace.fs
      .writeFile(
        folder.uri.with({
          path: filePath,
        }),
        Buffer.from(fileContent, "utf8")
      )
      .then(() => {
        workspace.openTextDocument(filePath).then((doc) =>
          window.showTextDocument(doc, {
            viewColumn: ViewColumn.Active,
          })
        );
      });
  });

文件已正确创建,但是调用 window.showTextDocument 在文本编辑器中打开编辑器,而不是我注册的自定义编辑器(在上面的示例中,自定义编辑器将打开 .custom_file_format 文件).如果您在文件资源管理器中单击新创建的文件,它将在自定义编辑器中打开。

有没有办法让它在自定义编辑器中打开新文件?

事实证明这可以用...

commands.executeCommand(
  "vscode.openWith",
  folder.uri.with({
    path: filePath,
  }),
  MyCustomEditor.viewType
);