@devries-cameron20 / t3code-1 / commits / b81202748b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): typing before a chip no longer leaves the iOS caret behind (#33774)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Arun Krishnamurthy committed Nov 14, 2026 at 12:29 UTC b81202748b4d3727f351de43e452f7e3c7dc7349
2 files changed +60 -15
apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift
+25 -11
@@ -1,7 +1,7 @@
1 import ExpoModulesCore
2 import UIKit
3
4 -private struct ComposerTokenPayload: Decodable {
4 +private struct ComposerTokenPayload: Decodable, Equatable {
5 let type: String
6 let source: String
7 let label: String
@@ -9,8 +9,16 @@ private struct ComposerTokenPayload: Decodable {
9 let accent: String?
10 let tintsIcon: Bool?
11 let detail: String?
12 - let start: Int
13 - let end: Int
12 + var start: Int
13 + var end: Int
14 +
15 + func rendersLike(_ other: Self) -> Bool {
16 + var moved = other
17 + moved.start = start
18 + moved.end = end
19 + // String == accepts canonically equivalent sources, but their UTF-16 lengths differ.
20 + return moved == self && source.utf16.elementsEqual(other.source.utf16)
21 + }
22 }
23
24 private struct ComposerSelectionPayload: Decodable, Equatable {
@@ -714,8 +722,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
722 hasControlledDocument = true
723 if tokensJson != document.tokensJson {
724 tokensJson = document.tokensJson
717 - tokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? []
718 - tokensNeedRebuild = true
725 + let nextTokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? []
726 + // Typing before a chip only moves its offsets; rebuilding then makes iOS autocorrect behind the caret.
727 + tokensNeedRebuild = tokensNeedRebuild || !nextTokens.elementsEqual(tokens, by: { $0.rendersLike($1) })
728 + tokens = nextTokens
729 }
730 let previousSelection = sourceSelection()
731 value = document.value
@@ -1330,7 +1340,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
1340
1341 private func documentMatchesExpectedTokens() -> Bool {
1342 let source = value as NSString
1333 - let expectedSources = tokens.compactMap { token -> String? in
1343 + let expectedChips = tokens.compactMap { token -> (Int, String)? in
1344 guard token.start >= 0,
1345 token.end > token.start,
1346 token.end <= source.length,
@@ -1339,18 +1349,22 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
1349 ) == token.source else {
1350 return nil
1351 }
1342 - return token.source
1352 + return (token.start, token.source)
1353 }
1344 - var renderedSources: [String] = []
1354 + var renderedChips: [(Int, String)] = []
1355 + var sourceOffset = 0
1356 textView.attributedText.enumerateAttribute(
1357 .attachment,
1358 in: NSRange(location: 0, length: textView.attributedText.length)
1348 - ) { value, _, _ in
1359 + ) { value, range, _ in
1360 if let attachment = value as? ComposerTextAttachment {
1350 - renderedSources.append(attachment.source)
1361 + renderedChips.append((sourceOffset, attachment.source))
1362 + sourceOffset += (attachment.source as NSString).length
1363 + } else {
1364 + sourceOffset += range.length
1365 }
1366 }
1353 - return renderedSources == expectedSources
1367 + return renderedChips.elementsEqual(expectedChips, by: ==)
1368 }
1369 }
1370
apps/mobile/modules/t3-composer-editor/tests/ios/main.swift
+35 -4
@@ -11,13 +11,16 @@ func check(_ passed: Bool, _ message: String) {
11 extension T3ComposerEditorView {
12 var editor: UITextView { textView }
13 var text: String { textView.serializedText() }
14 + func chip(at location: Int) -> NSTextAttachment? {
15 + textView.textStorage.attribute(.attachment, at: location, effectiveRange: nil) as? NSTextAttachment
16 + }
17 }
18
16 -func document(_ value: String, revision: Int, resetGeneration: Int = 0, isNativeEcho: Bool = false) -> String {
19 +func document(_ value: String, revision: Int, resetGeneration: Int = 0, isNativeEcho: Bool = false, tokensJson: String = "[]") -> String {
20 let payload: [String: Any] = [
21 "value": value,
22 "selection": NSNull(),
20 - "tokensJson": "[]",
23 + "tokensJson": tokensJson,
24 "mostRecentEventCount": revision,
25 "isNativeEcho": isNativeEcho,
26 "resetGeneration": resetGeneration,
@@ -33,10 +36,14 @@ func replace(_ view: T3ComposerEditorView, _ target: String, with replacement: S
36 editor.replace(editor.textRange(from: start, to: end)!, withText: replacement)
37 }
38
36 -func makeEditor(_ value: String) -> T3ComposerEditorView {
39 +func mention(_ label: String, at start: Int, source: String = "@README.md") -> String {
40 + #"[{"type":"mention","source":"\#(source)","label":"\#(label)","start":\#(start),"end":\#(start + source.utf16.count)}]"#
41 +}
42 +
43 +func makeEditor(_ value: String, tokensJson: String = "[]") -> T3ComposerEditorView {
44 let view = T3ComposerEditorView()
45 view.frame = CGRect(x: 0, y: 0, width: 320, height: 200)
39 - view.setControlledDocumentJson(document(value, revision: 0))
46 + view.setControlledDocumentJson(document(value, revision: 0, tokensJson: tokensJson))
47 check(view.text == value, "Initial controlled value was not applied")
48 return view
49 }
@@ -108,6 +115,30 @@ do {
115 )
116 }
117
118 +// An echo that only shifts a chip keeps the rendered text; moving or relabeling the chip redraws it.
119 +do {
120 + let draft = "hi! @README.md @README.md"
121 + let view = makeEditor("hi @README.md @README.md", tokensJson: mention("README.md", at: 3))
122 + let rendered = view.chip(at: 3)
123 + check(rendered != nil, "Harness could not render the chip")
124 + view.editor.selectedRange = NSRange(location: 2, length: 0)
125 + view.editor.insertText("!")
126 + let revision = view.onComposerChange.payloads.last?["eventCount"] as? Int ?? 0
127 + view.setControlledDocumentJson(document(draft, revision: revision, isNativeEcho: true, tokensJson: mention("README.md", at: 4)))
128 + check(view.chip(at: 4) === rendered, "An echo that only moved a chip replaced the text")
129 + view.setControlledDocumentJson(document(draft, revision: revision, tokensJson: mention("Readme", at: 4)))
130 + check((view.chip(at: 4) as? ComposerTextAttachment)?.label == "Readme", "A relabeled chip was not redrawn")
131 + view.setControlledDocumentJson(document(draft, revision: revision, tokensJson: mention("Readme", at: 15)))
132 + check(view.chip(at: 4) == nil && view.chip(at: 15) != nil, "A chip moved to another copy of its source stayed on the old one")
133 +}
134 +
135 +// A chip source that changes only in Unicode normalization is rebuilt from the new text.
136 +do {
137 + let view = makeEditor("@\u{00E9}", tokensJson: mention("e", at: 0, source: "@\u{00E9}"))
138 + view.setControlledDocumentJson(document("@e\u{0301}", revision: 0, tokensJson: mention("e", at: 0, source: "@e\u{0301}")))
139 + check(view.text.utf16.elementsEqual("@e\u{0301}".utf16), "A renormalized chip source kept the old text")
140 +}
141 +
142 print("composer editor tests passed")
143
144 private let dropEditor = ComposerTextView()