开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第23天,点击查看活动详情
// 为vscode提供一个调试器
"debuggers": [
{
"type": "my-debug-type", // ./vscode/launch.json 中的唯一ID
"label": "my-debug-label",// 在UI中显示的调试器名字
"program": "./out/debugAdapter.js", // 指向实现vscode调试协议的可执行程序
"runtime": "node",// 指向实现vscode调试协议的运行时
"languages": [ // 语言默认的调试器
"typescript"
],
"configurationAttributes": { // 启动配置参数的格式以及属性
"launch": {}
},
"initialConfigurations": [ // 填充launch.json的初始配置参数
{
"type": "my-debug-type",
"request": "launch",
"name": "initialConfigurations-name",
"program": ""
}
],
"configurationSnippets":[ // 当编辑launch.json时,列出来智能感知的调试配置
]
}
]
configurationSnippets
如下配置:
{
"label": "cc-plugin-debugger",
"description": "描述",
"body": {
"type": "node",
"request": "attach",
"port": 5263
}
}
当我们修改launch.json时,就会感知到这个调试器配置
注册配置并运行
当插件激活时,注册配置,这样在launch.json就能识别到这个type,这里的注册类型需要和launch.json中对应
const type = 'cc-plugin-debugger';
const provider = new MyDebugCfgProvider();
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider(type, provider));
当我们运行配置时:
就会触发resolveDebugConfiguration
import { CancellationToken, DebugConfiguration, DebugConfigurationProvider, ProviderResult, WorkspaceFolder } from 'vscode';
export class MyDebugCfgProvider implements DebugConfigurationProvider {
resolveDebugConfiguration(folder: WorkspaceFolder | undefined, debugConfiguration: DebugConfiguration, token?: CancellationToken | undefined): ProviderResult<DebugConfiguration> {
return debugConfiguration;
}
dispose() {
}
}
/**
* 为特定的调试类型注册调试配置提供程序。
* 可选的triggerKind可以用来指定何时触发提供程序的' provideDebugConfigurations '方法。
* 当前可能有两种触发器类型:带有' Initial '值(或者如果没有给定触发器类型参数)的' provideDebugConfigurations '方法用于提供要复制到新创建的launch.json中的初始调试配置。
* 对于触发器类型' Dynamic ', ' provideDebugConfigurations '方法用于动态地决定呈现给用户的调试配置(除了launch.json中的静态配置)。
* 请注意,' triggerKind '参数只适用于' provideDebugConfigurations '方法:所以' resolveDebugConfiguration '方法根本不受影响。
* 将单个提供者注册为针对不同触发器类型的解析方法,将导致多次调用相同的解析方法。
* 可以为同一类型注册多个提供者。
*
* @param type 提供程序为其注册的调试类型。
* @param provider 要注册的调试配置提供程序。
* @param triggerKind 为其注册提供程序的“提供调试配置”方法的触发器。如果“触发类型”缺失,则值“调试配置提供程序触发类型”。最初的假设。
* @return 释放时取消注册此提供程序的disposable对象。
*/
export function registerDebugConfigurationProvider(debugType: string, provider: DebugConfigurationProvider, triggerKind?: DebugConfigurationProviderTriggerKind): Disposable;
/**
* 为特定的调试类型注册一个调试适配器描述符工厂。
* 扩展只允许为扩展定义的调试类型注册调试适配器描述符工厂。
* 否则将抛出一个错误。
* 为一个调试类型注册多个调试适配器描述符工厂会导致错误。
*
* @param debugType 工厂注册的调试类型。
* @param factory 要注册的debug适配器描述符工厂。
* @return 丢弃时取消该工厂的注册。
只有注册了这个函数,才会调用DebugSession里面的initializeRequest
*/
export function registerDebugAdapterDescriptorFactory(debugType: string, factory: DebugAdapterDescriptorFactory): Disposable;
调试器初始化请求
protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {}
详细的参数含义:
const args = {
adapterID: 'cc-plugin-debugger',// 调试器的ID
clientID: 'vscode', // 使用调试器的客户端ID
clientName: 'Visual Studio Code',// 具有可读性的使用调试器客户端名字
columnsStartAt1: true,// 如果为true,所有的列号从1开始,默认为true
linesStartAt1: true,// 如果为true,所有的行号从1开始,默认为true
locale: 'en', // 客户端区域
pathFormat: 'path',// 路径的格式
supportsArgsCanBeInterpretedByShell: true,// 客户端支持“runInTerminal”请求的“argsCanBeInterpretedByShell”属性。
supportsInvalidatedEvent: true,// 客户端支持无效事件。
supportsMemoryEvent: true,// 支持内存事件
supportsMemoryReferences: true,// 客户端支持内存引用
supportsProgressReporting: true,// 客户端支持进度报告
supportsRunInTerminalRequest: true,// 客户端支持“runInTerminal”请求
supportsVariablePaging: true,// 客户端支持变量的分页属性
supportsVariableType: true, // 客户端支持变量的type属性
}
const res = {
body: {}, // 调试器的功能
command: 'initialize',
request_seq: 1,
seq: 0,
success: true,
type: 'response',
}