diff --git a/mobile/package.json b/mobile/package.json index 7fbcd30f8b7..9e9d0097727 100644 --- a/mobile/package.json +++ b/mobile/package.json @@ -33,6 +33,7 @@ "@xterm/addon-webgl": "0.20.0-beta.329", "@xterm/xterm": "6.1.0-beta.336", "buffer": "^6.0.3", + "diff": "8.0.4", "expo": "^55.0.31", "expo-build-properties": "^55.0.18", "expo-camera": "^55.0.23", diff --git a/mobile/pnpm-lock.yaml b/mobile/pnpm-lock.yaml index fffdc5f0a6a..b4a2078a022 100644 --- a/mobile/pnpm-lock.yaml +++ b/mobile/pnpm-lock.yaml @@ -221,6 +221,9 @@ importers: buffer: specifier: ^6.0.3 version: 6.0.3 + diff: + specifier: 8.0.4 + version: 8.0.4 expo: specifier: ^55.0.31 version: 55.0.31(0def556739f9e1a9e497ee4bc3b81b41) @@ -4089,6 +4092,10 @@ packages: resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} + diff@8.0.4: + resolution: {integrity: sha512-DPi0FmjiSU5EvQV0++GFDOJ9ASQUVFh5kD+OzOnYdi7n3Wpm9hWWGfB/O2blfHcMVTL5WkQXSnRiK9makhrcnw==} + engines: {node: '>=0.3.1'} + dnssd-advertise@1.1.6: resolution: {integrity: sha512-Ndrrf6BMPalkQPd/zubL+4YghH2J9NspapQ09uDXwYbvOPkP0oaqf5CkcwJ0b50kS2O3ul6yVu+jz+RY62Cejg==} @@ -11728,6 +11735,8 @@ snapshots: diff-sequences@29.6.3: {} + diff@8.0.4: {} + dnssd-advertise@1.1.6: {} doctrine@2.1.0: diff --git a/mobile/src/source-control/review/mobile-diff-lines.test.ts b/mobile/src/source-control/review/mobile-diff-lines.test.ts new file mode 100644 index 00000000000..4f1514b71bf --- /dev/null +++ b/mobile/src/source-control/review/mobile-diff-lines.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from 'vitest' +import { buildMobileDiffLines } from './mobile-diff-lines' + +describe('buildMobileDiffLines', () => { + it('shows only the edited lines of a file too large for the LCS table', () => { + const original = Array.from({ length: 450 }, (_, index) => `line-${index + 1}`) + const modified = ['changed-first', ...original.slice(1, -1), 'changed-last'] + + const { lines, truncated } = buildMobileDiffLines( + `${original.join('\n')}\n`, + `${modified.join('\n')}\n` + ) + + expect(truncated).toBe(false) + expect(lines).toHaveLength(452) + expect(lines.slice(0, 3)).toEqual([ + { kind: 'delete', text: 'line-1', oldLineNumber: 1 }, + { kind: 'add', text: 'changed-first', newLineNumber: 1 }, + { kind: 'context', text: 'line-2', oldLineNumber: 2, newLineNumber: 2 } + ]) + expect(lines.slice(-2)).toEqual([ + { kind: 'delete', text: 'line-450', oldLineNumber: 450 }, + { kind: 'add', text: 'changed-last', newLineNumber: 450 } + ]) + }) +}) diff --git a/mobile/src/source-control/review/mobile-diff-lines.ts b/mobile/src/source-control/review/mobile-diff-lines.ts index d3652de8b96..aa6c0b27c9e 100644 --- a/mobile/src/source-control/review/mobile-diff-lines.ts +++ b/mobile/src/source-control/review/mobile-diff-lines.ts @@ -1,3 +1,5 @@ +import { diffArrays, type ArrayChange } from 'diff' + export type MobileDiffLineKind = 'context' | 'add' | 'delete' export type MobileDiffLine = { @@ -8,6 +10,9 @@ export type MobileDiffLine = { } const MAX_DIFF_CELLS = 200_000 +// Why: Myers cost grows with the edit count; past this a mass rewrite falls back +// to prefix/suffix, at about the cost of a full-size LCS table on Hermes. +const MAX_EDIT_LENGTH = 250 const MAX_MOBILE_DIFF_LINES = 2_500 const MAX_BUILT_DIFF_LINES = MAX_MOBILE_DIFF_LINES + 1 const TRUNCATED_LINE: MobileDiffLine = { @@ -29,12 +34,12 @@ export function buildMobileDiffLines( const modifiedKeys = options?.ignoreTrimWhitespace ? modifiedLines.map((line) => line.trim()) : modifiedLines - // Why: the LCS table is quadratic. Large generated files still need a - // responsive mobile preview, so fall back to prefix/suffix diffing. + // Why: the LCS table is quadratic, so large files use a Myers diff bounded by + // edit count, which keeps small edits in big files exact. const lines = originalLines.length * modifiedLines.length <= MAX_DIFF_CELLS ? buildLcsDiffLines(originalLines, modifiedLines, originalKeys, modifiedKeys) - : buildPrefixSuffixDiffLines(originalLines, modifiedLines, originalKeys, modifiedKeys) + : buildBoundedMyersDiffLines(originalLines, modifiedLines, originalKeys, modifiedKeys) return finalizeMobileDiffLines(lines) } @@ -141,6 +146,61 @@ function buildLcsDiffLines( return lines } +function buildBoundedMyersDiffLines( + originalLines: string[], + modifiedLines: string[], + originalKeys: string[], + modifiedKeys: string[] +): MobileDiffLine[] { + const changes = diffArrays(originalKeys, modifiedKeys, { maxEditLength: MAX_EDIT_LENGTH }) + return changes + ? buildChangeDiffLines(changes, originalLines, modifiedLines) + : buildPrefixSuffixDiffLines(originalLines, modifiedLines, originalKeys, modifiedKeys) +} + +function buildChangeDiffLines( + changes: ArrayChange[], + originalLines: string[], + modifiedLines: string[] +): MobileDiffLine[] { + const lines: MobileDiffLine[] = [] + let originalIndex = 0 + let modifiedIndex = 0 + for (const change of changes) { + for (let i = 0; i < change.count; i += 1) { + let line: MobileDiffLine + if (change.removed) { + line = { + kind: 'delete', + text: originalLines[originalIndex] ?? '', + oldLineNumber: originalIndex + 1 + } + originalIndex += 1 + } else if (change.added) { + line = { + kind: 'add', + text: modifiedLines[modifiedIndex] ?? '', + newLineNumber: modifiedIndex + 1 + } + modifiedIndex += 1 + } else { + line = { + kind: 'context', + text: modifiedLines[modifiedIndex] ?? '', + oldLineNumber: originalIndex + 1, + newLineNumber: modifiedIndex + 1 + } + originalIndex += 1 + modifiedIndex += 1 + } + if (!appendDiffLine(lines, line)) { + return lines + } + } + } + return lines +} + function buildPrefixSuffixDiffLines( originalLines: string[], modifiedLines: string[],