Skip to content

Add generateOriginalAndDiff method and test class #164

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

Merged
merged 4 commits into from
Apr 27, 2023

Conversation

1506085843
Copy link
Contributor

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.

diff

Comment on lines 22 to 23
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import java.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 37 to 38


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra lines.



//Insert the diff format to the original file
private static List<String> insertOrig(List<String> original, List<String> unifiedDiff) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@1506085843
Copy link
Contributor Author

Now, I have modified the related question you mentioned above

@wumpz
Copy link
Collaborator

wumpz commented Mar 21, 2023

Correct the building problems.

@1506085843
Copy link
Contributor Author

Build issues have been fixed.

@wumpz wumpz merged commit 1664490 into java-diff-utils:master Apr 27, 2023
@wumpz
Copy link
Collaborator

wumpz commented Apr 27, 2023

Thx for your contribution.

1506085843 added a commit to 1506085843/java-diff-utils that referenced this pull request Jun 16, 2023
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
wumpz pushed a commit that referenced this pull request Aug 13, 2023
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants