word-navigation.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { getWordSegmenter, isWhitespaceChar, PUNCTUATION_REGEX } from "./utils.ts";
  2. const wordSegmenter = getWordSegmenter();
  3. /**
  4. * Options for word navigation functions.
  5. * When omitted, uses the default Intl.Segmenter word segmentation.
  6. */
  7. export interface WordNavigationOptions {
  8. /** Custom segmenter returning word segments for the given text. */
  9. segment?: (text: string) => Iterable<Intl.SegmentData>;
  10. /** Predicate identifying atomic segments that should be treated as single units (e.g. paste markers). */
  11. isAtomicSegment?: (segment: string) => boolean;
  12. }
  13. /**
  14. * Find the cursor position after moving one word backward from `cursor` in `text`.
  15. * Skips trailing whitespace, then stops at the next word/punctuation boundary.
  16. *
  17. * Pure function - does not mutate any state.
  18. */
  19. export function findWordBackward(text: string, cursor: number, options?: WordNavigationOptions): number {
  20. if (cursor <= 0) return 0;
  21. const textBeforeCursor = text.slice(0, cursor);
  22. const segmentFn = options?.segment;
  23. const isAtomic = options?.isAtomicSegment;
  24. const segments = segmentFn ? [...segmentFn(textBeforeCursor)] : [...wordSegmenter.segment(textBeforeCursor)];
  25. let newCursor = cursor;
  26. // Skip trailing whitespace
  27. while (
  28. segments.length > 0 &&
  29. !isAtomic?.(segments[segments.length - 1]?.segment || "") &&
  30. isWhitespaceChar(segments[segments.length - 1]?.segment || "")
  31. ) {
  32. newCursor -= segments.pop()?.segment.length || 0;
  33. }
  34. if (segments.length === 0) return newCursor;
  35. const last = segments[segments.length - 1]!;
  36. if (isAtomic?.(last.segment)) {
  37. // Skip one atomic segment.
  38. newCursor -= last.segment.length;
  39. } else if (last.isWordLike) {
  40. // Skip inside one word-like segment, preserving ASCII punctuation boundaries.
  41. const segment = last.segment;
  42. const matches = [...segment.matchAll(new RegExp(PUNCTUATION_REGEX, "g"))];
  43. if (matches.length <= 0) {
  44. newCursor -= segment.length;
  45. } else {
  46. const lastMatch = matches[matches.length - 1]!;
  47. newCursor -= segment.length - (lastMatch.index + lastMatch[0].length);
  48. }
  49. } else {
  50. // Skip non-word non-whitespace run (punctuation)
  51. while (
  52. segments.length > 0 &&
  53. !isAtomic?.(segments[segments.length - 1]?.segment || "") &&
  54. !segments[segments.length - 1]?.isWordLike &&
  55. !isWhitespaceChar(segments[segments.length - 1]?.segment || "")
  56. ) {
  57. newCursor -= segments.pop()?.segment.length || 0;
  58. }
  59. }
  60. return newCursor;
  61. }
  62. /**
  63. * Find the cursor position after moving one word forward from `cursor` in `text`.
  64. * Skips leading whitespace, then stops at the next word/punctuation boundary.
  65. *
  66. * Pure function - does not mutate any state.
  67. */
  68. export function findWordForward(text: string, cursor: number, options?: WordNavigationOptions): number {
  69. if (cursor >= text.length) return text.length;
  70. const textAfterCursor = text.slice(cursor);
  71. const segmentFn = options?.segment;
  72. const isAtomic = options?.isAtomicSegment;
  73. const segments = segmentFn ? segmentFn(textAfterCursor) : wordSegmenter.segment(textAfterCursor);
  74. const iterator = segments[Symbol.iterator]();
  75. let next = iterator.next();
  76. let newCursor = cursor;
  77. // Skip leading whitespace
  78. while (!next.done && !isAtomic?.(next.value.segment) && isWhitespaceChar(next.value.segment)) {
  79. newCursor += next.value.segment.length;
  80. next = iterator.next();
  81. }
  82. if (next.done) return newCursor;
  83. if (isAtomic?.(next.value.segment)) {
  84. // Skip one atomic segment.
  85. newCursor += next.value.segment.length;
  86. } else if (next.value.isWordLike) {
  87. // Skip inside one word-like segment, preserving ASCII punctuation boundaries.
  88. newCursor += PUNCTUATION_REGEX.exec(next.value.segment)?.index ?? next.value.segment.length;
  89. } else {
  90. // Skip non-word non-whitespace run (punctuation)
  91. while (
  92. !next.done &&
  93. !isAtomic?.(next.value.segment) &&
  94. !next.value.isWordLike &&
  95. !isWhitespaceChar(next.value.segment)
  96. ) {
  97. newCursor += next.value.segment.length;
  98. next = iterator.next();
  99. }
  100. }
  101. return newCursor;
  102. }