From 2075e77ee5784e72396c64c9ca059763508219ff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 May 2020 17:29:55 +0200 Subject: CodeAction groups --- editors/code/src/client.ts | 41 ++++++++++++++++++++++++++++++++++---- editors/code/src/commands/index.ts | 14 +++++-------- editors/code/src/main.ts | 2 +- 3 files changed, 43 insertions(+), 14 deletions(-) (limited to 'editors') diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index fac1a0be3..d64f9a3f9 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -41,10 +41,12 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient return client.sendRequest(lc.CodeActionRequest.type, params, token).then((values) => { if (values === null) return undefined; const result: (vscode.CodeAction | vscode.Command)[] = []; + const groups = new Map(); for (const item of values) { if (lc.CodeAction.is(item)) { const action = client.protocol2CodeConverter.asCodeAction(item); - if (isSnippetEdit(item)) { + const group = actionGroup(item); + if (isSnippetEdit(item) || group) { action.command = { command: "rust-analyzer.applySnippetWorkspaceEdit", title: "", @@ -52,12 +54,38 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient }; action.edit = undefined; } - result.push(action); + + if (group) { + let entry = groups.get(group); + if (!entry) { + entry = { index: result.length, items: [] }; + groups.set(group, entry); + result.push(action); + } + entry.items.push(action); + } else { + result.push(action); + } } else { const command = client.protocol2CodeConverter.asCommand(item); result.push(command); } } + for (const [group, { index, items }] of groups) { + if (items.length === 1) { + result[index] = items[0]; + } else { + const action = new vscode.CodeAction(group); + action.command = { + command: "rust-analyzer.applyActionGroup", + title: "", + arguments: [items.map((item) => { + return { label: item.title, edit: item.command!!.arguments!![0] }; + })], + }; + result[index] = action; + } + } return result; }, (_error) => undefined @@ -81,15 +109,16 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient // implementations are still in the "proposed" category for 3.16. client.registerFeature(new CallHierarchyFeature(client)); client.registerFeature(new SemanticTokensFeature(client)); - client.registerFeature(new SnippetTextEditFeature()); + client.registerFeature(new ExperimentalFeatures()); return client; } -class SnippetTextEditFeature implements lc.StaticFeature { +class ExperimentalFeatures implements lc.StaticFeature { fillClientCapabilities(capabilities: lc.ClientCapabilities): void { const caps: any = capabilities.experimental ?? {}; caps.snippetTextEdit = true; + caps.codeActionGroup = true; capabilities.experimental = caps; } initialize(_capabilities: lc.ServerCapabilities, _documentSelector: lc.DocumentSelector | undefined): void { @@ -107,3 +136,7 @@ function isSnippetEdit(action: lc.CodeAction): boolean { } return false; } + +function actionGroup(action: lc.CodeAction): string | undefined { + return (action as any).group; +} diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index e5ed77e32..abb53a248 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -41,15 +41,11 @@ export function applySourceChange(ctx: Ctx): Cmd { }; } -export function selectAndApplySourceChange(ctx: Ctx): Cmd { - return async (changes: ra.SourceChange[]) => { - if (changes.length === 1) { - await sourceChange.applySourceChange(ctx, changes[0]); - } else if (changes.length > 0) { - const selectedChange = await vscode.window.showQuickPick(changes); - if (!selectedChange) return; - await sourceChange.applySourceChange(ctx, selectedChange); - } +export function applyActionGroup(_ctx: Ctx): Cmd { + return async (actions: { label: string; edit: vscode.WorkspaceEdit }[]) => { + const selectedAction = await vscode.window.showQuickPick(actions); + if (!selectedAction) return; + await applySnippetWorkspaceEdit(selectedAction.edit); }; } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 8b0a9d870..4d4513869 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -92,7 +92,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('showReferences', commands.showReferences); ctx.registerCommand('applySourceChange', commands.applySourceChange); ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand); - ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); + ctx.registerCommand('applyActionGroup', commands.applyActionGroup); ctx.pushCleanup(activateTaskProvider(workspaceFolder)); -- cgit v1.2.3