Skip to content

Commit

Permalink
perf(language-service): quick exit if no code fixes can exist
Browse files Browse the repository at this point in the history
This is a performance optimization that would exit early when
code actions are requested, but we know Angular cannot provide fixes
based on the error codes.

Previously, we would unnecessarily compute and analyze the application
for semantic diagnostics.

This will be helpful for: angular/vscode-ng-language-service#2050
  • Loading branch information
devversion committed Jul 16, 2024
1 parent 2a71f05 commit 6504dc8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/language-service/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface NgLanguageService extends ts.LanguageService {
refactorName: string,
reportProgress: ApplyRefactoringProgressFn,
): ts.RefactorEditInfo | undefined;

hasCodeFixesForErrorCode(errorCode: number): boolean;
}

export function isNgLanguageService(
Expand Down
16 changes: 16 additions & 0 deletions packages/language-service/src/language_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,17 @@ export class LanguageService {
});
}

/**
* Performance helper that can help make quick decisions for
* the VSCode language server to decide whether a code fix exists
* for the given error code.
*
* Related context: https://github.com/angular/vscode-ng-language-service/pull/2050#discussion_r1673079263
*/
hasCodeFixesForErrorCode(errorCode: number): boolean {
return this.codeFixes.codeActionMetas.some((m) => m.errorCodes.includes(errorCode));
}

getCodeFixesAtPosition(
fileName: string,
start: number,
Expand All @@ -367,6 +378,11 @@ export class LanguageService {
return this.withCompilerAndPerfTracing<readonly ts.CodeFixAction[]>(
PerfPhase.LsCodeFixes,
(compiler) => {
// Fast exit if we know no code fix can exist for the given range/and error codes.
if (errorCodes.every((code) => !this.hasCodeFixesForErrorCode(code))) {
return [];
}

const templateInfo = getTemplateInfoAtPosition(fileName, start, compiler);
if (templateInfo === undefined) {
return [];
Expand Down
1 change: 1 addition & 0 deletions packages/language-service/src/ts_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
getSignatureHelpItems,
getOutliningSpans,
getTemplateLocationForComponent,
hasCodeFixesForErrorCode: ngLS.hasCodeFixesForErrorCode.bind(ngLS),
getCodeFixesAtPosition,
getCombinedCodeFix,
getTypescriptLanguageService,
Expand Down

0 comments on commit 6504dc8

Please sign in to comment.