Open text document in custom editor from vscode extension
Asked Answered
E

1

6

I'm developing a Visual Studio Code extension that opens a webview custom editor.

Part of this is a command that prompts the user for a filename, creates the file, and then opens it.

It looks something like this:

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

The file gets properly created, however the call to window.showTextDocument opens the editor in the text editor and not my registered custom editor (in the example above, the custom editor will open .custom_file_format files). If you click on the newly created file in the file explorer, it will open in the custom editor.

Is there a way to get it to open the new file in the custom editor?

Ethiopic answered 15/7, 2021 at 22:36 Comment(0)
E
6

Turns out this can be done with...

commands.executeCommand(
  "vscode.openWith",
  folder.uri.with({
    path: filePath,
  }),
  MyCustomEditor.viewType
);
Ethiopic answered 15/7, 2021 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.