Skip to content

Commit 3453361

Browse files
authored
feat: Add option to disable code actions (#1849)
Code actions require additional analysis and developers may want to disable them for performance reasons. Resolves #1840
1 parent ca5212c commit 3453361

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

client/src/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ function constructArgs(
452452
if (disableAutomaticNgcc) {
453453
args.push('--disableAutomaticNgcc');
454454
}
455+
const disableCodeActions = config.get<boolean>('angular.disableCodeActions');
456+
if (disableCodeActions) {
457+
args.push('--disableCodeActions');
458+
}
455459

456460
const forceStrictTemplates = config.get<boolean>('angular.forceStrictTemplates');
457461
if (forceStrictTemplates) {

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
"default": false,
147147
"markdownDescription": "Disable the step to automatically run ngcc. [ngcc](https://github.com/angular/angular/blob/main/packages/compiler/design/architecture.md#high-level-proposal) is required to run and gather metadata from libraries not published with Ivy instructions. This can be run outside of VSCode instead (for example, as part of the build/rebuild in the CLI). Note that ngcc needs to run not only at startup, but also whenever the dependencies change. Failing to run ngcc when required can result in incomplete information and spurious errors reported by the language service."
148148
},
149+
"angular.disableCodeActions": {
150+
"type": "boolean",
151+
"default": false,
152+
"markdownDescription": "Disable code actions in Angular contexts, including quick fixes in template files which add missing imports. Some code actions require global project analysis, so it may be desirable to disable them for performance reasons."
153+
},
149154
"angular.forceStrictTemplates": {
150155
"type": "boolean",
151156
"default": false,

server/src/cmdline_utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ interface CommandLineOptions {
3636
* If true, skips the running ngcc when using Ivy LS.
3737
*/
3838
disableAutomaticNgcc: boolean;
39+
disableCodeActions: boolean;
3940
logFile?: string;
4041
logVerbosity?: string;
4142
logToConsole: boolean;
@@ -52,6 +53,7 @@ export function parseCommandLine(argv: string[]): CommandLineOptions {
5253
help: hasArgument(argv, '--help'),
5354
ivy: !hasArgument(argv, '--viewEngine'),
5455
disableAutomaticNgcc: hasArgument(argv, '--disableAutomaticNgcc'),
56+
disableCodeActions: hasArgument(argv, '--disableCodeActions'),
5557
logFile: findArgument(argv, '--logFile'),
5658
logVerbosity: findArgument(argv, '--logVerbosity'),
5759
logToConsole: hasArgument(argv, '--logToConsole'),

server/src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function main() {
4444
resolvedNgLsPath: ng.resolvedPath,
4545
ivy: isG3 ? true : options.ivy,
4646
disableAutomaticNgcc: options.disableAutomaticNgcc || options.untrustedWorkspace,
47+
disableCodeActions: options.disableCodeActions,
4748
logToConsole: options.logToConsole,
4849
includeAutomaticOptionalChainCompletions: options.includeAutomaticOptionalChainCompletions,
4950
includeCompletionsWithSnippetText: options.includeCompletionsWithSnippetText,

server/src/session.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface SessionOptions {
3131
resolvedNgLsPath: string;
3232
ivy: boolean;
3333
disableAutomaticNgcc: boolean;
34+
disableCodeActions: boolean;
3435
logToConsole: boolean;
3536
includeAutomaticOptionalChainCompletions: boolean;
3637
includeCompletionsWithSnippetText: boolean;
@@ -115,7 +116,7 @@ export class Session {
115116
}
116117
});
117118

118-
this.addProtocolHandlers(this.connection);
119+
this.addProtocolHandlers(this.connection, options);
119120
this.projectService = this.createProjectService(options);
120121
}
121122

@@ -183,8 +184,8 @@ export class Session {
183184
return projSvc;
184185
}
185186

186-
private addProtocolHandlers(conn: lsp.Connection) {
187-
conn.onInitialize(p => this.onInitialize(p));
187+
private addProtocolHandlers(conn: lsp.Connection, options: SessionOptions) {
188+
conn.onInitialize(p => this.onInitialize(p, options));
188189
conn.onDidOpenTextDocument(p => this.onDidOpenTextDocument(p));
189190
conn.onDidCloseTextDocument(p => this.onDidCloseTextDocument(p));
190191
conn.onDidChangeTextDocument(p => this.onDidChangeTextDocument(p));
@@ -206,8 +207,10 @@ export class Session {
206207
conn.onCodeLens(p => this.onCodeLens(p));
207208
conn.onCodeLensResolve(p => this.onCodeLensResolve(p));
208209
conn.onSignatureHelp(p => this.onSignatureHelp(p));
209-
conn.onCodeAction(p => this.onCodeAction(p));
210-
conn.onCodeActionResolve(p => this.onCodeActionResolve(p));
210+
if (!options.disableCodeActions) {
211+
conn.onCodeAction(p => this.onCodeAction(p));
212+
conn.onCodeActionResolve(p => this.onCodeActionResolve(p));
213+
}
211214
}
212215

213216
private onCodeAction(params: lsp.CodeActionParams): lsp.CodeAction[]|null {
@@ -707,7 +710,8 @@ export class Session {
707710
return project;
708711
}
709712

710-
private onInitialize(params: lsp.InitializeParams): lsp.InitializeResult {
713+
private onInitialize(params: lsp.InitializeParams, options: SessionOptions):
714+
lsp.InitializeResult {
711715
this.snippetSupport =
712716
params.capabilities.textDocument?.completion?.completionItem?.snippetSupport;
713717
const serverOptions: ServerOptions = {
@@ -741,7 +745,7 @@ export class Session {
741745
workspace: {
742746
workspaceFolders: {supported: true},
743747
},
744-
codeActionProvider: this.ivy ? {
748+
codeActionProvider: (this.ivy && !options.disableCodeActions) ? {
745749
resolveProvider: true,
746750
// Now the Angular code action provider only supports `QuickFix`. If leave the
747751
// `codeActionKinds` empty, all action requests will be sent to the Angular language
@@ -752,7 +756,7 @@ export class Session {
752756
// [here](https://github.com/angular/vscode-ng-language-service/issues/1828)
753757
codeActionKinds: [lsp.CodeActionKind.QuickFix],
754758
} :
755-
undefined,
759+
undefined,
756760
},
757761
serverOptions,
758762
};

0 commit comments

Comments
 (0)