fix(vscode): guard tag auto-close against replaced selections and stale round-trips
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -37,17 +37,31 @@ function registerAutoCloseTags(context, client) {
|
||||
|
||||
const change = event.contentChanges[0];
|
||||
if (!change || (change.text !== ">" && change.text !== "/")) return;
|
||||
// A replaced selection (overtype/select-and-type) makes `range.start + text.length`
|
||||
// an incorrect offset for both the query and the insertion; decline rather than guess.
|
||||
if (change.rangeLength !== 0) return;
|
||||
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (!editor || editor.document !== event.document) return;
|
||||
|
||||
const documentVersion = event.document.version;
|
||||
const position = change.range.start.translate(0, change.text.length);
|
||||
if (!editor.selection.isEmpty || !editor.selection.active.isEqual(position)) return;
|
||||
|
||||
const snippet = await client.sendRequest("wrn/tagComplete", {
|
||||
textDocument: { uri: event.document.uri.toString() },
|
||||
position: { line: position.line, character: position.character },
|
||||
});
|
||||
if (typeof snippet !== "string" || !snippet) return;
|
||||
|
||||
// The user may have kept typing during the round-trip; re-validate everything the
|
||||
// insertion depends on before touching the document, since a stale offset would
|
||||
// silently corrupt it.
|
||||
if (vscode.window.activeTextEditor !== editor) return;
|
||||
if (editor.document !== event.document) return;
|
||||
if (editor.document.version !== documentVersion) return;
|
||||
if (!editor.selection.isEmpty || !editor.selection.active.isEqual(position)) return;
|
||||
|
||||
await editor.insertSnippet(new vscode.SnippetString(snippet), position);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user