{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../../src/core/lsp/position.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;GAKG;AAEH,MAAM,WAAW,SAAS;IACzB,8CAA8C;IAC9C,UAAU,EAAE,MAAM,EAAE,CAAC;CACrB;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAQxD;AAaD,gFAAgF;AAChF,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,CAgB5F;AAED,8DAA8D;AAC9D,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,WAAW,GAAG,MAAM,CAKzF;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,CAE/E","sourcesContent":["import type { LspPosition } from \"./types.js\";\n\n/**\n * Position/offset conversion for LSP using UTF-16 code units (the LSP default\n * and the only encoding we negotiate from servers on initialize). Operates on\n * a per-file line index and is CRLF/LF aware: line breaks are \\n (with optional\n * \\r ignored as part of the line terminator).\n */\n\nexport interface LineIndex {\n\t/** Byte offsets of each line start (0..n). */\n\tlineStarts: number[];\n}\n\nexport function computeLineIndex(text: string): LineIndex {\n\tconst lineStarts: number[] = [0];\n\tfor (let i = 0; i < text.length; i++) {\n\t\tif (text.charCodeAt(i) === 10) {\n\t\t\tlineStarts.push(i + 1);\n\t\t}\n\t}\n\treturn { lineStarts };\n}\n\nfunction lineStart(index: LineIndex, line: number): number {\n\tif (line <= 0) return 0;\n\tif (line >= index.lineStarts.length) return index.lineStarts[index.lineStarts.length - 1];\n\treturn index.lineStarts[line];\n}\n\nfunction lineEnd(index: LineIndex, text: string, line: number): number {\n\tif (line >= index.lineStarts.length - 1) return text.length;\n\treturn index.lineStarts[line + 1] - (text.charCodeAt(index.lineStarts[line + 1] - 1) === 10 ? 1 : 0);\n}\n\n/** Convert a UTF-16 code-unit offset to an LSP position (0-based line/char). */\nexport function offsetToPosition(text: string, index: LineIndex, offset: number): LspPosition {\n\tconst clamped = Math.max(0, Math.min(offset, text.length));\n\t// Binary search for the line.\n\tlet lo = 0;\n\tlet hi = index.lineStarts.length - 1;\n\twhile (lo < hi) {\n\t\tconst mid = (lo + hi + 1) >> 1;\n\t\tif (index.lineStarts[mid] <= clamped) lo = mid;\n\t\telse hi = mid - 1;\n\t}\n\tconst line = lo;\n\tconst lineStartOffset = lineStart(index, line);\n\t// LSP character is a UTF-16 code-unit count; JS string offsets ARE UTF-16\n\t// code units, so character == offset delta within the line.\n\tconst character = clamped - lineStartOffset;\n\treturn { line, character };\n}\n\n/** Convert an LSP position (UTF-16) to a JS string offset. */\nexport function positionToOffset(text: string, index: LineIndex, pos: LspPosition): number {\n\tconst start = lineStart(index, pos.line);\n\tconst end = lineEnd(index, text, pos.line);\n\t// JS string offsets are UTF-16 code units.\n\treturn Math.max(start, Math.min(end, start + pos.character));\n}\n\nexport function offsetToLineCharacter(text: string, offset: number): LspPosition {\n\treturn offsetToPosition(text, computeLineIndex(text), offset);\n}\n"]}