{"version":3,"file":"word-navigation.d.ts","sourceRoot":"","sources":["../src/word-navigation.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACrC,mEAAmE;IACnE,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACvD,yGAAyG;IACzG,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC;CAC/C;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,MAAM,CAgDtG;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,MAAM,CAuCrG","sourcesContent":["import { getWordSegmenter, isWhitespaceChar, PUNCTUATION_REGEX } from \"./utils.ts\";\n\nconst wordSegmenter = getWordSegmenter();\n\n/**\n * Options for word navigation functions.\n * When omitted, uses the default Intl.Segmenter word segmentation.\n */\nexport interface WordNavigationOptions {\n\t/** Custom segmenter returning word segments for the given text. */\n\tsegment?: (text: string) => Iterable<Intl.SegmentData>;\n\t/** Predicate identifying atomic segments that should be treated as single units (e.g. paste markers). */\n\tisAtomicSegment?: (segment: string) => boolean;\n}\n\n/**\n * Find the cursor position after moving one word backward from `cursor` in `text`.\n * Skips trailing whitespace, then stops at the next word/punctuation boundary.\n *\n * Pure function - does not mutate any state.\n */\nexport function findWordBackward(text: string, cursor: number, options?: WordNavigationOptions): number {\n\tif (cursor <= 0) return 0;\n\n\tconst textBeforeCursor = text.slice(0, cursor);\n\tconst segmentFn = options?.segment;\n\tconst isAtomic = options?.isAtomicSegment;\n\tconst segments = segmentFn ? [...segmentFn(textBeforeCursor)] : [...wordSegmenter.segment(textBeforeCursor)];\n\tlet newCursor = cursor;\n\n\t// Skip trailing whitespace\n\twhile (\n\t\tsegments.length > 0 &&\n\t\t!isAtomic?.(segments[segments.length - 1]?.segment || \"\") &&\n\t\tisWhitespaceChar(segments[segments.length - 1]?.segment || \"\")\n\t) {\n\t\tnewCursor -= segments.pop()?.segment.length || 0;\n\t}\n\n\tif (segments.length === 0) return newCursor;\n\n\tconst last = segments[segments.length - 1]!;\n\n\tif (isAtomic?.(last.segment)) {\n\t\t// Skip one atomic segment.\n\t\tnewCursor -= last.segment.length;\n\t} else if (last.isWordLike) {\n\t\t// Skip inside one word-like segment, preserving ASCII punctuation boundaries.\n\t\tconst segment = last.segment;\n\t\tconst matches = [...segment.matchAll(new RegExp(PUNCTUATION_REGEX, \"g\"))];\n\t\tif (matches.length <= 0) {\n\t\t\tnewCursor -= segment.length;\n\t\t} else {\n\t\t\tconst lastMatch = matches[matches.length - 1]!;\n\t\t\tnewCursor -= segment.length - (lastMatch.index + lastMatch[0].length);\n\t\t}\n\t} else {\n\t\t// Skip non-word non-whitespace run (punctuation)\n\t\twhile (\n\t\t\tsegments.length > 0 &&\n\t\t\t!isAtomic?.(segments[segments.length - 1]?.segment || \"\") &&\n\t\t\t!segments[segments.length - 1]?.isWordLike &&\n\t\t\t!isWhitespaceChar(segments[segments.length - 1]?.segment || \"\")\n\t\t) {\n\t\t\tnewCursor -= segments.pop()?.segment.length || 0;\n\t\t}\n\t}\n\n\treturn newCursor;\n}\n\n/**\n * Find the cursor position after moving one word forward from `cursor` in `text`.\n * Skips leading whitespace, then stops at the next word/punctuation boundary.\n *\n * Pure function - does not mutate any state.\n */\nexport function findWordForward(text: string, cursor: number, options?: WordNavigationOptions): number {\n\tif (cursor >= text.length) return text.length;\n\n\tconst textAfterCursor = text.slice(cursor);\n\tconst segmentFn = options?.segment;\n\tconst isAtomic = options?.isAtomicSegment;\n\tconst segments = segmentFn ? segmentFn(textAfterCursor) : wordSegmenter.segment(textAfterCursor);\n\tconst iterator = segments[Symbol.iterator]();\n\tlet next = iterator.next();\n\tlet newCursor = cursor;\n\n\t// Skip leading whitespace\n\twhile (!next.done && !isAtomic?.(next.value.segment) && isWhitespaceChar(next.value.segment)) {\n\t\tnewCursor += next.value.segment.length;\n\t\tnext = iterator.next();\n\t}\n\n\tif (next.done) return newCursor;\n\n\tif (isAtomic?.(next.value.segment)) {\n\t\t// Skip one atomic segment.\n\t\tnewCursor += next.value.segment.length;\n\t} else if (next.value.isWordLike) {\n\t\t// Skip inside one word-like segment, preserving ASCII punctuation boundaries.\n\t\tnewCursor += PUNCTUATION_REGEX.exec(next.value.segment)?.index ?? next.value.segment.length;\n\t} else {\n\t\t// Skip non-word non-whitespace run (punctuation)\n\t\twhile (\n\t\t\t!next.done &&\n\t\t\t!isAtomic?.(next.value.segment) &&\n\t\t\t!next.value.isWordLike &&\n\t\t\t!isWhitespaceChar(next.value.segment)\n\t\t) {\n\t\t\tnewCursor += next.value.segment.length;\n\t\t\tnext = iterator.next();\n\t\t}\n\t}\n\n\treturn newCursor;\n}\n"]}