Skip to content

Commit

Permalink
feat: support custom onApplyCodeActions
Browse files Browse the repository at this point in the history
Co-Authored-By: Alex Butler <[email protected]>
  • Loading branch information
aminya and alexheretic committed Jun 14, 2021
1 parent 949e624 commit abb5719
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/adapters/code-action-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> = (action) => Promise.resolve(true)
): Promise<atomIde.CodeAction[]> {
if (linterAdapter == null) {
return []
Expand All @@ -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<boolean>
): 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)
Expand Down
11 changes: 10 additions & 1 deletion lib/auto-languageclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,8 @@ export default class AutoLanguageClient {
editor,
range,
diagnostics,
this.filterCodeActions.bind(this)
this.filterCodeActions.bind(this),
this.onApplyCodeActions.bind(this)
)
}

Expand All @@ -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<boolean> {
return true
}

public provideRefactor(): atomIde.RefactorProvider {
return {
grammarScopes: this.getGrammarScopes(),
Expand Down

0 comments on commit abb5719

Please sign in to comment.