fix(tui): preserve ASCII punctuation word boundaries with Intl.Segmenter (#5067)

This commit is contained in:
xu0o0
2026-05-27 18:12:10 +08:00
committed by GitHub
parent 493efd422d
commit b62776e4a2
4 changed files with 66 additions and 6 deletions

View File

@@ -4,7 +4,14 @@ import { decodePrintableKey, matchesKey } from "../keys.ts";
import { KillRing } from "../kill-ring.ts";
import { type Component, CURSOR_MARKER, type Focusable, type TUI } from "../tui.ts";
import { UndoStack } from "../undo-stack.ts";
import { getGraphemeSegmenter, getWordSegmenter, isWhitespaceChar, truncateToWidth, visibleWidth } from "../utils.ts";
import {
getGraphemeSegmenter,
getWordSegmenter,
isWhitespaceChar,
PUNCTUATION_REGEX,
truncateToWidth,
visibleWidth,
} from "../utils.ts";
import { SelectList, type SelectListLayoutOptions, type SelectListTheme } from "./select-list.ts";
const graphemeSegmenter = getGraphemeSegmenter();
@@ -1788,9 +1795,19 @@ export class Editor implements Component, Focusable {
if (segments.length > 0) {
const last = segments[segments.length - 1]!;
if (isPasteMarker(last.segment) || last.isWordLike) {
// Skip one word-like segment (or paste marker)
if (isPasteMarker(last.segment)) {
// Skip one paste marker.
newCol -= segments.pop()?.segment.length || 0;
} else if (last.isWordLike) {
// Skip inside one word-like segment, preserving existing ASCII punctuation boundaries.
const segment = last.segment;
const matches = [...segment.matchAll(new RegExp(PUNCTUATION_REGEX, "g"))];
if (matches.length <= 0) {
newCol -= segment.length;
} else {
const lastMatch = matches[matches.length - 1]!;
newCol -= segment.length - (lastMatch.index + lastMatch[0].length);
}
} else {
// Skip non-word non-whitespace run (punctuation)
while (
@@ -2004,9 +2021,12 @@ export class Editor implements Component, Focusable {
}
if (!next.done) {
if (isPasteMarker(next.value.segment) || next.value.isWordLike) {
// Skip one word-like segment (or paste marker)
if (isPasteMarker(next.value.segment)) {
// Skip one paste marker.
newCol += next.value.segment.length;
} else if (next.value.isWordLike) {
// Skip inside one word-like segment, preserving existing ASCII punctuation boundaries.
newCol += PUNCTUATION_REGEX.exec(next.value.segment)?.index ?? next.value.segment.length;
} else {
// Skip non-word non-whitespace run (punctuation)
while (