CodeAction groups
This commit is contained in:
@@ -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<string, { index: number; items: vscode.CodeAction[] }>();
|
||||
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<any>, _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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user