【问题标题】:Get notification in a VS Code extension when the Python interpreter is changed更改 Python 解释器时在 VS 代码扩展中获取通知
【发布时间】:2023-01-31 16:31:41
【问题描述】:

我正在编写一个依赖于当前设置的 Python 解释器的 VS 代码扩展。当我通过 VS 代码 UI 更改 Python 解释器时,扩展需要刷新并获取最新的 Python 路径(主要是为了在 TreeView 中显示正确的环境设置)。现在,我的自定义 TreeView 中有一个刷新按钮,我需要在选择不同的 Python 解释器后按下它。

但是,这是第二个手动步骤。当用户更改 Python 解释器时,有没有办法在我的扩展中获得通知,例如,扩展可以监听的事件?

我只找到了 VS Code 的激活事件,但看起来这不会有帮助。我没有发现执行命令python.setInterpreter 后触发的任何其他事件

【问题讨论】:

  • 为python.setInterpreter设置设置更改回调

标签: visual-studio-code vscode-extensions python-extensions


【解决方案1】:

终于找到了。要注意的正确配置是python.defaultInterpreterPath

vscode.workspace.onDidChangeConfiguration(event => {
    let affected = event.affectsConfiguration("python.defaultInterpreterPath");
    if (affected) {
        doSomething();
    }
});

要支持 usingNewInterpreterStorage 案例(今天默认),请添加:

const extension = vscode.extensions.getExtension('ms-python.python')!;
await extension.activate();
extension.exports.settings.onDidChangeExecutionDetails((event: any) => {
    doSomething();
});

【讨论】:

  • 它工作了一段时间,但在另一台机器上没有。看起来当 extension.packageJSON.featureFlags.usingNewInterpreterStorage 是 true 时,更改解释器路径 onDidChangeConfiguration 不会被触发(假设)
猜你喜欢
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2020-03-13
  • 2023-01-31
相关资源
最近更新 更多