diff options
author | Aleksey Kladov <[email protected]> | 2020-05-21 13:26:44 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-05-21 14:08:03 +0100 |
commit | 4b495da368162a5b373d078be4ff51e55bffdf69 (patch) | |
tree | c35bf74905b9f03343fc10d834926e3457bc181c /editors/code | |
parent | a4e6963a2313971fe7bbec97d03bc67266ef68a9 (diff) |
Transition OnEnter to WorkspaceSnippetEdit
This also changes our handiling of snippet edits on the client side.
`editor.insertSnippet` unfortunately forces indentation, which we
really don't want to have to deal with. So, let's just implement our
manual hacky way of dealing with a simple subset of snippets we
actually use in rust-analyzer
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/commands/index.ts | 75 | ||||
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 5 | ||||
-rw-r--r-- | editors/code/src/main.ts | 2 | ||||
-rw-r--r-- | editors/code/src/rust-analyzer-api.ts | 2 |
4 files changed, 53 insertions, 31 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 0937b495c..e5ed77e32 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts | |||
@@ -53,36 +53,57 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd { | |||
53 | }; | 53 | }; |
54 | } | 54 | } |
55 | 55 | ||
56 | export function applySnippetWorkspaceEdit(_ctx: Ctx): Cmd { | 56 | export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd { |
57 | return async (edit: vscode.WorkspaceEdit) => { | 57 | return async (edit: vscode.WorkspaceEdit) => { |
58 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); | 58 | await applySnippetWorkspaceEdit(edit); |
59 | const [uri, edits] = edit.entries()[0]; | 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]; | ||
60 | 65 | ||
61 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); | 66 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); |
62 | if (!editor) return; | 67 | if (!editor) return; |
63 | 68 | ||
64 | let editWithSnippet: vscode.TextEdit | undefined = undefined; | 69 | let selection: vscode.Selection | undefined = undefined; |
65 | let lineDelta = 0; | 70 | let lineDelta = 0; |
66 | await editor.edit((builder) => { | 71 | await editor.edit((builder) => { |
67 | for (const indel of edits) { | 72 | for (const indel of edits) { |
68 | const isSnippet = indel.newText.indexOf('$0') !== -1 || indel.newText.indexOf('${') !== -1; | 73 | const parsed = parseSnippet(indel.newText); |
69 | if (isSnippet) { | 74 | if (parsed) { |
70 | editWithSnippet = indel; | 75 | const [newText, [placeholderStart, placeholderLength]] = parsed; |
71 | } else { | 76 | const prefix = newText.substr(0, placeholderStart); |
72 | if (!editWithSnippet) { | 77 | const lastNewline = prefix.lastIndexOf('\n'); |
73 | lineDelta = (indel.newText.match(/\n/g) || []).length - (indel.range.end.line - indel.range.start.line); | 78 | |
74 | } | 79 | const startLine = indel.range.start.line + lineDelta + countLines(prefix); |
75 | builder.replace(indel.range, indel.newText); | 80 | const startColumn = lastNewline === -1 ? |
76 | } | 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); | ||
77 | } | 92 | } |
78 | }); | ||
79 | if (editWithSnippet) { | ||
80 | const snip = editWithSnippet as vscode.TextEdit; | ||
81 | const range = snip.range.with( | ||
82 | snip.range.start.with(snip.range.start.line + lineDelta), | ||
83 | snip.range.end.with(snip.range.end.line + lineDelta), | ||
84 | ); | ||
85 | await editor.insertSnippet(new vscode.SnippetString(snip.newText), range); | ||
86 | } | 93 | } |
87 | }; | 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; | ||
88 | } | 109 | } |
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/main.ts b/editors/code/src/main.ts index ac3bb365e..8b0a9d870 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -91,7 +91,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
91 | ctx.registerCommand('debugSingle', commands.debugSingle); | 91 | ctx.registerCommand('debugSingle', commands.debugSingle); |
92 | ctx.registerCommand('showReferences', commands.showReferences); | 92 | ctx.registerCommand('showReferences', commands.showReferences); |
93 | ctx.registerCommand('applySourceChange', commands.applySourceChange); | 93 | ctx.registerCommand('applySourceChange', commands.applySourceChange); |
94 | ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEdit); | 94 | ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand); |
95 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); | 95 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); |
96 | 96 | ||
97 | ctx.pushCleanup(activateTaskProvider(workspaceFolder)); | 97 | ctx.pushCleanup(activateTaskProvider(workspaceFolder)); |
diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts index 400ac3714..3b83b10e3 100644 --- a/editors/code/src/rust-analyzer-api.ts +++ b/editors/code/src/rust-analyzer-api.ts | |||
@@ -69,7 +69,7 @@ export interface JoinLinesParams { | |||
69 | export const joinLines = request<JoinLinesParams, SourceChange>("joinLines"); | 69 | export const joinLines = request<JoinLinesParams, SourceChange>("joinLines"); |
70 | 70 | ||
71 | 71 | ||
72 | export const onEnter = request<lc.TextDocumentPositionParams, Option<SourceChange>>("onEnter"); | 72 | export const onEnter = request<lc.TextDocumentPositionParams, Option<lc.WorkspaceEdit>>("onEnter"); |
73 | 73 | ||
74 | export interface RunnablesParams { | 74 | export interface RunnablesParams { |
75 | textDocument: lc.TextDocumentIdentifier; | 75 | textDocument: lc.TextDocumentIdentifier; |