Skip to content

Commit 868f883

Browse files
aminyaalexheretic
andcommitted
feat: support custom onApplyCodeActions
Co-Authored-By: Alex Butler <[email protected]>
1 parent 949e624 commit 868f883

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/adapters/code-action-adapter.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export default class CodeActionAdapter {
4242
editor: TextEditor,
4343
range: Range,
4444
linterMessages: linter.Message[] | atomIde.Diagnostic[],
45-
filterActions: (actions: (Command | CodeAction)[] | null) => (Command | CodeAction)[] | null = (actions) => actions
45+
filterActions: (actions: (Command | CodeAction)[] | null) => (Command | CodeAction)[] | null = (actions) => actions,
46+
onApply: (action: Command | CodeAction) => Promise<boolean> = () => Promise.resolve(true)
4647
): Promise<atomIde.CodeAction[]> {
4748
if (linterAdapter == null) {
4849
return []
@@ -54,15 +55,19 @@ export default class CodeActionAdapter {
5455
if (actions === null) {
5556
return []
5657
}
57-
return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection))
58+
return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection, onApply))
5859
}
5960

6061
private static createCodeAction(
6162
action: Command | CodeAction,
62-
connection: LanguageClientConnection
63+
connection: LanguageClientConnection,
64+
onApply: (action: Command | CodeAction) => Promise<boolean>
6365
): atomIde.CodeAction {
6466
return {
6567
async apply() {
68+
if ((await onApply(action)) === false) {
69+
return
70+
}
6671
if (CodeAction.is(action)) {
6772
CodeActionAdapter.applyWorkspaceEdit(action.edit)
6873
await CodeActionAdapter.executeCommand(action.command, connection)

lib/auto-languageclient.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ export default class AutoLanguageClient {
945945
editor,
946946
range,
947947
diagnostics,
948-
this.filterCodeActions.bind(this)
948+
this.filterCodeActions.bind(this),
949+
this.onApplyCodeActions.bind(this)
949950
)
950951
}
951952

@@ -954,6 +955,14 @@ export default class AutoLanguageClient {
954955
return actions
955956
}
956957

958+
/**
959+
* Optionally handle a code action before default handling. Return `false` to prevent default handling, `true` to
960+
* continue with default handling.
961+
*/
962+
protected async onApplyCodeActions(_action: ls.Command | ls.CodeAction): Promise<boolean> {
963+
return true
964+
}
965+
957966
public provideRefactor(): atomIde.RefactorProvider {
958967
return {
959968
grammarScopes: this.getGrammarScopes(),

0 commit comments

Comments
 (0)