The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
fix(mobile): show complete unwrapped diff lines containing tabs (#14605)
Jake Leventhal committed
Oct 18, 2026 at 15:20 UTC
98277652122c2f7376f737c81f1719cea1eeeb75
3 files changed
+74
-26
apps/mobile/modules/t3-review-diff/ios/ReviewDiffCodeLayout.swift
+12
-3
@@ -1,7 +1,10 @@
1
import UIKit
2
3
/// ASCII uses fixed-pitch columns. TextKit handles shaping, tabs, and Unicode highlights.
4
+/// Pass `unboundedWidth` to lay a row out on one line for horizontal scrolling.
5
final class ReviewDiffCodeLayout: NSObject {
6
+ static let unboundedWidth = CGFloat.greatestFiniteMagnitude
7
+
8
// Measurement reuses one engine; only recently drawn rows retain a full TextKit layout.
9
private static var measurer: ReviewDiffTextLayout {
10
let key = "T3ReviewDiff.textMeasurer"
@@ -20,6 +23,8 @@ final class ReviewDiffCodeLayout: NSObject {
23
let lineHeight: CGFloat
24
let firstLineHeight: CGFloat
25
let extraHeight: CGFloat
26
+ /// Widest visual line, which sizes horizontal scrolling when the row is not wrapped.
27
+ let usedWidth: CGFloat
28
private let font: UIFont
29
private let width: CGFloat
30
private let characterWidth: CGFloat
@@ -32,10 +37,12 @@ final class ReviewDiffCodeLayout: NSObject {
37
self.characterWidth = characterWidth
38
lineHeight = ceil(font.lineHeight)
39
if text.utf8.allSatisfy({ $0 >= 32 && $0 <= 126 }) {
35
- let columns = max(1, Int(width / characterWidth))
36
- starts = Array(stride(from: 0, to: max(1, text.utf8.count), by: columns))
40
+ let length = max(1, text.utf8.count)
41
+ let columns = max(1, Int(min(width / characterWidth, CGFloat(length))))
42
+ starts = Array(stride(from: 0, to: length, by: columns))
43
firstLineHeight = font.lineHeight
44
extraHeight = CGFloat(starts.count - 1) * lineHeight
45
+ usedWidth = CGFloat(min(text.utf8.count, columns)) * characterWidth
46
usesNativeLayout = false
47
} else {
48
let layout = Self.measurer
@@ -46,7 +53,9 @@ final class ReviewDiffCodeLayout: NSObject {
53
starts = [0]
54
firstLineHeight = manager.numberOfGlyphs > 0
55
? manager.lineFragmentRect(forGlyphAt: 0, effectiveRange: nil).height : font.lineHeight
49
- extraHeight = max(0, manager.usedRect(for: container).height - firstLineHeight)
56
+ let used = manager.usedRect(for: container)
57
+ extraHeight = max(0, used.height - firstLineHeight)
58
+ usedWidth = ceil(used.width)
59
}
60
}
61
apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffView.swift
+28
-22
@@ -1120,6 +1120,14 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
1120
ceil(codeFont.lineHeight)
1121
}
1122
1123
+ /// Wrapped rows fill the code viewport; unwrapped rows keep one line and scroll horizontally.
1124
+ private func codeLayoutWidthLimit(font: UIFont) -> CGFloat? {
1125
+ guard style.wordWrap else { return ReviewDiffCodeLayout.unboundedWidth }
1126
+ let width = viewportWidth - codeStartX - style.codePadding
1127
+ let characterWidth = monospaceCharacterWidth(font: font)
1128
+ return characterWidth > 0 && width >= characterWidth ? width : nil
1129
+ }
1130
+
1131
func frameForRow(at index: Int) -> CGRect? {
1132
guard rows.indices.contains(index), rowOffsets.indices.contains(index) else {
1133
return nil
@@ -1141,9 +1149,8 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
1149
completion: @escaping () -> Void
1150
) {
1151
let font = codeFont
1144
- let width = viewportWidth - codeStartX - style.codePadding
1152
let characterWidth = monospaceCharacterWidth(font: font)
1146
- guard style.wordWrap, width >= characterWidth, characterWidth > 0 else {
1153
+ guard let width = codeLayoutWidthLimit(font: font) else {
1154
completion()
1155
return
1156
}
@@ -1160,7 +1167,7 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
1167
}
1168
DispatchQueue.main.async { [weak self] in
1169
guard let self, isCurrent() else { return }
1163
- if self.codeFont != font || self.viewportWidth - self.codeStartX - self.style.codePadding != width {
1170
+ if self.codeFont != font || self.codeLayoutWidthLimit(font: font) != width {
1171
self.prepareRows(rows, on: queue, isCurrent: isCurrent, completion: completion)
1172
return
1173
}
@@ -1176,18 +1183,16 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
1183
var nextOffsets: [CGFloat] = []
1184
var nextFileHeaderRowIndices: [Int] = []
1185
nextOffsets.reserveCapacity(rows.count)
1179
- var maxColumnCountsByFileId: [String: Int] = [:]
1186
+ var maxWidthsByFileId: [String: CGFloat] = [:]
1187
var nextCodeLayouts: [String: ReviewDiffCodeLayout] = [:]
1188
var offset: CGFloat = 0
1189
let font = codeFont
1190
let characterWidth = monospaceCharacterWidth(font: font)
1184
- let wrapAvailableWidth = viewportWidth - codeStartX - style.codePadding
1185
- let wrapColumns = style.wordWrap && characterWidth > 0 && wrapAvailableWidth >= characterWidth
1186
- ? Int(wrapAvailableWidth / characterWidth)
1187
- : nil
1188
- if codeLayoutWidth != wrapAvailableWidth || codeLayoutFont != font {
1191
+ let layoutWidth = codeLayoutWidthLimit(font: font)
1192
+ let wrapColumns = style.wordWrap ? layoutWidth.map { Int($0 / characterWidth) } : nil
1193
+ if let layoutWidth, codeLayoutWidth != layoutWidth || codeLayoutFont != font {
1194
codeLayoutsByRowId.removeAll()
1190
- codeLayoutWidth = wrapAvailableWidth
1195
+ codeLayoutWidth = layoutWidth
1196
codeLayoutFont = font
1197
}
1198
@@ -1202,27 +1207,30 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
1207
switch row.kind {
1208
case "line":
1209
// UTF-16 columns match the word diff ranges and the segments drawCodeLines draws.
1205
- let columnCount = row.content?.utf16.count ?? 0
1206
- maxColumnCountsByFileId[fileId] = max(maxColumnCountsByFileId[fileId] ?? 0, columnCount)
1207
- if wrapColumns != nil, let content = row.content {
1210
+ var width = CGFloat(row.content?.utf16.count ?? 0) * characterWidth
1211
+ if let layoutWidth, let content = row.content {
1212
let cached = codeLayoutsByRowId[row.id]
1213
let layout: ReviewDiffCodeLayout
1214
if let cached, cached.text == content {
1215
layout = cached
1216
} else {
1217
layout = ReviewDiffCodeLayout(
1214
- text: content, font: font, width: wrapAvailableWidth, characterWidth: characterWidth
1218
+ text: content, font: font, width: layoutWidth, characterWidth: characterWidth
1219
)
1220
}
1221
nextCodeLayouts[row.id] = layout
1222
+ // Tabs and fallback glyphs are wider than one column.
1223
+ width = layout.usedWidth
1224
if rowHeight > 0 {
1225
rowHeight = max(rowHeight, layout.firstLineHeight) + layout.extraHeight
1226
}
1227
}
1228
+ maxWidthsByFileId[fileId] = max(maxWidthsByFileId[fileId] ?? 0, width)
1229
case "hunk":
1223
- maxColumnCountsByFileId[fileId] = max(
1224
- maxColumnCountsByFileId[fileId] ?? 0,
1225
- row.text?.utf16.count ?? 0
1230
+ // Hunk context can hold tabs and draws in its own font, so measure what drawHunkRow draws.
1231
+ maxWidthsByFileId[fileId] = max(
1232
+ maxWidthsByFileId[fileId] ?? 0,
1233
+ textWidth(row.text ?? "", font: hunkFont)
1234
)
1235
default:
1236
break
@@ -1233,10 +1241,8 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
1241
codeCharacterWidth = characterWidth
1242
codeWrapColumns = wrapColumns
1243
codeLayoutsByRowId = nextCodeLayouts
1236
- contentWidthsByFileId = maxColumnCountsByFileId.mapValues { maxColumnCount in
1237
- let measuredWidth = ceil(CGFloat(maxColumnCount) * characterWidth) + style.codePadding * 2
1238
- return max(0, min(style.contentWidth, measuredWidth))
1239
- }
1244
+ // Measured widths are not capped, so the end of every line stays reachable by scrolling.
1245
+ contentWidthsByFileId = maxWidthsByFileId.mapValues { ceil($0) + style.codePadding * 2 }
1246
rowOffsets = nextOffsets
1247
fileHeaderRowIndices = nextFileHeaderRowIndices
1248
contentHeight = offset
@@ -2375,7 +2381,7 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
2381
version: codeDecorationVersion
2382
)
2383
layout.draw(
2378
- at: CGPoint(x: codeStartX, y: rect.minY + max(0, (firstLineRect.height - layout.firstLineHeight) / 2)),
2384
+ at: CGPoint(x: codeStartX - horizontalOffset, y: rect.minY + max(0, (firstLineRect.height - layout.firstLineHeight) / 2)),
2385
clip: context.boundingBoxOfClipPath
2386
)
2387
context.restoreGState()
apps/mobile/modules/t3-review-diff/tests/ios/main.swift
+34
-1
@@ -46,6 +46,39 @@ for fixture in fixtures {
46
cases += 1
47
}
48
}
49
+// Unwrapped rows stay on one line, and their measured width covers every drawn glyph.
50
+for text in ["\t\t\"extends\": \"@riptech/tsconfig/tsconfig.test.json\",", "漢字\tvalue", "مرحبا\tvalue"] {
51
+ let layout = ReviewDiffCodeLayout(
52
+ text: text, font: font, width: ReviewDiffCodeLayout.unboundedWidth, characterWidth: characterWidth
53
+ )
54
+ check(layout.usesNativeLayout && layout.extraHeight == 0, "Unwrapped text must stay on one line: \(text)")
55
+ check(layout.usedWidth > CGFloat(text.utf16.count - 1) * characterWidth, "Tabs must widen the row: \(text)")
56
+ layout.decorate(text: NSAttributedString(string: text, attributes: [.foregroundColor: UIColor.black]), highlights: [], color: .clear, version: 0)
57
+ let format = UIGraphicsImageRendererFormat()
58
+ format.scale = 1
59
+ format.opaque = false
60
+ format.preferredRange = .standard
61
+ let size = CGSize(width: layout.usedWidth + 40, height: ceil(layout.firstLineHeight))
62
+ let image = UIGraphicsImageRenderer(size: size, format: format).image { _ in
63
+ layout.draw(at: .zero, clip: CGRect(origin: .zero, size: size))
64
+ }
65
+ let bitmap = image.cgImage!
66
+ let bytes = CFDataGetBytePtr(bitmap.dataProvider!.data!)!
67
+ var inkMaxX = -1
68
+ for y in 0..<bitmap.height {
69
+ for x in 0..<bitmap.width where bytes[y * bitmap.bytesPerRow + x * 4 + 3] != 0 {
70
+ inkMaxX = max(inkMaxX, x)
71
+ }
72
+ }
73
+ check(inkMaxX <= Int(layout.usedWidth) + 1, "Ink beyond measured width: \(text) x=\(inkMaxX)")
74
+ check(CGFloat(inkMaxX) >= layout.usedWidth - characterWidth * 2, "Row end was not drawn: \(text) x=\(inkMaxX)")
75
+ cases += 1
76
+}
77
+let unwrappedAscii = ReviewDiffCodeLayout(
78
+ text: "const value = 123;", font: font, width: ReviewDiffCodeLayout.unboundedWidth, characterWidth: characterWidth
79
+)
80
+check(unwrappedAscii.starts == [0] && unwrappedAscii.usedWidth == 18 * characterWidth)
81
+
82
let ascii = String(repeating: "const value = 123; ", count: 100)
83
let layout = ReviewDiffCodeLayout(text: ascii, font: font, width: 280, characterWidth: characterWidth)
84
let string = ascii as NSString
@@ -57,4 +90,4 @@ for (index, start) in layout.starts.enumerated() {
90
reconstructed += segment
91
}
92
check(reconstructed == ascii)
60
-print("Passed \(cases) Unicode/width cases, color-update geometry, and ASCII coverage")
93
+print("Passed \(cases) Unicode/width and unwrapped cases, color-update geometry, and ASCII coverage")