我正在创建Visual Studio Code扩展,并已阅读该扩展,以便为StatusBarItem onclick事件提供功能,我需要register a command for the status bar item。我该怎么做呢?当我使用window.createStatusBarItem创建StatusBarItem时,我得到一个似乎没有任何字符串作为标识符的对象,但是命令注册过程需要将字符串作为第一个参数,并且我不确定如何将其与StatusBarItem关联。
我已经使用yeoman引导JavaScript扩展。
extension.js
const vscode = require('vscode');
const { exec } = require('child_process')
function activate(context) {
console.log('Thanks for installing me! Remember to add a script to run if you want this extension to do anything \
instersting at all :P');
// TODO: make status bar item clickable
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 1000);
function runCommandAndUpdateStatusBarItem () {
// TODO: fetch command from settings
const command = 'git describe --dirty="-$USER"';
exec(`cd ${vscode.workspace.rootPath} && ${command}`, [], (error, stdout, stderr) => {
if (stdout) {
statusBarItem.text = `$(git-commit) ${stdout}`;
statusBarItem.show();
}
else {
vscode.window.showErrorMessage(stderr);
statusBarItem.hide();
}
});
}
statusBarItem.hide();
let disposable = vscode.commands.registerCommand('extension.sayHello', runCommandAndUpdateStatusBarItem);
context.subscriptions.push(statusBarItem);
context.subscriptions.push(disposable);
}
exports.activate = activate;
function deactivate() {
console.log('Oh...so, you hate me? Ok I go away now :(');
}
exports.deactivate = deactivate;
package.json
{
"name": "script-result-info",
"displayName": "Script Result Info",
"description": "Runs a script in shell and puts the result in the statusBar",
"version": "0.0.1",
"publisher": "emilioidk",
"engines": {
"vscode": "^1.23.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onCommand:extension.sayHello"
],
"main": "./extension",
"contributes": {
"commands": [
{
"command": "extension.sayHello",
"title": "Hello world"
}
]
},
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"typescript": "^2.6.1",
"vscode": "^1.1.6",
"eslint": "^4.11.0",
"@types/node": "^7.0.43",
"@types/mocha": "^2.2.42"
}
}
最佳答案
当我使用window.createStatusBarItem创建StatusBarItem时,我得到一个似乎没有任何字符串作为标识符的对象
默认情况下它不是可选的,因为它是可选的,是的,但是您可以在其上设置command
:
statusBarItem.command = 'extension.sayHello';
检查API文档中的
StatusBarItem
:https://code.visualstudio.com/docs/extensionAPI/vscode-api#StatusBarItem