Skip to content
TypeScript

fix(mobile): Android review diff scrolls to the end of long lines (#19431)

shivamcaa8c601acdb
diff --git a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt
index 80d0410643f..67579a1cad8 100644
--- a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt
+++ b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt
@@ -10,6 +10,7 @@ import android.graphics.RectF
import android.graphics.Shader
import android.graphics.Typeface
import android.text.TextPaint
+import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min
@@ -211,6 +212,68 @@ internal class ReviewDiffCanvasDrawing(context: Context) {
return codeLayouts.layout(rows, tokens, textPaint, style, theme, availableWidth.toInt())
}
+ private data class WidthEntry(val row: DiffRow, val tokens: List<DiffToken>?, val width: Float)
+ private var widthEntries = emptyMap<String, WidthEntry>()
+ private var widthRows: List<DiffRow>? = null
+ private var widthTokens: Map<String, List<DiffToken>>? = null
+ private var widthStyle: DiffStyle? = null
+ private var measuredContentWidth = 0
+
+ fun contentWidth(
+ rows: List<DiffRow>,
+ tokens: Map<String, List<DiffToken>>,
+ style: DiffStyle
+ ): Int {
+ if (style.wordWrap) {
+ widthEntries = emptyMap()
+ widthRows = null
+ widthTokens = null
+ widthStyle = null
+ return 0
+ }
+ // Comment heights and viewport resizing do not change unwrapped text widths.
+ if (widthRows !== rows || widthTokens !== tokens || widthStyle != style) {
+ if (widthStyle != style) widthEntries = emptyMap()
+ val next = HashMap<String, WidthEntry>()
+ var widest = 0f
+ for (row in rows) {
+ if (row.kind != "line" && row.kind != "hunk") continue
+ val rowTokens = if (row.kind == "line") tokens[row.id] else null
+ val cached = widthEntries[row.id]
+ val entry = if (cached?.row == row && cached.tokens == rowTokens) {
+ cached
+ } else {
+ WidthEntry(row, rowTokens, measureRowWidth(row, rowTokens, style))
+ }
+ next[row.id] = entry
+ widest = max(widest, entry.width)
+ }
+ widthEntries = next
+ widthRows = rows
+ widthTokens = tokens
+ widthStyle = style
+ measuredContentWidth = ceil(
+ style.changeBarWidthPx + style.gutterWidthPx + style.codePaddingPx * 2f + widest,
+ ).toInt()
+ }
+ return measuredContentWidth
+ }
+
+ private fun measureRowWidth(row: DiffRow, tokens: List<DiffToken>?, style: DiffStyle): Float {
+ if (row.kind == "hunk") {
+ configureMonospacePaint(theme.hunkText, style.hunkFontSizePx, style.hunkFontWeight)
+ return textPaint.measureText(row.text.ifEmpty { row.content })
+ }
+ val runs = if (tokens.isNullOrEmpty()) listOf(DiffToken(row.content, null, 0)) else tokens
+ var width = 0f
+ // Match drawCode's run boundaries and fonts, including fallback glyphs and tabs.
+ for (run in runs) {
+ configureCodePaint(run.color ?: theme.text, run.fontStyle, style)
+ width += textPaint.measureText(run.content)
+ }
+ return width
+ }
+
fun lineNumberColor(change: String): Int = when (change) {
"add" -> theme.addText
"delete" -> theme.deleteText
diff --git a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt
index 58422149016..ee6740cf34c 100644
--- a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt
+++ b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt
@@ -38,9 +38,8 @@ class T3ReviewDiffModule : Module() {
Prop("rowHeight") { view: T3ReviewDiffView, rowHeight: Double ->
view.setRowHeight(rowHeight.toFloat())
}
- Prop("contentWidth") { view: T3ReviewDiffView, contentWidth: Double ->
- view.setContentWidth(contentWidth.toFloat())
- }
+ // Accept the shared prop while deriving Android's scroll range from the drawn text.
+ Prop("contentWidth") { _: T3ReviewDiffView, _: Double -> }
Prop("contentInsetBottom") { view: T3ReviewDiffView, value: Double ->
view.setContentInsetBottom(value.toFloat())
}
diff --git a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt
index 495a63719a7..e39fdeb9634 100644
--- a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt
+++ b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt
@@ -139,10 +139,6 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
canvasView.style = canvasView.style.copy(rowHeightPx = dp(value))
}
- fun setContentWidth(value: Float) {
- canvasView.contentWidthPx = max(width, dp(value).toInt())
- }
-
fun setContentInsetBottom(value: Float) {
canvasView.setPadding(0, 0, 0, max(0, dp(value).toInt()))
canvasView.setVerticalOffset(canvasView.verticalOffset())
@@ -718,6 +714,8 @@ private class DiffCanvasView(context: Context) : View(context) {
private var codeWrap = CodeWrapLayout.NONE
private var verticalOffset = 0
private var horizontalOffset = 0
+
+ private var contentWidthPx = 0
private val headerPathOffsetsByFileId = mutableMapOf<String, Int>()
private var lastVisibleRange: Pair<Int, Int>? = null
var rows: List<DiffRow> = emptyList()
@@ -731,7 +729,12 @@ private class DiffCanvasView(context: Context) : View(context) {
var tokensByRowId: Map<String, List<DiffToken>> = emptyMap()
set(value) {
field = value
- if (style.wordWrap) rebuildOffsets()
+ if (style.wordWrap) {
+ rebuildOffsets()
+ } else {
+ contentWidthPx = drawing.contentWidth(rows, value, style)
+ setHorizontalOffset(horizontalOffset)
+ }
invalidate()
}
var viewedFileIds: Set<String> = emptySet()
@@ -766,13 +769,6 @@ private class DiffCanvasView(context: Context) : View(context) {
field = value
rebuildOffsets()
}
- var contentWidthPx: Int = (1200 * density).toInt()
- set(value) {
- field = max(value, suggestedMinimumWidth)
- setHorizontalOffset(horizontalOffset)
- clampHeaderPathOffsets()
- invalidate()
- }
var onRowTap: ((DiffRow, String, RowTapTarget) -> Unit)? = null
var onVisibleRowsChanged: ((Int, Int) -> Unit)? = null
var onContentHeightChanged: ((Int) -> Unit)? = null
@@ -894,6 +890,7 @@ private class DiffCanvasView(context: Context) : View(context) {
}
private fun layoutRows() {
+ contentWidthPx = drawing.contentWidth(rows, tokensByRowId, style)
codeWrap = drawing.codeWrapLayout(rows, tokensByRowId, style, width)
rowOffsets = IntArray(rows.size + 1)
rows.forEachIndexed { index, row ->
diff --git a/apps/mobile/modules/t3-review-diff/android/src/test/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayoutTest.kt b/apps/mobile/modules/t3-review-diff/android/src/test/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayoutTest.kt
index 1f4234b7175..7c4f531fbec 100644
--- a/apps/mobile/modules/t3-review-diff/android/src/test/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayoutTest.kt
+++ b/apps/mobile/modules/t3-review-diff/android/src/test/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayoutTest.kt
@@ -14,6 +14,7 @@ import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
+import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
@@ -141,6 +142,57 @@ class ReviewDiffCodeLayoutTest {
assertEquals(21, text.getSpanEnd(span))
}
+ @Test
+ fun cachedContentWidthMatchesFreshMeasurementAfterUpdatesAndRemoval() {
+ val context = RuntimeEnvironment.getApplication()
+ val drawing = ReviewDiffCanvasDrawing(context)
+ val style = DiffStyle.defaults(1f)
+ fun assertFresh(
+ rows: List<DiffRow>,
+ tokens: Map<String, List<DiffToken>> = emptyMap(),
+ currentStyle: DiffStyle = style
+ ) {
+ assertEquals(
+ ReviewDiffCanvasDrawing(context).contentWidth(rows, tokens, currentStyle),
+ drawing.contentWidth(rows, tokens, currentStyle),
+ )
+ }
+
+ val line = row("مرحبا\t漢字 e\u0301 ".repeat(20))
+ val rows = listOf(line)
+ val tokens = mapOf(
+ line.id to listOf(
+ DiffToken("مرحبا", Color.RED, 2),
+ DiffToken(line.content.drop(5), Color.BLUE, 1),
+ ),
+ )
+ assertFresh(rows)
+ assertFresh(rows, tokens)
+ assertFresh(rows, tokens)
+ assertFresh(
+ rows,
+ tokens,
+ style.copy(
+ codeFontSizePx = 24f,
+ codeFontWeight = "bold",
+ gutterWidthPx = 90f,
+ codePaddingPx = 16f,
+ changeBarWidthPx = 5f,
+ ),
+ )
+
+ val hunk = line.copy(kind = "hunk", id = "hunk", text = "@@ context @@ ".repeat(100))
+ val hunkRows = listOf(line, hunk)
+ assertFresh(hunkRows, tokens)
+ assertFresh(hunkRows, tokens, style.copy(hunkFontSizePx = 22f, hunkFontWeight = "bold"))
+ val shortHunk = hunk.copy(text = "@@ short @@")
+ assertFresh(listOf(line, shortHunk), tokens)
+ assertFresh(listOf(shortHunk), tokens)
+ assertFresh(rows, tokens, style.copy(wordWrap = true))
+ assertFresh(rows, tokens)
+ assertFresh(rows)
+ }
+
private fun row(content: String) = DiffRow(
kind = "line", id = "line", fileId = "file", filePath = "test.ts", previousPath = null,
changeType = "modified", additions = 1, deletions = 0, text = "", content = content,
diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts
index 21d7d372740..9d51c72272b 100644
--- a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts
+++ b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts
@@ -122,6 +122,7 @@ export interface NativeReviewDiffViewProps extends ViewProps {
readonly themeJson: string;
readonly styleJson?: string;
readonly rowHeight: number;
+ /** Android ignores this and measures the widest row. */
readonly contentWidth: number;
readonly initialRowIndex?: number;
readonly scrollEnabled?: boolean;