VSCode 插件开发入门:脚手架 / 进度提示框 / 获取安装根路径
VSCode 插件开发入门:脚手架 / 进度提示框 / 获取安装根路径
一、用官方脚手架创建插件
# 安装 Yeoman 与官方插件生成器
npm install -g yo generator-code
# 运行生成器,按提示选择插件类型、语言(TS/JS)等
yo code
生成后用 VSCode 打开项目,按 F5 即可启动一个加载了该插件的「扩展开发宿主」窗口进行调试。
二、长任务进度提示框
通过 vscode.window.withProgress 在通知区域显示带进度的提示,支持取消:
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "I am long running!",
cancellable: true
}, (progress, token) => {
token.onCancellationRequested(() => {
console.log("User canceled the long running operation");
});
progress.report({ increment: 0 });
setTimeout(() => {
progress.report({ increment: 10, message: "still going..." });
}, 1000);
setTimeout(() => {
progress.report({ increment: 40, message: "still going even more..." });
}, 2000);
setTimeout(() => {
progress.report({ increment: 50, message: "almost there..." });
}, 3000);
return new Promise(resolve => setTimeout(resolve, 5000));
});
location还可选ProgressLocation.Window(状态栏)、ProgressLocation.SourceControl。increment是「增量」百分比,累加到 100。
三、读取 VSCode 安装根路径下的 product.json
function getProductConfiguration(): IProductConfiguration {
const content = fs.readFileSync(path.join(vscode.env.appRoot, 'product.json')).toString();
return JSON.parse(content) as IProductConfiguration;
}
vscode.env.appRoot 指向 VSCode 安装目录,可据此读取 product.json 等内置配置文件。