从vscode自定义文本编辑器扩展程序中重新打开默认编辑器中的文件

发布于 2025-01-30 20:05:37 字数 1016 浏览 3 评论 0原文

我正在创建VSCODE CustomTextEditor扩展程序。 扩展名在YAML文件上激活,但设计仅支持其中的一小部分。 不是可以通过文件名/扩展名或文件的第一行可靠地检测支持的文件。

我希望扩展名在VSCODE的标准文本编辑器中重新打开未支撑的文件,以使用户体验不会降低。

我尝试使用“ workbench.action.reopenwitheditor”命令,它仅显示用户必须注意的编辑器选择菜单,然后单击。

我尝试使用“ vscode.openwith”命令在默认文本编辑器中打开文件。该文件打开,但不能替换原始的自定义编辑器选项卡,该选项卡保持空白。

如何在默认文本编辑器中编程重新打开当前文件?

re rep open

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;
    }
    ...
  }

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;
    }
    ...
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷月断魂刀 2025-02-06 20:05:37

您可以使用以下命令在打开默认一个之前关闭活动的WebView编辑器:

vscode.commands.executeCommand('workbench.action.closeActiveEditor')

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

vscode.commands.executeCommand('workbench.action.closeActiveEditor')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文