TranquilMarmot
TranquilMarmot

Reputation: 2314

Open text document in custom editor from vscode extension

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?

Upvotes: 6

Views: 1791

Answers (1)

TranquilMarmot
TranquilMarmot

Reputation: 2314

Turns out this can be done with...

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

Upvotes: 6

Related Questions