ProvideCodeLens states that:
This call should return as fast as possible and if computing the commands is expensive implementors should only return code lens objects with the range set and implement resolve.
My async network call to my API returns ranges and the info I put in the CodeLens, for example:
[{
"stenographyResult": {
"data": "...",
},
"startPosition": {
"row": 9,
"column": 3
},
"endPosition": {
"row": 11,
"column": 4
}
}]
where startPosition
and endPosition
are start and end lines of a code block respectively, and data
is used for command.
This seems to become a blocking call and VSC will stall if this call takes time to return, and causes other functions on VSC like file creation to halt.
If one makes an async call that returns an array
of ranges
and commands like above, how can we make this call not stall the entire editor, and ideally use resolveCodeLens
(but we don't know the ranges until the network call finishes)
Relevant code:
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;
}
...
}
Full file: https://github.com/StenographyDev/autopilot-vsc/blob/main/src/CodeLensProvider.ts