Skip to content

Commit 3dd68c1

Browse files
committed
Implement client-side of SnippetTextEdit
1 parent 2bf6b16 commit 3dd68c1

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

editors/code/src/client.ts

+46-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,39 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient
3131
const res = await next(document, token);
3232
if (res === undefined) throw new Error('busy');
3333
return res;
34+
},
35+
async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken, _next: lc.ProvideCodeActionsSignature) {
36+
const params: lc.CodeActionParams = {
37+
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
38+
range: client.code2ProtocolConverter.asRange(range),
39+
context: client.code2ProtocolConverter.asCodeActionContext(context)
40+
};
41+
return client.sendRequest(lc.CodeActionRequest.type, params, token).then((values) => {
42+
if (values === null) return undefined;
43+
const result: (vscode.CodeAction | vscode.Command)[] = [];
44+
for (const item of values) {
45+
if (lc.CodeAction.is(item)) {
46+
const action = client.protocol2CodeConverter.asCodeAction(item);
47+
if (isSnippetEdit(item)) {
48+
action.command = {
49+
command: "rust-analyzer.applySnippetWorkspaceEdit",
50+
title: "",
51+
arguments: [action.edit],
52+
};
53+
action.edit = undefined;
54+
}
55+
result.push(action);
56+
} else {
57+
const command = client.protocol2CodeConverter.asCommand(item);
58+
result.push(command);
59+
}
60+
}
61+
return result;
62+
},
63+
(_error) => undefined
64+
);
3465
}
66+
3567
} as any
3668
};
3769

@@ -42,7 +74,7 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient
4274
clientOptions,
4375
);
4476

45-
// To turn on all proposed features use: res.registerProposedFeatures();
77+
// To turn on all proposed features use: client.registerProposedFeatures();
4678
// Here we want to enable CallHierarchyFeature and SemanticTokensFeature
4779
// since they are available on stable.
4880
// Note that while these features are stable in vscode their LSP protocol
@@ -58,8 +90,20 @@ class SnippetTextEditFeature implements lc.StaticFeature {
5890
fillClientCapabilities(capabilities: lc.ClientCapabilities): void {
5991
const caps: any = capabilities.experimental ?? {};
6092
caps.snippetTextEdit = true;
61-
capabilities.experimental = caps
93+
capabilities.experimental = caps;
6294
}
6395
initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void {
6496
}
6597
}
98+
99+
function isSnippetEdit(action: lc.CodeAction): boolean {
100+
const documentChanges = action.edit?.documentChanges ?? [];
101+
for (const edit of documentChanges) {
102+
if (lc.TextDocumentEdit.is(edit)) {
103+
if (edit.edits.some((indel) => (indel as any).insertTextFormat === lc.InsertTextFormat.Snippet)) {
104+
return true;
105+
}
106+
}
107+
}
108+
return false;
109+
}

editors/code/src/commands/index.ts

+34
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as ra from '../rust-analyzer-api';
44

55
import { Ctx, Cmd } from '../ctx';
66
import * as sourceChange from '../source_change';
7+
import { assert } from '../util';
78

89
export * from './analyzer_status';
910
export * from './matching_brace';
@@ -51,3 +52,36 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd {
5152
}
5253
};
5354
}
55+
56+
export function applySnippetWorkspaceEdit(_ctx: Ctx): Cmd {
57+
return async (edit: vscode.WorkspaceEdit) => {
58+
assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`);
59+
const [uri, edits] = edit.entries()[0];
60+
61+
const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString());
62+
if (!editor) return;
63+
64+
let editWithSnippet: vscode.TextEdit | undefined = undefined;
65+
let lineDelta = 0;
66+
await editor.edit((builder) => {
67+
for (const indel of edits) {
68+
if (indel.newText.indexOf('$0') !== -1) {
69+
editWithSnippet = indel;
70+
} else {
71+
if (!editWithSnippet) {
72+
lineDelta = (indel.newText.match(/\n/g) || []).length - (indel.range.end.line - indel.range.start.line);
73+
}
74+
builder.replace(indel.range, indel.newText);
75+
}
76+
}
77+
});
78+
if (editWithSnippet) {
79+
const snip = editWithSnippet as vscode.TextEdit;
80+
const range = snip.range.with(
81+
snip.range.start.with(snip.range.start.line + lineDelta),
82+
snip.range.end.with(snip.range.end.line + lineDelta),
83+
);
84+
await editor.insertSnippet(new vscode.SnippetString(snip.newText), range);
85+
}
86+
};
87+
}

editors/code/src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export async function activate(context: vscode.ExtensionContext) {
9191
ctx.registerCommand('debugSingle', commands.debugSingle);
9292
ctx.registerCommand('showReferences', commands.showReferences);
9393
ctx.registerCommand('applySourceChange', commands.applySourceChange);
94+
ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEdit);
9495
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
9596

9697
ctx.pushCleanup(activateTaskProvider(workspaceFolder));

0 commit comments

Comments
 (0)