Skip to content

Commit

Permalink
feat: partly SnippetTextEdit support
Browse files Browse the repository at this point in the history
1. apply snippet's placeholder
2. move the cursor to `$0`

Couldn't jump the snippets for now, need coc.nvim to apply SNIP session

#1279
#1228
  • Loading branch information
fannheyward committed Dec 11, 2024
1 parent 4283825 commit 1b6aafd
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ function codeFormat(expanded: ra.ExpandedMacro): string {
return result;
}

function parseSnippet(snip: string): [string, [number, number]] | undefined {
const m = snip.match(/\$(0|\{0:([^}]*)\})/);
if (!m) return undefined;
const placeholder = m[2] ?? '';
const range: [number, number] = [m.index!, placeholder.length];
const insert = snip.replace(m[0], placeholder);
return [insert, range];
}

function countLines(text: string): number {
return (text.match(/\n/g) || []).length;
}
Expand Down Expand Up @@ -593,36 +584,25 @@ export async function applySnippetWorkspaceEdit(edit: WorkspaceEdit) {
return;
}

let selection: Range | undefined = undefined;
let position: Position | undefined = undefined;
let lineDelta = 0;
const change = edit.documentChanges[0];
if (TextDocumentEdit.is(change)) {
const newEdits: TextEdit[] = [];

for (const indel of change.edits) {
const { range } = indel;
let newText = indel.newText.replaceAll('\\}', '}');
const parsed = parseSnippet(newText);
if (parsed) {
const [insert, [snipStart, snipLength]] = parsed;
const prefix = insert.substring(0, snipStart);
const parsed = indel.newText.replaceAll('\\}', '}').replaceAll(/\$\{[1-9]+:([^\}]+)\}/g, '$1');
const index0 = parsed.indexOf('$0');
if (index0 !== -1) {
const prefix = parsed.substring(0, index0);
const lastNewline = prefix.lastIndexOf('\n');

const startLine = range.start.line + lineDelta + countLines(prefix);
const startCol = lastNewline === -1 ? range.start.character + snipStart : prefix.length - lastNewline - 1;
if (snipLength) {
selection = Range.create(startLine, startCol, startLine, startCol + snipLength);
} else {
position = Position.create(startLine, startCol);
}

newText = insert;
} else {
lineDelta += countLines(indel.newText) - (indel.range.end.line - indel.range.start.line);
const line = range.start.line + countLines(prefix);
const col = lastNewline === -1 ? range.start.character + index0 : prefix.length - lastNewline - 1;
position = Position.create(line, col);
}

newEdits.push(TextEdit.replace(range, newText));
newEdits.push(TextEdit.replace(range, parsed.replaceAll('$0', '')));
}

const current = await workspace.document;
Expand All @@ -631,16 +611,9 @@ export async function applySnippetWorkspaceEdit(edit: WorkspaceEdit) {
await workspace.jumpTo(change.textDocument.uri);
}

const wsEdit: WorkspaceEdit = {
changes: {
[change.textDocument.uri]: newEdits,
},
};
await workspace.applyEdit(wsEdit);
await workspace.applyEdit({ changes: { [change.textDocument.uri]: newEdits } });

if (selection) {
await window.selectRange(selection);
} else if (position) {
if (position) {
await window.moveTo(position);
}
}
Expand Down

0 comments on commit 1b6aafd

Please sign in to comment.