繁体   English   中英

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

[英]Open text document in custom editor from vscode extension

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

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

它看起来像这样:

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

暂无
暂无

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

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