diff options
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r-- | editors/code/src/commands/index.ts | 52 | ||||
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 2 |
2 files changed, 2 insertions, 52 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'; | |||
3 | import * as ra from '../rust-analyzer-api'; | 3 | import * as ra from '../rust-analyzer-api'; |
4 | 4 | ||
5 | import { Ctx, Cmd } from '../ctx'; | 5 | import { Ctx, Cmd } from '../ctx'; |
6 | import * as sourceChange from '../source_change'; | 6 | import { applySnippetWorkspaceEdit } from '../snippets'; |
7 | import { assert } from '../util'; | ||
8 | 7 | ||
9 | export * from './analyzer_status'; | 8 | export * from './analyzer_status'; |
10 | export * from './matching_brace'; | 9 | export * 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 | |||
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 | } | ||
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index a7871c31e..0e4769633 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts | |||
@@ -2,7 +2,7 @@ import * as vscode from 'vscode'; | |||
2 | import * as ra from '../rust-analyzer-api'; | 2 | import * as ra from '../rust-analyzer-api'; |
3 | 3 | ||
4 | import { Cmd, Ctx } from '../ctx'; | 4 | import { Cmd, Ctx } from '../ctx'; |
5 | import { applySnippetWorkspaceEdit } from '.'; | 5 | import { applySnippetWorkspaceEdit } from '../snippets'; |
6 | 6 | ||
7 | async function handleKeypress(ctx: Ctx) { | 7 | async function handleKeypress(ctx: Ctx) { |
8 | const editor = ctx.activeRustEditor; | 8 | const editor = ctx.activeRustEditor; |