diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-19 19:29:46 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-19 19:29:46 +0100 |
commit | 1bc1f28bc58b2dbcf8f8f548c277e2c90e3075cd (patch) | |
tree | 7d059b65919b1b64196cc3fc6830eeb99f2f9af0 /editors/code/src/commands | |
parent | 131849f2abd94dc8143f0c5d65e022136f29561a (diff) | |
parent | 3e9bf7ebabdaa8e9a2972af2dd8e8089a3a0341e (diff) |
Merge #4494
4494: Support snippet text edit r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r-- | editors/code/src/commands/index.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index bdb7fc3b0..770d11bd3 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts | |||
@@ -4,6 +4,7 @@ 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 * as sourceChange from '../source_change'; |
7 | import { assert } from '../util'; | ||
7 | 8 | ||
8 | export * from './analyzer_status'; | 9 | export * from './analyzer_status'; |
9 | export * from './matching_brace'; | 10 | export * from './matching_brace'; |
@@ -51,3 +52,36 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd { | |||
51 | } | 52 | } |
52 | }; | 53 | }; |
53 | } | 54 | } |
55 | |||
56 | export function applySnippetWorkspaceEdit(_ctx: Ctx): Cmd { | ||
57 | return async (edit: vscode.WorkspaceEdit) => { | ||
58 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); | ||
59 | const [uri, edits] = edit.entries()[0]; | ||
60 | |||
61 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); | ||
62 | if (!editor) return; | ||
63 | |||
64 | let editWithSnippet: vscode.TextEdit | undefined = undefined; | ||
65 | let lineDelta = 0; | ||
66 | await editor.edit((builder) => { | ||
67 | for (const indel of edits) { | ||
68 | if (indel.newText.indexOf('$0') !== -1) { | ||
69 | editWithSnippet = indel; | ||
70 | } else { | ||
71 | if (!editWithSnippet) { | ||
72 | lineDelta = (indel.newText.match(/\n/g) || []).length - (indel.range.end.line - indel.range.start.line); | ||
73 | } | ||
74 | builder.replace(indel.range, indel.newText); | ||
75 | } | ||
76 | } | ||
77 | }); | ||
78 | if (editWithSnippet) { | ||
79 | const snip = editWithSnippet as vscode.TextEdit; | ||
80 | const range = snip.range.with( | ||
81 | snip.range.start.with(snip.range.start.line + lineDelta), | ||
82 | snip.range.end.with(snip.range.end.line + lineDelta), | ||
83 | ); | ||
84 | await editor.insertSnippet(new vscode.SnippetString(snip.newText), range); | ||
85 | } | ||
86 | }; | ||
87 | } | ||