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.ts52
1 files changed, 1 insertions, 51 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index c2a232d5f..1ed8258d8 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -3,8 +3,7 @@ import * as lc from 'vscode-languageclient';
3import * as ra from '../rust-analyzer-api'; 3import * as ra from '../rust-analyzer-api';
4 4
5import { Ctx, Cmd } from '../ctx'; 5import { Ctx, Cmd } from '../ctx';
6import * as sourceChange from '../source_change'; 6import { applySnippetWorkspaceEdit } from '../snippets';
7import { assert } from '../util';
8 7
9export * from './analyzer_status'; 8export * from './analyzer_status';
10export * from './matching_brace'; 9export * from './matching_brace';
@@ -55,52 +54,3 @@ export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd {
55 await applySnippetWorkspaceEdit(edit); 54 await applySnippetWorkspaceEdit(edit);
56 }; 55 };
57} 56}
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}