Skip to content

Commit fa5212f

Browse files
committed
fix: avoid showing MISSING: command for code lens in templates (#1370)
When we do not provide a command in the code lens resolve request, VSCode will show `!!MISSING: command!!`. Instead, we throw an error and VSCode will simply show "no commands". It is documented that the resolution of a code lens can throw an error here: https://microsoft.github.io/language-server-protocol/specification#codeLens_resolve In addition, this change ensures that the html file is part of an angular project before returning a code lens in the first place. Fixes #1368 (cherry picked from commit cd35a52)
1 parent f83b02e commit fa5212f

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

server/src/session.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export class Session {
290290
}
291291

292292
private onCodeLens(params: lsp.CodeLensParams): lsp.CodeLens[]|undefined {
293-
if (!params.textDocument.uri.endsWith('.html')) {
293+
if (!params.textDocument.uri.endsWith('.html') || !this.isInAngularProject(params)) {
294294
return undefined;
295295
}
296296
const position = lsp.Position.create(0, 0);
@@ -306,24 +306,21 @@ export class Session {
306306
}
307307

308308
private onCodeLensResolve(params: lsp.CodeLens): lsp.CodeLens {
309-
const lsInfo = this.getLSAndScriptInfo(params.data);
310-
if (lsInfo === undefined) {
311-
return params;
312-
}
313-
const project = this.getDefaultProjectForScriptInfo(lsInfo.scriptInfo);
314-
// If the language service is disabled, the angular command will not be available.
315-
if (!project?.languageServiceEnabled) {
316-
return params;
317-
}
318-
319309
const components = this.onGetComponentsWithTemplateFile({textDocument: params.data});
320-
if (components !== undefined && components.length > 0) {
321-
params.command = {
322-
command: 'angular.goToComponentWithTemplateFile',
323-
title: components.length > 1 ? `Used as templateUrl in ${components.length} components` :
324-
'Go to component',
325-
};
326-
}
310+
if (components === undefined || components.length === 0) {
311+
// While the command is supposed to be optional, vscode will show `!!MISSING: command!!` that
312+
// fails if you click on it when a command is not provided. Instead, throwing an error will
313+
// make vscode show the text "no commands" (and it's not a link).
314+
// It is documented that code lens resolution can throw an error:
315+
// https://microsoft.github.io/language-server-protocol/specification#codeLens_resolve
316+
throw new Error(
317+
'Could not determine component for ' + (params.data as lsp.TextDocumentIdentifier).uri);
318+
}
319+
params.command = {
320+
command: 'angular.goToComponentWithTemplateFile',
321+
title: components.length > 1 ? `Used as templateUrl in ${components.length} components` :
322+
'Go to component',
323+
};
327324
return params;
328325
}
329326

0 commit comments

Comments
 (0)