import {documentKey} from "./files.js"; import type {LspPosition, LspTextEdit, WorkspaceEdit} from "./types.js"; export function positionAt(text: string, offset: number): LspPosition { const boundedOffset = Math.max(0, Math.min(offset, text.length)); let line = 0; let lineStart = 0; for (let index = 0; index < boundedOffset; index += 1) { if (text[index] === "\n") { line += 1; lineStart = index + 1; } } return {line, character: boundedOffset - lineStart}; } function offsetAt(text: string, position: LspPosition) { let line = 0; let lineStart = 0; for (let index = 0; index < text.length && line < position.line; index += 1) { if (text[index] === "\n") { line += 1; lineStart = index + 1; } } if (line < position.line) return text.length; let lineEnd = text.indexOf("\n", lineStart); if (lineEnd < 0) lineEnd = text.length; return Math.min(lineStart + position.character, lineEnd); } export function applyTextEdits(text: string, edits: LspTextEdit[]) { const sortedEdits = positionTextEdits(text, edits).sort((left, right) => { if (left.start !== right.start) return right.start - left.start; if (left.end !== right.end) return right.end - left.end; return right.index - left.index; }); // Walk the sorted edits back to front, emitting the untouched gap plus // each replacement into a parts buffer: every character of the input is // copied exactly once, instead of once per edit (a large file with many // fixes used to pay O(edits x file size) in intermediate strings). const parts: string[] = []; let cursor = text.length; for (const {edit, start, end} of sortedEdits) { // Overlaps are rejected upstream; end <= cursor always holds here. if (end < cursor) parts.push(text.slice(end, cursor)); parts.push(edit.newText); cursor = start; } parts.push(text.slice(0, cursor)); return parts.reverse().join(""); } export function hasOverlappingTextEdits(text: string, edits: LspTextEdit[]) { const positionedEdits = positionTextEdits(text, edits); for (let leftIndex = 0; leftIndex < positionedEdits.length; leftIndex += 1) { for ( let rightIndex = leftIndex + 1; rightIndex < positionedEdits.length; rightIndex += 1 ) { if ( textEditRangesConflict( positionedEdits[leftIndex], positionedEdits[rightIndex], ) ) { return true; } } } return false; } function positionTextEdits(text: string, edits: LspTextEdit[]) { return edits.map((edit, index) => ({ edit, index, start: offsetAt(text, edit.range.start), end: offsetAt(text, edit.range.end), })); } function textEditRangesConflict( left: { start: number; end: number }, right: { start: number; end: number }, ) { if (left.start === left.end && right.start === right.end) return false; if (left.start === left.end || right.start === right.end) { const insert = left.start === left.end ? left : right; const replacement = left.start === left.end ? right : left; return replacement.start < insert.start && insert.start < replacement.end; } return Math.max(left.start, right.start) < Math.min(left.end, right.end); } export interface WorkspaceEditSplit { /** Edits targeting the requested document. */ edits: LspTextEdit[]; /** Other documents the server wants to change; lsp_fix never touches them. */ otherDocumentUris: string[]; /** Non-text documentChanges (create/rename/delete) reported but not applied. */ resourceOperations: string[]; } /** * Split a WorkspaceEdit into the edits for the requested document and * everything else the server returned. URIs are compared through the same * canonical key the diagnostics cache uses: a server may echo a differently * encoded form of the URI the client sent (marksman-style `c%3A`), and a raw * string compare would misfile same-document edits as cross-document. * Cross-document and resource-level changes are surfaced to the caller * instead of being silently dropped. */ export function splitWorkspaceEdits( edit: WorkspaceEdit | undefined, uri: string, ): WorkspaceEditSplit { if (!edit) { return {edits: [], otherDocumentUris: [], resourceOperations: []}; } const requestedKey = documentKey(uri); const edits: LspTextEdit[] = []; // Normalized key -> raw URI: equivalent encodings of one other document // are reported once, under the server's own spelling. const otherDocuments = new Map(); const resourceOperations: string[] = []; if (edit.documentChanges) { for (const change of edit.documentChanges) { const changeUri = change.textDocument?.uri; if (!changeUri) { resourceOperations.push( String((change as { kind?: string }).kind ?? "resource"), ); continue; } const key = documentKey(changeUri); if (key === requestedKey) edits.push(...(change.edits ?? [])); else if (!otherDocuments.has(key)) otherDocuments.set(key, changeUri); } return { edits, otherDocumentUris: [...otherDocuments.values()], resourceOperations, }; } for (const [changeUri, changeEdits] of Object.entries(edit.changes ?? {})) { const key = documentKey(changeUri); if (key === requestedKey) edits.push(...changeEdits); else if (!otherDocuments.has(key)) otherDocuments.set(key, changeUri); } return { edits, otherDocumentUris: [...otherDocuments.values()], resourceOperations, }; }