在启用扩展时,如何默认执行VS代码命令?
当在VS代码中启用扩展时,如何默认执行命令?
我正在创建我的第一个VS代码扩展名,并且我创建了一个命令,但是它仅在命令调色板中可执行。
在VS代码中启用后,如何使其立即执行?
let myStatusBarItem: vscode.StatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left,0);
export function activate(context: vscode.ExtensionContext) {
let countLines = vscode.commands.registerCommand('line-counter.countLines', () => {
vscode.window.showInformationMessage('Hello from line-counter');
});
context.subscriptions.push(countLines);
context.subscriptions.push(myStatusBarItem);
updateStatusBarItem();
}
function updateStatusBarItem(): void {
const n = getNumberOfSelectedLines(vscode.window.activeTextEditor);
if (n > 0) {
myStatusBarItem.text = `$(megaphone) ${n} line(s) selected`;
myStatusBarItem.show();
} else {
myStatusBarItem.hide();
}
}
function getNumberOfSelectedLines(editor: vscode.TextEditor | undefined): number {
let lines = 0;
if (editor) {
lines = editor.selections.reduce((prev, curr) => prev + (curr.end.line - curr.start.line), 0);
}
return lines;
}
How to make a command be executed by default when extension enabled in VS Code?
I am creating my first VS Code extension and I have created a command but it is only executable from the command palette.
How do I make it execute as soon as it is enabled in VS Code?
let myStatusBarItem: vscode.StatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left,0);
export function activate(context: vscode.ExtensionContext) {
let countLines = vscode.commands.registerCommand('line-counter.countLines', () => {
vscode.window.showInformationMessage('Hello from line-counter');
});
context.subscriptions.push(countLines);
context.subscriptions.push(myStatusBarItem);
updateStatusBarItem();
}
function updateStatusBarItem(): void {
const n = getNumberOfSelectedLines(vscode.window.activeTextEditor);
if (n > 0) {
myStatusBarItem.text = `$(megaphone) ${n} line(s) selected`;
myStatusBarItem.show();
} else {
myStatusBarItem.hide();
}
}
function getNumberOfSelectedLines(editor: vscode.TextEditor | undefined): number {
let lines = 0;
if (editor) {
lines = editor.selections.reduce((prev, curr) => prev + (curr.end.line - curr.start.line), 0);
}
return lines;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
激活函数最高级别的任何内容都将在激活时调用。
。
Anything on the top level of the activation function will be called on activation.
We do this at watermelon for our activity bar object.