|
1 | 1 | import { commands, Uri, workspace } from 'coc.nvim';
|
2 |
| -import { Location, Position } from 'vscode-languageserver-protocol'; |
| 2 | +import { Location, Position, Range, TextDocumentEdit, TextEdit, WorkspaceEdit } from 'vscode-languageserver-protocol'; |
3 | 3 | import { Cmd, Ctx } from '../ctx';
|
4 | 4 | import * as ra from '../rust-analyzer-api';
|
5 | 5 | import * as sourceChange from '../source_change';
|
@@ -67,3 +67,67 @@ export function toggleInlayHints(ctx: Ctx) {
|
67 | 67 | }
|
68 | 68 | };
|
69 | 69 | }
|
| 70 | + |
| 71 | +function parseSnippet(snip: string): [string, [number, number]] | undefined { |
| 72 | + const m = snip.match(/\$(0|\{0:([^}]*)\})/); |
| 73 | + if (!m) return undefined; |
| 74 | + const placeholder = m[2] ?? ''; |
| 75 | + const range: [number, number] = [m.index!!, placeholder.length]; |
| 76 | + const insert = snip.replace(m[0], placeholder); |
| 77 | + return [insert, range]; |
| 78 | +} |
| 79 | + |
| 80 | +function countLines(text: string): number { |
| 81 | + return (text.match(/\n/g) || []).length; |
| 82 | +} |
| 83 | + |
| 84 | +export async function applySnippetWorkspaceEdit(edit: WorkspaceEdit) { |
| 85 | + if (!edit.documentChanges?.length) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + let selection: Range | undefined = undefined; |
| 90 | + let lineDelta = 0; |
| 91 | + const change = edit.documentChanges[0]; |
| 92 | + if (TextDocumentEdit.is(change)) { |
| 93 | + for (const indel of change.edits) { |
| 94 | + const wsEdit: WorkspaceEdit = {}; |
| 95 | + const parsed = parseSnippet(indel.newText); |
| 96 | + if (parsed) { |
| 97 | + const [newText, [placeholderStart, placeholderLength]] = parsed; |
| 98 | + const prefix = newText.substr(0, placeholderStart); |
| 99 | + const lastNewline = prefix.lastIndexOf('\n'); |
| 100 | + |
| 101 | + const startLine = indel.range.start.line + lineDelta + countLines(prefix); |
| 102 | + const startColumn = lastNewline === -1 ? indel.range.start.character + placeholderStart : prefix.length - lastNewline - 1; |
| 103 | + const endColumn = startColumn + placeholderLength; |
| 104 | + selection = Range.create(startLine, startColumn, startLine, endColumn); |
| 105 | + |
| 106 | + const newChange = TextDocumentEdit.create(change.textDocument, [TextEdit.replace(indel.range, newText)]); |
| 107 | + wsEdit.documentChanges = [newChange]; |
| 108 | + } else { |
| 109 | + lineDelta = countLines(indel.newText) - (indel.range.end.line - indel.range.start.line); |
| 110 | + wsEdit.documentChanges = [change]; |
| 111 | + } |
| 112 | + |
| 113 | + await workspace.applyEdit(wsEdit); |
| 114 | + } |
| 115 | + |
| 116 | + if (selection) { |
| 117 | + const current = await workspace.document; |
| 118 | + if (current.uri !== change.textDocument.uri) { |
| 119 | + await workspace.loadFile(change.textDocument.uri); |
| 120 | + await workspace.jumpTo(change.textDocument.uri); |
| 121 | + // FIXME |
| 122 | + return; |
| 123 | + } |
| 124 | + await workspace.selectRange(selection); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +export function applySnippetWorkspaceEditCommand(): Cmd { |
| 130 | + return async (edit: WorkspaceEdit) => { |
| 131 | + await applySnippetWorkspaceEdit(edit); |
| 132 | + }; |
| 133 | +} |
0 commit comments