Add generateOriginalAndDiff method and test class#164
Add generateOriginalAndDiff method and test class#164wumpz merged 4 commits intojava-diff-utils:masterfrom
Conversation
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import java.util.*; |
There was a problem hiding this comment.
It is not recommended to use wildcard imports, because they can make code less readable, and potentially import unnecessary modules that can slow down performance.
|
|
||
|
|
|
|
||
|
|
||
| //Insert the diff format to the original file | ||
| private static List<String> insertOrig(List<String> original, List<String> unifiedDiff) { |
There was a problem hiding this comment.
Refactor the method to use more descriptive variable names. For example, instead of using "d", you can use "diff".
| unifiedDiff.set(1, unifiedDiff.get(1)); | ||
| unifiedDiff.add(2, "@@ -0,0 +0,0 @@"); | ||
| } | ||
| List<String> original1 = original.stream().map(v -> " " + v).collect(Collectors.toList()); |
There was a problem hiding this comment.
You can use more meaningful name. For example, use originalWithPrefix instead of original1.
| revisedFileName = revisedFileName == null ? "revised" : revisedFileName; | ||
| Patch<String> patch = com.github.difflib.DiffUtils.diff(original, revised); | ||
| List<String> unifiedDiff = generateUnifiedDiff(originalFileName, revisedFileName, original, patch, 0); | ||
| if (unifiedDiff.size() == 0) { |
There was a problem hiding this comment.
You can use unifiedDiff.isEmpty() instead of unifiedDiff.size() == 0.
|
|
||
|
|
||
| //Insert the diff format to the original file | ||
| private static List<String> insertOrig(List<String> original, List<String> unifiedDiff) { |
There was a problem hiding this comment.
The method is very long, it could be broken into multiple small methods to increase readability of the code.
…nto short methods, changed ambiguous variable names to explicit variable names
|
Now, I have modified the related question you mentioned above |
|
Correct the building problems. |
|
Build issues have been fixed. |
|
Thx for your contribution. |
When deleting the first line of comparison text and adding several lines of text, the first diff of the result returned by the generateOriginalAndDiff method is inserted incorrectly
* Add generateOriginalAndDiff method and test class * Add generateOriginalAndDiff method and test class.I split long code into short methods, changed ambiguous variable names to explicit variable names * fixes #164 * fix build issues * fix issues about (#164),detail: When deleting the first line of comparison text and adding several lines of text, the first diff of the result returned by the generateOriginalAndDiff method is inserted incorrectly * add a test for #170 --------- Co-authored-by: xutao <xutao@apexsoft.com.cn>
The generateOriginalAndDiff method is added to compare the original file with the reference file, get the diff, and insert the diff into the corresponding location of the original file.
You can see all the differences and unmodified places from the original file.
In addition, it is very easy and useful for making side-by-side comparison display applications,
For example, if you use something like diff2html( https://github.com/rtfpessoa/diff2html#usage )
Such a tool displays your differences on the html page. You only need to insert the return value of the method into your js code, and you can get a beautiful html comparison page.