diff options
author | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
---|---|---|
committer | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
commit | 7fece3bdd2450c0807f7dd742239cae95f0cc65e (patch) | |
tree | 866c4db826c959e79c63a6727bdb9f2c61e6fc4f /editors/code/src/commands/index.ts | |
parent | db926218b2082077750291f8426ddd28b284cd08 (diff) | |
parent | 59732df8d40dfadc6dcf5951265416576399712a (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into modname_spacing
Diffstat (limited to 'editors/code/src/commands/index.ts')
-rw-r--r-- | editors/code/src/commands/index.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index bdb7fc3b0..e5ed77e32 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,58 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd { | |||
51 | } | 52 | } |
52 | }; | 53 | }; |
53 | } | 54 | } |
55 | |||
56 | export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd { | ||
57 | return async (edit: vscode.WorkspaceEdit) => { | ||
58 | await applySnippetWorkspaceEdit(edit); | ||
59 | }; | ||
60 | } | ||
61 | |||
62 | export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { | ||
63 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); | ||
64 | const [uri, edits] = edit.entries()[0]; | ||
65 | |||
66 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); | ||
67 | if (!editor) return; | ||
68 | |||
69 | let selection: vscode.Selection | undefined = undefined; | ||
70 | let lineDelta = 0; | ||
71 | await editor.edit((builder) => { | ||
72 | for (const indel of edits) { | ||
73 | const parsed = parseSnippet(indel.newText); | ||
74 | if (parsed) { | ||
75 | const [newText, [placeholderStart, placeholderLength]] = parsed; | ||
76 | const prefix = newText.substr(0, placeholderStart); | ||
77 | const lastNewline = prefix.lastIndexOf('\n'); | ||
78 | |||
79 | const startLine = indel.range.start.line + lineDelta + countLines(prefix); | ||
80 | const startColumn = lastNewline === -1 ? | ||
81 | indel.range.start.character + placeholderStart | ||
82 | : prefix.length - lastNewline - 1; | ||
83 | const endColumn = startColumn + placeholderLength; | ||
84 | selection = new vscode.Selection( | ||
85 | new vscode.Position(startLine, startColumn), | ||
86 | new vscode.Position(startLine, endColumn), | ||
87 | ); | ||
88 | builder.replace(indel.range, newText); | ||
89 | } else { | ||
90 | lineDelta = countLines(indel.newText) - (indel.range.end.line - indel.range.start.line); | ||
91 | builder.replace(indel.range, indel.newText); | ||
92 | } | ||
93 | } | ||
94 | }); | ||
95 | if (selection) editor.selection = selection; | ||
96 | } | ||
97 | |||
98 | function parseSnippet(snip: string): [string, [number, number]] | undefined { | ||
99 | const m = snip.match(/\$(0|\{0:([^}]*)\})/); | ||
100 | if (!m) return undefined; | ||
101 | const placeholder = m[2] ?? ""; | ||
102 | const range: [number, number] = [m.index!!, placeholder.length]; | ||
103 | const insert = snip.replace(m[0], placeholder); | ||
104 | return [insert, range]; | ||
105 | } | ||
106 | |||
107 | function countLines(text: string): number { | ||
108 | return (text.match(/\n/g) || []).length; | ||
109 | } | ||