diff --git a/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java b/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java index e926560d..727008db 100644 --- a/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java +++ b/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java @@ -420,11 +420,11 @@ private static void insertOrig(List> diffList, List result, int end = nexMap.get("revRow") - 2; insert(result, getOrigList(original, start, end)); } + int start = map.get("orgRow") + map.get("orgDel") - 1; + start = start == -1 ? 0 : start; if (simb.contains("@@ -1,") && null == nexSimb && map.get("orgDel") != original.size()) { - insert(result, getOrigList(original, 0, original.size() - 1)); + insert(result, getOrigList(original, start, original.size() - 1)); } else if (null == nexSimb && (map.get("orgRow") + map.get("orgDel") - 1) < original.size()) { - int start = map.get("orgRow") + map.get("orgDel") - 1; - start = start == -1 ? 0 : start; insert(result, getOrigList(original, start, original.size() - 1)); } } diff --git a/java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java b/java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java index 7f2d3b29..17283b4d 100644 --- a/java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java +++ b/java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java @@ -31,6 +31,21 @@ public void testGenerateOriginalAndDiff() { System.out.println(originalAndDiff.stream().collect(joining("\n"))); } + @Test + public void testGenerateOriginalAndDiffFirstLineChange() { + List origLines = null; + List revLines = null; + try { + origLines = fileToLines(TestConstants.MOCK_FOLDER + "issue_170_original.txt"); + revLines = fileToLines(TestConstants.MOCK_FOLDER + "issue_170_revised.txt"); + } catch (IOException e) { + fail(e.getMessage()); + } + + List originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines); + System.out.println(originalAndDiff.stream().collect(joining("\n"))); + } + public static List fileToLines(String filename) throws FileNotFoundException, IOException { List lines = new ArrayList<>(); String line = ""; diff --git a/java-diff-utils/src/test/resources/mocks/issue_170_original.txt b/java-diff-utils/src/test/resources/mocks/issue_170_original.txt new file mode 100644 index 00000000..8aa703f0 --- /dev/null +++ b/java-diff-utils/src/test/resources/mocks/issue_170_original.txt @@ -0,0 +1,95 @@ +//According to the original text, an html will be generated by comparing the text +public class generateDiffHtmlTest { + /** + * Here's a simple example of getting a nice html page based on the original text and the contrasted text,Read n1.txt and n2.txt of D disk, and finally generate an html file + * + */ + @Test + public static void generateOriginalAndDiffDemo(){ + List origLines = getFileContent("D:\\n1.txt"); + List revLines =getFileContent("D:\\n2.txt"); + List originalAndDiff =UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines); + //System.out.println(originalAndDiff.size()); + generateDiffHtml(originalAndDiff,"D:\\diff.html"); + } + + /** + * get file content + * @param filePath file path + */ + public static List getFileContent(String filePath) { + //origin + List fileContent =null; + File file = new File(filePath); + try { + fileContent = Files.readAllLines(file.toPath()); + } catch (IOException e) { + e.printStackTrace(); + } + return fileContent; + } + + /** + * The html file is generated by the difference diff between the two files, and the detailed content of the file comparison can be seen by opening the html file + * + */ + public static void generateDiffHtml(List diffString, String htmlPath) { + StringBuilder builder = new StringBuilder(); + for (String line : diffString) { + builder.append(line); + builder.append("\n"); + } + String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css"; + String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css"; + String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"; + + String template = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + ""; + template = template.replace("temp", builder.toString()); + FileWriter fileWriter = null; + try { + fileWriter = new FileWriter(htmlPath); + BufferedWriter buf = new BufferedWriter(fileWriter); + buf.write(template); + buf.close(); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/java-diff-utils/src/test/resources/mocks/issue_170_revised.txt b/java-diff-utils/src/test/resources/mocks/issue_170_revised.txt new file mode 100644 index 00000000..7aa91dc2 --- /dev/null +++ b/java-diff-utils/src/test/resources/mocks/issue_170_revised.txt @@ -0,0 +1,114 @@ +package com.github.difflib.examples; + +import com.github.difflib.UnifiedDiffUtils; +import org.junit.jupiter.api.Test; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; + +// According to the original text, an html will be generated by comparing the text. +public class generateDiffHtmlTest { + + /** + * Here's a simple example of getting a nice html page based on the original text and the contrasted text, + * Read n1.txt and n2.txt of D disk, and finally generate an html file + * + */ + @Test + public static void generateOriginalAndDiffDemo(){ + List origLines = getFileContent("D:\\n1.txt"); + List revLines = getFileContent("D:\\n2.txt"); + List originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines); + + //generateDiffHtml + generateDiffHtml(originalAndDiff,"D:\\diff.html"); + } + + /** + * get file content + * + * @param filePath file path + */ + public static List getFileContent(String filePath) { + //原始文件 + List fileContent = null; + File file = new File(filePath); + try { + fileContent = Files.readAllLines(file.toPath()); + } catch (IOException e) { + e.printStackTrace(); + } + return fileContent; + } + + /** + * The html file is generated by the difference diff between the two files, + * and the detailed content of the file comparison can be seen by opening the html file + * + * @param diffString The comparison result obtained by calling the above diffString method + * @param htmlPath Generated html path,e.g:/user/var/mbos/ent/21231/diff.html + */ + public static void generateDiffHtml(List diffString, String htmlPath) { + StringBuilder builder = new StringBuilder(); + for (String line : diffString) { + builder.append(line); + builder.append("\n"); + } + String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css"; + String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css"; + String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"; + + String template = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + ""; + template = template.replace("temp", builder.toString()); + FileWriter fileWriter = null; + try { + fileWriter = new FileWriter(htmlPath); + BufferedWriter buf = new BufferedWriter(fileWriter); + buf.write(template); + buf.close(); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} +