From ca023340d2a597fec732219154db396a41acabe6 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 14 Jun 2021 11:52:12 -0500 Subject: [PATCH 1/3] feat: send codeActionLiteralSupport Co-Authored-By: Alex Butler --- lib/auto-languageclient.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/auto-languageclient.ts b/lib/auto-languageclient.ts index d5cd70ff..4fd296e8 100644 --- a/lib/auto-languageclient.ts +++ b/lib/auto-languageclient.ts @@ -219,6 +219,11 @@ export default class AutoLanguageClient { }, codeAction: { dynamicRegistration: false, + codeActionLiteralSupport: { + codeActionKind: { + valueSet: [""], // TODO explicitly support more? + }, + }, }, codeLens: { dynamicRegistration: false, From 949e62452186db03291c74b10d2175b4c14cf40f Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 14 Jun 2021 11:58:14 -0500 Subject: [PATCH 2/3] feat: support custom filterCodeActions Co-Authored-By: Alex Butler --- lib/adapters/code-action-adapter.ts | 5 +++-- lib/auto-languageclient.ts | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/adapters/code-action-adapter.ts b/lib/adapters/code-action-adapter.ts index 2e6c2bc0..8ced702b 100644 --- a/lib/adapters/code-action-adapter.ts +++ b/lib/adapters/code-action-adapter.ts @@ -41,7 +41,8 @@ export default class CodeActionAdapter { linterAdapter: LinterPushV2Adapter | IdeDiagnosticAdapter | undefined, editor: TextEditor, range: Range, - linterMessages: linter.Message[] | atomIde.Diagnostic[] + linterMessages: linter.Message[] | atomIde.Diagnostic[], + filterActions: (actions: (Command | CodeAction)[] | null) => (Command | CodeAction)[] | null = (actions) => actions ): Promise { if (linterAdapter == null) { return [] @@ -49,7 +50,7 @@ export default class CodeActionAdapter { assert(serverCapabilities.codeActionProvider, "Must have the textDocument/codeAction capability") const params = createCodeActionParams(linterAdapter, editor, range, linterMessages) - const actions = await connection.codeAction(params) + const actions = filterActions(await connection.codeAction(params)) if (actions === null) { return [] } diff --git a/lib/auto-languageclient.ts b/lib/auto-languageclient.ts index 4fd296e8..5445840e 100644 --- a/lib/auto-languageclient.ts +++ b/lib/auto-languageclient.ts @@ -944,10 +944,16 @@ export default class AutoLanguageClient { this.getServerAdapter(server, "linterPushV2"), editor, range, - diagnostics + diagnostics, + this.filterCodeActions.bind(this) ) } + /** Optionally filter code action before they're displayed */ + protected filterCodeActions(actions: (ls.Command | ls.CodeAction)[] | null): (ls.Command | ls.CodeAction)[] | null { + return actions + } + public provideRefactor(): atomIde.RefactorProvider { return { grammarScopes: this.getGrammarScopes(), From 868f883ae729eaf0f32c208b1af3a2a4691c408d Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 14 Jun 2021 12:08:58 -0500 Subject: [PATCH 3/3] feat: support custom onApplyCodeActions Co-Authored-By: Alex Butler --- lib/adapters/code-action-adapter.ts | 11 ++++++++--- lib/auto-languageclient.ts | 11 ++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/adapters/code-action-adapter.ts b/lib/adapters/code-action-adapter.ts index 8ced702b..fc8c3a94 100644 --- a/lib/adapters/code-action-adapter.ts +++ b/lib/adapters/code-action-adapter.ts @@ -42,7 +42,8 @@ export default class CodeActionAdapter { editor: TextEditor, range: Range, linterMessages: linter.Message[] | atomIde.Diagnostic[], - filterActions: (actions: (Command | CodeAction)[] | null) => (Command | CodeAction)[] | null = (actions) => actions + filterActions: (actions: (Command | CodeAction)[] | null) => (Command | CodeAction)[] | null = (actions) => actions, + onApply: (action: Command | CodeAction) => Promise = () => Promise.resolve(true) ): Promise { if (linterAdapter == null) { return [] @@ -54,15 +55,19 @@ export default class CodeActionAdapter { if (actions === null) { return [] } - return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection)) + return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection, onApply)) } private static createCodeAction( action: Command | CodeAction, - connection: LanguageClientConnection + connection: LanguageClientConnection, + onApply: (action: Command | CodeAction) => Promise ): atomIde.CodeAction { return { async apply() { + if ((await onApply(action)) === false) { + return + } if (CodeAction.is(action)) { CodeActionAdapter.applyWorkspaceEdit(action.edit) await CodeActionAdapter.executeCommand(action.command, connection) diff --git a/lib/auto-languageclient.ts b/lib/auto-languageclient.ts index 5445840e..926e4dd1 100644 --- a/lib/auto-languageclient.ts +++ b/lib/auto-languageclient.ts @@ -945,7 +945,8 @@ export default class AutoLanguageClient { editor, range, diagnostics, - this.filterCodeActions.bind(this) + this.filterCodeActions.bind(this), + this.onApplyCodeActions.bind(this) ) } @@ -954,6 +955,14 @@ export default class AutoLanguageClient { return actions } + /** + * Optionally handle a code action before default handling. Return `false` to prevent default handling, `true` to + * continue with default handling. + */ + protected async onApplyCodeActions(_action: ls.Command | ls.CodeAction): Promise { + return true + } + public provideRefactor(): atomIde.RefactorProvider { return { grammarScopes: this.getGrammarScopes(),