Skip to content

Commit 3018f2e

Browse files
authored
Fixing line level diff bug. (#33)
Fixing line level diff bug.
2 parents fe11bb3 + ab00692 commit 3018f2e

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

src/compute-lines.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,23 @@ 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,
135+
evaluateOnlyFirstLine?: boolean,
135136
): LineInformation[] => {
136-
if (ignoreDiffIndexes.includes(diffIndex)) {
137-
return [];
138-
}
139137
const lines = constructLines(value);
140138

141-
return lines.map((line: string): LineInformation => {
139+
return lines.map((line: string, lineIndex): LineInformation => {
142140
const left: DiffInformation = {};
143141
const right: DiffInformation = {};
142+
if (ignoreDiffIndexes.includes(`${diffIndex}-${lineIndex}`)
143+
|| (evaluateOnlyFirstLine && lineIndex !== 0)) {
144+
return undefined;
145+
}
144146
if (added || removed) {
145147
if (!diffLines.includes(counter)) {
146148
diffLines.push(counter);
@@ -156,25 +158,28 @@ const computeLineInformation = (
156158
// current line is a modification.
157159
const nextDiff = diffArray[diffIndex + 1];
158160
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;
161+
const nextDiffLines = constructLines(nextDiff.value)[lineIndex];
162+
if (nextDiffLines) {
163+
const {
164+
value: rightValue,
165+
lineNumber,
166+
type,
167+
} = getLineInformation(nextDiff.value, diffIndex, true, false, true)[0].right;
168+
// When identified as modification, push the next diff to ignore
169+
// list as the next value will be added in this line computation as
170+
// right and left values.
171+
ignoreDiffIndexes.push(`${diffIndex + 1}-${lineIndex}`);
172+
right.lineNumber = lineNumber;
173+
right.type = type;
174+
// Do word level diff and assign the corresponding values to the
175+
// left and right diff information object.
176+
if (disableWordDiff) {
177+
right.value = rightValue;
178+
} else {
179+
const wordDiff = computeWordDiff(line, rightValue as string);
180+
right.value = wordDiff.right;
181+
left.value = wordDiff.left;
182+
}
178183
}
179184
}
180185
} else {
@@ -197,7 +202,7 @@ const computeLineInformation = (
197202

198203
counter += 1;
199204
return { right, left };
200-
});
205+
}).filter(Boolean);
201206
};
202207

203208
diffArray

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
462462
? this.renderSplitView(line, i)
463463
: this.renderInlineView(line, i);
464464

465-
if (currentPosition === extraLines && skippedLines.length > 1) {
465+
if (currentPosition === extraLines && skippedLines.length > 0) {
466466
const { length } = skippedLines;
467467
skippedLines = [];
468468
return (

test/react-diff-viewer-test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Testing react diff viewer', (): void => {
3939
newValue={newCode}
4040
/>);
4141

42-
expect(node.find('table > tbody tr').length).toEqual(6);
42+
expect(node.find('table > tbody tr').length).toEqual(7);
4343
});
4444

4545
it('It should render diff lines in inline view', (): void => {

0 commit comments

Comments
 (0)