【问题标题】:How to close TextDocument in VS Code?如何在 VS Code 中关闭 TextDocument?
【发布时间】:2017-06-24 05:17:50
【问题描述】:

我正在编写 vscode 扩展。

我用vscode.window.showTextDocument 打开了一个TextDocument,想稍后再关闭它。但我找不到 api 来关闭它。最后我找到了this commit,它删除了closeTextDocument。我现在该怎么办?

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    我遇到了同样的问题。我设法做到这一点的唯一方法是通过workbench.action.closeActiveEditor,正如TextEditor.hide 的内联文档所推荐的那样。

    这很 hackish - 基本上是在编辑器中显示文档,然后关闭活动编辑器:

    vscode.window.showTextDocument(entry.uri, {preview: true, preserveFocus: false})
        .then(() => {
            return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
        });
    

    【讨论】:

    • showTextDocument() 返回一个Thenable<TextEditor> 那么为什么需要调用命令而不是使用promise 中的值呢?即then((textEditor) => textEditor.hide())
    • @Sebastian 因为textEditor.hide 已被弃用。根据 VSCode 文档: > 改用命令 workbench.action.closeActiveEditor。此方法显示意外行为,将在下一次重大更新中删除。
    • 您能否阻止或以编程方式回答“我应该保存更改吗?”的问题
    猜你喜欢
    • 1970-01-01
    • 2019-02-06
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2021-02-03
    • 2019-06-17
    • 2018-01-31
    相关资源
    最近更新 更多