diff options
author | Aleksey Kladov <[email protected]> | 2020-05-25 09:59:54 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-05-25 09:59:54 +0100 |
commit | 4a013ec62d2dfacaf6010b08c96947aa38481721 (patch) | |
tree | e4fe67c781801e5e75c1cece914b315bed546b4d | |
parent | 1527feb744c7911b6ca482554f0399d3ef0ebfdc (diff) |
Remove dead code
-rw-r--r-- | editors/code/src/commands/index.ts | 52 | ||||
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 2 | ||||
-rw-r--r-- | editors/code/src/snippets.ts | 52 | ||||
-rw-r--r-- | editors/code/src/source_change.ts | 54 |
4 files changed, 54 insertions, 106 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; |
diff --git a/editors/code/src/snippets.ts b/editors/code/src/snippets.ts new file mode 100644 index 000000000..794530162 --- /dev/null +++ b/editors/code/src/snippets.ts | |||
@@ -0,0 +1,52 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | |||
3 | import { assert } from './util'; | ||
4 | |||
5 | export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { | ||
6 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); | ||
7 | const [uri, edits] = edit.entries()[0]; | ||
8 | |||
9 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); | ||
10 | if (!editor) return; | ||
11 | |||
12 | let selection: vscode.Selection | undefined = undefined; | ||
13 | let lineDelta = 0; | ||
14 | await editor.edit((builder) => { | ||
15 | for (const indel of edits) { | ||
16 | const parsed = parseSnippet(indel.newText); | ||
17 | if (parsed) { | ||
18 | const [newText, [placeholderStart, placeholderLength]] = parsed; | ||
19 | const prefix = newText.substr(0, placeholderStart); | ||
20 | const lastNewline = prefix.lastIndexOf('\n'); | ||
21 | |||
22 | const startLine = indel.range.start.line + lineDelta + countLines(prefix); | ||
23 | const startColumn = lastNewline === -1 ? | ||
24 | indel.range.start.character + placeholderStart | ||
25 | : prefix.length - lastNewline - 1; | ||
26 | const endColumn = startColumn + placeholderLength; | ||
27 | selection = new vscode.Selection( | ||
28 | new vscode.Position(startLine, startColumn), | ||
29 | new vscode.Position(startLine, endColumn), | ||
30 | ); | ||
31 | builder.replace(indel.range, newText); | ||
32 | } else { | ||
33 | lineDelta = countLines(indel.newText) - (indel.range.end.line - indel.range.start.line); | ||
34 | builder.replace(indel.range, indel.newText); | ||
35 | } | ||
36 | } | ||
37 | }); | ||
38 | if (selection) editor.selection = selection; | ||
39 | } | ||
40 | |||
41 | function parseSnippet(snip: string): [string, [number, number]] | undefined { | ||
42 | const m = snip.match(/\$(0|\{0:([^}]*)\})/); | ||
43 | if (!m) return undefined; | ||
44 | const placeholder = m[2] ?? ""; | ||
45 | const range: [number, number] = [m.index!!, placeholder.length]; | ||
46 | const insert = snip.replace(m[0], placeholder); | ||
47 | return [insert, range]; | ||
48 | } | ||
49 | |||
50 | function countLines(text: string): number { | ||
51 | return (text.match(/\n/g) || []).length; | ||
52 | } | ||
diff --git a/editors/code/src/source_change.ts b/editors/code/src/source_change.ts deleted file mode 100644 index af8f1df51..000000000 --- a/editors/code/src/source_change.ts +++ /dev/null | |||
@@ -1,54 +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 } from './ctx'; | ||
6 | |||
7 | export async function applySourceChange(ctx: Ctx, change: ra.SourceChange) { | ||
8 | const client = ctx.client; | ||
9 | if (!client) return; | ||
10 | |||
11 | const wsEdit = client.protocol2CodeConverter.asWorkspaceEdit( | ||
12 | change.workspaceEdit, | ||
13 | ); | ||
14 | let created; | ||
15 | let moved; | ||
16 | if (change.workspaceEdit.documentChanges) { | ||
17 | for (const docChange of change.workspaceEdit.documentChanges) { | ||
18 | if (lc.CreateFile.is(docChange)) { | ||
19 | created = docChange.uri; | ||
20 | } else if (lc.RenameFile.is(docChange)) { | ||
21 | moved = docChange.newUri; | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | const toOpen = created || moved; | ||
26 | const toReveal = change.cursorPosition; | ||
27 | await vscode.workspace.applyEdit(wsEdit); | ||
28 | if (toOpen) { | ||
29 | const toOpenUri = vscode.Uri.parse(toOpen); | ||
30 | const doc = await vscode.workspace.openTextDocument(toOpenUri); | ||
31 | await vscode.window.showTextDocument(doc); | ||
32 | } else if (toReveal) { | ||
33 | const uri = client.protocol2CodeConverter.asUri( | ||
34 | toReveal.textDocument.uri, | ||
35 | ); | ||
36 | const position = client.protocol2CodeConverter.asPosition( | ||
37 | toReveal.position, | ||
38 | ); | ||
39 | const editor = vscode.window.activeTextEditor; | ||
40 | if (!editor || !editor.selection.isEmpty) { | ||
41 | return; | ||
42 | } | ||
43 | |||
44 | if (editor.document.uri !== uri) { | ||
45 | const doc = await vscode.workspace.openTextDocument(uri); | ||
46 | await vscode.window.showTextDocument(doc); | ||
47 | } | ||
48 | editor.selection = new vscode.Selection(position, position); | ||
49 | editor.revealRange( | ||
50 | new vscode.Range(position, position), | ||
51 | vscode.TextEditorRevealType.Default, | ||
52 | ); | ||
53 | } | ||
54 | } | ||