aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/commands/index.ts')
-rw-r--r--editors/code/src/commands/index.ts106
1 files changed, 0 insertions, 106 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
deleted file mode 100644
index c2a232d5f..000000000
--- a/editors/code/src/commands/index.ts
+++ /dev/null
@@ -1,106 +0,0 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3import * as ra from '../rust-analyzer-api';
4
5import { Ctx, Cmd } from '../ctx';
6import * as sourceChange from '../source_change';
7import { assert } from '../util';
8
9export * from './analyzer_status';
10export * from './matching_brace';
11export * from './join_lines';
12export * from './on_enter';
13export * from './parent_module';
14export * from './syntax_tree';
15export * from './expand_macro';
16export * from './runnables';
17export * from './ssr';
18export * from './server_version';
19export * from './toggle_inlay_hints';
20
21export function collectGarbage(ctx: Ctx): Cmd {
22 return async () => ctx.client.sendRequest(ra.collectGarbage, null);
23}
24
25export function showReferences(ctx: Ctx): Cmd {
26 return (uri: string, position: lc.Position, locations: lc.Location[]) => {
27 const client = ctx.client;
28 if (client) {
29 vscode.commands.executeCommand(
30 'editor.action.showReferences',
31 vscode.Uri.parse(uri),
32 client.protocol2CodeConverter.asPosition(position),
33 locations.map(client.protocol2CodeConverter.asLocation),
34 );
35 }
36 };
37}
38
39export function applySourceChange(ctx: Ctx): Cmd {
40 return async (change: ra.SourceChange) => {
41 await sourceChange.applySourceChange(ctx, change);
42 };
43}
44
45export function applyActionGroup(_ctx: Ctx): Cmd {
46 return async (actions: { label: string; edit: vscode.WorkspaceEdit }[]) => {
47 const selectedAction = await vscode.window.showQuickPick(actions);
48 if (!selectedAction) return;
49 await applySnippetWorkspaceEdit(selectedAction.edit);
50 };
51}
52
53export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd {
54 return async (edit: vscode.WorkspaceEdit) => {
55 await applySnippetWorkspaceEdit(edit);
56 };
57}
58
59export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) {
60 assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`);
61 const [uri, edits] = edit.entries()[0];
62
63 const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString());
64 if (!editor) return;
65
66 let selection: vscode.Selection | undefined = undefined;
67 let lineDelta = 0;
68 await editor.edit((builder) => {
69 for (const indel of edits) {
70 const parsed = parseSnippet(indel.newText);
71 if (parsed) {
72 const [newText, [placeholderStart, placeholderLength]] = parsed;
73 const prefix = newText.substr(0, placeholderStart);
74 const lastNewline = prefix.lastIndexOf('\n');
75
76 const startLine = indel.range.start.line + lineDelta + countLines(prefix);
77 const startColumn = lastNewline === -1 ?
78 indel.range.start.character + placeholderStart
79 : prefix.length - lastNewline - 1;
80 const endColumn = startColumn + placeholderLength;
81 selection = new vscode.Selection(
82 new vscode.Position(startLine, startColumn),
83 new vscode.Position(startLine, endColumn),
84 );
85 builder.replace(indel.range, newText);
86 } else {
87 lineDelta = countLines(indel.newText) - (indel.range.end.line - indel.range.start.line);
88 builder.replace(indel.range, indel.newText);
89 }
90 }
91 });
92 if (selection) editor.selection = selection;
93}
94
95function parseSnippet(snip: string): [string, [number, number]] | undefined {
96 const m = snip.match(/\$(0|\{0:([^}]*)\})/);
97 if (!m) return undefined;
98 const placeholder = m[2] ?? "";
99 const range: [number, number] = [m.index!!, placeholder.length];
100 const insert = snip.replace(m[0], placeholder);
101 return [insert, range];
102}
103
104function countLines(text: string): number {
105 return (text.match(/\n/g) || []).length;
106}