Skip to content

Commit 81f91c6

Browse files
committed
[Partial] Fixing line level diff bug.
1 parent fe11bb3 commit 81f91c6

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/compute-lines.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,21 @@ const computeLineInformation = (
126126
let lineInformation: LineInformation[] = [];
127127
let counter = 0;
128128
const diffLines: number[] = [];
129-
const ignoreDiffIndexes: number[] = [];
129+
const ignoreDiffIndexes: string[] = [];
130130
const getLineInformation = (
131131
value: string,
132132
diffIndex: number,
133133
added?: boolean,
134134
removed?: boolean,
135135
): LineInformation[] => {
136-
if (ignoreDiffIndexes.includes(diffIndex)) {
137-
return [];
138-
}
139136
const lines = constructLines(value);
140137

141-
return lines.map((line: string): LineInformation => {
138+
return lines.map((line: string, lineIndex): LineInformation => {
142139
const left: DiffInformation = {};
143140
const right: DiffInformation = {};
141+
if (ignoreDiffIndexes.includes(`${diffIndex}-${lineIndex}`)) {
142+
return { left: {}, right: {}};
143+
}
144144
if (added || removed) {
145145
if (!diffLines.includes(counter)) {
146146
diffLines.push(counter);
@@ -156,25 +156,28 @@ const computeLineInformation = (
156156
// current line is a modification.
157157
const nextDiff = diffArray[diffIndex + 1];
158158
if (nextDiff && nextDiff.added) {
159-
const {
160-
value: rightValue,
161-
lineNumber,
162-
type,
163-
} = getLineInformation(nextDiff.value, diffIndex, true)[0].right;
164-
// When identified as modification, push the next diff to ignore
165-
// list as the next value will be added in this line computation as
166-
// right and left values.
167-
ignoreDiffIndexes.push(diffIndex + 1);
168-
right.lineNumber = lineNumber;
169-
right.type = type;
170-
// Do word level diff and assign the corresponding values to the
171-
// left and right diff information object.
172-
if (disableWordDiff) {
173-
right.value = rightValue;
174-
} else {
175-
const wordDiff = computeWordDiff(line, rightValue as string);
176-
right.value = wordDiff.right;
177-
left.value = wordDiff.left;
159+
const nextDiffLines = constructLines(nextDiff.value)[lineIndex];
160+
if (nextDiffLines) {
161+
const {
162+
value: rightValue,
163+
lineNumber,
164+
type,
165+
} = getLineInformation(nextDiff.value, diffIndex, true)[lineIndex].right;
166+
// When identified as modification, push the next diff to ignore
167+
// list as the next value will be added in this line computation as
168+
// right and left values.
169+
ignoreDiffIndexes.push(`${diffIndex + 1}-${lineIndex}`);
170+
right.lineNumber = lineNumber;
171+
right.type = type;
172+
// Do word level diff and assign the corresponding values to the
173+
// left and right diff information object.
174+
if (disableWordDiff) {
175+
right.value = rightValue;
176+
} else {
177+
const wordDiff = computeWordDiff(line, rightValue as string);
178+
right.value = wordDiff.right;
179+
left.value = wordDiff.left;
180+
}
178181
}
179182
}
180183
} else {

0 commit comments

Comments
 (0)