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