Skip to content

Commit

Permalink
Merge pull request #167 from atom-community/code-actions-rust
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jun 14, 2021
2 parents 9d6df6b + 868f883 commit 0f12e3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/adapters/code-action-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,33 @@ 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,
onApply: (action: Command | CodeAction) => Promise<boolean> = () => Promise.resolve(true)
): Promise<atomIde.CodeAction[]> {
if (linterAdapter == null) {
return []
}
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 []
}
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
22 changes: 21 additions & 1 deletion lib/auto-languageclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ export default class AutoLanguageClient {
},
codeAction: {
dynamicRegistration: false,
codeActionLiteralSupport: {
codeActionKind: {
valueSet: [""], // TODO explicitly support more?
},
},
},
codeLens: {
dynamicRegistration: false,
Expand Down Expand Up @@ -939,10 +944,25 @@ export default class AutoLanguageClient {
this.getServerAdapter(server, "linterPushV2"),
editor,
range,
diagnostics
diagnostics,
this.filterCodeActions.bind(this),
this.onApplyCodeActions.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
}

/**
* 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 0f12e3b

Please sign in to comment.