@pierre/diffs 1.3.0-beta.6 → 1.3.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/CodeView.d.ts +4 -1
- package/dist/components/CodeView.d.ts.map +1 -1
- package/dist/components/CodeView.js +45 -6
- package/dist/components/CodeView.js.map +1 -1
- package/dist/components/File.d.ts.map +1 -1
- package/dist/components/File.js +5 -2
- package/dist/components/File.js.map +1 -1
- package/dist/components/FileDiff.d.ts +36 -23
- package/dist/components/FileDiff.d.ts.map +1 -1
- package/dist/components/FileDiff.js +126 -58
- package/dist/components/FileDiff.js.map +1 -1
- package/dist/components/UnresolvedFile.d.ts +3 -2
- package/dist/components/UnresolvedFile.d.ts.map +1 -1
- package/dist/components/UnresolvedFile.js +4 -2
- package/dist/components/UnresolvedFile.js.map +1 -1
- package/dist/components/VirtualizedFile.d.ts.map +1 -1
- package/dist/components/VirtualizedFile.js +3 -7
- package/dist/components/VirtualizedFile.js.map +1 -1
- package/dist/components/VirtualizedFileDiff.d.ts +10 -4
- package/dist/components/VirtualizedFileDiff.d.ts.map +1 -1
- package/dist/components/VirtualizedFileDiff.js +178 -49
- package/dist/components/VirtualizedFileDiff.js.map +1 -1
- package/dist/editor/editor.d.ts +2 -2
- package/dist/editor/editor.d.ts.map +1 -1
- package/dist/editor/editor.js +163 -106
- package/dist/editor/editor.js.map +1 -1
- package/dist/editor/editor2.js +1 -1
- package/dist/editor/selection.d.ts +1 -1
- package/dist/editor/selection.d.ts.map +1 -1
- package/dist/editor/selection.js +87 -37
- package/dist/editor/selection.js.map +1 -1
- package/dist/editor/textMeasure.d.ts.map +1 -1
- package/dist/editor/textMeasure.js +25 -7
- package/dist/editor/textMeasure.js.map +1 -1
- package/dist/editor/tokenzier.d.ts +2 -0
- package/dist/editor/tokenzier.d.ts.map +1 -1
- package/dist/editor/tokenzier.js +11 -3
- package/dist/editor/tokenzier.js.map +1 -1
- package/dist/editor/utils.d.ts +3 -1
- package/dist/editor/utils.d.ts.map +1 -1
- package/dist/editor/utils.js +14 -1
- package/dist/editor/utils.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -1
- package/dist/managers/InteractionManager.d.ts.map +1 -1
- package/dist/managers/InteractionManager.js +0 -1
- package/dist/managers/InteractionManager.js.map +1 -1
- package/dist/react/EditorContext.js.map +1 -1
- package/dist/react/MultiFileDiff.d.ts +3 -4
- package/dist/react/MultiFileDiff.d.ts.map +1 -1
- package/dist/react/MultiFileDiff.js.map +1 -1
- package/dist/react/index.d.ts +2 -2
- package/dist/react/utils/useFileDiffInstance.js +14 -15
- package/dist/react/utils/useFileDiffInstance.js.map +1 -1
- package/dist/renderers/DiffHunksRenderer.d.ts +2 -2
- package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
- package/dist/renderers/DiffHunksRenderer.js +29 -16
- package/dist/renderers/DiffHunksRenderer.js.map +1 -1
- package/dist/renderers/FileRenderer.js.map +1 -1
- package/dist/ssr/index.d.ts +2 -2
- package/dist/ssr/preloadDiffs.d.ts +11 -10
- package/dist/ssr/preloadDiffs.d.ts.map +1 -1
- package/dist/ssr/preloadDiffs.js +14 -6
- package/dist/ssr/preloadDiffs.js.map +1 -1
- package/dist/types.d.ts +59 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/areHunkDataEqual.js +1 -1
- package/dist/utils/areHunkDataEqual.js.map +1 -1
- package/dist/utils/awaitWithTimeout.d.ts +5 -0
- package/dist/utils/awaitWithTimeout.d.ts.map +1 -0
- package/dist/utils/awaitWithTimeout.js +15 -0
- package/dist/utils/awaitWithTimeout.js.map +1 -0
- package/dist/utils/cloneFileDiffMetadata.d.ts +7 -0
- package/dist/utils/cloneFileDiffMetadata.d.ts.map +1 -0
- package/dist/utils/cloneFileDiffMetadata.js +16 -0
- package/dist/utils/cloneFileDiffMetadata.js.map +1 -0
- package/dist/utils/computeEstimatedDiffHeights.d.ts +3 -1
- package/dist/utils/computeEstimatedDiffHeights.d.ts.map +1 -1
- package/dist/utils/computeEstimatedDiffHeights.js +8 -1
- package/dist/utils/computeEstimatedDiffHeights.js.map +1 -1
- package/dist/utils/createPreElement.js +0 -1
- package/dist/utils/createPreElement.js.map +1 -1
- package/dist/utils/getDiffFileInput.d.ts +14 -0
- package/dist/utils/getDiffFileInput.d.ts.map +1 -0
- package/dist/utils/getDiffFileInput.js +24 -0
- package/dist/utils/getDiffFileInput.js.map +1 -0
- package/dist/utils/getDiffHunksRendererOptions.js +1 -0
- package/dist/utils/getDiffHunksRendererOptions.js.map +1 -1
- package/dist/utils/getFiletypeFromFileName.d.ts.map +1 -1
- package/dist/utils/getFiletypeFromFileName.js +2 -0
- package/dist/utils/getFiletypeFromFileName.js.map +1 -1
- package/dist/utils/hydratePartialDiff.d.ts +10 -0
- package/dist/utils/hydratePartialDiff.d.ts.map +1 -0
- package/dist/utils/hydratePartialDiff.js +140 -0
- package/dist/utils/hydratePartialDiff.js.map +1 -0
- package/dist/utils/iterateOverDiff.js +3 -3
- package/dist/utils/iterateOverDiff.js.map +1 -1
- package/dist/utils/parseDiffFromFile.d.ts +1 -1
- package/dist/utils/parseDiffFromFile.d.ts.map +1 -1
- package/dist/utils/parseDiffFromFile.js +26 -5
- package/dist/utils/parseDiffFromFile.js.map +1 -1
- package/dist/utils/setWrapperNodeProps.js +0 -1
- package/dist/utils/setWrapperNodeProps.js.map +1 -1
- package/dist/utils/updateDiffHunks.d.ts +5 -1
- package/dist/utils/updateDiffHunks.d.ts.map +1 -1
- package/dist/utils/updateDiffHunks.js +26 -4
- package/dist/utils/updateDiffHunks.js.map +1 -1
- package/dist/worker/WorkerPoolManager.d.ts +7 -2
- package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
- package/dist/worker/WorkerPoolManager.js +78 -15
- package/dist/worker/WorkerPoolManager.js.map +1 -1
- package/dist/worker/index.d.ts +2 -2
- package/dist/worker/types.d.ts +7 -1
- package/dist/worker/types.d.ts.map +1 -1
- package/dist/worker/worker-portable.js +5 -3
- package/dist/worker/worker-portable.js.map +1 -1
- package/dist/worker/worker.js +5 -3
- package/dist/worker/worker.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selection.js","names":[],"sources":["../../src/editor/selection.ts"],"sourcesContent":["import type { DiffLineAnnotation } from '../types';\nimport { applyDocumentChangeToLineAnnotations } from './lineAnnotations';\nimport type {\n Position,\n Range,\n ResolvedTextEdit,\n TextDocument,\n TextDocumentChange,\n TextEdit,\n} from './textDocument';\nimport { endsWithLineBreak } from './utils';\n\nexport const DirectionBackward = -1;\nexport const DirectionNone = 0;\nexport const DirectionForward = 1;\n\nexport type SelectionDirection =\n | typeof DirectionBackward\n | typeof DirectionNone\n | typeof DirectionForward;\n\nexport interface EditorSelection extends Range {\n direction: SelectionDirection;\n}\n\n/**\n * Converts a selection from a web selection to an editor selection.\n */\nexport function convertSelection(\n range: StaticRange,\n direction: SelectionDirection = DirectionNone\n): EditorSelection | undefined {\n const start = boundaryToPosition(range.startContainer, range.startOffset);\n const end = boundaryToPosition(range.endContainer, range.endOffset);\n if (start === null || end === null) {\n return undefined;\n }\n return {\n start,\n end,\n direction,\n };\n}\n\n/**\n * Resolves the indent edits for a selection.\n */\nexport function resolveIndentEdits(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection,\n tabSize: number,\n outdent: boolean\n): [edits: TextEdit[], nextSelection: EditorSelection] {\n if (textDocument === undefined) {\n return [[], selection];\n }\n const { start, end } = selection;\n const edits: TextEdit[] = [];\n let newSelection: EditorSelection = { ...selection };\n let endLine = end.line;\n if (start.line < end.line && end.character === 0) {\n endLine--;\n }\n for (let line = start.line; line <= endLine; line++) {\n const lineText = textDocument.getLineText(line);\n if (lineText === undefined) {\n continue;\n }\n const indentUnit = lineText.startsWith('\\t') ? '\\t' : ' '.repeat(tabSize);\n let deleteLength = 0;\n let newText = indentUnit;\n if (outdent) {\n if (lineText.startsWith('\\t')) {\n deleteLength = 1;\n } else if (lineText.startsWith(' ')) {\n const leadingSpacesLength =\n lineText.length - lineText.trimStart().length;\n deleteLength = Math.min(indentUnit.length, leadingSpacesLength);\n }\n if (deleteLength === 0) {\n continue;\n }\n newText = '';\n }\n edits.push({\n range: {\n start: { line, character: 0 },\n end: { line, character: deleteLength },\n },\n newText,\n });\n const delta = newText.length - deleteLength;\n if (line === start.line) {\n newSelection = {\n ...newSelection,\n start: {\n ...start,\n character: Math.max(0, start.character + delta),\n },\n };\n }\n if (line === end.line) {\n newSelection = {\n ...newSelection,\n end: {\n ...end,\n character: Math.max(0, end.character + delta),\n },\n };\n }\n }\n return [edits, newSelection];\n}\n\n/**\n * Maps the cursor move to all selections.\n */\nexport function mapCursorMove(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n const lineCount = textDocument.lineCount;\n return selections.map((selection) => {\n let { line, character } =\n shortcut === 'up' || shortcut === 'left'\n ? selection.start\n : selection.end;\n if (\n shortcut === 'textStart' ||\n shortcut === 'start' ||\n shortcut === 'end'\n ) {\n if (shortcut === 'textStart') {\n const indent = getLeadingSpaces(textDocument.getLineText(line));\n character = character === indent ? 0 : indent;\n } else {\n character = shortcut === 'start' ? 0 : textDocument.getLineLength(line);\n }\n if (selection.direction === DirectionBackward) {\n line = selection.start.line;\n } else {\n line = selection.end.line;\n }\n } else if (shortcut === 'up') {\n line = Math.max(0, line - 1);\n } else if (shortcut === 'down') {\n line = Math.min(Math.max(lineCount - 1, 0), line + 1);\n } else if (isCollapsedSelection(selection)) {\n const lineLength = textDocument.getLineLength(line);\n character = Math.min(character, lineLength);\n if (shortcut === 'left') {\n if (character > 0) {\n // Step left by a whole grapheme so the caret never lands inside an\n // emoji or other multi-code-unit character.\n character = stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n false\n );\n } else if (line > 0) {\n line = line - 1;\n character = textDocument.getLineLength(line);\n }\n } else if (character < lineLength) {\n character = stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n true\n );\n } else if (line < lineCount - 1) {\n line = line + 1;\n character = 0;\n }\n }\n const pos = { line, character };\n return {\n start: pos,\n end: pos,\n direction: DirectionNone,\n };\n });\n}\n\n/**\n * Same as mapCursorMove, but with shift key pressed.\n */\nexport function mapSelectionShift(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n return selections.map((selection) => {\n const focusPosition =\n selection.direction === DirectionBackward\n ? selection.start\n : selection.end;\n const [movedFocusSelection] = mapCursorMove(\n textDocument,\n [\n {\n start: focusPosition,\n end: focusPosition,\n direction: DirectionNone,\n },\n ],\n shortcut\n );\n return createSelectionFrom(selection, movedFocusSelection);\n });\n}\n\n/**\n * Applies a text change to the given text document\n */\nexport function applyTextChangeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n edit: ResolvedTextEdit,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n tabSize = 2,\n undoBoundary = false\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const primarySelection = selections[selections.length - 1];\n if (primarySelection === undefined) {\n return { nextSelections: [] };\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = selectionPositions.map((position) =>\n textDocument.offsetAt(position)\n );\n const primaryStartOffset = selectionOffsets[(selections.length - 1) * 2];\n const primaryEndOffset = selectionOffsets[(selections.length - 1) * 2 + 1];\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const adjustedChange = normalizeLeadingIndentForChange(\n textDocument,\n edit,\n tabSize\n );\n const edits: ResolvedTextEdit[] = [];\n const nextSelectionOffsets: Array<[number, number] | undefined> = Array.from({\n length: selections.length,\n });\n let offsetDelta = 0;\n let mergedGroup:\n | {\n start: number;\n end: number;\n indices: number[];\n }\n | undefined;\n const finalizeMergedGroup = () => {\n if (mergedGroup === undefined) {\n return;\n }\n const perGroupChange = normalizeLeadingIndentForChange(\n textDocument,\n {\n start: mergedGroup.start,\n end: mergedGroup.end,\n text: adjustedChange.text,\n },\n tabSize\n );\n const newText = expandSingleNewlineInsert(\n textDocument,\n perGroupChange.text,\n perGroupChange.start\n );\n edits.push({\n start: perGroupChange.start,\n end: perGroupChange.end,\n text: newText,\n });\n const nextOffsets: [number, number] = [\n mergedGroup.start + offsetDelta + newText.length,\n mergedGroup.start + offsetDelta + newText.length,\n ];\n for (const index of mergedGroup.indices) {\n nextSelectionOffsets[index] = nextOffsets;\n }\n offsetDelta += newText.length - (perGroupChange.end - perGroupChange.start);\n mergedGroup = undefined;\n };\n for (const entry of ordered) {\n const startOffset = Math.max(\n 0,\n entry.start + (adjustedChange.start - primaryStartOffset)\n );\n const endOffset = Math.max(\n startOffset,\n entry.end + (adjustedChange.end - primaryEndOffset)\n );\n if (mergedGroup !== undefined && startOffset < mergedGroup.end) {\n mergedGroup.end = Math.max(mergedGroup.end, endOffset);\n mergedGroup.indices.push(entry.index);\n continue;\n }\n finalizeMergedGroup();\n mergedGroup = {\n start: startOffset,\n end: endOffset,\n indices: [entry.index],\n };\n }\n finalizeMergedGroup();\n\n const change = textDocument.applyResolvedEdits(\n edits,\n true,\n selections,\n undefined,\n undoBoundary\n );\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsets.map((offsets) => {\n if (offsets === undefined) {\n throw new Error('Missing next selection offsets');\n }\n return offsets;\n })\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Returns the next anchor/focus offsets after replacing a selection range.\n * When the inserted text still contains the original selection (auto-surround),\n * the inner range is reselected to match VS Code/CodeMirror behavior.\n */\nfunction getNextSelectionOffsetPairAfterReplace(\n textDocument: TextDocument<unknown>,\n entry: { start: number; end: number },\n offsetDelta: number,\n newText: string\n): [number, number] {\n const insertStart = entry.start + offsetDelta;\n const insertEnd = insertStart + newText.length;\n const originalLength = entry.end - entry.start;\n if (originalLength > 0) {\n const originalText = textDocument.getText().slice(entry.start, entry.end);\n const preservedOffset = newText.indexOf(originalText);\n if (\n preservedOffset !== -1 &&\n preservedOffset + originalText.length <= newText.length\n ) {\n const rangeStart = insertStart + preservedOffset;\n return [rangeStart, rangeStart + originalText.length];\n }\n }\n return [insertEnd, insertEnd];\n}\n\n/**\n * Applies a text replace to multiple selections.\n */\nexport function applyTextReplaceToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n texts: string[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n undoBoundary = false\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n if (selections.length !== texts.length) {\n throw new Error(\n 'Selection text replacements must match the selection count'\n );\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = selectionPositions.map((position) =>\n textDocument.offsetAt(position)\n );\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n text: string;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n text: texts[index],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const allDeletes = texts.every((text) => text === '');\n let edits: ResolvedTextEdit[];\n const nextSelectionOffsetPairs: Array<[number, number] | undefined> =\n Array.from({\n length: selections.length,\n });\n if (allDeletes) {\n edits = [];\n let hasEffect = false;\n for (const entry of ordered) {\n nextSelectionOffsetPairs[entry.index] = [entry.end, entry.end];\n if (entry.start >= entry.end) {\n continue;\n }\n hasEffect = true;\n const last = edits[edits.length - 1];\n if (last !== undefined && entry.start < last.end) {\n edits[edits.length - 1] = {\n start: last.start,\n end: Math.max(last.end, entry.end),\n text: '',\n };\n } else {\n edits.push({ start: entry.start, end: entry.end, text: '' });\n }\n }\n if (!hasEffect) {\n return { nextSelections: selections };\n }\n for (const entry of ordered) {\n const caret = entry.end;\n let delta = 0;\n let next = caret;\n for (const edit of edits) {\n if (caret <= edit.start) {\n break;\n }\n if (caret >= edit.end) {\n delta -= edit.end - edit.start;\n continue;\n }\n next = edit.start + delta;\n break;\n }\n if (next === caret) {\n next += delta;\n }\n nextSelectionOffsetPairs[entry.index] = [next, next];\n }\n } else {\n edits = [];\n let offsetDelta = 0;\n let previousEditEnd = -1;\n for (const entry of ordered) {\n if (entry.start < previousEditEnd) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n previousEditEnd = entry.end;\n const newText = expandSingleNewlineInsert(\n textDocument,\n entry.text,\n entry.start\n );\n edits.push({\n start: entry.start,\n end: entry.end,\n text: newText,\n });\n nextSelectionOffsetPairs[entry.index] =\n getNextSelectionOffsetPairAfterReplace(\n textDocument,\n entry,\n offsetDelta,\n newText\n );\n offsetDelta += newText.length - (entry.end - entry.start);\n }\n }\n\n const change = textDocument.applyResolvedEdits(\n edits,\n true,\n selections,\n undefined,\n undoBoundary\n );\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsetPairs.map((offsets) => {\n if (offsets === undefined) {\n throw new Error('Missing next selection offsets');\n }\n return offsets;\n })\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\nconst SURROUNDING_PAIRS: Array<[openChar: string, closeChar: string]> = [\n [\"'\", \"'\"],\n ['\"', '\"'],\n ['`', '`'],\n ['{', '}'],\n ['[', ']'],\n ['<', '>'],\n ['(', ')'],\n];\n\nconst AUTO_SURROUND_CLOSE_CHARS = new Map(SURROUNDING_PAIRS);\nconst AUTO_SURROUND_QUOTE_CHARS = new Set([\"'\", '\"', '`']);\nconst AUTO_SURROUND_BRACKET_CHARS = new Set(['{', '[', '(', '<']);\n\nexport type AutoSurround =\n | 'default'\n | 'never'\n | 'brackets'\n | 'quotes'\n | 'languageDefined';\n\nfunction shouldAutoSurroundChar(\n autoSurround: AutoSurround | undefined,\n char: string\n): boolean {\n if (autoSurround === 'never') {\n return false;\n }\n if (autoSurround === 'brackets') {\n return AUTO_SURROUND_BRACKET_CHARS.has(char);\n }\n if (autoSurround === 'quotes') {\n return AUTO_SURROUND_QUOTE_CHARS.has(char);\n }\n return true;\n}\n\n/**\n * Returns per-selection replacement text when typing a surround character over\n * non-collapsed selections, matching VS Code auto-surround behavior.\n */\nexport function getAutoSurroundReplacementTexts<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n char: string,\n autoSurround?: AutoSurround\n): string[] | undefined {\n if (char.length !== 1 || selections.length === 0) {\n return undefined;\n }\n const closeChar = AUTO_SURROUND_CLOSE_CHARS.get(char);\n if (closeChar === undefined || !shouldAutoSurroundChar(autoSurround, char)) {\n return undefined;\n }\n const replacements: string[] = [];\n for (const selection of selections) {\n if (isCollapsedSelection(selection)) {\n return undefined;\n }\n replacements.push(char + textDocument.getText(selection) + closeChar);\n }\n return replacements;\n}\n\n/**\n * Swaps the two characters adjacent to a collapsed selection, matching browser\n * insertTranspose (Ctrl+T) behavior.\n */\nexport function applyTransposeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const text = textDocument.getText();\n const edits: ResolvedTextEdit[] = [];\n const nextOffsetPairs: Array<[number, number]> = [];\n\n for (const selection of selections) {\n const [anchor, focus] = getSelectionAnchorAndFocusOffsets(\n textDocument,\n selection\n );\n if (!isCollapsedSelection(selection)) {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n const { line, character } = selection.start;\n const offset = anchor;\n const lineText = textDocument.getLineText(line);\n const lineLength = lineText.length;\n // Document offset of column 0 on this line, used to translate the\n // grapheme-cluster columns below back into document offsets.\n const lineStart = offset - character;\n const graphemeStarts = getLineGraphemeStarts(lineText);\n let edit: ResolvedTextEdit | undefined;\n\n if (character > 0 && character < lineLength) {\n // Swap the whole grapheme before the caret with the one after it so a\n // surrogate pair (emoji) on either side is moved intact.\n const before = findClusterBreak(\n lineText,\n character,\n false,\n graphemeStarts\n );\n const after = findClusterBreak(lineText, character, true, graphemeStarts);\n edit = {\n start: lineStart + before,\n end: lineStart + after,\n text:\n lineText.slice(character, after) + lineText.slice(before, character),\n };\n nextOffsetPairs.push([lineStart + after, lineStart + after]);\n } else if (character === lineLength && graphemeStarts.length >= 2) {\n // Swap the last two graphemes on the line.\n const lastStart = graphemeStarts[graphemeStarts.length - 1];\n const secondLastStart = graphemeStarts[graphemeStarts.length - 2];\n edit = {\n start: lineStart + secondLastStart,\n end: offset,\n text:\n lineText.slice(lastStart, lineLength) +\n lineText.slice(secondLastStart, lastStart),\n };\n nextOffsetPairs.push([offset, offset]);\n } else if (character === 0 && line > 0 && lineLength > 0) {\n // Carry the previous line's last grapheme and this line's first grapheme\n // across the line break, swapping their order.\n const prevLine = line - 1;\n const prevLineText = textDocument.getLineText(prevLine);\n const prevLength = prevLineText.length;\n const prevEnd = textDocument.offsetAt({\n line: prevLine,\n character: prevLength,\n });\n const prevGraphemeStart =\n prevLength > 0\n ? findClusterBreak(\n prevLineText,\n prevLength,\n false,\n getLineGraphemeStarts(prevLineText)\n )\n : prevLength;\n const firstEnd = findClusterBreak(lineText, 0, true, graphemeStarts);\n const prevStart = prevEnd - (prevLength - prevGraphemeStart);\n const newText =\n lineText.slice(0, firstEnd) +\n text.slice(prevEnd, offset) +\n prevLineText.slice(prevGraphemeStart, prevLength);\n edit = {\n start: prevStart,\n end: offset + firstEnd,\n text: newText,\n };\n const caret = prevStart + newText.length;\n nextOffsetPairs.push([caret, caret]);\n } else {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n edits.push(edit);\n }\n\n if (edits.length === 0) {\n return { nextSelections: selections };\n }\n\n edits.sort((a, b) => a.start - b.start);\n for (let index = 1; index < edits.length; index++) {\n if (edits[index].start < edits[index - 1].end) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n }\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextOffsetPairs\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Deletes from each selection to the end of its line, including the line break\n * when the caret is already at the end of a non-final line. Non-collapsed\n * selections delete their selected text instead.\n */\nexport function applyDeleteHardLineForwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n const range = resolveDeleteHardLineForwardRange(textDocument, selection);\n return {\n start: range.start,\n end: range.end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Deletes from each selection back to the start of its soft (visual) line.\n * Non-collapsed selections delete their selected text instead.\n */\nexport function applyDeleteSoftLineBackwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n getSoftLineStart?: (line: number, character: number) => number,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n if (!isCollapsedSelection(selection)) {\n return {\n start: selection.start,\n end: selection.end,\n direction: DirectionNone,\n };\n }\n const caret = getCaretPosition(selection);\n const { line, character } = caret;\n const softLineStart = getSoftLineStart?.(line, character) ?? 0;\n if (character > softLineStart) {\n return {\n start: { line, character: softLineStart },\n end: { line, character },\n direction: DirectionNone,\n };\n }\n if (line === 0) {\n return {\n start: caret,\n end: caret,\n direction: DirectionNone,\n };\n }\n const prevLineLength = textDocument.getLineLength(line - 1);\n return {\n start: { line: line - 1, character: prevLineLength },\n end: { line, character: 0 },\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Deletes the word or separator group immediately before each selection.\n * Non-collapsed selections delete their selected text instead.\n */\nexport function applyDeleteWordBackwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n const [start, end] = resolveDeleteWordBackwardRange(\n textDocument,\n selection\n );\n return {\n start,\n end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Resolves the document range deleted by Backspace or Delete at a collapsed\n * caret. Non-collapsed selections delete their selected text instead.\n */\nexport function resolveDeleteCharacterRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection,\n forward: boolean\n): [start: Position, end: Position] {\n if (!isCollapsedSelection(selection)) {\n return [selection.start, selection.end];\n }\n\n const caret = getCaretPosition(selection);\n let { line, character } = caret;\n const lineLength = textDocument.getLineLength(line);\n const lineCount = textDocument.lineCount;\n\n // A preserved vertical-move goal column can overshoot a shorter line; clamp\n // before stepping so Backspace/Delete target the visible caret position.\n character = Math.min(character, lineLength);\n\n if (forward) {\n if (character < lineLength) {\n return [\n { line, character },\n {\n line,\n character: stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n true\n ),\n },\n ];\n }\n if (line < lineCount - 1) {\n return [\n { line, character: lineLength },\n { line: line + 1, character: 0 },\n ];\n }\n return [caret, caret];\n }\n\n if (character > 0) {\n return [\n {\n line,\n character: stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n false\n ),\n },\n { line, character },\n ];\n }\n if (line > 0) {\n const prevLineLength = textDocument.getLineLength(line - 1);\n return [\n { line: line - 1, character: prevLineLength },\n { line, character: 0 },\n ];\n }\n return [caret, caret];\n}\n\n/**\n * Deletes one grapheme (or selected text) at each selection.\n */\nexport function applyDeleteCharacterToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n forward: boolean,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n tabSize = 2\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n let [start, end] = resolveDeleteCharacterRange(\n textDocument,\n selection,\n forward\n );\n // Only grow a Backspace into a full indent unit for a collapsed caret. When\n // the user has an explicit selection, Backspace must delete exactly that\n // selection rather than expanding it to a whole soft tab.\n if (!forward && isCollapsedSelection(selection)) {\n const normalized = normalizeLeadingIndentForChange(\n textDocument,\n {\n start: textDocument.offsetAt(start),\n end: textDocument.offsetAt(end),\n text: '',\n },\n tabSize\n );\n start = textDocument.positionAt(normalized.start);\n end = textDocument.positionAt(normalized.end);\n }\n return {\n start,\n end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Checks if a selection is collapsed.\n */\nexport function isCollapsedSelection(\n selection: EditorSelection | Range\n): boolean {\n return (\n selection.start.line === selection.end.line &&\n selection.start.character === selection.end.character\n );\n}\n\n/**\n * Returns the caret (focus) position for a selection.\n */\nexport function getCaretPosition(selection: EditorSelection): Position {\n const { start, end, direction } = selection;\n return direction === DirectionBackward ? start : end;\n}\n\n/**\n * Checks if a line is editable.\n */\nexport function isLineEditable(lineType: string): boolean {\n return (\n lineType === 'context' ||\n lineType === 'context-expanded' ||\n lineType === 'change-addition'\n );\n}\n\n/**\n * Checks whether selections `a` and `b` intersect.\n */\nexport function selectionIntersects(\n a: EditorSelection | Range,\n b: EditorSelection | Range\n): boolean {\n const aCollapsed = isCollapsedSelection(a);\n const bCollapsed = isCollapsedSelection(b);\n if (aCollapsed && bCollapsed) {\n return comparePosition(a.start, b.start) === 0;\n }\n if (aCollapsed) {\n return (\n comparePosition(b.start, a.start) <= 0 &&\n comparePosition(a.start, b.end) <= 0\n );\n }\n if (bCollapsed) {\n return (\n comparePosition(a.start, b.start) <= 0 &&\n comparePosition(b.start, a.end) <= 0\n );\n }\n return (\n comparePosition(a.start, b.end) < 0 && comparePosition(b.start, a.end) < 0\n );\n}\n\n/**\n * Compares two positions.\n */\nexport function comparePosition(a: Position, b: Position): number {\n if (a.line !== b.line) {\n return a.line - b.line;\n }\n return a.character - b.character;\n}\n\n/**\n * Creates a selection from anchor and focus offsets.\n */\nexport function createSelectionFromAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n anchorOffset: number,\n focusOffset: number\n): EditorSelection {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n const start = Math.min(anchorOffset, focusOffset);\n const end = Math.max(anchorOffset, focusOffset);\n return {\n start: textDocument.positionAt(start),\n end: textDocument.positionAt(end),\n direction,\n };\n}\n\n/**\n * Maps a single offset from the pre-edit document into the post-edit document.\n * `edits` are resolved edits in pre-edit offsets, sorted ascending and\n * non-overlapping. An offset at or after an edit's start shifts to the end of\n * that edit's replacement (right gravity), so text inserted at the caret pushes\n * the caret past it; an offset strictly before an edit is only shifted by the\n * net length change of the edits that precede it.\n */\nfunction remapOffsetThroughEdits(\n offset: number,\n edits: readonly ResolvedTextEdit[]\n): number {\n let delta = 0;\n for (const edit of edits) {\n if (offset < edit.start) {\n break;\n }\n if (offset >= edit.end) {\n delta += edit.text.length - (edit.end - edit.start);\n } else {\n return edit.start + delta + edit.text.length;\n }\n }\n return offset + delta;\n}\n\n/**\n * Re-anchors selections after a batch of text edits has been applied, so the\n * caret keeps pointing at the same logical location in the changed buffer.\n *\n * `selectionOffsets` (one `[start, end]` pair per selection) and `edits` are\n * measured in the PRE-edit document; the returned selections are built from\n * `textDocument`, which must already reflect the applied edits. Selection\n * direction is preserved by remapping each edge and re-deriving anchor/focus.\n */\nexport function remapSelectionsAfterEdits(\n textDocument: TextDocument<unknown>,\n selections: readonly EditorSelection[],\n selectionOffsets: ReadonlyArray<readonly [number, number]>,\n edits: readonly ResolvedTextEdit[]\n): EditorSelection[] {\n return selections.map((selection, index) => {\n const [startOffset, endOffset] = selectionOffsets[index];\n const nextStart = remapOffsetThroughEdits(startOffset, edits);\n const nextEnd = remapOffsetThroughEdits(endOffset, edits);\n const anchorOffset =\n selection.direction === DirectionBackward ? nextEnd : nextStart;\n const focusOffset =\n selection.direction === DirectionBackward ? nextStart : nextEnd;\n return createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n anchorOffset,\n focusOffset\n );\n });\n}\n\n/**\n * Creates a selection from a anchor and focus selection.\n */\nexport function createSelectionFrom(\n anchorSelection: EditorSelection,\n focusSelection: EditorSelection\n): EditorSelection {\n const anchor =\n anchorSelection.direction === DirectionBackward\n ? anchorSelection.end\n : anchorSelection.start;\n const currentStartOrder = comparePosition(anchor, focusSelection.start);\n const currentEndOrder = comparePosition(anchor, focusSelection.end);\n let focus = focusSelection.end;\n if (currentStartOrder <= 0) {\n focus = focusSelection.end;\n } else if (currentEndOrder >= 0) {\n focus = focusSelection.start;\n } else {\n // When the original anchor sits inside `current`, keep whichever edge\n // stayed at the anchor so drag direction remains stable.\n focus = currentStartOrder === 0 ? focusSelection.end : focusSelection.start;\n }\n const anchorVsFocus = comparePosition(anchor, focus);\n const direction: SelectionDirection =\n anchorVsFocus === 0\n ? DirectionNone\n : anchorVsFocus < 0\n ? DirectionForward\n : DirectionBackward;\n const selectionStart = anchorVsFocus <= 0 ? anchor : focus;\n const selectionEnd = anchorVsFocus <= 0 ? focus : anchor;\n return {\n start: selectionStart,\n end: selectionEnd,\n direction,\n };\n}\n\n/**\n * Extends or shrinks the selection `original` using the endpoints of `target`, \\\n * matching contenteditable shift + click extend behavior.\n */\nexport function extendSelection(\n original: EditorSelection,\n target: EditorSelection\n): EditorSelection {\n const leftExtended = comparePosition(target.start, original.start) < 0;\n const rightExtended = comparePosition(target.end, original.end) > 0;\n\n if (leftExtended && !rightExtended) {\n return {\n start: target.start,\n end: original.end,\n direction: DirectionBackward,\n };\n }\n\n if (rightExtended && !leftExtended) {\n return {\n start: original.start,\n end: target.end,\n direction: DirectionForward,\n };\n }\n\n if (original.direction === DirectionBackward) {\n return {\n start: target.start,\n end: original.end,\n direction:\n comparePosition(target.start, original.end) === 0\n ? DirectionNone\n : DirectionBackward,\n };\n }\n\n return {\n start: original.start,\n end: target.end,\n direction:\n comparePosition(original.start, target.end) === 0\n ? DirectionNone\n : DirectionForward,\n };\n}\n\n/**\n * Extends multiple selections.\n */\nexport function extendSelections(\n selections: EditorSelection[],\n target: EditorSelection\n): EditorSelection[] {\n const newSelections = selections.map((selection) => {\n return extendSelection(selection, target);\n });\n return mergeOverlappingSelections(newSelections);\n}\n\n/**\n * Merges overlapping selections.\n */\nexport function mergeOverlappingSelections(\n selections: EditorSelection[]\n): EditorSelection[] {\n if (selections.length <= 1) {\n return selections;\n }\n const selected = new Set<number>();\n const accepted: {\n index: number;\n selection: EditorSelection;\n }[] = [];\n for (let i = selections.length - 1; i >= 0; i--) {\n const selection = selections[i];\n if (selection === undefined) {\n continue;\n }\n let left = 0;\n let right = accepted.length;\n while (left < right) {\n const mid = Math.floor((left + right) / 2);\n const candidate = accepted[mid]?.selection;\n if (candidate === undefined) {\n break;\n }\n if (comparePosition(candidate.start, selection.start) < 0) {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n const previous = accepted[left - 1]?.selection;\n const next = accepted[left]?.selection;\n if (\n (previous !== undefined && selectionIntersects(previous, selection)) ||\n (next !== undefined && selectionIntersects(next, selection))\n ) {\n continue;\n }\n accepted.splice(left, 0, { index: i, selection });\n selected.add(i);\n }\n return selections.filter((_, index) => selected.has(index));\n}\n\n/**\n * Finds the next matching word and updates the selections.\n */\nexport function findNexMatch(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): EditorSelection[] | undefined {\n if (selections.length === 0) {\n return undefined;\n }\n\n const normalizedSelections = selections.map((selection) =>\n isCollapsedSelection(selection)\n ? expandCollapsedSelectionToWord(textDocument, selection)\n : selection\n );\n const texts = normalizedSelections.map((s) => textDocument.getText(s));\n const needle = texts[0];\n if (needle.length === 0 || texts.some((t) => t !== needle)) {\n return undefined;\n }\n\n const occupied = normalizedSelections.map(\n (s) =>\n [textDocument.offsetAt(s.start), textDocument.offsetAt(s.end)] as [\n number,\n number,\n ]\n );\n const nextOffset = textDocument.findNextNonOverlappingSubstring(\n needle,\n occupied\n );\n if (nextOffset === undefined) {\n return normalizedSelections.some((selection, index) => {\n const original = selections[index];\n return (\n comparePosition(selection.start, original.start) !== 0 ||\n comparePosition(selection.end, original.end) !== 0 ||\n selection.direction !== original.direction\n );\n })\n ? normalizedSelections\n : undefined;\n }\n const added = createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n nextOffset,\n nextOffset + needle.length\n );\n return [...normalizedSelections, added];\n}\n\n/**\n * Get the full selection of the document.\n */\nexport function getDocumentFullSelection(\n textDocument: TextDocument<unknown>\n): EditorSelection {\n const lastLine = textDocument.lineCount - 1;\n const lastCharacter = textDocument.getLineLength(lastLine);\n return {\n start: { line: 0, character: 0 },\n end: { line: lastLine, character: lastCharacter },\n direction: DirectionForward,\n };\n}\n\n/**\n * Get the boundary selection of the document.\n */\nexport function getDocumentBoundarySelection(\n textDocument: TextDocument<unknown>,\n atEnd: boolean\n): EditorSelection {\n const line = atEnd ? textDocument.lineCount - 1 : 0;\n const character = atEnd ? textDocument.getLineLength(line) : 0;\n const start = { line, character };\n return {\n start: start,\n end: start,\n direction: DirectionForward,\n };\n}\n\ninterface ClipboardRegion {\n start: number;\n end: number;\n}\n\n/**\n * Resolves the document offset range each selection contributes to the\n * clipboard, ordered by position. A collapsed selection contributes its whole\n * logical line including the trailing line break; the final line has no\n * trailing break to include. A ranged selection contributes the selected text.\n */\nfunction resolveClipboardRegions(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): ClipboardRegion[] {\n return selections\n .map((selection) => {\n if (isCollapsedSelection(selection)) {\n const line = selection.start.line;\n const start = textDocument.offsetAt({ line, character: 0 });\n const end =\n line < textDocument.lineCount - 1\n ? textDocument.offsetAt({ line: line + 1, character: 0 })\n : textDocument.offsetAt({\n line,\n character: textDocument.getLineLength(line),\n });\n return { start, end };\n }\n const start = textDocument.offsetAt(selection.start);\n const end = textDocument.offsetAt(selection.end);\n return start <= end ? { start, end } : { start: end, end: start };\n })\n .sort((a, b) => {\n const startOrder = a.start - b.start;\n return startOrder !== 0 ? startOrder : a.end - b.end;\n });\n}\n\n/**\n * Get the clipboard text of the selections for the given text document. Used by\n * both copy and cut so the two stay in sync. Overlapping regions (e.g. several\n * carets on one line) are merged so the same text is never emitted twice, and a\n * line-ending separator is inserted only between regions that aren't already\n * contiguous in the document.\n */\nexport function getSelectionText(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): string {\n const regions = resolveClipboardRegions(textDocument, selections);\n const eol = textDocument.eol;\n let result = '';\n let prevEnd = -1;\n for (const region of regions) {\n if (region.end <= region.start) {\n continue;\n }\n if (region.start <= prevEnd) {\n // Contiguous with or overlapping the previous region: extend it rather\n // than repeat any shared text.\n if (region.end > prevEnd) {\n result += textDocument.getTextSlice(prevEnd, region.end);\n prevEnd = region.end;\n }\n continue;\n }\n if (result.length > 0 && !endsWithLineBreak(result)) {\n result += eol;\n }\n result += textDocument.getTextSlice(region.start, region.end);\n prevEnd = region.end;\n }\n return result;\n}\n\ninterface SelectionCut {\n index: number;\n edit: ResolvedTextEdit;\n}\n\n// Resolves the deletion an individual selection contributes to a cut: a\n// collapsed caret removes its whole logical line, while a ranged selection\n// removes only the selected text.\nfunction resolveSelectionCutEdit(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): ResolvedTextEdit {\n if (isCollapsedSelection(selection)) {\n return resolveCollapsedSelectionCutEdit(textDocument, selection);\n }\n const [start, end] =\n comparePosition(selection.start, selection.end) <= 0\n ? [selection.start, selection.end]\n : [selection.end, selection.start];\n return {\n start: textDocument.offsetAt(start),\n end: textDocument.offsetAt(end),\n text: '',\n };\n}\n\n// A caret-only cut removes the whole logical line. Non-final lines delete\n// their trailing line break; the final line deletes the preceding break so\n// no empty line is left behind.\nfunction resolveCollapsedSelectionCutEdit(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): ResolvedTextEdit {\n const line = selection.start.line;\n const lineStart = textDocument.offsetAt({ line, character: 0 });\n const lineEnd = textDocument.offsetAt({\n line,\n character: textDocument.getLineLength(line),\n });\n\n if (line < textDocument.lineCount - 1) {\n const nextLineStart = textDocument.offsetAt({\n line: line + 1,\n character: 0,\n });\n return { start: lineStart, end: nextLineStart, text: '' };\n }\n\n if (line > 0) {\n const previousLineEnd = textDocument.offsetAt({\n line: line - 1,\n character: textDocument.getLineLength(line - 1),\n });\n return { start: previousLineEnd, end: lineEnd, text: '' };\n }\n\n return { start: lineStart, end: lineEnd, text: '' };\n}\n\nfunction mergeCutEdits(orderedCuts: SelectionCut[]): ResolvedTextEdit[] {\n const edits: ResolvedTextEdit[] = [];\n for (const { edit } of orderedCuts) {\n if (edit.start >= edit.end) {\n continue;\n }\n const last = edits.at(-1);\n if (last !== undefined && edit.start <= last.end) {\n edits[edits.length - 1] = {\n start: last.start,\n end: Math.max(last.end, edit.end),\n text: '',\n };\n } else {\n edits.push(edit);\n }\n }\n return edits;\n}\n\nfunction mapCutSelectionOffsets(\n orderedCuts: SelectionCut[],\n edits: ResolvedTextEdit[]\n): number[] {\n const nextOffsets: number[] = Array.from({ length: orderedCuts.length });\n let editIndex = 0;\n let offsetDelta = 0;\n for (const cut of orderedCuts) {\n while (editIndex < edits.length && cut.edit.start > edits[editIndex].end) {\n const edit = edits[editIndex];\n offsetDelta -= edit.end - edit.start;\n editIndex++;\n }\n\n const edit = edits[editIndex];\n if (\n edit !== undefined &&\n cut.edit.start >= edit.start &&\n cut.edit.start <= edit.end\n ) {\n nextOffsets[cut.index] = edit.start + offsetDelta;\n } else {\n nextOffsets[cut.index] = cut.edit.start + offsetDelta;\n }\n }\n return nextOffsets;\n}\n\n// Resolves the clipboard text, merged deletions, and resulting caret offsets\n// for a cut. The clipboard text comes from the shared getSelectionText helper\n// so cut stays in sync with copy, while the deletions are computed per\n// selection and merged so overlapping cuts delete their shared text once.\nexport function resolveSelectionCut(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): {\n text: string;\n edits: ResolvedTextEdit[];\n nextSelectionOffsets: number[];\n} {\n const cuts = selections.map<SelectionCut>((selection, index) => ({\n index,\n edit: resolveSelectionCutEdit(textDocument, selection),\n }));\n const orderedCuts = [...cuts].sort((a, b) => {\n const startOrder = a.edit.start - b.edit.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.edit.end - b.edit.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n const edits = mergeCutEdits(orderedCuts);\n return {\n text: getSelectionText(textDocument, selections),\n edits,\n nextSelectionOffsets: mapCutSelectionOffsets(orderedCuts, edits),\n };\n}\n\n/**\n * Get the anchor node and offset for a selection.\n */\nexport function getSelectionAnchor(\n lineElement: HTMLElement,\n character: number\n): [Node, number] {\n const ch = Math.max(0, character);\n const tokens = collectTokens(lineElement);\n\n let last: HTMLElement | null = null;\n for (const token of tokens) {\n last = token;\n const base = getCharacterIndex(token)!;\n const end = base + (token.textContent?.length ?? 0);\n if (ch <= end) {\n const anchor = textAt(token, ch < base ? 0 : ch - base);\n if (anchor !== null) {\n return anchor;\n }\n }\n }\n\n if (last !== null) {\n const anchor = textAt(last, last.textContent?.length ?? 0);\n if (anchor !== null) {\n return anchor;\n }\n return [last, 0];\n }\n\n let textOffset = 0;\n let lastTextNode: Text | null = null;\n for (const child of lineElement.childNodes) {\n if (child.nodeType === 1 && (child as HTMLElement).tagName === 'BR') {\n return [child, 0];\n }\n if (child.nodeType !== 3) {\n continue;\n }\n lastTextNode = child as Text;\n const len = getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n );\n if (ch <= textOffset + len) {\n return [\n lastTextNode,\n getTextOffset(lastTextNode.textContent, ch - textOffset),\n ];\n }\n textOffset += len;\n }\n\n if (lastTextNode !== null) {\n return [\n lastTextNode,\n getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n ),\n ];\n }\n return [lineElement, 0];\n}\n\n/**\n * Expands a zero-width selection to the word-like segment that contains the caret.\n */\nexport function expandCollapsedSelectionToWord(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): EditorSelection {\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const ch = Math.max(0, Math.min(character, lineText.length));\n const span = expandCollapsedLineWord(lineText, ch);\n if (span === undefined) {\n return selection;\n }\n return {\n start: { line, character: span.start },\n end: { line, character: span.end },\n direction: DirectionForward,\n };\n}\n\nfunction expandCollapsedLineWord(\n lineText: string,\n character: number\n): { start: number; end: number } | undefined {\n const segmenter = new Intl.Segmenter(undefined, {\n granularity: 'word',\n });\n for (const seg of segmenter.segment(lineText)) {\n if (seg.isWordLike !== true) {\n continue;\n }\n const lo = seg.index;\n const hi = lo + seg.segment.length;\n // Match when the cursor is inside the word or immediately touching\n // one of its boundaries — not when separated by non-word characters.\n if (character >= lo && character <= hi) {\n return { start: lo, end: hi };\n }\n }\n return undefined;\n}\n\n// Resolves the range removed by deleteWordBackward for one selection.\nfunction resolveDeleteWordBackwardRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): [start: Position, end: Position] {\n if (!isCollapsedSelection(selection)) {\n return [selection.start, selection.end];\n }\n const caret = getCaretPosition(selection);\n const { line, character: head } = caret;\n if (head === 0) {\n if (line === 0) {\n return [caret, caret];\n }\n const prevLineLength = textDocument.getLineLength(line - 1);\n return [\n { line: line - 1, character: prevLineLength },\n { line, character: 0 },\n ];\n }\n const lineText = textDocument.getLineText(line);\n const graphemeStarts = getLineGraphemeStarts(lineText);\n let pos = head;\n let match: number | undefined;\n while (pos > 0) {\n const prev = findClusterBreak(lineText, pos, false, graphemeStarts);\n const nextChar = lineText.slice(prev, pos);\n const nextMatch = !/\\S/.test(nextChar)\n ? 0\n : /\\p{Alphabetic}|\\p{Number}|_/u.test(nextChar)\n ? 1\n : 2;\n if (match !== undefined && nextMatch !== match) {\n break;\n }\n if (nextChar !== ' ' || pos !== head) {\n match = nextMatch;\n }\n pos = prev;\n }\n return [\n { line, character: pos },\n { line, character: head },\n ];\n}\n\nfunction findClusterBreak(\n text: string,\n pos: number,\n forward: boolean,\n graphemeStarts: number[]\n): number {\n if (forward) {\n for (const start of graphemeStarts) {\n if (start > pos) {\n return start;\n }\n }\n return text.length;\n }\n for (let i = graphemeStarts.length - 1; i >= 0; i--) {\n const start = graphemeStarts[i];\n if (start < pos) {\n return start;\n }\n }\n return 0;\n}\n\nconst graphemeSegmenter = new Intl.Segmenter(undefined, {\n granularity: 'grapheme',\n});\n\n// Lists the start column of every grapheme cluster on a line (always starting\n// at 0). Used to step the caret and to transpose by whole graphemes so a\n// surrogate pair (emoji) or combining sequence is never split.\nfunction getLineGraphemeStarts(lineText: string): number[] {\n const graphemeStarts = [0];\n for (const segment of graphemeSegmenter.segment(lineText)) {\n if (segment.index > 0) {\n graphemeStarts.push(segment.index);\n }\n }\n return graphemeStarts;\n}\n\n// Returns the character column one grapheme cluster to the left or right of\n// `character` on `line`. Stepping by grapheme rather than by a single UTF-16\n// code unit keeps the caret from landing inside an emoji or other multi-unit\n// character. Crossing a line boundary is left to the caller.\nfunction stepCharacterByGrapheme(\n textDocument: TextDocument<unknown>,\n line: number,\n character: number,\n forward: boolean\n): number {\n const lineLength = textDocument.getLineLength(line);\n if (forward) {\n if (character >= lineLength) {\n return lineLength;\n }\n const lineStart = textDocument.offsetAt({ line, character: 0 });\n const suffix = textDocument.getTextSlice(\n lineStart + character,\n lineStart + lineLength\n );\n for (const segment of graphemeSegmenter.segment(suffix)) {\n return character + segment.segment.length;\n }\n return lineLength;\n }\n if (character <= 0) {\n return 0;\n }\n const lineStart = textDocument.offsetAt({ line, character: 0 });\n const prefix = textDocument.getTextSlice(lineStart, lineStart + character);\n let prevStart = 0;\n for (const segment of graphemeSegmenter.segment(prefix)) {\n prevStart = segment.index;\n }\n return prevStart;\n}\n\nfunction getSelectionAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): [anchorOffset: number, focusOffset: number] {\n const isBackward = selection.direction === DirectionBackward;\n return [\n textDocument.offsetAt(isBackward ? selection.end : selection.start),\n textDocument.offsetAt(getCaretPosition(selection)),\n ];\n}\n\n// Resolves the range removed by deleteHardLineForward for one selection.\nfunction resolveDeleteHardLineForwardRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): Range {\n if (!isCollapsedSelection(selection)) {\n return { start: selection.start, end: selection.end };\n }\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const lineLength = lineText.length;\n if (character < lineLength) {\n return {\n start: { line, character },\n end: { line, character: lineLength },\n };\n }\n if (line < textDocument.lineCount - 1) {\n return {\n start: { line, character },\n end: { line: line + 1, character: 0 },\n };\n }\n return {\n start: { line, character },\n end: { line, character },\n };\n}\n\n// When the user inserts a lone line break, copy the current line's indentation onto the new line.\nfunction expandSingleNewlineInsert(\n textDocument: TextDocument<unknown>,\n insertText: string,\n insertStartOffset: number\n): string {\n if (insertText !== '\\n' && insertText !== '\\r' && insertText !== '\\r\\n') {\n return insertText;\n }\n const line = textDocument.positionAt(insertStartOffset).line;\n const lineText = textDocument.getLineText(line);\n const indentLen = getLeadingSpaces(lineText);\n if (indentLen === 0) {\n return insertText;\n }\n return insertText + lineText.slice(0, indentLen);\n}\n\nfunction getLeadingSpaces(text: string): number {\n let indent = 0;\n for (; indent < text.length; indent++) {\n const c = text.charCodeAt(indent);\n if (c !== /* space */ 32 && c !== /* tab */ 9) {\n break;\n }\n }\n return indent;\n}\n\nfunction createSelectionsFromOffsetPairs(\n textDocument: TextDocument<unknown>,\n offsetPairs: readonly [anchorOffset: number, focusOffset: number][]\n): EditorSelection[] {\n const normalizedOffsets: number[] = [];\n for (const [anchorOffset, focusOffset] of offsetPairs) {\n normalizedOffsets.push(\n Math.min(anchorOffset, focusOffset),\n Math.max(anchorOffset, focusOffset)\n );\n }\n const positions = textDocument.positionsAt(normalizedOffsets);\n return offsetPairs.map(([anchorOffset, focusOffset], index) => {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n return {\n start: positions[index * 2],\n end: positions[index * 2 + 1],\n direction,\n };\n });\n}\n\n// Expands a backspace over leading spaces into one soft-tab width so mixed hard/soft indentation\n// behaves like the explicit outdent command.\nfunction normalizeLeadingIndentForChange(\n textDocument: TextDocument<unknown>,\n change: ResolvedTextEdit,\n tabSize: number\n): ResolvedTextEdit {\n if (change.text !== '' || change.start !== change.end - 1) {\n return change;\n }\n const caretPosition = textDocument.positionAt(change.end);\n if (caretPosition.character === 0) {\n return change;\n }\n const lineText = textDocument.getLineText(caretPosition.line);\n const leadingText = lineText.slice(0, caretPosition.character);\n if (/[^ \\t]/.test(leadingText)) {\n return change;\n }\n if (lineText[caretPosition.character - 1] === '\\t') {\n return change;\n }\n const softTabStart = Math.max(0, caretPosition.character - tabSize);\n const softTabText = lineText.slice(softTabStart, caretPosition.character);\n if (softTabText.length === tabSize && /^ +$/.test(softTabText)) {\n return {\n ...change,\n start: change.end - softTabText.length,\n };\n }\n return change;\n}\n\nfunction boundaryToPosition(node: Node, offset: number): Position | null {\n const host = node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n let lineEl: HTMLElement | null = host;\n while (lineEl !== null && getLineIndex(lineEl) === undefined) {\n lineEl = lineEl.parentElement;\n }\n if (lineEl === null) {\n return null;\n }\n const line = getLineIndex(lineEl);\n if (line === undefined) {\n return null;\n }\n\n if (node.nodeType === 3) {\n if (node.parentElement === null) {\n return null;\n }\n if (findTokenSpan(node.parentElement) !== null) {\n return { line, character: getLineChildEnd(node, offset) };\n }\n return {\n line,\n character:\n offsetBefore(lineEl, node) + getTextOffset(node.textContent, offset),\n };\n }\n\n if (node.nodeType === 1) {\n const el = node as HTMLElement;\n if (el.tagName === 'DIV') {\n let character = 0;\n for (let i = 0; i < offset; i++) {\n character = getLineChildEnd(el.childNodes[i]);\n }\n return { line, character };\n }\n if (el.tagName === 'BR') {\n return { line, character: 0 };\n }\n if (el.tagName === 'SPAN') {\n if (offset < el.childNodes.length) {\n const next = el.childNodes[offset];\n if (next?.nodeType === 1) {\n const nextBase = getCharacterIndex(next as HTMLElement);\n if (nextBase !== undefined) {\n return { line, character: nextBase };\n }\n const token = findTokenSpan(next as HTMLElement);\n const tokenBase =\n token === null ? undefined : getCharacterIndex(token);\n if (tokenBase !== undefined) {\n return { line, character: tokenBase };\n }\n }\n }\n return {\n line,\n character:\n offset > 0\n ? getLineChildEnd(el.childNodes[offset - 1])\n : offsetBefore(lineEl, el),\n };\n }\n return { line, character: offsetBefore(lineEl, el) };\n }\n return null;\n}\n\nfunction collectTokens(line: HTMLElement): HTMLElement[] {\n const tokens: HTMLElement[] = [];\n for (const child of line.childNodes) {\n if (child.nodeType !== 1) {\n continue;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN') {\n continue;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n tokens.push(el);\n continue;\n }\n for (const nested of el.childNodes) {\n if (\n nested.nodeType === 1 &&\n getCharacterIndex(nested as HTMLElement) !== undefined\n ) {\n tokens.push(nested as HTMLElement);\n }\n }\n }\n return tokens;\n}\n\nfunction textAt(token: HTMLElement, offset: number): [Node, number] | null {\n let remaining = Math.max(0, offset);\n const stack: Array<{ container: Node; index: number }> = [\n { container: token, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n frame.index++;\n if (walkNode.nodeType === 3) {\n const len = getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n if (remaining <= len) {\n return [walkNode, remaining];\n }\n remaining -= len;\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return null;\n}\n\nfunction textLengthBefore(root: Node, target: Node): number {\n let before = 0;\n const stack: Array<{ container: Node; index: number }> = [\n { container: root, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n if (walkNode === target) {\n return before;\n }\n frame.index++;\n if (walkNode.nodeType === 3) {\n before += getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return before;\n}\n\nfunction isInside(token: HTMLElement, node: Node): boolean {\n let current: Node | null = node;\n while (current !== null) {\n if (current === token) {\n return true;\n }\n current = current.parentElement;\n }\n return false;\n}\n\nfunction offsetBefore(line: HTMLElement, node: Node): number {\n if (node.parentElement === line) {\n let offset = 0;\n const index = Array.prototype.indexOf.call(line.childNodes, node);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(line.childNodes[i]);\n }\n return offset;\n }\n for (const token of collectTokens(line)) {\n if (isInside(token, node)) {\n const base = getCharacterIndex(token)!;\n return base + (node.nodeType === 3 ? textLengthBefore(token, node) : 0);\n }\n }\n let offset = 0;\n let target: HTMLElement | null =\n node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n while (target !== null && target.parentElement !== null) {\n if (getLineIndex(target.parentElement) !== undefined) {\n break;\n }\n const parent = target.parentElement;\n const index = Array.prototype.indexOf.call(parent.childNodes, target);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(parent.childNodes[i]);\n }\n target = parent;\n }\n return offset;\n}\n\nfunction findTokenSpan(el: HTMLElement): HTMLElement | null {\n let current: HTMLElement | null = el;\n while (current !== null) {\n if (getLineIndex(current) !== undefined) {\n return null;\n }\n if (getCharacterIndex(current) !== undefined) {\n return current;\n }\n current = current.parentElement;\n }\n return null;\n}\n\nfunction getLineChildEnd(\n child: Node | undefined,\n textOffsetInChild?: number\n): number {\n if (child === undefined) {\n return 0;\n }\n if (child.nodeType === 3) {\n const parent = child.parentElement;\n if (parent === null) {\n return 0;\n }\n const token = findTokenSpan(parent);\n if (token === null) {\n return 0;\n }\n const base = getCharacterIndex(token);\n if (base === undefined) {\n return 0;\n }\n const length =\n textOffsetInChild === undefined\n ? getTextOffset(child.textContent, child.textContent?.length ?? 0)\n : getTextOffset(child.textContent, textOffsetInChild);\n return base + textLengthBefore(token, child) + length;\n }\n if (child.nodeType !== 1) {\n return 0;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN' && el.tagName !== 'BR') {\n return 0;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n return base + (el.textContent?.length ?? 0);\n }\n let end = 0;\n for (const token of el.childNodes) {\n end = Math.max(end, getLineChildEnd(token));\n }\n return end;\n}\n\nfunction getLineIndex(el: HTMLElement): number | undefined {\n const { line, lineType } = el.dataset;\n if (line !== undefined && lineType !== 'change-deletion') {\n const lineNumber = parseInt(line, 10);\n if (!Number.isNaN(lineNumber)) {\n return lineNumber - 1;\n }\n }\n return undefined;\n}\n\nfunction getCharacterIndex(el: HTMLElement): number | undefined {\n const { char } = el.dataset;\n if (char !== undefined) {\n const charIndex = parseInt(char, 10);\n if (!Number.isNaN(charIndex)) {\n return charIndex;\n }\n }\n return undefined;\n}\n\nfunction getTextOffset(\n text: string | null | undefined,\n offset: number\n): number {\n const value = text ?? '';\n const lineBreakIndex = value.search(/[\\r\\n]/);\n return Math.min(\n offset,\n lineBreakIndex === -1 ? value.length : lineBreakIndex\n );\n}\n"],"mappings":";;;AAYA,MAAa,oBAAoB;AACjC,MAAa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,kBAAkB,IAAqC;CAC9D,MAAM,EAAE,SAAS,GAAG;CACpB,IAAI,SAAS,KAAA,GAAW;EACtB,MAAM,YAAY,SAAS,MAAM,EAAE;EACnC,IAAI,CAAC,OAAO,MAAM,SAAS,GACzB,OAAO;CAEX;AAEF;AAEA,SAAS,cACP,MACA,QACQ;CACR,MAAM,QAAQ,QAAQ;CACtB,MAAM,iBAAiB,MAAM,OAAO,QAAQ;CAC5C,OAAO,KAAK,IACV,QACA,mBAAmB,KAAK,MAAM,SAAS,cACzC;AACF"}
|
|
1
|
+
{"version":3,"file":"selection.js","names":[],"sources":["../../src/editor/selection.ts"],"sourcesContent":["import type { DiffLineAnnotation } from '../types';\nimport { applyDocumentChangeToLineAnnotations } from './lineAnnotations';\nimport type {\n Position,\n Range,\n ResolvedTextEdit,\n TextDocument,\n TextDocumentChange,\n TextEdit,\n} from './textDocument';\nimport {\n createSegmenter,\n endsWithLineBreak,\n getGraphemeSegmenter,\n} from './utils';\n\nexport const DirectionBackward = -1;\nexport const DirectionNone = 0;\nexport const DirectionForward = 1;\n\nexport type SelectionDirection =\n | typeof DirectionBackward\n | typeof DirectionNone\n | typeof DirectionForward;\n\nexport interface EditorSelection extends Range {\n direction: SelectionDirection;\n}\n\n/**\n * Converts a selection from a web selection to an editor selection.\n */\nexport function convertSelection(\n range: StaticRange,\n direction: SelectionDirection = DirectionNone\n): EditorSelection | undefined {\n const start = boundaryToPosition(range.startContainer, range.startOffset);\n const end = boundaryToPosition(range.endContainer, range.endOffset);\n if (start === null || end === null) {\n return undefined;\n }\n return {\n start,\n end,\n direction,\n };\n}\n\n/**\n * Resolves the indent edits for a selection.\n */\nexport function resolveIndentEdits(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection,\n tabSize: number,\n outdent: boolean\n): [edits: TextEdit[], nextSelection: EditorSelection] {\n if (textDocument === undefined) {\n return [[], selection];\n }\n const { start, end } = selection;\n const edits: TextEdit[] = [];\n let newSelection: EditorSelection = { ...selection };\n let endLine = end.line;\n if (start.line < end.line && end.character === 0) {\n endLine--;\n }\n for (let line = start.line; line <= endLine; line++) {\n const lineText = textDocument.getLineText(line);\n if (lineText === undefined) {\n continue;\n }\n const indentUnit = lineText.startsWith('\\t') ? '\\t' : ' '.repeat(tabSize);\n let deleteLength = 0;\n let newText = indentUnit;\n if (outdent) {\n if (lineText.startsWith('\\t')) {\n deleteLength = 1;\n } else if (lineText.startsWith(' ')) {\n const leadingSpacesLength =\n lineText.length - lineText.trimStart().length;\n deleteLength = Math.min(indentUnit.length, leadingSpacesLength);\n }\n if (deleteLength === 0) {\n continue;\n }\n newText = '';\n }\n edits.push({\n range: {\n start: { line, character: 0 },\n end: { line, character: deleteLength },\n },\n newText,\n });\n const delta = newText.length - deleteLength;\n if (line === start.line) {\n newSelection = {\n ...newSelection,\n start: {\n ...start,\n character: Math.max(0, start.character + delta),\n },\n };\n }\n if (line === end.line) {\n newSelection = {\n ...newSelection,\n end: {\n ...end,\n character: Math.max(0, end.character + delta),\n },\n };\n }\n }\n return [edits, newSelection];\n}\n\n/**\n * Maps the cursor move to all selections.\n */\nexport function mapCursorMove(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n const lineCount = textDocument.lineCount;\n return selections.map((selection) => {\n let { line, character } =\n shortcut === 'up' || shortcut === 'left'\n ? selection.start\n : selection.end;\n if (\n shortcut === 'textStart' ||\n shortcut === 'start' ||\n shortcut === 'end'\n ) {\n const caret = getCaretPosition(selection);\n line = caret.line;\n character = caret.character;\n if (shortcut === 'textStart') {\n const indent = getLeadingSpaces(textDocument.getLineText(line));\n character = character === indent ? 0 : indent;\n } else {\n character = shortcut === 'start' ? 0 : textDocument.getLineLength(line);\n }\n } else if (shortcut === 'up') {\n line = Math.max(0, line - 1);\n } else if (shortcut === 'down') {\n line = Math.min(Math.max(lineCount - 1, 0), line + 1);\n } else if (isCollapsedSelection(selection)) {\n const lineLength = textDocument.getLineLength(line);\n character = Math.min(character, lineLength);\n if (shortcut === 'left') {\n if (character > 0) {\n // Step left by a whole grapheme so the caret never lands inside an\n // emoji or other multi-code-unit character.\n character = stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n false\n );\n } else if (line > 0) {\n line = line - 1;\n character = textDocument.getLineLength(line);\n }\n } else if (character < lineLength) {\n character = stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n true\n );\n } else if (line < lineCount - 1) {\n line = line + 1;\n character = 0;\n }\n }\n const pos = { line, character };\n return {\n start: pos,\n end: pos,\n direction: DirectionNone,\n };\n });\n}\n\n/**\n * Same as mapCursorMove, but with shift key pressed.\n */\nexport function mapSelectionShift(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n return selections.map((selection) => {\n const focusPosition =\n selection.direction === DirectionBackward\n ? selection.start\n : selection.end;\n const [movedFocusSelection] = mapCursorMove(\n textDocument,\n [\n {\n start: focusPosition,\n end: focusPosition,\n direction: DirectionNone,\n },\n ],\n shortcut\n );\n return createSelectionFrom(selection, movedFocusSelection);\n });\n}\n\n/**\n * Applies a text change to the given text document\n */\nexport function applyTextChangeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n edit: ResolvedTextEdit,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n tabSize = 2,\n undoBoundary = false\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const primarySelection = selections[selections.length - 1];\n if (primarySelection === undefined) {\n return { nextSelections: [] };\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = selectionPositions.map((position) =>\n textDocument.offsetAt(position)\n );\n const primaryStartOffset = selectionOffsets[(selections.length - 1) * 2];\n const primaryEndOffset = selectionOffsets[(selections.length - 1) * 2 + 1];\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const adjustedChange = normalizeLeadingIndentForChange(\n textDocument,\n edit,\n tabSize\n );\n const edits: ResolvedTextEdit[] = [];\n const nextSelectionOffsets: Array<[number, number] | undefined> = Array.from({\n length: selections.length,\n });\n let offsetDelta = 0;\n let mergedGroup:\n | {\n start: number;\n end: number;\n indices: number[];\n }\n | undefined;\n const finalizeMergedGroup = () => {\n if (mergedGroup === undefined) {\n return;\n }\n const perGroupChange = normalizeLeadingIndentForChange(\n textDocument,\n {\n start: mergedGroup.start,\n end: mergedGroup.end,\n text: adjustedChange.text,\n },\n tabSize\n );\n const newText = expandSingleNewlineInsert(\n textDocument,\n perGroupChange.text,\n perGroupChange.start\n );\n edits.push({\n start: perGroupChange.start,\n end: perGroupChange.end,\n text: newText,\n });\n const nextOffsets: [number, number] = [\n mergedGroup.start + offsetDelta + newText.length,\n mergedGroup.start + offsetDelta + newText.length,\n ];\n for (const index of mergedGroup.indices) {\n nextSelectionOffsets[index] = nextOffsets;\n }\n offsetDelta += newText.length - (perGroupChange.end - perGroupChange.start);\n mergedGroup = undefined;\n };\n for (const entry of ordered) {\n const startOffset = Math.max(\n 0,\n entry.start + (adjustedChange.start - primaryStartOffset)\n );\n const endOffset = Math.max(\n startOffset,\n entry.end + (adjustedChange.end - primaryEndOffset)\n );\n if (mergedGroup !== undefined && startOffset < mergedGroup.end) {\n mergedGroup.end = Math.max(mergedGroup.end, endOffset);\n mergedGroup.indices.push(entry.index);\n continue;\n }\n finalizeMergedGroup();\n mergedGroup = {\n start: startOffset,\n end: endOffset,\n indices: [entry.index],\n };\n }\n finalizeMergedGroup();\n\n const change = textDocument.applyResolvedEdits(\n edits,\n true,\n selections,\n undefined,\n undoBoundary\n );\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsets.map((offsets) => {\n if (offsets === undefined) {\n throw new Error('Missing next selection offsets');\n }\n return offsets;\n })\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Returns the next anchor/focus offsets after replacing a selection range.\n * When the inserted text still contains the original selection (auto-surround),\n * the inner range is reselected to match VS Code/CodeMirror behavior.\n */\nfunction getNextSelectionOffsetPairAfterReplace(\n textDocument: TextDocument<unknown>,\n entry: { start: number; end: number },\n offsetDelta: number,\n newText: string\n): [number, number] {\n const insertStart = entry.start + offsetDelta;\n const insertEnd = insertStart + newText.length;\n const originalLength = entry.end - entry.start;\n if (originalLength > 0) {\n const originalText = textDocument.getText().slice(entry.start, entry.end);\n const preservedOffset = newText.indexOf(originalText);\n if (\n preservedOffset !== -1 &&\n preservedOffset + originalText.length <= newText.length\n ) {\n const rangeStart = insertStart + preservedOffset;\n return [rangeStart, rangeStart + originalText.length];\n }\n }\n return [insertEnd, insertEnd];\n}\n\n/**\n * Applies a text replace to multiple selections.\n */\nexport function applyTextReplaceToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n texts: string[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n undoBoundary = false\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n if (selections.length !== texts.length) {\n throw new Error(\n 'Selection text replacements must match the selection count'\n );\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = selectionPositions.map((position) =>\n textDocument.offsetAt(position)\n );\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n text: string;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n text: texts[index],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const allDeletes = texts.every((text) => text === '');\n let edits: ResolvedTextEdit[];\n const nextSelectionOffsetPairs: Array<[number, number] | undefined> =\n Array.from({\n length: selections.length,\n });\n if (allDeletes) {\n edits = [];\n let hasEffect = false;\n for (const entry of ordered) {\n nextSelectionOffsetPairs[entry.index] = [entry.end, entry.end];\n if (entry.start >= entry.end) {\n continue;\n }\n hasEffect = true;\n const last = edits[edits.length - 1];\n if (last !== undefined && entry.start < last.end) {\n edits[edits.length - 1] = {\n start: last.start,\n end: Math.max(last.end, entry.end),\n text: '',\n };\n } else {\n edits.push({ start: entry.start, end: entry.end, text: '' });\n }\n }\n if (!hasEffect) {\n return { nextSelections: selections };\n }\n for (const entry of ordered) {\n const caret = entry.end;\n let delta = 0;\n let next = caret;\n for (const edit of edits) {\n if (caret <= edit.start) {\n break;\n }\n if (caret >= edit.end) {\n delta -= edit.end - edit.start;\n continue;\n }\n next = edit.start + delta;\n break;\n }\n if (next === caret) {\n next += delta;\n }\n nextSelectionOffsetPairs[entry.index] = [next, next];\n }\n } else {\n edits = [];\n let offsetDelta = 0;\n let previousEditEnd = -1;\n for (const entry of ordered) {\n if (entry.start < previousEditEnd) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n previousEditEnd = entry.end;\n const newText = expandSingleNewlineInsert(\n textDocument,\n entry.text,\n entry.start\n );\n edits.push({\n start: entry.start,\n end: entry.end,\n text: newText,\n });\n nextSelectionOffsetPairs[entry.index] =\n getNextSelectionOffsetPairAfterReplace(\n textDocument,\n entry,\n offsetDelta,\n newText\n );\n offsetDelta += newText.length - (entry.end - entry.start);\n }\n }\n\n const change = textDocument.applyResolvedEdits(\n edits,\n true,\n selections,\n undefined,\n undoBoundary\n );\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsetPairs.map((offsets) => {\n if (offsets === undefined) {\n throw new Error('Missing next selection offsets');\n }\n return offsets;\n })\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\nconst SURROUNDING_PAIRS: Array<[openChar: string, closeChar: string]> = [\n [\"'\", \"'\"],\n ['\"', '\"'],\n ['`', '`'],\n ['{', '}'],\n ['[', ']'],\n ['<', '>'],\n ['(', ')'],\n];\n\nconst AUTO_SURROUND_CLOSE_CHARS = new Map(SURROUNDING_PAIRS);\nconst AUTO_SURROUND_QUOTE_CHARS = new Set([\"'\", '\"', '`']);\nconst AUTO_SURROUND_BRACKET_CHARS = new Set(['{', '[', '(', '<']);\n\nexport type AutoSurround =\n | 'default'\n | 'never'\n | 'brackets'\n | 'quotes'\n | 'languageDefined';\n\nfunction shouldAutoSurroundChar(\n autoSurround: AutoSurround | undefined,\n char: string\n): boolean {\n if (autoSurround === 'never') {\n return false;\n }\n if (autoSurround === 'brackets') {\n return AUTO_SURROUND_BRACKET_CHARS.has(char);\n }\n if (autoSurround === 'quotes') {\n return AUTO_SURROUND_QUOTE_CHARS.has(char);\n }\n return true;\n}\n\n/**\n * Returns per-selection replacement text when typing a surround character over\n * non-collapsed selections, matching VS Code auto-surround behavior.\n */\nexport function getAutoSurroundReplacementTexts<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n char: string,\n autoSurround?: AutoSurround\n): string[] | undefined {\n if (char.length !== 1 || selections.length === 0) {\n return undefined;\n }\n const closeChar = AUTO_SURROUND_CLOSE_CHARS.get(char);\n if (closeChar === undefined || !shouldAutoSurroundChar(autoSurround, char)) {\n return undefined;\n }\n const replacements: string[] = [];\n for (const selection of selections) {\n if (isCollapsedSelection(selection)) {\n return undefined;\n }\n replacements.push(char + textDocument.getText(selection) + closeChar);\n }\n return replacements;\n}\n\n/**\n * Swaps the two characters adjacent to a collapsed selection, matching browser\n * insertTranspose (Ctrl+T) behavior.\n */\nexport function applyTransposeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const text = textDocument.getText();\n const edits: ResolvedTextEdit[] = [];\n const nextOffsetPairs: Array<[number, number]> = [];\n\n for (const selection of selections) {\n const [anchor, focus] = getSelectionAnchorAndFocusOffsets(\n textDocument,\n selection\n );\n if (!isCollapsedSelection(selection)) {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n const { line, character } = selection.start;\n const offset = anchor;\n const lineText = textDocument.getLineText(line);\n const lineLength = lineText.length;\n // Document offset of column 0 on this line, used to translate the\n // grapheme-cluster columns below back into document offsets.\n const lineStart = offset - character;\n const graphemeStarts = getLineGraphemeStarts(lineText);\n let edit: ResolvedTextEdit | undefined;\n\n if (character > 0 && character < lineLength) {\n // Swap the whole grapheme before the caret with the one after it so a\n // surrogate pair (emoji) on either side is moved intact.\n const before = findClusterBreak(\n lineText,\n character,\n false,\n graphemeStarts\n );\n const after = findClusterBreak(lineText, character, true, graphemeStarts);\n edit = {\n start: lineStart + before,\n end: lineStart + after,\n text:\n lineText.slice(character, after) + lineText.slice(before, character),\n };\n nextOffsetPairs.push([lineStart + after, lineStart + after]);\n } else if (character === lineLength && graphemeStarts.length >= 2) {\n // Swap the last two graphemes on the line.\n const lastStart = graphemeStarts[graphemeStarts.length - 1];\n const secondLastStart = graphemeStarts[graphemeStarts.length - 2];\n edit = {\n start: lineStart + secondLastStart,\n end: offset,\n text:\n lineText.slice(lastStart, lineLength) +\n lineText.slice(secondLastStart, lastStart),\n };\n nextOffsetPairs.push([offset, offset]);\n } else if (character === 0 && line > 0 && lineLength > 0) {\n // Carry the previous line's last grapheme and this line's first grapheme\n // across the line break, swapping their order.\n const prevLine = line - 1;\n const prevLineText = textDocument.getLineText(prevLine);\n const prevLength = prevLineText.length;\n const prevEnd = textDocument.offsetAt({\n line: prevLine,\n character: prevLength,\n });\n const prevGraphemeStart =\n prevLength > 0\n ? findClusterBreak(\n prevLineText,\n prevLength,\n false,\n getLineGraphemeStarts(prevLineText)\n )\n : prevLength;\n const firstEnd = findClusterBreak(lineText, 0, true, graphemeStarts);\n const prevStart = prevEnd - (prevLength - prevGraphemeStart);\n const newText =\n lineText.slice(0, firstEnd) +\n text.slice(prevEnd, offset) +\n prevLineText.slice(prevGraphemeStart, prevLength);\n edit = {\n start: prevStart,\n end: offset + firstEnd,\n text: newText,\n };\n const caret = prevStart + newText.length;\n nextOffsetPairs.push([caret, caret]);\n } else {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n edits.push(edit);\n }\n\n if (edits.length === 0) {\n return { nextSelections: selections };\n }\n\n edits.sort((a, b) => a.start - b.start);\n for (let index = 1; index < edits.length; index++) {\n if (edits[index].start < edits[index - 1].end) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n }\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextOffsetPairs\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Deletes from each selection to the end of its line, including the line break\n * when the caret is already at the end of a non-final line. Non-collapsed\n * selections delete their selected text instead.\n */\nexport function applyDeleteHardLineForwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n const range = resolveDeleteHardLineForwardRange(textDocument, selection);\n return {\n start: range.start,\n end: range.end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Deletes from each selection back to the start of its soft (visual) line.\n * Non-collapsed selections delete their selected text instead.\n */\nexport function applyDeleteSoftLineBackwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n getSoftLineStart?: (line: number, character: number) => number,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n if (!isCollapsedSelection(selection)) {\n return {\n start: selection.start,\n end: selection.end,\n direction: DirectionNone,\n };\n }\n const caret = getCaretPosition(selection);\n const { line, character } = caret;\n const softLineStart = getSoftLineStart?.(line, character) ?? 0;\n if (character > softLineStart) {\n return {\n start: { line, character: softLineStart },\n end: { line, character },\n direction: DirectionNone,\n };\n }\n if (line === 0) {\n return {\n start: caret,\n end: caret,\n direction: DirectionNone,\n };\n }\n const prevLineLength = textDocument.getLineLength(line - 1);\n return {\n start: { line: line - 1, character: prevLineLength },\n end: { line, character: 0 },\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Deletes the word or separator group immediately before each selection.\n * Non-collapsed selections delete their selected text instead.\n */\nexport function applyDeleteWordBackwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n const [start, end] = resolveDeleteWordBackwardRange(\n textDocument,\n selection\n );\n return {\n start,\n end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Resolves the document range deleted by Backspace or Delete at a collapsed\n * caret. Non-collapsed selections delete their selected text instead.\n */\nexport function resolveDeleteCharacterRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection,\n forward: boolean\n): [start: Position, end: Position] {\n if (!isCollapsedSelection(selection)) {\n return [selection.start, selection.end];\n }\n\n const caret = getCaretPosition(selection);\n let { line, character } = caret;\n const lineLength = textDocument.getLineLength(line);\n const lineCount = textDocument.lineCount;\n\n // A preserved vertical-move goal column can overshoot a shorter line; clamp\n // before stepping so Backspace/Delete target the visible caret position.\n character = Math.min(character, lineLength);\n\n if (forward) {\n if (character < lineLength) {\n return [\n { line, character },\n {\n line,\n character: stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n true\n ),\n },\n ];\n }\n if (line < lineCount - 1) {\n return [\n { line, character: lineLength },\n { line: line + 1, character: 0 },\n ];\n }\n return [caret, caret];\n }\n\n if (character > 0) {\n return [\n {\n line,\n character: stepCharacterByGrapheme(\n textDocument,\n line,\n character,\n false\n ),\n },\n { line, character },\n ];\n }\n if (line > 0) {\n const prevLineLength = textDocument.getLineLength(line - 1);\n return [\n { line: line - 1, character: prevLineLength },\n { line, character: 0 },\n ];\n }\n return [caret, caret];\n}\n\n/**\n * Deletes one grapheme (or selected text) at each selection.\n */\nexport function applyDeleteCharacterToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n forward: boolean,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n tabSize = 2\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n let [start, end] = resolveDeleteCharacterRange(\n textDocument,\n selection,\n forward\n );\n // Only grow a Backspace into a full indent unit for a collapsed caret. When\n // the user has an explicit selection, Backspace must delete exactly that\n // selection rather than expanding it to a whole soft tab.\n if (!forward && isCollapsedSelection(selection)) {\n const normalized = normalizeLeadingIndentForChange(\n textDocument,\n {\n start: textDocument.offsetAt(start),\n end: textDocument.offsetAt(end),\n text: '',\n },\n tabSize\n );\n start = textDocument.positionAt(normalized.start);\n end = textDocument.positionAt(normalized.end);\n }\n return {\n start,\n end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Checks if a selection is collapsed.\n */\nexport function isCollapsedSelection(\n selection: EditorSelection | Range\n): boolean {\n return (\n selection.start.line === selection.end.line &&\n selection.start.character === selection.end.character\n );\n}\n\n/**\n * Returns the caret (focus) position for a selection.\n */\nexport function getCaretPosition(selection: EditorSelection): Position {\n const { start, end, direction } = selection;\n return direction === DirectionBackward ? start : end;\n}\n\n/**\n * Checks if a line is editable.\n */\nexport function isLineEditable(lineType: string): boolean {\n return (\n lineType === 'context' ||\n lineType === 'context-expanded' ||\n lineType === 'change-addition'\n );\n}\n\n/**\n * Checks whether selections `a` and `b` intersect.\n */\nexport function selectionIntersects(\n a: EditorSelection | Range,\n b: EditorSelection | Range\n): boolean {\n const aCollapsed = isCollapsedSelection(a);\n const bCollapsed = isCollapsedSelection(b);\n if (aCollapsed && bCollapsed) {\n return comparePosition(a.start, b.start) === 0;\n }\n if (aCollapsed) {\n return (\n comparePosition(b.start, a.start) <= 0 &&\n comparePosition(a.start, b.end) <= 0\n );\n }\n if (bCollapsed) {\n return (\n comparePosition(a.start, b.start) <= 0 &&\n comparePosition(b.start, a.end) <= 0\n );\n }\n return (\n comparePosition(a.start, b.end) < 0 && comparePosition(b.start, a.end) < 0\n );\n}\n\n/**\n * Compares two positions.\n */\nexport function comparePosition(a: Position, b: Position): number {\n if (a.line !== b.line) {\n return a.line - b.line;\n }\n return a.character - b.character;\n}\n\n/**\n * Creates a selection from anchor and focus offsets.\n */\nexport function createSelectionFromAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n anchorOffset: number,\n focusOffset: number\n): EditorSelection {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n const start = Math.min(anchorOffset, focusOffset);\n const end = Math.max(anchorOffset, focusOffset);\n return {\n start: textDocument.positionAt(start),\n end: textDocument.positionAt(end),\n direction,\n };\n}\n\n/**\n * Maps a single offset from the pre-edit document into the post-edit document.\n * `edits` are resolved edits in pre-edit offsets, sorted ascending and\n * non-overlapping. An offset at or after an edit's start shifts to the end of\n * that edit's replacement (right gravity), so text inserted at the caret pushes\n * the caret past it; an offset strictly before an edit is only shifted by the\n * net length change of the edits that precede it.\n */\nfunction remapOffsetThroughEdits(\n offset: number,\n edits: readonly ResolvedTextEdit[]\n): number {\n let delta = 0;\n for (const edit of edits) {\n if (offset < edit.start) {\n break;\n }\n if (offset >= edit.end) {\n delta += edit.text.length - (edit.end - edit.start);\n } else {\n return edit.start + delta + edit.text.length;\n }\n }\n return offset + delta;\n}\n\n/**\n * Re-anchors selections after a batch of text edits has been applied, so the\n * caret keeps pointing at the same logical location in the changed buffer.\n *\n * `selectionOffsets` (one `[start, end]` pair per selection) and `edits` are\n * measured in the PRE-edit document; the returned selections are built from\n * `textDocument`, which must already reflect the applied edits. Selection\n * direction is preserved by remapping each edge and re-deriving anchor/focus.\n */\nexport function remapSelectionsAfterEdits(\n textDocument: TextDocument<unknown>,\n selections: readonly EditorSelection[],\n selectionOffsets: ReadonlyArray<readonly [number, number]>,\n edits: readonly ResolvedTextEdit[]\n): EditorSelection[] {\n return selections.map((selection, index) => {\n const [startOffset, endOffset] = selectionOffsets[index];\n const nextStart = remapOffsetThroughEdits(startOffset, edits);\n const nextEnd = remapOffsetThroughEdits(endOffset, edits);\n const anchorOffset =\n selection.direction === DirectionBackward ? nextEnd : nextStart;\n const focusOffset =\n selection.direction === DirectionBackward ? nextStart : nextEnd;\n return createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n anchorOffset,\n focusOffset\n );\n });\n}\n\n/**\n * Creates a selection from a anchor and focus selection.\n */\nexport function createSelectionFrom(\n anchorSelection: EditorSelection,\n focusSelection: EditorSelection\n): EditorSelection {\n const anchor =\n anchorSelection.direction === DirectionBackward\n ? anchorSelection.end\n : anchorSelection.start;\n const currentStartOrder = comparePosition(anchor, focusSelection.start);\n const currentEndOrder = comparePosition(anchor, focusSelection.end);\n let focus = focusSelection.end;\n if (currentStartOrder <= 0) {\n focus = focusSelection.end;\n } else if (currentEndOrder >= 0) {\n focus = focusSelection.start;\n } else {\n // When the original anchor sits inside `current`, keep whichever edge\n // stayed at the anchor so drag direction remains stable.\n focus = currentStartOrder === 0 ? focusSelection.end : focusSelection.start;\n }\n const anchorVsFocus = comparePosition(anchor, focus);\n const direction: SelectionDirection =\n anchorVsFocus === 0\n ? DirectionNone\n : anchorVsFocus < 0\n ? DirectionForward\n : DirectionBackward;\n const selectionStart = anchorVsFocus <= 0 ? anchor : focus;\n const selectionEnd = anchorVsFocus <= 0 ? focus : anchor;\n return {\n start: selectionStart,\n end: selectionEnd,\n direction,\n };\n}\n\n/**\n * Extends or shrinks the selection `original` using the endpoints of `target`, \\\n * matching contenteditable shift + click extend behavior.\n */\nexport function extendSelection(\n original: EditorSelection,\n target: EditorSelection\n): EditorSelection {\n const leftExtended = comparePosition(target.start, original.start) < 0;\n const rightExtended = comparePosition(target.end, original.end) > 0;\n\n if (leftExtended && !rightExtended) {\n return {\n start: target.start,\n end: original.end,\n direction: DirectionBackward,\n };\n }\n\n if (rightExtended && !leftExtended) {\n return {\n start: original.start,\n end: target.end,\n direction: DirectionForward,\n };\n }\n\n if (original.direction === DirectionBackward) {\n return {\n start: target.start,\n end: original.end,\n direction:\n comparePosition(target.start, original.end) === 0\n ? DirectionNone\n : DirectionBackward,\n };\n }\n\n return {\n start: original.start,\n end: target.end,\n direction:\n comparePosition(original.start, target.end) === 0\n ? DirectionNone\n : DirectionForward,\n };\n}\n\n/**\n * Extends multiple selections.\n */\nexport function extendSelections(\n selections: EditorSelection[],\n target: EditorSelection\n): EditorSelection[] {\n const newSelections = selections.map((selection) => {\n return extendSelection(selection, target);\n });\n return mergeOverlappingSelections(newSelections);\n}\n\n/**\n * Merges overlapping selections.\n */\nexport function mergeOverlappingSelections(\n selections: EditorSelection[]\n): EditorSelection[] {\n if (selections.length <= 1) {\n return selections;\n }\n const ordered = selections\n .map((selection, index) => ({ index, selection }))\n .sort((a, b) => {\n const startOrder = comparePosition(a.selection.start, b.selection.start);\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = comparePosition(a.selection.end, b.selection.end);\n return endOrder !== 0 ? endOrder : a.index - b.index;\n });\n const merged: {\n index: number;\n selection: EditorSelection;\n }[] = [];\n\n let current = ordered[0];\n for (const entry of ordered.slice(1)) {\n if (selectionIntersects(current.selection, entry.selection)) {\n const latest = entry.index > current.index ? entry : current;\n const start =\n comparePosition(entry.selection.start, current.selection.start) < 0\n ? entry.selection.start\n : current.selection.start;\n const end =\n comparePosition(entry.selection.end, current.selection.end) > 0\n ? entry.selection.end\n : current.selection.end;\n let direction = latest.selection.direction;\n if (direction === DirectionNone && comparePosition(start, end) !== 0) {\n direction =\n comparePosition(latest.selection.start, start) === 0\n ? DirectionBackward\n : DirectionForward;\n }\n current = { index: latest.index, selection: { direction, end, start } };\n continue;\n }\n\n merged.push(current);\n current = entry;\n }\n merged.push(current);\n\n return merged\n .sort((a, b) => a.index - b.index)\n .map(({ selection }) => selection);\n}\n\n/**\n * Finds the next matching word and updates the selections.\n */\nexport function findNexMatch(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): EditorSelection[] | undefined {\n if (selections.length === 0) {\n return undefined;\n }\n\n const normalizedSelections = selections.map((selection) =>\n isCollapsedSelection(selection)\n ? expandCollapsedSelectionToWord(textDocument, selection)\n : selection\n );\n const texts = normalizedSelections.map((s) => textDocument.getText(s));\n const needle = texts[0];\n if (needle.length === 0 || texts.some((t) => t !== needle)) {\n return undefined;\n }\n\n const occupied = normalizedSelections.map(\n (s) =>\n [textDocument.offsetAt(s.start), textDocument.offsetAt(s.end)] as [\n number,\n number,\n ]\n );\n const nextOffset = textDocument.findNextNonOverlappingSubstring(\n needle,\n occupied\n );\n if (nextOffset === undefined) {\n return normalizedSelections.some((selection, index) => {\n const original = selections[index];\n return (\n comparePosition(selection.start, original.start) !== 0 ||\n comparePosition(selection.end, original.end) !== 0 ||\n selection.direction !== original.direction\n );\n })\n ? normalizedSelections\n : undefined;\n }\n const added = createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n nextOffset,\n nextOffset + needle.length\n );\n return [...normalizedSelections, added];\n}\n\n/**\n * Get the full selection of the document.\n */\nexport function getDocumentFullSelection(\n textDocument: TextDocument<unknown>\n): EditorSelection {\n const lastLine = textDocument.lineCount - 1;\n const lastCharacter = textDocument.getLineLength(lastLine);\n return {\n start: { line: 0, character: 0 },\n end: { line: lastLine, character: lastCharacter },\n direction: DirectionForward,\n };\n}\n\n/**\n * Get the boundary selection of the document.\n */\nexport function getDocumentBoundarySelection(\n textDocument: TextDocument<unknown>,\n atEnd: boolean,\n trimmedEndNewLine?: boolean\n): EditorSelection {\n let line = 0;\n if (atEnd) {\n const lastLine = textDocument.lineCount - 1;\n const hasTrailingBlankLine =\n trimmedEndNewLine === true &&\n lastLine > 0 &&\n textDocument.getLineLength(lastLine) === 0;\n line = hasTrailingBlankLine ? lastLine - 1 : lastLine;\n }\n const character = atEnd ? textDocument.getLineLength(line) : 0;\n const start = { line, character };\n return {\n start: start,\n end: start,\n direction: DirectionForward,\n };\n}\n\ninterface ClipboardRegion {\n start: number;\n end: number;\n}\n\n/**\n * Resolves the document offset range each selection contributes to the\n * clipboard, ordered by position. A collapsed selection contributes its whole\n * logical line including the trailing line break; the final line has no\n * trailing break to include. A ranged selection contributes the selected text.\n */\nfunction resolveClipboardRegions(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): ClipboardRegion[] {\n return selections\n .map((selection) => {\n if (isCollapsedSelection(selection)) {\n const line = selection.start.line;\n const start = textDocument.offsetAt({ line, character: 0 });\n const end =\n line < textDocument.lineCount - 1\n ? textDocument.offsetAt({ line: line + 1, character: 0 })\n : textDocument.offsetAt({\n line,\n character: textDocument.getLineLength(line),\n });\n return { start, end };\n }\n const start = textDocument.offsetAt(selection.start);\n const end = textDocument.offsetAt(selection.end);\n return start <= end ? { start, end } : { start: end, end: start };\n })\n .sort((a, b) => {\n const startOrder = a.start - b.start;\n return startOrder !== 0 ? startOrder : a.end - b.end;\n });\n}\n\n/**\n * Get the clipboard text of the selections for the given text document. Used by\n * both copy and cut so the two stay in sync. Overlapping regions (e.g. several\n * carets on one line) are merged so the same text is never emitted twice, and a\n * line-ending separator is inserted only between regions that aren't already\n * contiguous in the document.\n */\nexport function getSelectionText(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): string {\n const regions = resolveClipboardRegions(textDocument, selections);\n const eol = textDocument.eol;\n let result = '';\n let prevEnd = -1;\n for (const region of regions) {\n if (region.end <= region.start) {\n continue;\n }\n if (region.start <= prevEnd) {\n // Contiguous with or overlapping the previous region: extend it rather\n // than repeat any shared text.\n if (region.end > prevEnd) {\n result += textDocument.getTextSlice(prevEnd, region.end);\n prevEnd = region.end;\n }\n continue;\n }\n if (result.length > 0 && !endsWithLineBreak(result)) {\n result += eol;\n }\n result += textDocument.getTextSlice(region.start, region.end);\n prevEnd = region.end;\n }\n return result;\n}\n\ninterface SelectionCut {\n index: number;\n edit: ResolvedTextEdit;\n}\n\n// Resolves the deletion an individual selection contributes to a cut: a\n// collapsed caret removes its whole logical line, while a ranged selection\n// removes only the selected text.\nfunction resolveSelectionCutEdit(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): ResolvedTextEdit {\n if (isCollapsedSelection(selection)) {\n return resolveCollapsedSelectionCutEdit(textDocument, selection);\n }\n const [start, end] =\n comparePosition(selection.start, selection.end) <= 0\n ? [selection.start, selection.end]\n : [selection.end, selection.start];\n return {\n start: textDocument.offsetAt(start),\n end: textDocument.offsetAt(end),\n text: '',\n };\n}\n\n// A caret-only cut removes the whole logical line. Non-final lines delete\n// their trailing line break; the final line deletes the preceding break so\n// no empty line is left behind.\nfunction resolveCollapsedSelectionCutEdit(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): ResolvedTextEdit {\n const line = selection.start.line;\n const lineStart = textDocument.offsetAt({ line, character: 0 });\n const lineEnd = textDocument.offsetAt({\n line,\n character: textDocument.getLineLength(line),\n });\n\n if (line < textDocument.lineCount - 1) {\n const nextLineStart = textDocument.offsetAt({\n line: line + 1,\n character: 0,\n });\n return { start: lineStart, end: nextLineStart, text: '' };\n }\n\n if (line > 0) {\n const previousLineEnd = textDocument.offsetAt({\n line: line - 1,\n character: textDocument.getLineLength(line - 1),\n });\n return { start: previousLineEnd, end: lineEnd, text: '' };\n }\n\n return { start: lineStart, end: lineEnd, text: '' };\n}\n\nfunction mergeCutEdits(orderedCuts: SelectionCut[]): ResolvedTextEdit[] {\n const edits: ResolvedTextEdit[] = [];\n for (const { edit } of orderedCuts) {\n if (edit.start >= edit.end) {\n continue;\n }\n const last = edits.at(-1);\n if (last !== undefined && edit.start <= last.end) {\n edits[edits.length - 1] = {\n start: last.start,\n end: Math.max(last.end, edit.end),\n text: '',\n };\n } else {\n edits.push(edit);\n }\n }\n return edits;\n}\n\nfunction mapCutSelectionOffsets(\n orderedCuts: SelectionCut[],\n edits: ResolvedTextEdit[]\n): number[] {\n const nextOffsets: number[] = Array.from({ length: orderedCuts.length });\n let editIndex = 0;\n let offsetDelta = 0;\n for (const cut of orderedCuts) {\n while (editIndex < edits.length && cut.edit.start > edits[editIndex].end) {\n const edit = edits[editIndex];\n offsetDelta -= edit.end - edit.start;\n editIndex++;\n }\n\n const edit = edits[editIndex];\n if (\n edit !== undefined &&\n cut.edit.start >= edit.start &&\n cut.edit.start <= edit.end\n ) {\n nextOffsets[cut.index] = edit.start + offsetDelta;\n } else {\n nextOffsets[cut.index] = cut.edit.start + offsetDelta;\n }\n }\n return nextOffsets;\n}\n\n// Resolves the clipboard text, merged deletions, and resulting caret offsets\n// for a cut. The clipboard text comes from the shared getSelectionText helper\n// so cut stays in sync with copy, while the deletions are computed per\n// selection and merged so overlapping cuts delete their shared text once.\nexport function resolveSelectionCut(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): {\n text: string;\n edits: ResolvedTextEdit[];\n nextSelectionOffsets: number[];\n} {\n const cuts = selections.map<SelectionCut>((selection, index) => ({\n index,\n edit: resolveSelectionCutEdit(textDocument, selection),\n }));\n const orderedCuts = [...cuts].sort((a, b) => {\n const startOrder = a.edit.start - b.edit.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.edit.end - b.edit.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n const edits = mergeCutEdits(orderedCuts);\n return {\n text: getSelectionText(textDocument, selections),\n edits,\n nextSelectionOffsets: mapCutSelectionOffsets(orderedCuts, edits),\n };\n}\n\n/**\n * Get the anchor node and offset for a selection.\n */\nexport function getSelectionAnchor(\n lineElement: HTMLElement,\n character: number\n): [Node, number] {\n const ch = Math.max(0, character);\n const tokens = collectTokens(lineElement);\n\n let last: HTMLElement | null = null;\n for (const token of tokens) {\n last = token;\n const base = getCharacterIndex(token)!;\n const end = base + (token.textContent?.length ?? 0);\n if (ch <= end) {\n const anchor = textAt(token, ch < base ? 0 : ch - base);\n if (anchor !== null) {\n return anchor;\n }\n }\n }\n\n if (last !== null) {\n const anchor = textAt(last, last.textContent?.length ?? 0);\n if (anchor !== null) {\n return anchor;\n }\n return [last, 0];\n }\n\n let textOffset = 0;\n let lastTextNode: Text | null = null;\n for (const child of lineElement.childNodes) {\n if (child.nodeType === 1 && (child as HTMLElement).tagName === 'BR') {\n return [child, 0];\n }\n if (child.nodeType !== 3) {\n continue;\n }\n lastTextNode = child as Text;\n const len = getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n );\n if (ch <= textOffset + len) {\n return [\n lastTextNode,\n getTextOffset(lastTextNode.textContent, ch - textOffset),\n ];\n }\n textOffset += len;\n }\n\n if (lastTextNode !== null) {\n return [\n lastTextNode,\n getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n ),\n ];\n }\n return [lineElement, 0];\n}\n\n/**\n * Expands a zero-width selection to the word-like segment that contains the caret.\n */\nexport function expandCollapsedSelectionToWord(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): EditorSelection {\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const ch = Math.max(0, Math.min(character, lineText.length));\n const span = expandCollapsedLineWord(lineText, ch);\n if (span === undefined) {\n return selection;\n }\n return {\n start: { line, character: span.start },\n end: { line, character: span.end },\n direction: DirectionForward,\n };\n}\n\nfunction expandCollapsedLineWord(\n lineText: string,\n character: number\n): { start: number; end: number } | undefined {\n const segmenter = createSegmenter({ granularity: 'word' });\n if (segmenter !== undefined) {\n for (const seg of segmenter.segment(lineText)) {\n if (seg.isWordLike !== true) {\n continue;\n }\n const lo = seg.index;\n const hi = lo + seg.segment.length;\n // Match when the cursor is inside the word or immediately touching\n // one of its boundaries — not when separated by non-word characters.\n if (character >= lo && character <= hi) {\n return { start: lo, end: hi };\n }\n }\n return undefined;\n }\n // Degraded path for engines lacking Intl.Segmenter: treat runs of\n // alphanumeric/underscore characters as words.\n const wordRe = /[\\p{Alphabetic}\\p{Number}_]+/gu;\n let match: RegExpExecArray | null;\n while ((match = wordRe.exec(lineText)) !== null) {\n const lo = match.index;\n const hi = lo + match[0].length;\n if (character >= lo && character <= hi) {\n return { start: lo, end: hi };\n }\n }\n return undefined;\n}\n\n// Resolves the range removed by deleteWordBackward for one selection.\nfunction resolveDeleteWordBackwardRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): [start: Position, end: Position] {\n if (!isCollapsedSelection(selection)) {\n return [selection.start, selection.end];\n }\n const caret = getCaretPosition(selection);\n const { line, character: head } = caret;\n if (head === 0) {\n if (line === 0) {\n return [caret, caret];\n }\n const prevLineLength = textDocument.getLineLength(line - 1);\n return [\n { line: line - 1, character: prevLineLength },\n { line, character: 0 },\n ];\n }\n const lineText = textDocument.getLineText(line);\n const graphemeStarts = getLineGraphemeStarts(lineText);\n let pos = head;\n let match: number | undefined;\n while (pos > 0) {\n const prev = findClusterBreak(lineText, pos, false, graphemeStarts);\n const nextChar = lineText.slice(prev, pos);\n const nextMatch = !/\\S/.test(nextChar)\n ? 0\n : /\\p{Alphabetic}|\\p{Number}|_/u.test(nextChar)\n ? 1\n : 2;\n if (match !== undefined && nextMatch !== match) {\n break;\n }\n if (nextChar !== ' ' || pos !== head) {\n match = nextMatch;\n }\n pos = prev;\n }\n return [\n { line, character: pos },\n { line, character: head },\n ];\n}\n\nfunction findClusterBreak(\n text: string,\n pos: number,\n forward: boolean,\n graphemeStarts: number[]\n): number {\n if (forward) {\n for (const start of graphemeStarts) {\n if (start > pos) {\n return start;\n }\n }\n return text.length;\n }\n for (let i = graphemeStarts.length - 1; i >= 0; i--) {\n const start = graphemeStarts[i];\n if (start < pos) {\n return start;\n }\n }\n return 0;\n}\n\n// Lists the start column of every grapheme cluster on a line (always starting\n// at 0). Used to step the caret and to transpose by whole graphemes so a\n// surrogate pair (emoji) or combining sequence is never split.\nfunction getLineGraphemeStarts(lineText: string): number[] {\n const graphemeStarts = [0];\n const segmenter = getGraphemeSegmenter();\n if (segmenter !== undefined) {\n for (const segment of segmenter.segment(lineText)) {\n if (segment.index > 0) {\n graphemeStarts.push(segment.index);\n }\n }\n return graphemeStarts;\n }\n // Degraded path for engines lacking Intl.Segmenter: step by code point\n // (keeps surrogate pairs intact, but not combining sequences).\n let index = 0;\n for (const codePoint of lineText) {\n if (index > 0) {\n graphemeStarts.push(index);\n }\n index += codePoint.length;\n }\n return graphemeStarts;\n}\n\n// Returns the character column one grapheme cluster to the left or right of\n// `character` on `line`. Stepping by grapheme rather than by a single UTF-16\n// code unit keeps the caret from landing inside an emoji or other multi-unit\n// character. Crossing a line boundary is left to the caller.\nfunction stepCharacterByGrapheme(\n textDocument: TextDocument<unknown>,\n line: number,\n character: number,\n forward: boolean\n): number {\n const lineLength = textDocument.getLineLength(line);\n if (forward) {\n if (character >= lineLength) {\n return lineLength;\n }\n const lineStart = textDocument.offsetAt({ line, character: 0 });\n const suffix = textDocument.getTextSlice(\n lineStart + character,\n lineStart + lineLength\n );\n const segmenter = getGraphemeSegmenter();\n if (segmenter !== undefined) {\n for (const segment of segmenter.segment(suffix)) {\n return character + segment.segment.length;\n }\n return lineLength;\n }\n // Degraded path for engines lacking Intl.Segmenter: step one code point.\n for (const codePoint of suffix) {\n return character + codePoint.length;\n }\n return lineLength;\n }\n if (character <= 0) {\n return 0;\n }\n const lineStart = textDocument.offsetAt({ line, character: 0 });\n const prefix = textDocument.getTextSlice(lineStart, lineStart + character);\n let prevStart = 0;\n const segmenter = getGraphemeSegmenter();\n if (segmenter !== undefined) {\n for (const segment of segmenter.segment(prefix)) {\n prevStart = segment.index;\n }\n return prevStart;\n }\n // Degraded path for engines lacking Intl.Segmenter: step one code point.\n let index = 0;\n for (const codePoint of prefix) {\n prevStart = index;\n index += codePoint.length;\n }\n return prevStart;\n}\n\nfunction getSelectionAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): [anchorOffset: number, focusOffset: number] {\n const isBackward = selection.direction === DirectionBackward;\n return [\n textDocument.offsetAt(isBackward ? selection.end : selection.start),\n textDocument.offsetAt(getCaretPosition(selection)),\n ];\n}\n\n// Resolves the range removed by deleteHardLineForward for one selection.\nfunction resolveDeleteHardLineForwardRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): Range {\n if (!isCollapsedSelection(selection)) {\n return { start: selection.start, end: selection.end };\n }\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const lineLength = lineText.length;\n if (character < lineLength) {\n return {\n start: { line, character },\n end: { line, character: lineLength },\n };\n }\n if (line < textDocument.lineCount - 1) {\n return {\n start: { line, character },\n end: { line: line + 1, character: 0 },\n };\n }\n return {\n start: { line, character },\n end: { line, character },\n };\n}\n\n// When the user inserts a lone line break, copy the current line's indentation onto the new line.\nfunction expandSingleNewlineInsert(\n textDocument: TextDocument<unknown>,\n insertText: string,\n insertStartOffset: number\n): string {\n if (insertText !== '\\n' && insertText !== '\\r' && insertText !== '\\r\\n') {\n return insertText;\n }\n const line = textDocument.positionAt(insertStartOffset).line;\n const lineText = textDocument.getLineText(line);\n const indentLen = getLeadingSpaces(lineText);\n if (indentLen === 0) {\n return insertText;\n }\n return insertText + lineText.slice(0, indentLen);\n}\n\nfunction getLeadingSpaces(text: string): number {\n let indent = 0;\n for (; indent < text.length; indent++) {\n const c = text.charCodeAt(indent);\n if (c !== /* space */ 32 && c !== /* tab */ 9) {\n break;\n }\n }\n return indent;\n}\n\nfunction createSelectionsFromOffsetPairs(\n textDocument: TextDocument<unknown>,\n offsetPairs: readonly [anchorOffset: number, focusOffset: number][]\n): EditorSelection[] {\n const normalizedOffsets: number[] = [];\n for (const [anchorOffset, focusOffset] of offsetPairs) {\n normalizedOffsets.push(\n Math.min(anchorOffset, focusOffset),\n Math.max(anchorOffset, focusOffset)\n );\n }\n const positions = textDocument.positionsAt(normalizedOffsets);\n return offsetPairs.map(([anchorOffset, focusOffset], index) => {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n return {\n start: positions[index * 2],\n end: positions[index * 2 + 1],\n direction,\n };\n });\n}\n\n// Expands a backspace over leading spaces into one soft-tab width so mixed hard/soft indentation\n// behaves like the explicit outdent command.\nfunction normalizeLeadingIndentForChange(\n textDocument: TextDocument<unknown>,\n change: ResolvedTextEdit,\n tabSize: number\n): ResolvedTextEdit {\n if (change.text !== '' || change.start !== change.end - 1) {\n return change;\n }\n const caretPosition = textDocument.positionAt(change.end);\n if (caretPosition.character === 0) {\n return change;\n }\n const lineText = textDocument.getLineText(caretPosition.line);\n const leadingText = lineText.slice(0, caretPosition.character);\n if (/[^ \\t]/.test(leadingText)) {\n return change;\n }\n if (lineText[caretPosition.character - 1] === '\\t') {\n return change;\n }\n const softTabStart = Math.max(0, caretPosition.character - tabSize);\n const softTabText = lineText.slice(softTabStart, caretPosition.character);\n if (softTabText.length === tabSize && /^ +$/.test(softTabText)) {\n return {\n ...change,\n start: change.end - softTabText.length,\n };\n }\n return change;\n}\n\nfunction boundaryToPosition(node: Node, offset: number): Position | null {\n const host = node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n let lineEl: HTMLElement | null = host;\n while (lineEl !== null && getLineIndex(lineEl) === undefined) {\n lineEl = lineEl.parentElement;\n }\n if (lineEl === null) {\n return null;\n }\n const line = getLineIndex(lineEl);\n if (line === undefined) {\n return null;\n }\n\n if (node.nodeType === 3) {\n if (node.parentElement === null) {\n return null;\n }\n if (findTokenSpan(node.parentElement) !== null) {\n return { line, character: getLineChildEnd(node, offset) };\n }\n return {\n line,\n character:\n offsetBefore(lineEl, node) + getTextOffset(node.textContent, offset),\n };\n }\n\n if (node.nodeType === 1) {\n const el = node as HTMLElement;\n if (el.tagName === 'DIV') {\n let character = 0;\n for (let i = 0; i < offset; i++) {\n character = getLineChildEnd(el.childNodes[i]);\n }\n return { line, character };\n }\n if (el.tagName === 'BR') {\n return { line, character: 0 };\n }\n if (el.tagName === 'SPAN') {\n if (offset < el.childNodes.length) {\n const next = el.childNodes[offset];\n if (next?.nodeType === 1) {\n const nextBase = getCharacterIndex(next as HTMLElement);\n if (nextBase !== undefined) {\n return { line, character: nextBase };\n }\n const token = findTokenSpan(next as HTMLElement);\n const tokenBase =\n token === null ? undefined : getCharacterIndex(token);\n if (tokenBase !== undefined) {\n return { line, character: tokenBase };\n }\n }\n }\n return {\n line,\n character:\n offset > 0\n ? getLineChildEnd(el.childNodes[offset - 1])\n : offsetBefore(lineEl, el),\n };\n }\n return { line, character: offsetBefore(lineEl, el) };\n }\n return null;\n}\n\nfunction collectTokens(line: HTMLElement): HTMLElement[] {\n const tokens: HTMLElement[] = [];\n for (const child of line.childNodes) {\n if (child.nodeType !== 1) {\n continue;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN') {\n continue;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n tokens.push(el);\n continue;\n }\n for (const nested of el.childNodes) {\n if (\n nested.nodeType === 1 &&\n getCharacterIndex(nested as HTMLElement) !== undefined\n ) {\n tokens.push(nested as HTMLElement);\n }\n }\n }\n return tokens;\n}\n\nfunction textAt(token: HTMLElement, offset: number): [Node, number] | null {\n let remaining = Math.max(0, offset);\n const stack: Array<{ container: Node; index: number }> = [\n { container: token, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n frame.index++;\n if (walkNode.nodeType === 3) {\n const len = getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n if (remaining <= len) {\n return [walkNode, remaining];\n }\n remaining -= len;\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return null;\n}\n\nfunction textLengthBefore(root: Node, target: Node): number {\n let before = 0;\n const stack: Array<{ container: Node; index: number }> = [\n { container: root, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n if (walkNode === target) {\n return before;\n }\n frame.index++;\n if (walkNode.nodeType === 3) {\n before += getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return before;\n}\n\nfunction isInside(token: HTMLElement, node: Node): boolean {\n let current: Node | null = node;\n while (current !== null) {\n if (current === token) {\n return true;\n }\n current = current.parentElement;\n }\n return false;\n}\n\nfunction offsetBefore(line: HTMLElement, node: Node): number {\n if (node.parentElement === line) {\n let offset = 0;\n const index = Array.prototype.indexOf.call(line.childNodes, node);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(line.childNodes[i]);\n }\n return offset;\n }\n for (const token of collectTokens(line)) {\n if (isInside(token, node)) {\n const base = getCharacterIndex(token)!;\n return base + (node.nodeType === 3 ? textLengthBefore(token, node) : 0);\n }\n }\n let offset = 0;\n let target: HTMLElement | null =\n node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n while (target !== null && target.parentElement !== null) {\n if (getLineIndex(target.parentElement) !== undefined) {\n break;\n }\n const parent = target.parentElement;\n const index = Array.prototype.indexOf.call(parent.childNodes, target);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(parent.childNodes[i]);\n }\n target = parent;\n }\n return offset;\n}\n\nfunction findTokenSpan(el: HTMLElement): HTMLElement | null {\n let current: HTMLElement | null = el;\n while (current !== null) {\n if (getLineIndex(current) !== undefined) {\n return null;\n }\n if (getCharacterIndex(current) !== undefined) {\n return current;\n }\n current = current.parentElement;\n }\n return null;\n}\n\nfunction getLineChildEnd(\n child: Node | undefined,\n textOffsetInChild?: number\n): number {\n if (child === undefined) {\n return 0;\n }\n if (child.nodeType === 3) {\n const parent = child.parentElement;\n if (parent === null) {\n return 0;\n }\n const token = findTokenSpan(parent);\n if (token === null) {\n return 0;\n }\n const base = getCharacterIndex(token);\n if (base === undefined) {\n return 0;\n }\n const length =\n textOffsetInChild === undefined\n ? getTextOffset(child.textContent, child.textContent?.length ?? 0)\n : getTextOffset(child.textContent, textOffsetInChild);\n return base + textLengthBefore(token, child) + length;\n }\n if (child.nodeType !== 1) {\n return 0;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN' && el.tagName !== 'BR') {\n return 0;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n return base + (el.textContent?.length ?? 0);\n }\n let end = 0;\n for (const token of el.childNodes) {\n end = Math.max(end, getLineChildEnd(token));\n }\n return end;\n}\n\nfunction getLineIndex(el: HTMLElement): number | undefined {\n const { line, lineType } = el.dataset;\n if (line !== undefined && lineType !== 'change-deletion') {\n const lineNumber = parseInt(line, 10);\n if (!Number.isNaN(lineNumber)) {\n return lineNumber - 1;\n }\n }\n return undefined;\n}\n\nfunction getCharacterIndex(el: HTMLElement): number | undefined {\n const { char } = el.dataset;\n if (char !== undefined) {\n const charIndex = parseInt(char, 10);\n if (!Number.isNaN(charIndex)) {\n return charIndex;\n }\n }\n return undefined;\n}\n\nfunction getTextOffset(\n text: string | null | undefined,\n offset: number\n): number {\n const value = text ?? '';\n const lineBreakIndex = value.search(/[\\r\\n]/);\n return Math.min(\n offset,\n lineBreakIndex === -1 ? value.length : lineBreakIndex\n );\n}\n"],"mappings":";;;AAgBA,MAAa,oBAAoB;AACjC,MAAa,gBAAgB;AAC7B,MAAa,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,MAAM,SAAS,GACzB,OAAO;CAEX;AAEF;AAEA,SAAS,cACP,MACA,QACQ;CACR,MAAM,QAAQ,QAAQ;CACtB,MAAM,iBAAiB,MAAM,OAAO,QAAQ;CAC5C,OAAO,KAAK,IACV,QACA,mBAAmB,KAAK,MAAM,SAAS,cACzC;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"textMeasure.d.ts","names":[],"sources":["../../src/editor/textMeasure.ts"],"mappings":";cAQa,OAAA;EAAA;EAAO;EASlB,EAAA;EASsB;EAPtB,OAAA;EAFA;EAIA,UAAA;EAAA;EAEA,UAAA;EAGA;EAAA,IAAA,CAAK,IAAA,EAAM,WAAW;EAAjB;;;;;;;;;;AAgJc;AAMrB;EA1FE,uBAAA;;EAcA,gBAAA,CAAiB,IAAA;EA4EiC;EAnElD,sBAAA,CAAuB,IAAA;EAmFsB;;;AAE/B;
|
|
1
|
+
{"version":3,"file":"textMeasure.d.ts","names":[],"sources":["../../src/editor/textMeasure.ts"],"mappings":";cAQa,OAAA;EAAA;EAAO;EASlB,EAAA;EASsB;EAPtB,OAAA;EAFA;EAIA,UAAA;EAAA;EAEA,UAAA;EAGA;EAAA,IAAA,CAAK,IAAA,EAAM,WAAW;EAAjB;;;;;;;;;;AAgJc;AAMrB;EA1FE,uBAAA;;EAcA,gBAAA,CAAiB,IAAA;EA4EiC;EAnElD,sBAAA,CAAuB,IAAA;EAmFsB;;;AAE/B;AA6ChB;EAtHE,mBAAA,CAAoB,IAAA;;;AAuHR;AA6Bd;;EAnGE,mBAAA;AAAA;AAmG8D;AAAA,iBA7FhD,uBAAA,CAAwB,IAAY;;iBAgBpC,+BAAA,CACd,IAAA,UACA,MAAc;;iBA6CA,4BAAA,CACd,IAAY;;;;;;;;iBA6BE,kBAAA,CAAmB,IAAA,UAAc,OAAe;;;;;;iBAwBhD,2BAAA,CACd,IAAA,UACA,OAAe"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { h, round } from "./utils.js";
|
|
1
|
+
import { getGraphemeSegmenter, h, round } from "./utils.js";
|
|
2
2
|
//#region src/editor/textMeasure.ts
|
|
3
3
|
const TEXT_WIDTH_CACHE_LIMIT = 4096;
|
|
4
4
|
var Metrics = class {
|
|
@@ -125,12 +125,22 @@ function needsDomTextMeasurement(text) {
|
|
|
125
125
|
function snapTextOffsetToUnicodeBoundary(text, offset) {
|
|
126
126
|
const boundedOffset = Math.max(0, Math.min(offset, text.length));
|
|
127
127
|
if (boundedOffset === 0 || boundedOffset === text.length || !needsDomTextMeasurement(text)) return boundedOffset;
|
|
128
|
-
const segmenter =
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
|
|
128
|
+
const segmenter = getGraphemeSegmenter();
|
|
129
|
+
if (segmenter !== void 0) {
|
|
130
|
+
for (const segment of segmenter.segment(text)) {
|
|
131
|
+
const segmentStart = segment.index;
|
|
132
|
+
const segmentEnd = segmentStart + segment.segment.length;
|
|
133
|
+
if (boundedOffset > segmentStart && boundedOffset < segmentEnd) return segmentEnd;
|
|
134
|
+
if (boundedOffset <= segmentStart) break;
|
|
135
|
+
}
|
|
136
|
+
return boundedOffset;
|
|
137
|
+
}
|
|
138
|
+
let segmentStart = 0;
|
|
139
|
+
for (const codePoint of text) {
|
|
140
|
+
const segmentEnd = segmentStart + codePoint.length;
|
|
132
141
|
if (boundedOffset > segmentStart && boundedOffset < segmentEnd) return segmentEnd;
|
|
133
142
|
if (boundedOffset <= segmentStart) break;
|
|
143
|
+
segmentStart = segmentEnd;
|
|
134
144
|
}
|
|
135
145
|
return boundedOffset;
|
|
136
146
|
}
|
|
@@ -138,8 +148,16 @@ function snapTextOffsetToUnicodeBoundary(text, offset) {
|
|
|
138
148
|
function getUnicodeMeasurementOffsets(text) {
|
|
139
149
|
if (!needsDomTextMeasurement(text)) return;
|
|
140
150
|
const offsets = [0];
|
|
141
|
-
const segmenter =
|
|
142
|
-
|
|
151
|
+
const segmenter = getGraphemeSegmenter();
|
|
152
|
+
if (segmenter !== void 0) {
|
|
153
|
+
for (const segment of segmenter.segment(text)) offsets.push(segment.index + segment.segment.length);
|
|
154
|
+
return offsets;
|
|
155
|
+
}
|
|
156
|
+
let offset = 0;
|
|
157
|
+
for (const codePoint of text) {
|
|
158
|
+
offset += codePoint.length;
|
|
159
|
+
offsets.push(offset);
|
|
160
|
+
}
|
|
143
161
|
return offsets;
|
|
144
162
|
}
|
|
145
163
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"textMeasure.js","names":["#root","#canvasCtx","#font","#textWidthCache"],"sources":["../../src/editor/textMeasure.ts"],"sourcesContent":["import { h, round } from './utils';\n\n// Upper bound on cached DOM text-width measurements. The cache only holds\n// non-ASCII runs (emoji, ZWJ sequences, variation selectors), so it stays\n// small for ordinary code, but capping it prevents unbounded growth on\n// emoji-heavy documents. Past the cap the oldest entry is evicted.\nconst TEXT_WIDTH_CACHE_LIMIT = 4096;\n\nexport class Metrics {\n #root?: HTMLElement;\n #canvasCtx?: CanvasRenderingContext2D;\n #font?: string;\n\n // Memoizes domMeasureTextWidth() results\n #textWidthCache = new Map<string, number>();\n\n /** Width of the '0' character. */\n ch: number = -1;\n /** Size of a tab(\\t) character. */\n tabSize: number = 2;\n /** Height of the code line. */\n lineHeight: number = 20;\n /** Padding top of the root element. */\n paddingTop: number = 0;\n\n /** initialize the metrics */\n init(root: HTMLElement): void {\n if (\n this.#root === root &&\n this.#canvasCtx !== undefined &&\n this.ch !== -1\n ) {\n // already initialized\n return;\n }\n\n this.#root = root;\n this.#canvasCtx ??=\n document.createElement('canvas').getContext('2d') ?? undefined;\n if (this.#canvasCtx === undefined) {\n throw new Error('Could not get canvas context');\n }\n\n const parent = root.parentElement;\n if (parent !== null) {\n const { paddingTop } = getComputedStyle(parent);\n if (paddingTop.endsWith('px')) {\n this.paddingTop = parseFloat(paddingTop.slice(0, -2));\n }\n }\n\n const { fontSize, fontFamily, tabSize, lineHeight } =\n getComputedStyle(root);\n if (lineHeight.endsWith('px')) {\n this.lineHeight = parseFloat(lineHeight.slice(0, -2));\n } else if (fontSize.endsWith('px')) {\n this.lineHeight = round(\n parseFloat(fontSize.slice(0, -2)) * parseFloat(lineHeight)\n );\n }\n const font = fontSize + ' ' + fontFamily;\n if (this.#font !== font || this.ch === -1) {\n this.#font = font;\n this.#canvasCtx.font = font;\n this.ch = this.canvasMeasureTextWidth('0');\n // Cached DOM widths were measured against the previous font.\n this.clearTextWidthCache();\n }\n const nextTabSize = parseInt(tabSize, 10);\n if (!Number.isNaN(nextTabSize)) {\n this.tabSize = nextTabSize;\n }\n }\n\n /**\n * Re-measure the '0' character width against the font that is loaded right\n * now, returning true when it changed.\n *\n * A custom web font can finish loading after the editor first renders.\n * Until then canvas measureText reports the fallback font's width, and\n * getComputedStyle returns the same font-family string before and after the\n * file arrives, so init()'s font guard never re-measures on its own. Call\n * this once fonts have settled (e.g. on document.fonts.ready) to replace a\n * width measured against the fallback font with the real glyph width. The\n * boolean return lets the caller skip re-rendering when nothing changed.\n */\n remeasureCharacterWidth(): boolean {\n if (this.#canvasCtx === undefined || this.#font === undefined) {\n return false;\n }\n this.#canvasCtx.font = this.#font;\n const ch = this.canvasMeasureTextWidth('0');\n if (ch === this.ch) {\n return false;\n }\n this.ch = ch;\n return true;\n }\n\n /** measure the width of the text */\n measureTextWidth(text: string): number {\n const textWithExpandedTabs = expandTabsToSpaces(text, this.tabSize);\n if (needsDomTextMeasurement(textWithExpandedTabs)) {\n return this.domMeasureTextWidth(textWithExpandedTabs);\n }\n return this.canvasMeasureTextWidth(textWithExpandedTabs);\n }\n\n /** measure the width of the text using the canvas measureText API */\n canvasMeasureTextWidth(text: string): number {\n if (this.#canvasCtx === undefined) {\n throw new Error('Metrics not initialized');\n }\n return round(this.#canvasCtx.measureText(text).width);\n }\n\n /**\n * measure the width of the text using the DOM\n * this is slow because it cause a reflow, use it for non-ascii text;\n * results are memoized per text so repeated measurements skip the reflow\n */\n domMeasureTextWidth(text: string): number {\n if (this.#root === undefined) {\n throw new Error('Metrics not initialized');\n }\n const cacheKey = text + '|' + this.#font;\n const cached = this.#textWidthCache.get(cacheKey);\n if (cached !== undefined) {\n return cached;\n }\n const measureEl = h(\n 'span',\n {\n style: {\n position: 'absolute',\n top: '0',\n left: '0',\n visibility: 'hidden',\n pointerEvents: 'none',\n whiteSpace: 'pre',\n font: 'inherit',\n },\n textContent: text,\n },\n this.#root\n );\n let width: number;\n try {\n // round() to match canvasMeasureTextWidth and ch; otherwise the DOM path\n // returns raw sub-pixel widths and caret/selection offsets drift between\n // ASCII and non-ASCII runs on the same line.\n width = round(measureEl.getBoundingClientRect().width);\n } finally {\n measureEl.remove();\n }\n if (this.#textWidthCache.size >= TEXT_WIDTH_CACHE_LIMIT) {\n const oldestKey = this.#textWidthCache.keys().next().value;\n if (oldestKey !== undefined) {\n this.#textWidthCache.delete(oldestKey);\n }\n }\n this.#textWidthCache.set(cacheKey, width);\n return width;\n }\n\n /**\n * discard memoized DOM text widths\n * call this when the inherited font may have changed without re-running\n * init(), e.g. on a layout reflow, so stale widths are not reused\n */\n clearTextWidthCache(): void {\n this.#textWidthCache.clear();\n }\n}\n\n/** Check if the text needs DOM text measurement. */\nexport function needsDomTextMeasurement(text: string): boolean {\n for (let i = 0; i < text.length; i++) {\n const code = text.charCodeAt(i);\n if (\n (code >= 0xd800 && code <= 0xdfff) ||\n code === 0x200d ||\n code === 0xfe0e ||\n code === 0xfe0f\n ) {\n return true;\n }\n }\n return false;\n}\n\n/** snap the text offset to the Unicode boundary */\nexport function snapTextOffsetToUnicodeBoundary(\n text: string,\n offset: number\n): number {\n const boundedOffset = Math.max(0, Math.min(offset, text.length));\n if (\n boundedOffset === 0 ||\n boundedOffset === text.length ||\n !needsDomTextMeasurement(text)\n ) {\n return boundedOffset;\n }\n // Avoid measuring a caret position inside one visual emoji/grapheme.\n // Browser caret movement can report offsets around UTF-16 surrogate\n // pairs and emoji joiners; measuring a partial sequence gives a\n // replacement-glyph width.\n const segmenter = new Intl.Segmenter(undefined, {\n granularity: 'grapheme',\n });\n for (const segment of segmenter.segment(text)) {\n const segmentStart = segment.index;\n const segmentEnd = segmentStart + segment.segment.length;\n if (boundedOffset > segmentStart && boundedOffset < segmentEnd) {\n return segmentEnd;\n }\n if (boundedOffset <= segmentStart) {\n break;\n }\n }\n return boundedOffset;\n}\n\n/** get the offsets of the Unicode grapheme clusters in the text */\nexport function getUnicodeMeasurementOffsets(\n text: string\n): number[] | undefined {\n if (!needsDomTextMeasurement(text)) {\n return undefined;\n }\n const offsets = [0];\n const segmenter = new Intl.Segmenter(undefined, {\n granularity: 'grapheme',\n });\n for (const segment of segmenter.segment(text)) {\n offsets.push(segment.index + segment.segment.length);\n }\n return offsets;\n}\n\n/**\n * Expand tab characters to spaces using fixed tab stops: each tab advances to\n * the next multiple of tabSize from its running column, matching how the\n * rendered text expands tabs via CSS `tab-size`. Expanding every tab to a flat\n * tabSize would mis-measure tabs that follow other characters on the same line\n * (e.g. an alignment tab in `foo\\tbar`).\n */\nexport function expandTabsToSpaces(text: string, tabSize: number): string {\n if (!text.includes('\\t')) {\n return text;\n }\n let result = '';\n let column = 0;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === /* '\\t' */ 9) {\n const advance = tabSize - (column % tabSize);\n result += ' '.repeat(advance);\n column += advance;\n } else {\n result += text[i];\n column += 1;\n }\n }\n return result;\n}\n\n/**\n * Count the rendered columns of ASCII text, advancing each tab to the next\n * fixed tab stop (a multiple of tabSize) to match CSS `tab-size`. Returns -1\n * for non-ASCII text, which must be measured glyph-by-glyph instead.\n */\nexport function getExpandedAsciiTextColumns(\n text: string,\n tabSize: number\n): number {\n let columns = 0;\n for (let i = 0; i < text.length; i++) {\n const code = text.charCodeAt(i);\n if (code > 127) {\n return -1;\n }\n columns += code === /* '\\t' */ 9 ? tabSize - (columns % tabSize) : 1;\n }\n return columns;\n}\n"],"mappings":";;AAMA,MAAM,yBAAyB;AAE/B,IAAa,UAAb,MAAqB;CACnB;CACA;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UAAU,KAAK;EACf,UAAU;CACZ;CAEF,OAAO;AACT;;;;;;AAOA,SAAgB,4BACd,MACA,SACQ;CACR,IAAI,UAAU;CACd,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;EACpC,MAAM,OAAO,KAAK,WAAW,CAAC;EAC9B,IAAI,OAAO,KACT,OAAO;EAET,WAAW,SAAoB,IAAI,UAAW,UAAU,UAAW;CACrE;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"textMeasure.js","names":["#root","#canvasCtx","#font","#textWidthCache"],"sources":["../../src/editor/textMeasure.ts"],"sourcesContent":["import { getGraphemeSegmenter, h, round } from './utils';\n\n// Upper bound on cached DOM text-width measurements. The cache only holds\n// non-ASCII runs (emoji, ZWJ sequences, variation selectors), so it stays\n// small for ordinary code, but capping it prevents unbounded growth on\n// emoji-heavy documents. Past the cap the oldest entry is evicted.\nconst TEXT_WIDTH_CACHE_LIMIT = 4096;\n\nexport class Metrics {\n #root?: HTMLElement;\n #canvasCtx?: CanvasRenderingContext2D;\n #font?: string;\n\n // Memoizes domMeasureTextWidth() results\n #textWidthCache = new Map<string, number>();\n\n /** Width of the '0' character. */\n ch: number = -1;\n /** Size of a tab(\\t) character. */\n tabSize: number = 2;\n /** Height of the code line. */\n lineHeight: number = 20;\n /** Padding top of the root element. */\n paddingTop: number = 0;\n\n /** initialize the metrics */\n init(root: HTMLElement): void {\n if (\n this.#root === root &&\n this.#canvasCtx !== undefined &&\n this.ch !== -1\n ) {\n // already initialized\n return;\n }\n\n this.#root = root;\n this.#canvasCtx ??=\n document.createElement('canvas').getContext('2d') ?? undefined;\n if (this.#canvasCtx === undefined) {\n throw new Error('Could not get canvas context');\n }\n\n const parent = root.parentElement;\n if (parent !== null) {\n const { paddingTop } = getComputedStyle(parent);\n if (paddingTop.endsWith('px')) {\n this.paddingTop = parseFloat(paddingTop.slice(0, -2));\n }\n }\n\n const { fontSize, fontFamily, tabSize, lineHeight } =\n getComputedStyle(root);\n if (lineHeight.endsWith('px')) {\n this.lineHeight = parseFloat(lineHeight.slice(0, -2));\n } else if (fontSize.endsWith('px')) {\n this.lineHeight = round(\n parseFloat(fontSize.slice(0, -2)) * parseFloat(lineHeight)\n );\n }\n const font = fontSize + ' ' + fontFamily;\n if (this.#font !== font || this.ch === -1) {\n this.#font = font;\n this.#canvasCtx.font = font;\n this.ch = this.canvasMeasureTextWidth('0');\n // Cached DOM widths were measured against the previous font.\n this.clearTextWidthCache();\n }\n const nextTabSize = parseInt(tabSize, 10);\n if (!Number.isNaN(nextTabSize)) {\n this.tabSize = nextTabSize;\n }\n }\n\n /**\n * Re-measure the '0' character width against the font that is loaded right\n * now, returning true when it changed.\n *\n * A custom web font can finish loading after the editor first renders.\n * Until then canvas measureText reports the fallback font's width, and\n * getComputedStyle returns the same font-family string before and after the\n * file arrives, so init()'s font guard never re-measures on its own. Call\n * this once fonts have settled (e.g. on document.fonts.ready) to replace a\n * width measured against the fallback font with the real glyph width. The\n * boolean return lets the caller skip re-rendering when nothing changed.\n */\n remeasureCharacterWidth(): boolean {\n if (this.#canvasCtx === undefined || this.#font === undefined) {\n return false;\n }\n this.#canvasCtx.font = this.#font;\n const ch = this.canvasMeasureTextWidth('0');\n if (ch === this.ch) {\n return false;\n }\n this.ch = ch;\n return true;\n }\n\n /** measure the width of the text */\n measureTextWidth(text: string): number {\n const textWithExpandedTabs = expandTabsToSpaces(text, this.tabSize);\n if (needsDomTextMeasurement(textWithExpandedTabs)) {\n return this.domMeasureTextWidth(textWithExpandedTabs);\n }\n return this.canvasMeasureTextWidth(textWithExpandedTabs);\n }\n\n /** measure the width of the text using the canvas measureText API */\n canvasMeasureTextWidth(text: string): number {\n if (this.#canvasCtx === undefined) {\n throw new Error('Metrics not initialized');\n }\n return round(this.#canvasCtx.measureText(text).width);\n }\n\n /**\n * measure the width of the text using the DOM\n * this is slow because it cause a reflow, use it for non-ascii text;\n * results are memoized per text so repeated measurements skip the reflow\n */\n domMeasureTextWidth(text: string): number {\n if (this.#root === undefined) {\n throw new Error('Metrics not initialized');\n }\n const cacheKey = text + '|' + this.#font;\n const cached = this.#textWidthCache.get(cacheKey);\n if (cached !== undefined) {\n return cached;\n }\n const measureEl = h(\n 'span',\n {\n style: {\n position: 'absolute',\n top: '0',\n left: '0',\n visibility: 'hidden',\n pointerEvents: 'none',\n whiteSpace: 'pre',\n font: 'inherit',\n },\n textContent: text,\n },\n this.#root\n );\n let width: number;\n try {\n // round() to match canvasMeasureTextWidth and ch; otherwise the DOM path\n // returns raw sub-pixel widths and caret/selection offsets drift between\n // ASCII and non-ASCII runs on the same line.\n width = round(measureEl.getBoundingClientRect().width);\n } finally {\n measureEl.remove();\n }\n if (this.#textWidthCache.size >= TEXT_WIDTH_CACHE_LIMIT) {\n const oldestKey = this.#textWidthCache.keys().next().value;\n if (oldestKey !== undefined) {\n this.#textWidthCache.delete(oldestKey);\n }\n }\n this.#textWidthCache.set(cacheKey, width);\n return width;\n }\n\n /**\n * discard memoized DOM text widths\n * call this when the inherited font may have changed without re-running\n * init(), e.g. on a layout reflow, so stale widths are not reused\n */\n clearTextWidthCache(): void {\n this.#textWidthCache.clear();\n }\n}\n\n/** Check if the text needs DOM text measurement. */\nexport function needsDomTextMeasurement(text: string): boolean {\n for (let i = 0; i < text.length; i++) {\n const code = text.charCodeAt(i);\n if (\n (code >= 0xd800 && code <= 0xdfff) ||\n code === 0x200d ||\n code === 0xfe0e ||\n code === 0xfe0f\n ) {\n return true;\n }\n }\n return false;\n}\n\n/** snap the text offset to the Unicode boundary */\nexport function snapTextOffsetToUnicodeBoundary(\n text: string,\n offset: number\n): number {\n const boundedOffset = Math.max(0, Math.min(offset, text.length));\n if (\n boundedOffset === 0 ||\n boundedOffset === text.length ||\n !needsDomTextMeasurement(text)\n ) {\n return boundedOffset;\n }\n // Avoid measuring a caret position inside one visual emoji/grapheme.\n // Browser caret movement can report offsets around UTF-16 surrogate\n // pairs and emoji joiners; measuring a partial sequence gives a\n // replacement-glyph width.\n const segmenter = getGraphemeSegmenter();\n if (segmenter !== undefined) {\n for (const segment of segmenter.segment(text)) {\n const segmentStart = segment.index;\n const segmentEnd = segmentStart + segment.segment.length;\n if (boundedOffset > segmentStart && boundedOffset < segmentEnd) {\n return segmentEnd;\n }\n if (boundedOffset <= segmentStart) {\n break;\n }\n }\n return boundedOffset;\n }\n // Degraded path for engines lacking Intl.Segmenter: snap out of a\n // surrogate pair by stepping over code points.\n let segmentStart = 0;\n for (const codePoint of text) {\n const segmentEnd = segmentStart + codePoint.length;\n if (boundedOffset > segmentStart && boundedOffset < segmentEnd) {\n return segmentEnd;\n }\n if (boundedOffset <= segmentStart) {\n break;\n }\n segmentStart = segmentEnd;\n }\n return boundedOffset;\n}\n\n/** get the offsets of the Unicode grapheme clusters in the text */\nexport function getUnicodeMeasurementOffsets(\n text: string\n): number[] | undefined {\n if (!needsDomTextMeasurement(text)) {\n return undefined;\n }\n const offsets = [0];\n const segmenter = getGraphemeSegmenter();\n if (segmenter !== undefined) {\n for (const segment of segmenter.segment(text)) {\n offsets.push(segment.index + segment.segment.length);\n }\n return offsets;\n }\n // Degraded path for engines lacking Intl.Segmenter: step by code point.\n let offset = 0;\n for (const codePoint of text) {\n offset += codePoint.length;\n offsets.push(offset);\n }\n return offsets;\n}\n\n/**\n * Expand tab characters to spaces using fixed tab stops: each tab advances to\n * the next multiple of tabSize from its running column, matching how the\n * rendered text expands tabs via CSS `tab-size`. Expanding every tab to a flat\n * tabSize would mis-measure tabs that follow other characters on the same line\n * (e.g. an alignment tab in `foo\\tbar`).\n */\nexport function expandTabsToSpaces(text: string, tabSize: number): string {\n if (!text.includes('\\t')) {\n return text;\n }\n let result = '';\n let column = 0;\n for (let i = 0; i < text.length; i++) {\n if (text.charCodeAt(i) === /* '\\t' */ 9) {\n const advance = tabSize - (column % tabSize);\n result += ' '.repeat(advance);\n column += advance;\n } else {\n result += text[i];\n column += 1;\n }\n }\n return result;\n}\n\n/**\n * Count the rendered columns of ASCII text, advancing each tab to the next\n * fixed tab stop (a multiple of tabSize) to match CSS `tab-size`. Returns -1\n * for non-ASCII text, which must be measured glyph-by-glyph instead.\n */\nexport function getExpandedAsciiTextColumns(\n text: string,\n tabSize: number\n): number {\n let columns = 0;\n for (let i = 0; i < text.length; i++) {\n const code = text.charCodeAt(i);\n if (code > 127) {\n return -1;\n }\n columns += code === /* '\\t' */ 9 ? tabSize - (columns % tabSize) : 1;\n }\n return columns;\n}\n"],"mappings":";;AAMA,MAAM,yBAAyB;AAE/B,IAAa,UAAb,MAAqB;CACnB;CACA;CACA;CAGA,kCAAkB,IAAI,IAAoB;;CAG1C,KAAa;;CAEb,UAAkB;;CAElB,aAAqB;;CAErB,aAAqB;;CAGrB,KAAK,MAAyB;EAC5B,IACE,KAAKA,UAAU,QACf,KAAKC,eAAe,KAAA,KACpB,KAAK,OAAO,IAGZ;EAGF,KAAKD,QAAQ;EACb,KAAKC,eACH,SAAS,cAAc,QAAQ,CAAC,CAAC,WAAW,IAAI,KAAK,KAAA;EACvD,IAAI,KAAKA,eAAe,KAAA,GACtB,MAAM,IAAI,MAAM,8BAA8B;EAGhD,MAAM,SAAS,KAAK;EACpB,IAAI,WAAW,MAAM;GACnB,MAAM,EAAE,eAAe,iBAAiB,MAAM;GAC9C,IAAI,WAAW,SAAS,IAAI,GAC1B,KAAK,aAAa,WAAW,WAAW,MAAM,GAAG,EAAE,CAAC;EAExD;EAEA,MAAM,EAAE,UAAU,YAAY,SAAS,eACrC,iBAAiB,IAAI;EACvB,IAAI,WAAW,SAAS,IAAI,GAC1B,KAAK,aAAa,WAAW,WAAW,MAAM,GAAG,EAAE,CAAC;OAC/C,IAAI,SAAS,SAAS,IAAI,GAC/B,KAAK,aAAa,MAChB,WAAW,SAAS,MAAM,GAAG,EAAE,CAAC,IAAI,WAAW,UAAU,CAC3D;EAEF,MAAM,OAAO,WAAW,MAAM;EAC9B,IAAI,KAAKC,UAAU,QAAQ,KAAK,OAAO,IAAI;GACzC,KAAKA,QAAQ;GACb,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