命令
配置详情
本节描述了扩展中新增的命令,这些命令可以在开发环境中实现增强的交互和自动化。这些命令可以通过 API 以编程方式使用。
package.json
示例
此示例展示了如何将新命令添加到 package.json
,从而使其可在扩展中使用。每个命令都定义了唯一的标识符和在命令面板中显示的描述性标题。
{
"contributes": {
"commands": [
{
"command": "extension.exampleCommand",
"title": "Extension: Example Command"
},
{
"command": "extension.anotherCommand",
"title": "Extension: Another Command"
}
]
}
}
在 TypeScript 代码中,您可以这样使用这些命令:
const exampleCommand = extensionApi.commands.registerCommand('extension.exampleCommand', async () => {
// Implementation logic here
console.log('Executing Example Command');
});
const anotherCommand = extensionApi.commands.registerCommand('extension.anotherCommand', () => {
// Synchronous logic can be used if async processing is not required
console.log('Another Command Executed');
});
JSON Schema
{
"contributes": {
"commands": [
{
"command": "string",
"title": "string",
"category": "string (optional cateogry for prefix title)",
"enablement": "myProperty === myValue"
}
]
}
}
附加资源
添加命令后,它将列在命令面板上。有关详细信息,请参阅命令面板。
验证
要验证您的命令是否按预期工作:
- 在您的开发环境中安装扩展。
- 将命令添加到
package.json
。 - 执行命令并检查预期的输出/日志记录。