【发布时间】:2021-12-31 15:20:12
【问题描述】:
ProvideCodeLens 声明:
此调用应尽快返回,如果计算命令成本高昂,则实施者应仅返回具有范围集的代码镜头对象并实施解析。
我对 API 的异步网络调用返回范围和我放入 CodeLens 的信息,例如:
[{
"stenographyResult": {
"data": "...",
},
"startPosition": {
"row": 9,
"column": 3
},
"endPosition": {
"row": 11,
"column": 4
}
}]
其中startPosition 和endPosition 分别是代码块的开始行和结束行,data 用于命令。
这似乎成为一个阻塞调用,如果此调用需要时间返回,VSC 将停止,并导致 VSC 上的其他功能(如文件创建)停止。
如果进行异步调用,返回 array 的 ranges 和类似上面的命令,我们怎样才能使这个调用不停止整个编辑器,最好使用 resolveCodeLens(但我们不知道范围直到网络调用完成)
相关代码:
public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {
if (vscode.workspace.getConfiguration().get('stenography.codeLensMode')) {
...
return vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
cancellable: false,
title: 'Fetching from Stenography Autopilot'
}, async (progress) => {
try {
progress.report({ increment: 0 });
return fetchStenographyAutopilot(STENOGRAPHY_API_KEY!, document.getText(), language, false).then((data: any) => {
try {
...
data.code_blocks.forEach((block: any) => {
var firstLine = document.lineAt(block.startPosition.row - 1);
var textRange = new vscode.Range(firstLine.range.start, firstLine.range.end);
let command = {
title: "<stenography autopilot />",
tooltip: block.stenographyResult.pm,
command: "stenography.codelensAction",
arguments: [block.stenographyResult.pm]
};
this.codeLenses.push(new vscode.CodeLens(textRange, command));
this.cache.codeLensCache[filename]!.push({
boundTo: block.stenographyResult.code,
command: command
});
});
return this.codeLenses;
}
...
}
完整文件:https://github.com/StenographyDev/autopilot-vsc/blob/main/src/CodeLensProvider.ts
【问题讨论】:
标签: typescript visual-studio-code async-await