← 返回首页
Add generateOriginalAndDiff method and test class by 1506085843 · Pull Request #164 · java-diff-utils/java-diff-utils · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Add generateOriginalAndDiff method and test class#164

Merged
wumpz merged 4 commits into
java-diff-utils:masterfrom
1506085843:master
Apr 27, 2023
Merged

Add generateOriginalAndDiff method and test class#164
wumpz merged 4 commits into
java-diff-utils:masterfrom
1506085843:master

Conversation

Copy link
Copy Markdown
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.

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

import java.util.*;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide 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.

Comment on lines +37 to +38


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Remove extra lines.



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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide 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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide 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

Copy link
Copy Markdown
Contributor Author

Now, I have modified the related question you mentioned above

wumpz commented Mar 21, 2023

Copy link
Copy Markdown
Collaborator

Correct the building problems.

Copy link
Copy Markdown
Contributor Author

Build issues have been fixed.

wumpz merged commit 1664490 into java-diff-utils:master Apr 27, 2023

wumpz commented Apr 27, 2023

Copy link
Copy Markdown
Collaborator

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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Footer

© 2026 GitHub, Inc.