Skip to content

Commit 0f12e3b

Browse files
authored
Merge pull request #167 from atom-community/code-actions-rust
2 parents 9d6df6b + 868f883 commit 0f12e3b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/adapters/code-action-adapter.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,33 @@ export default class CodeActionAdapter {
4141
linterAdapter: LinterPushV2Adapter | IdeDiagnosticAdapter | undefined,
4242
editor: TextEditor,
4343
range: Range,
44-
linterMessages: linter.Message[] | atomIde.Diagnostic[]
44+
linterMessages: linter.Message[] | atomIde.Diagnostic[],
45+
filterActions: (actions: (Command | CodeAction)[] | null) => (Command | CodeAction)[] | null = (actions) => actions,
46+
onApply: (action: Command | CodeAction) => Promise<boolean> = () => Promise.resolve(true)
4547
): Promise<atomIde.CodeAction[]> {
4648
if (linterAdapter == null) {
4749
return []
4850
}
4951
assert(serverCapabilities.codeActionProvider, "Must have the textDocument/codeAction capability")
5052

5153
const params = createCodeActionParams(linterAdapter, editor, range, linterMessages)
52-
const actions = await connection.codeAction(params)
54+
const actions = filterActions(await connection.codeAction(params))
5355
if (actions === null) {
5456
return []
5557
}
56-
return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection))
58+
return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection, onApply))
5759
}
5860

5961
private static createCodeAction(
6062
action: Command | CodeAction,
61-
connection: LanguageClientConnection
63+
connection: LanguageClientConnection,
64+
onApply: (action: Command | CodeAction) => Promise<boolean>
6265
): atomIde.CodeAction {
6366
return {
6467
async apply() {
68+
if ((await onApply(action)) === false) {
69+
return
70+
}
6571
if (CodeAction.is(action)) {
6672
CodeActionAdapter.applyWorkspaceEdit(action.edit)
6773
await CodeActionAdapter.executeCommand(action.command, connection)

lib/auto-languageclient.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ export default class AutoLanguageClient {
219219
},
220220
codeAction: {
221221
dynamicRegistration: false,
222+
codeActionLiteralSupport: {
223+
codeActionKind: {
224+
valueSet: [""], // TODO explicitly support more?
225+
},
226+
},
222227
},
223228
codeLens: {
224229
dynamicRegistration: false,
@@ -939,10 +944,25 @@ export default class AutoLanguageClient {
939944
this.getServerAdapter(server, "linterPushV2"),
940945
editor,
941946
range,
942-
diagnostics
947+
diagnostics,
948+
this.filterCodeActions.bind(this),
949+
this.onApplyCodeActions.bind(this)
943950
)
944951
}
945952

953+
/** Optionally filter code action before they're displayed */
954+
protected filterCodeActions(actions: (ls.Command | ls.CodeAction)[] | null): (ls.Command | ls.CodeAction)[] | null {
955+
return actions
956+
}
957+
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+
946966
public provideRefactor(): atomIde.RefactorProvider {
947967
return {
948968
grammarScopes: this.getGrammarScopes(),

0 commit comments

Comments
 (0)