我尝试弄清楚如何实现 Visual Studio 代码扩展。 (基于“Hello World!”示例。)
我想要执行以下操作:
- 在 Node.js child_process 的编辑器中执行 example.cmd 文件(有效)
- 但在此之前,自动保存文件以激活最新的更改(这就是问题所在)
在https://code.visualstudio.com/Docs/extensionAPI/vscode-api有Visual Studio Code api的描述。
这是我认为要使用的命令:
executeCommand<T>(command: string, ...rest: any[]): Thenable<T>
我需要什么:
文件保存是异步的。例如。可能会出现一个文件保存对话框。用户可以正确保存或取消。我需要等到用户操作结束,然后在成功的情况下调用我的命令提示符。
在我的代码中:
let disposable = vscode.commands.registerCommand('extension.sayHello', () => {
....
var retval =
vscode.commands.executeCommand('workbench.action.files.save')
.then(function(result)
{
// It should wait til end of user action,
// But it never reach here
myOutputChannel.append('workbench.action.files.save:' + result + '\n');
});
// and immediately runs the child process code below
....
});
会发生什么: 它运行代码,不等待,不保存,处理不存在的文件,报告错误,退出函数。出现所有文件保存对话框后。 :(
谁能告诉我出了什么问题吗?这是 Visual Studio 代码的错误吗?或者我做错了什么?我是 node.js 的新手。我想,我不知道如何使用Thenable<T>
正确吗?
最佳答案
以下是有关如何正确保存文件的简短片段:
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
let doc = editor.document;
await editor.document.save();
关于node.js - Visual-studio-code:如何执行 Command<T>(command: string, ...rest: any[]): Thenable<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36069045/