Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing code action functions for ide-rust #167

Merged
merged 3 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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