Skip to content

Commit fa46e6d

Browse files
authored
Merge pull request #75 from bridgecrewio/master
support line offset
2 parents e03a81a + 37e4418 commit fa46e6d

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<p align="center">
23
<img src='https://i.ibb.co/DKrGhVQ/Frame-1-1.png' width="100%" alt='React Diff Viewer' />
34
</p>
@@ -78,6 +79,7 @@ class Diff extends PureComponent {
7879
| useDarkTheme | `boolean` | `true` | To enable/disable dark theme. |
7980
| leftTitle | `string` | `undefined` | Column title for left section of the diff in split view. This will be used as the only title in inline view. |
8081
| rightTitle | `string` | `undefined` | Column title for right section of the diff in split view. This will be ignored in inline view. |
82+
| linesOffset | `number` | `0` | Number to start count code lines from. |
8183

8284
## Instance Methods
8385

src/compute-lines.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ const computeDiff = (
133133
* @param newString New string to compare with old string.
134134
* @param disableWordDiff Flag to enable/disable word diff.
135135
* @param compareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
136+
* @param linesOffset line number to start counting from
136137
*/
137138
const computeLineInformation = (
138139
oldString: string,
139140
newString: string,
140141
disableWordDiff: boolean = false,
141142
compareMethod: string = DiffMethod.CHARS,
143+
linesOffset: number = 0,
142144
): ComputedLineInformation => {
143145
const diffArray = diff.diffLines(
144146
oldString.trimRight(),
@@ -149,8 +151,8 @@ const computeLineInformation = (
149151
ignoreCase: false,
150152
},
151153
);
152-
let rightLineNumber = 0;
153-
let leftLineNumber = 0;
154+
let rightLineNumber = linesOffset;
155+
let leftLineNumber = linesOffset;
154156
let lineInformation: LineInformation[] = [];
155157
let counter = 0;
156158
const diffLines: number[] = [];

src/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface ReactDiffViewerProps {
2828
newValue: string;
2929
// Enable/Disable split view.
3030
splitView?: boolean;
31+
// Set line Offset
32+
linesOffset?: number;
3133
// Enable/Disable word diff.
3234
disableWordDiff?: boolean;
3335
// JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
@@ -83,6 +85,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
8385
extraLinesSurroundingDiff: 3,
8486
showDiffOnly: true,
8587
useDarkTheme: false,
88+
linesOffset: 0,
8689
};
8790

8891
public static propTypes = {
@@ -106,6 +109,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
106109
PropTypes.string,
107110
PropTypes.element,
108111
]),
112+
linesOffset: PropTypes.number,
109113
};
110114

111115
public constructor(props: ReactDiffViewerProps) {
@@ -441,12 +445,13 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
441445
* Generates the entire diff view.
442446
*/
443447
private renderDiff = (): JSX.Element[] => {
444-
const { oldValue, newValue, splitView, disableWordDiff, compareMethod } = this.props;
448+
const { oldValue, newValue, splitView, disableWordDiff, compareMethod, linesOffset } = this.props;
445449
const { lineInformation, diffLines } = computeLineInformation(
446450
oldValue,
447451
newValue,
448452
disableWordDiff,
449453
compareMethod,
454+
linesOffset,
450455
);
451456
const extraLines = this.props.extraLinesSurroundingDiff < 0
452457
? 0

test/compute-lines-test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,40 @@ Also this info`;
374374
],
375375
});
376376
});
377+
378+
it('Should start line counting from offset', (): void => {
379+
const oldCode = 'Hello World';
380+
const newCode = `My Updated Name
381+
Also this info`;
382+
383+
expect(computeLineInformation(oldCode, newCode, true, DiffMethod.WORDS, 5))
384+
.toMatchObject({
385+
lineInformation: [
386+
{
387+
right: {
388+
lineNumber: 6,
389+
type: 1,
390+
value: 'My Updated Name',
391+
},
392+
left: {
393+
lineNumber: 6,
394+
type: 2,
395+
value: 'Hello World',
396+
},
397+
},
398+
{
399+
right: {
400+
lineNumber: 7,
401+
type: 1,
402+
value: 'Also this info',
403+
},
404+
left: {},
405+
},
406+
],
407+
diffLines: [
408+
0,
409+
2,
410+
],
411+
});
412+
});
377413
});

0 commit comments

Comments
 (0)