1

I'm creating a VSCode CustomTextEditor extension. The extension activates on YAML files, but by design only supports a small subset of them. It's not possible to reliably detect supported files by file name/extension or the first line of the file.

I want the extension to reopen the unsupported files in VSCode's standard text editor so that the user experience is not degraded.

I've tried to use the "workbench.action.reopenWithEditor" command, it just shows the editor selection menu that the user must notice and click.

I've tried to use the "vscode.openWith" command to open the file in the default text editor. The file opens, but it does not replace the original custom editor tab which remains blank.

How can I programmatically reopen the current file in the default text editor?

reopen

current

  public async resolveCustomTextEditor(
    document: vscode.TextDocument,
    webviewPanel: vscode.WebviewPanel,
    _token: vscode.CancellationToken
  ): Promise<void> {
    const documentText = document.getText();
    if (!documentText.match(/implementation:\s*graph:/))) {
      vscode.commands.executeCommand(
        "vscode.openWith",
        document.uri,
        "default",
        webviewPanel.viewColumn
      );
      // Not loading HTML into the webview
      return;
    }
    ...
  }
Ark-kun
  • 6,358
  • 2
  • 34
  • 70

1 Answers1

0

You can use the following command to close the active webview editor before opening the default one:

vscode.commands.executeCommand('workbench.action.closeActiveEditor')
Sara
  • 469
  • 2
  • 8
  • 24