diff options
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r-- | editors/code/src/commands/index.ts | 56 | ||||
-rw-r--r-- | editors/code/src/commands/join_lines.ts | 12 | ||||
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 5 | ||||
-rw-r--r-- | editors/code/src/commands/ssr.ts | 6 |
4 files changed, 69 insertions, 10 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 | } | ||
diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index de0614653..0bf1ee6e6 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import * as ra from '../rust-analyzer-api'; | 1 | import * as ra from '../rust-analyzer-api'; |
2 | import * as lc from 'vscode-languageclient'; | ||
2 | 3 | ||
3 | import { Ctx, Cmd } from '../ctx'; | 4 | import { Ctx, Cmd } from '../ctx'; |
4 | import { applySourceChange } from '../source_change'; | ||
5 | 5 | ||
6 | export function joinLines(ctx: Ctx): Cmd { | 6 | export function joinLines(ctx: Ctx): Cmd { |
7 | return async () => { | 7 | return async () => { |
@@ -9,10 +9,14 @@ export function joinLines(ctx: Ctx): Cmd { | |||
9 | const client = ctx.client; | 9 | const client = ctx.client; |
10 | if (!editor || !client) return; | 10 | if (!editor || !client) return; |
11 | 11 | ||
12 | const change = await client.sendRequest(ra.joinLines, { | 12 | const items: lc.TextEdit[] = await client.sendRequest(ra.joinLines, { |
13 | range: client.code2ProtocolConverter.asRange(editor.selection), | 13 | ranges: editor.selections.map((it) => client.code2ProtocolConverter.asRange(it)), |
14 | textDocument: { uri: editor.document.uri.toString() }, | 14 | textDocument: { uri: editor.document.uri.toString() }, |
15 | }); | 15 | }); |
16 | await applySourceChange(ctx, change); | 16 | editor.edit((builder) => { |
17 | client.protocol2CodeConverter.asTextEdits(items).forEach((edit) => { | ||
18 | builder.replace(edit.range, edit.newText); | ||
19 | }); | ||
20 | }); | ||
17 | }; | 21 | }; |
18 | } | 22 | } |
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 285849db7..a7871c31e 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | 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 { applySourceChange } from '../source_change'; | ||
5 | import { Cmd, Ctx } from '../ctx'; | 4 | import { Cmd, Ctx } from '../ctx'; |
5 | import { applySnippetWorkspaceEdit } from '.'; | ||
6 | 6 | ||
7 | async function handleKeypress(ctx: Ctx) { | 7 | async function handleKeypress(ctx: Ctx) { |
8 | const editor = ctx.activeRustEditor; | 8 | const editor = ctx.activeRustEditor; |
@@ -21,7 +21,8 @@ async function handleKeypress(ctx: Ctx) { | |||
21 | }); | 21 | }); |
22 | if (!change) return false; | 22 | if (!change) return false; |
23 | 23 | ||
24 | await applySourceChange(ctx, change); | 24 | const workspaceEdit = client.protocol2CodeConverter.asWorkspaceEdit(change); |
25 | await applySnippetWorkspaceEdit(workspaceEdit); | ||
25 | return true; | 26 | return true; |
26 | } | 27 | } |
27 | 28 | ||
diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts index 4ef8cdf04..5d40a64d2 100644 --- a/editors/code/src/commands/ssr.ts +++ b/editors/code/src/commands/ssr.ts | |||
@@ -2,7 +2,6 @@ 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 { Ctx, Cmd } from '../ctx'; | 4 | import { Ctx, Cmd } from '../ctx'; |
5 | import { applySourceChange } from '../source_change'; | ||
6 | 5 | ||
7 | export function ssr(ctx: Ctx): Cmd { | 6 | export function ssr(ctx: Ctx): Cmd { |
8 | return async () => { | 7 | return async () => { |
@@ -22,11 +21,10 @@ export function ssr(ctx: Ctx): Cmd { | |||
22 | } | 21 | } |
23 | }; | 22 | }; |
24 | const request = await vscode.window.showInputBox(options); | 23 | const request = await vscode.window.showInputBox(options); |
25 | |||
26 | if (!request) return; | 24 | if (!request) return; |
27 | 25 | ||
28 | const change = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); | 26 | const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); |
29 | 27 | ||
30 | await applySourceChange(ctx, change); | 28 | await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit)); |
31 | }; | 29 | }; |
32 | } | 30 | } |