要添加到上下文菜单,可以在 package.json 文件中指定命令和上下文菜单项。在contributes 块的commands 数组中,添加如下命令:
{
"command": "accurev.openFile",
"category": "AccuRev",
"title": "Open file"
}
然后从定义在例如menus 块的scm/resourceState/context 数组中的上下文菜单项中引用命令:
{
"command": "accurev.openFile",
"when": "scmProvider == accurev"
}
最后,为使用 Visual Studio Code 的内置 open 命令的新命令注册一个命令处理程序。这可以在扩展的初始 activate 函数期间运行的代码中完成:
globalState.disposables.push(vscode.commands.registerCommand('accurev.openFile', async (file: AccuRevFile) => {
try {
await vscode.commands.executeCommand('vscode.open', file.resourceUri);
}
catch(err) {
if (globalState) {
globalState.channel.appendLine(err);
}
}
}));
在此示例中,globalState 只是一个变量,用于跟踪扩展中的一些全局变量,包括将在扩展关闭时清理的一次性对象列表,以及写入日志文本的通道扩展名。