我正在编写一个扩展程序,用于连接到API,下载一些文本,创建文件,将文本插入此文件并在编辑器上打开。
为此,我正在使用以下代码段:
vscode.workspace.openTextDocument(file_uri).then(function(doc){
vscode.window.showTextDocument(doc).then(function(editor){
vscode.window.showInformationMessage(editor);
console.log("Editor..."+editor);
editor.edit(function(editBuilder){
editBuilder.insert(0, template_payload);
//myEditBuilder(editBuilder, template_payload);
}).then(function(result){vscode.window.showInformationMessage(result);});
//editor.edit(function(editBuilder){myEditBuilder(editBuilder, template_payload);});
});
});
运行代码时,我收到以下错误和堆栈跟踪:
rejected promise not handled within 1 second: Error: Invalid arguments
extensionHostProcess.js:730
stack trace: Error: Invalid arguments
at new g (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:329:834)
at new g (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:327:254)
at h.insert (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:443:123)
at /Users/igor.simoes/VSCode_extension/appdvelocitytemplateupdater/extension.js:166:18
at f.edit (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:448:510)
at /Users/igor.simoes/VSCode_extension/appdvelocitytemplateupdater/extension.js:165:12
在我的代码行165上是我的editor.edit()
命令,它似乎没有缺少参数。
任何帮助将不胜感激。
答案 0 :(得分:0)
你有
editBuilder.insert(0, template_payload);
但editBuilder
是TextEditorEdit
函数,其insert
为:
insert(location: Position, value: string): void
您的第一个参数是0
,而不是Position
,而是(line: number, character: number)
,所以new Position(0,0)
是您想要的。
https://code.visualstudio.com/api/references/vscode-api#TextEditorEdit