You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode API:如何检测当前启动器及启动器变更事件?

通过VSCode API获取并跟踪当前启动器的方法

一、获取当前使用的启动器

1. 获取正在运行的调试启动器

如果当前有活跃的调试会话,可直接通过vscode.debug.activeDebugSession获取启动器的配置信息:

import * as vscode from 'vscode';

// 获取当前活跃的调试会话
const activeSession = vscode.debug.activeDebugSession;
if (activeSession) {
    // 启动器名称对应调试配置的name字段
    const currentLauncher = activeSession.configuration.name;
    console.log(`当前启动器:${currentLauncher}`);
}

2. 获取未启动调试时选中的启动器

VSCode官方API未直接提供未启动状态下获取选中启动器的方法,可通过读取工作区调试配置间接获取:

// 读取工作区所有调试启动器配置
const launchConfigs = vscode.workspace.getConfiguration('launch').get<Array<vscode.DebugConfiguration>>('configurations') || [];
// 可结合用户最近调试记录(需自行存储或推断)来定位选中的启动器

注:实际场景中,通常以活跃调试会话对应的启动器作为当前有效启动器。

二、跟踪启动器的变更时机

通过以下VSCode API事件可监听启动器的各类变更:

1. 监听活跃调试会话切换

当用户切换启动器并启动调试时,触发该事件:

// 监听活跃调试会话变更
vscode.debug.onDidChangeActiveDebugSession(session => {
    if (session) {
        const newLauncher = session.configuration.name;
        console.log(`启动器已变更为:${newLauncher}`);
    }
});

2. 监听调试会话的启动与结束

精准跟踪启动器的启动、结束时机:

// 监听调试会话启动
vscode.debug.onDidStartDebugSession(session => {
    console.log(`启动器 ${session.configuration.name} 已启动`);
});

// 监听调试会话结束
vscode.debug.onDidTerminateDebugSession(session => {
    console.log(`启动器 ${session.configuration.name} 已结束`);
});

3. 监听调试启动器配置修改

当用户新增、修改启动器配置时,触发该事件:

// 监听launch配置变更
vscode.workspace.onDidChangeConfiguration(e => {
    if (e.affectsConfiguration('launch')) {
        console.log('调试启动器配置已变更');
        // 重新读取更新后的启动器配置
        const updatedConfigs = vscode.workspace.getConfiguration('launch').get<Array<vscode.DebugConfiguration>>('configurations') || [];
    }
});

调试面板启动器选择界面示意图

调试面板启动器选择界面

内容的提问来源于stack exchange,提问作者Listener

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 04:01:18