Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Cotabby/Support/SuggestionSessionReconciler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ enum SuggestionSessionReconciler {
index = remainingText.index(after: index)
}

// Space-less scripts (CJK, Japanese, Korean, Thai, ...) put no whitespace between words, so the
// whitespace scan above swallows an entire run as a single "word". When the token begins with
// such a script, segment it with ICU word breaking and accept only the first word, so one Tab
// advances by a single word the way it does in space-delimited text. Space-delimited scripts
// never enter this branch, so Latin / Cyrillic / Arabic acceptance stays byte-for-byte unchanged.
if tokenStart < index,
remainingText[tokenStart].beginsSpacelessScriptWord,
let wordEnd = firstSegmentedWordEnd(in: remainingText, from: tokenStart, notPast: index) {
index = wordEnd
}

if !autoAcceptTrailingPunctuation,
let wordEnd = wordEndTrimmingTrailingPunctuation(in: remainingText, from: tokenStart, to: index) {
index = wordEnd
Expand All @@ -255,6 +266,26 @@ enum SuggestionSessionReconciler {
return String(remainingText[..<index])
}

/// The index just past the first ICU word in `text[from..<limit]`, or nil when segmentation finds
/// no word there. Only the space-less-script branch of `nextAcceptanceChunk` calls this; the result
/// is clamped to `limit` so it can never extend past the non-whitespace token the caller already
/// bounded. `.substringNotRequired` skips materializing the word string we don't need.
private static func firstSegmentedWordEnd(
in text: String,
from start: String.Index,
notPast limit: String.Index
) -> String.Index? {
var wordEnd: String.Index?
text.enumerateSubstrings(in: start..<limit, options: [.byWords, .substringNotRequired]) { _, range, _, stop in
wordEnd = range.upperBound
stop = true
}
guard let wordEnd, wordEnd > start else {
return nil
}
return min(wordEnd, limit)
}

/// Accepts a full phrase up to the next sentence terminator (`.`, `!`, `?`, `\n`) or the end
/// of the buffered suggestion tail. Composes over `nextAcceptanceChunk` so word-boundary,
/// internal-punctuation, and leading-whitespace policy stay identical across the seams of a
Expand Down Expand Up @@ -490,6 +521,34 @@ private extension String {
}

private extension Character {
/// True when the character begins a word of a space-less script (Han, Hiragana, Katakana, Hangul,
/// Thai, Lao, Khmer, Myanmar, ...). These scripts write words without separating spaces, so the
/// whitespace-run acceptance rule would over-accept a whole run; `nextAcceptanceChunk` switches to
/// ICU word segmentation when a token begins with one of them. Detection is by leading scalar so it
/// stays a cheap, allocation-free check on the common (space-delimited) path.
var beginsSpacelessScriptWord: Bool {
guard let scalar = unicodeScalars.first else {
return false
}
switch scalar.value {
case 0x3040...0x30FF, // Hiragana + Katakana
0x3400...0x4DBF, // CJK Unified Ideographs Extension A
0x4E00...0x9FFF, // CJK Unified Ideographs
0xF900...0xFAFF, // CJK Compatibility Ideographs
0xAC00...0xD7A3, // Hangul syllables
0x1100...0x11FF, // Hangul Jamo
0x0E00...0x0E7F, // Thai
0x0E80...0x0EFF, // Lao
0x1780...0x17FF, // Khmer
0x1000...0x109F, // Myanmar
0x20000...0x2A6DF, // CJK Unified Ideographs Extension B
0x30000...0x3134F: // CJK Unified Ideographs Extension G
Comment on lines +544 to +545

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 CJK Unified Ideograph Extensions C, D, E, and F (U+2A700–U+2EBEF) are absent from the switch. Any suggestion that starts with a character from those blocks (rare/archaic Han characters appearing in scholarly texts) would pass beginsSpacelessScriptWord as false and fall through to the old whitespace-scan path, accepting the whole run on a single Tab. Extension B ends at U+2A6DF and Extension G starts at U+30000, so the gap is currently silent.

Suggested change
0x20000...0x2A6DF, // CJK Unified Ideographs Extension B
0x30000...0x3134F: // CJK Unified Ideographs Extension G
0x20000...0x2A6DF, // CJK Unified Ideographs Extension B
0x2A700...0x2B73F, // CJK Unified Ideographs Extension C
0x2B740...0x2B81F, // CJK Unified Ideographs Extension D
0x2B820...0x2CEAF, // CJK Unified Ideographs Extension E
0x2CEB0...0x2EBEF, // CJK Unified Ideographs Extension F
0x30000...0x3134F: // CJK Unified Ideographs Extension G

Fix in Codex Fix in Claude Code

return true
default:
return false
}
}

/// Alphanumerics form the core of a "word"; everything else trailing a word is punctuation that
/// can be peeled into its own acceptance part when auto-accept is disabled.
var isAcceptanceWordCharacter: Bool {
Expand Down
55 changes: 55 additions & 0 deletions CotabbyTests/SuggestionSessionReconcilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,61 @@ final class SuggestionSessionReconcilerTests: XCTestCase {
)
}

// MARK: - Space-less-script word acceptance

func test_nextAcceptanceChunk_latinAcceptanceIsUnchangedBySpacelessBranch() {
// Regression guard: the space-less branch must never alter space-delimited acceptance.
XCTAssertEqual(SuggestionSessionReconciler.nextAcceptanceChunk(from: "hello world"), "hello")
XCTAssertEqual(SuggestionSessionReconciler.nextAcceptanceChunk(from: "don't stop now"), "don't")
XCTAssertEqual(SuggestionSessionReconciler.nextAcceptanceChunk(from: "U.S.A today"), "U.S.A")
XCTAssertEqual(SuggestionSessionReconciler.nextAcceptanceChunk(from: "1.5 times"), "1.5")
XCTAssertEqual(SuggestionSessionReconciler.nextAcceptanceChunk(from: "café René"), "café")
// A space-less script appearing later in the tail must not pull the first Latin token early.
XCTAssertEqual(SuggestionSessionReconciler.nextAcceptanceChunk(from: "world 你好"), "world")
}

func test_nextAcceptanceChunk_segmentsChineseBelowWholeLength() {
// ICU word segmentation may be per-character or dictionary-based depending on the OS, so this
// asserts the robust property (accept one segment, not the whole run) rather than a pinned word.
let run = "你好世界"
let chunk = SuggestionSessionReconciler.nextAcceptanceChunk(from: run)
XCTAssertFalse(chunk.isEmpty)
XCTAssertTrue(run.hasPrefix(chunk))
XCTAssertLessThan(chunk.count, run.count, "a space-less Chinese run must segment, not accept the whole run")
}

func test_nextAcceptanceChunk_segmentsJapaneseRunBelowWholeLength() {
let run = "今日はいい天気です"
let chunk = SuggestionSessionReconciler.nextAcceptanceChunk(from: run)
XCTAssertFalse(chunk.isEmpty)
XCTAssertTrue(run.hasPrefix(chunk))
XCTAssertLessThan(chunk.count, run.count, "a space-less Japanese run must segment, not accept whole")
}

func test_nextAcceptanceChunk_segmentsThaiRunBelowWholeLength() {
let run = "สวัสดีครับ"
let chunk = SuggestionSessionReconciler.nextAcceptanceChunk(from: run)
XCTAssertFalse(chunk.isEmpty)
XCTAssertTrue(run.hasPrefix(chunk))
XCTAssertLessThan(chunk.count, run.count, "a space-less Thai run must segment, not accept whole")
}

func test_nextAcceptanceChunk_chineseAcceptanceStaysWithinRunBeforeSpace() {
let chunk = SuggestionSessionReconciler.nextAcceptanceChunk(from: "你好 world")
XCTAssertFalse(chunk.isEmpty)
XCTAssertTrue("你好".hasPrefix(chunk), "acceptance must stay within the CJK run and not cross the space")
XCTAssertFalse(chunk.contains(" "))
}

func test_nextAcceptanceChunk_keepsLeadingWhitespaceBeforeSpacelessWord() {
let chunk = SuggestionSessionReconciler.nextAcceptanceChunk(from: " 你好世界")
XCTAssertTrue(chunk.hasPrefix(" "), "leading whitespace is preserved before the segmented word")
let afterSpace = String(chunk.dropFirst())
XCTAssertFalse(afterSpace.isEmpty)
XCTAssertTrue("你好世界".hasPrefix(afterSpace))
XCTAssertLessThan(afterSpace.count, 4, "only the first segment is accepted, not the whole run")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Fragile magic-number bound in leading-whitespace test

The assertion afterSpace.count < 4 is tied to the length of the literal run "你好世界" (4 grapheme clusters). If a future maintainer changes the run to something shorter — say 3 characters — the guard would vacuously pass even if the entire run were accepted in one chunk. Expressing the bound relative to the actual run length (e.g. afterSpace.count < "你好世界".count) makes the intent self-documenting and resilient to edits.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Codex Fix in Claude Code

}

// MARK: - Phrase chunker

func test_nextAcceptancePhrase_returnsEmptyForEmptyTail() {
Expand Down