diff options
Diffstat (limited to 'editors/code/src/commands/index.ts')
-rw-r--r-- | editors/code/src/commands/index.ts | 106 |
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 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient'; | ||
3 | import * as ra from '../rust-analyzer-api'; | ||
4 | |||
5 | import { Ctx, Cmd } from '../ctx'; | ||
6 | import * as sourceChange from '../source_change'; | ||
7 | import { assert } from '../util'; | ||
8 | |||
9 | export * from './analyzer_status'; | ||
10 | export * from './matching_brace'; | ||
11 | export * from './join_lines'; | ||
12 | export * from './on_enter'; | ||
13 | export * from './parent_module'; | ||
14 | export * from './syntax_tree'; | ||
15 | export * from './expand_macro'; | ||
16 | export * from './runnables'; | ||
17 | export * from './ssr'; | ||
18 | export * from './server_version'; | ||
19 | export * from './toggle_inlay_hints'; | ||
20 | |||
21 | export function collectGarbage(ctx: Ctx): Cmd { | ||
22 | return async () => ctx.client.sendRequest(ra.collectGarbage, null); | ||
23 | } | ||
24 | |||
25 | export 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 | |||
39 | export function applySourceChange(ctx: Ctx): Cmd { | ||
40 | return async (change: ra.SourceChange) => { | ||
41 | await sourceChange.applySourceChange(ctx, change); | ||
42 | }; | ||
43 | } | ||
44 | |||
45 | export 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 | |||
53 | export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd { | ||
54 | return async (edit: vscode.WorkspaceEdit) => { | ||
55 | await applySnippetWorkspaceEdit(edit); | ||
56 | }; | ||
57 | } | ||
58 | |||
59 | export 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 | |||
95 | function 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 | |||
104 | function countLines(text: string): number { | ||
105 | return (text.match(/\n/g) || []).length; | ||
106 | } | ||