-
-
Notifications
You must be signed in to change notification settings - Fork 313
Add char diff option #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,9 +64,9 @@ const constructLines = (value: string): string[] => { | |
| * @param oldValue Old word in the line. | ||
| * @param newValue New word in the line. | ||
| */ | ||
| const computeWordDiff = (oldValue: string, newValue: string): WordDiffInformation => { | ||
| const computeDiff = (oldValue: string, newValue: string, useCharDiff: boolean): WordDiffInformation => { | ||
| const diffArray = diff | ||
| .diffChars(oldValue, newValue); | ||
| [useCharDiff ? 'diffChars' : 'diffWords'](oldValue, newValue); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be replaced with the following for better readability. const diffArray = useCharDiff ? diff.diffChars(oldValue, newValue) : diff.diffChars(oldValue, newValue) |
||
| const wordDiff: WordDiffInformation = { | ||
| left: [], | ||
| right: [], | ||
|
|
@@ -106,11 +106,13 @@ const computeWordDiff = (oldValue: string, newValue: string): WordDiffInformatio | |
| * @param oldString Old string to compare. | ||
| * @param newString New string to compare with old string. | ||
| * @param disableWordDiff Flag to enable/disable word diff. | ||
| * @param useCharDiff Flag to use. | ||
| */ | ||
| const computeLineInformation = ( | ||
| oldString: string, | ||
| newString: string, | ||
| disableWordDiff: boolean = false, | ||
| useCharDiff: boolean = true, | ||
| ): ComputedLineInformation => { | ||
| const diffArray = diff.diffLines( | ||
| oldString.trimRight(), | ||
|
|
@@ -172,9 +174,9 @@ const computeLineInformation = ( | |
| if (disableWordDiff) { | ||
| right.value = rightValue; | ||
| } else { | ||
| const wordDiff = computeWordDiff(line, rightValue as string); | ||
| right.value = wordDiff.right; | ||
| left.value = wordDiff.left; | ||
| const computedDiff = computeDiff(line, rightValue as string, useCharDiff); | ||
| right.value = computedDiff.right; | ||
| left.value = computedDiff.left; | ||
| } | ||
| } | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,8 @@ export interface ReactDiffViewerProps { | |
| highlightLines?: string[]; | ||
| // Style overrides. | ||
| styles?: ReactDiffViewerStylesOverride; | ||
| // Use char or word diff, true by default. | ||
| useCharDiff?: boolean; | ||
| } | ||
|
|
||
| export interface ReactDiffViewerState { | ||
|
|
@@ -72,6 +74,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt | |
| hideLineNumbers: false, | ||
| extraLinesSurroundingDiff: 3, | ||
| showDiffOnly: true, | ||
| useCharDiff: true, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value should be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the moment |
||
| }; | ||
|
|
||
| public static propTypes = { | ||
|
|
@@ -425,6 +428,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt | |
| oldValue, | ||
| newValue, | ||
| this.props.disableWordDiff, | ||
| this.props.useCharDiff, | ||
| ); | ||
| const extraLines = this.props.extraLinesSurroundingDiff < 0 | ||
| ? 0 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useCharDiffsounds confusing. What will happen whendisableWordDiff: trueanduseCharDiff: true?I would prefer to have one prop to control this. Say,
diffTypeand it can take 3 possible values.diffType: line|lineAndWord|lineAndCharline: It will be the default behaviour. It shows the line-wise diff.lineAndWord: It will show line and word-wise diff.lineAndChar: It will show line and char-wise diff.These three values can also be an enum. I prefer this over the previous method. For example,
Suggestions are welcome
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer this approach too.
But I'm now somewhat confused about the defaults. Right now
disableWordDiffis false by default. Which means that the diff is computed by usingdiff.diffCharsby default. If I enabledisableWordDiff— then neitherdiff.diffCharsnordiff.diffWordsis used, and the whole line is diffed. Which kind of implies that we need to enum this instead:Is my assumption correct?