Skip to content

Commit a76dfe0

Browse files
committed
fix(diff): pair complete change blocks in document comparisons
1 parent 4037b96 commit a76dfe0

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

‎apps/sim/components/diff/diff-view.tsx‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,21 @@ interface HunkRowsProps {
8686
wrapLines: boolean
8787
}
8888

89-
/** Pairs adjacent removed and added runs for bounded word-level highlighting. */
89+
/** Pairs removed and added lines within each change block for bounded word-level highlighting. */
9090
function wordSegments(lines: DiffLine[]): Map<DiffLine, Segment[]> {
9191
const segments = new Map<DiffLine, Segment[]>()
9292
for (let index = 0; index < lines.length; ) {
93-
if (lines[index].type !== 'del') {
93+
if (lines[index].type === 'context') {
9494
index++
9595
continue
9696
}
9797
const dels: DiffLine[] = []
98-
while (lines[index]?.type === 'del') dels.push(lines[index++])
9998
const adds: DiffLine[] = []
100-
while (lines[index]?.type === 'add') adds.push(lines[index++])
99+
while (index < lines.length && lines[index].type !== 'context') {
100+
const line = lines[index++]
101+
if (line.type === 'del') dels.push(line)
102+
else adds.push(line)
103+
}
101104
for (let pair = 0; pair < Math.min(dels.length, adds.length); pair++) {
102105
if (dels[pair].text.length + adds[pair].text.length > WORD_DIFF_MAX_CHARS) continue
103106
const parts = diffWordsWithSpace(dels[pair].text, adds[pair].text)
@@ -242,14 +245,9 @@ function pairedItems(items: VisibleItem[]): PairedItem[] {
242245
const key = item.key
243246
while (index < items.length) {
244247
const next = items[index]
245-
if (!('line' in next) || next.line.type !== 'del') break
246-
removed.push(next.line)
247-
index++
248-
}
249-
while (index < items.length) {
250-
const next = items[index]
251-
if (!('line' in next) || next.line.type !== 'add') break
252-
added.push(next.line)
248+
if (!('line' in next) || next.line.type === 'context') break
249+
if (next.line.type === 'del') removed.push(next.line)
250+
else added.push(next.line)
253251
index++
254252
}
255253
for (let pair = 0; pair < Math.max(removed.length, added.length); pair++) {

0 commit comments

Comments
 (0)