mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
fix(mobile): keep unchanged lines in diffs of large files
Files past the LCS cell budget fell back to prefix/suffix diffing, so two edits at the ends of a 450-line file showed every line as rewritten. Use jsdiff's Myers diff bounded by edit count for those files, keeping the prefix/suffix fallback for mass rewrites.
This commit is contained in:
@@ -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",
|
||||
|
||||
Generated
+9
@@ -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:
|
||||
|
||||
@@ -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 }
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -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<string>[],
|
||||
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[],
|
||||
|
||||
Reference in New Issue
Block a user