Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Provide feedback

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

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16cd9c6

Browse files
committed
1 parenta34c185 commit16cd9c6

File tree

5 files changed

+168
-33
lines changed

5 files changed

+168
-33
lines changed

‎src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void initFileIfNecessary() {
132132
}
133133
}
134134

135-
publicvoidprocessDiff(MatchResultmatch,Stringline) {
135+
privatevoidprocessDiff(MatchResultmatch,Stringline) {
136136
initFileIfNecessary();
137137
LOG.log(Level.INFO,"start {0}",line);
138138
String[]fromTo =parseFileNames(READER.lastLine());
@@ -158,23 +158,23 @@ private void finalizeChunk() {
158158
}
159159
}
160160

161-
publicvoidprocessNormalLine(MatchResultmatch,Stringline) {
161+
privatevoidprocessNormalLine(MatchResultmatch,Stringline) {
162162
Stringcline =line.substring(1);
163163
originalTxt.add(cline);
164164
revisedTxt.add(cline);
165165
}
166166

167-
publicvoidprocessAddLine(MatchResultmatch,Stringline) {
167+
privatevoidprocessAddLine(MatchResultmatch,Stringline) {
168168
Stringcline =line.substring(1);
169169
revisedTxt.add(cline);
170170
}
171171

172-
publicvoidprocessDelLine(MatchResultmatch,Stringline) {
172+
privatevoidprocessDelLine(MatchResultmatch,Stringline) {
173173
Stringcline =line.substring(1);
174174
originalTxt.add(cline);
175175
}
176176

177-
publicvoidprocessChunk(MatchResultmatch,StringchunkStart) {
177+
privatevoidprocessChunk(MatchResultmatch,StringchunkStart) {
178178
finalizeChunk();
179179
old_ln =match.group(1) ==null ?1 :Integer.parseInt(match.group(1));
180180
new_ln =match.group(3) ==null ?1 :Integer.parseInt(match.group(3));
@@ -186,7 +186,7 @@ public void processChunk(MatchResult match, String chunkStart) {
186186
}
187187
}
188188

189-
publicvoidprocessIndex(MatchResultmatch,Stringline) {
189+
privatevoidprocessIndex(MatchResultmatch,Stringline) {
190190
initFileIfNecessary();
191191
LOG.log(Level.INFO,"index {0}",line);
192192
actualFile.setIndex(line.substring(6));

‎src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,29 @@
2121
importjava.util.ArrayList;
2222
importjava.util.List;
2323
importjava.util.function.Consumer;
24+
importjava.util.function.Function;
2425
importjava.util.logging.Level;
2526
importjava.util.logging.Logger;
2627

2728
/**
28-
*
29+
* @todo use an instance to store contextSize and originalLinesProvider.
2930
* @author Tobias Warneke (t.warneke@gmx.net)
3031
*/
3132
publicclassUnifiedDiffWriter {
3233

3334
privatestaticfinalLoggerLOG =Logger.getLogger(UnifiedDiffWriter.class.getName());
3435

35-
publicstaticvoidwrite(UnifiedDiffdiff,Writerwriter)throwsIOException {
36-
write(diff,line -> {
36+
publicstaticvoidwrite(UnifiedDiffdiff,Function<String,List<String>>originalLinesProvider,Writerwriter,intcontextSize)throwsIOException {
37+
write(diff,originalLinesProvider,line -> {
3738
try {
3839
writer.append(line).append("\n");
3940
}catch (IOExceptionex) {
4041
LOG.log(Level.SEVERE,null,ex);
4142
}
42-
});
43+
},contextSize);
4344
}
4445

45-
publicstaticvoidwrite(UnifiedDiffdiff,Consumer<String>writer)throwsIOException {
46+
publicstaticvoidwrite(UnifiedDiffdiff,Function<String,List<String>>originalLinesProvider,Consumer<String>writer,intcontextSize)throwsIOException {
4647
writer.accept(diff.getHeader());
4748

4849
for (UnifiedDiffFilefile :diff.getFiles()) {
@@ -57,14 +58,13 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
5758
writer.accept("+++ " +file.getToFile());
5859
}
5960

61+
List<String>originalLines =originalLinesProvider.apply(file.getFromFile());
62+
6063
List<AbstractDelta<String>>patchDeltas =newArrayList<>(
6164
file.getPatch().getDeltas());
6265

6366
List<AbstractDelta<String>>deltas =newArrayList<>();
6467

65-
intcontextSize =0;
66-
List<String>originalLines =newArrayList<>();
67-
6868
AbstractDelta<String>delta =patchDeltas.get(0);
6969
deltas.add(delta);// add the first Delta to the current set
7070
// if there's more than 1 Delta, we may need to output them together
@@ -83,7 +83,7 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
8383
// if it isn't, output the current set,
8484
// then create a new set and add the current Delta to
8585
// it.
86-
processDeltas(writer,deltas);
86+
processDeltas(writer,originalLines,deltas,contextSize);
8787
deltas.clear();
8888
deltas.add(nextDelta);
8989
}
@@ -92,7 +92,7 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
9292

9393
}
9494
// don't forget to process the last set of Deltas
95-
processDeltas(writer,deltas);
95+
processDeltas(writer,originalLines,deltas,contextSize);
9696

9797
}
9898
if (diff.getTail() !=null) {
@@ -102,7 +102,8 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
102102
}
103103

104104
privatestaticvoidprocessDeltas(Consumer<String>writer,
105-
List<AbstractDelta<String>>deltas) {
105+
List<String>origLines,List<AbstractDelta<String>>deltas,
106+
intcontextSize) {
106107
List<String>buffer =newArrayList<>();
107108
intorigTotal =0;// counter for total lines output from Original
108109
intrevTotal =0;// counter for total lines output from Original
@@ -143,13 +144,13 @@ private static void processDeltas(Consumer<String> writer,
143144
AbstractDelta<String>nextDelta =deltas.get(deltaIndex);
144145
intintermediateStart =curDelta.getSource().getPosition()
145146
+curDelta.getSource().getLines().size();
146-
// for (line = intermediateStart; line < nextDelta.getSource()
147-
// .getPosition(); line++) {
148-
// // output the code between the last Delta and this one
149-
// buffer.add(" " + origLines.get(line));
150-
// origTotal++;
151-
// revTotal++;
152-
// }
147+
for (line =intermediateStart;line <nextDelta.getSource()
148+
.getPosition();line++) {
149+
// output the code between the last Delta and this one
150+
buffer.add(" " +origLines.get(line));
151+
origTotal++;
152+
revTotal++;
153+
}
153154
getDeltaText(txt ->buffer.add(txt),nextDelta);// output the Delta
154155
origTotal +=nextDelta.getSource().getLines().size();
155156
revTotal +=nextDelta.getTarget().getLines().size();
@@ -160,12 +161,12 @@ private static void processDeltas(Consumer<String> writer,
160161
// Now output the post-Delta context code, clamping the end of the file
161162
contextStart =curDelta.getSource().getPosition()
162163
+curDelta.getSource().getLines().size();
163-
// for (line = contextStart; (line < (contextStart + contextSize))
164-
// && (line < origLines.size()); line++) {
165-
// buffer.add(" " + origLines.get(line));
166-
// origTotal++;
167-
// revTotal++;
168-
// }
164+
for (line =contextStart; (line < (contextStart +contextSize))
165+
&& (line <origLines.size());line++) {
166+
buffer.add(" " +origLines.get(line));
167+
origTotal++;
168+
revTotal++;
169+
}
169170

170171
// Create and insert the block header, conforming to the Unified Diff
171172
// standard

‎src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
publicclassGenerateUnifiedDiffTest {
1818

19-
privatestaticList<String>fileToLines(Stringfilename)throwsFileNotFoundException,IOException {
19+
publicstaticList<String>fileToLines(Stringfilename)throwsFileNotFoundException,IOException {
2020
List<String>lines =newArrayList<>();
2121
Stringline ="";
2222
try (BufferedReaderin =newBufferedReader(newFileReader(filename))) {
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
packagecom.github.difflib.unifieddiff;
2+
3+
importcom.github.difflib.DiffUtils;
4+
importcom.github.difflib.algorithm.DiffException;
5+
importcom.github.difflib.patch.Patch;
6+
importjava.io.BufferedReader;
7+
importjava.io.FileNotFoundException;
8+
importjava.io.FileReader;
9+
importjava.io.IOException;
10+
importjava.io.StringWriter;
11+
importjava.util.ArrayList;
12+
importjava.util.Arrays;
13+
importjava.util.List;
14+
importorg.junit.Test;
15+
16+
publicclassUnifiedDiffRoundTripTest {
17+
18+
publicstaticList<String>fileToLines(Stringfilename)throwsFileNotFoundException,IOException {
19+
List<String>lines =newArrayList<>();
20+
Stringline ="";
21+
try (BufferedReaderin =newBufferedReader(newFileReader(filename))) {
22+
while ((line =in.readLine()) !=null) {
23+
lines.add(line);
24+
}
25+
}
26+
returnlines;
27+
}
28+
29+
// @Test
30+
// public void testGenerateUnified() throws DiffException, IOException {
31+
// List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
32+
// List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
33+
//
34+
// verify(origLines, revLines, "original.txt", "revised.txt");
35+
// }
36+
//
37+
// @Test
38+
// public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
39+
// List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_original.txt");
40+
// List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_revised.txt");
41+
//
42+
// verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
43+
// }
44+
@Test
45+
publicvoidtestGenerateUnifiedDiffWithoutAnyDeltas()throwsDiffException,IOException {
46+
List<String>test =Arrays.asList("abc");
47+
Patch<String>patch =DiffUtils.diff(test,test);
48+
StringWriterwriter =newStringWriter();
49+
50+
UnifiedDiffWriter.write(
51+
UnifiedDiff.from("header","tail",UnifiedDiffFile.from("abc","abc",patch)),
52+
name ->test,
53+
writer,0);
54+
55+
System.out.println(writer);
56+
}
57+
58+
// @Test
59+
// public void testDiff_Issue10() throws IOException {
60+
// final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
61+
// final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
62+
// final Patch<String> p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
63+
// try {
64+
// DiffUtils.patch(baseLines, p);
65+
// } catch (PatchFailedException e) {
66+
// fail(e.getMessage());
67+
// }
68+
// }
69+
//
70+
// /**
71+
// * Issue 12
72+
// */
73+
// @Test
74+
// public void testPatchWithNoDeltas() throws DiffException, IOException {
75+
// final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_1.txt");
76+
// final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_2.txt");
77+
// verify(lines1, lines2, "issue11_1.txt", "issue11_2.txt");
78+
// }
79+
//
80+
// @Test
81+
// public void testDiff5() throws DiffException, IOException {
82+
// final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "5A.txt");
83+
// final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "5B.txt");
84+
// verify(lines1, lines2, "5A.txt", "5B.txt");
85+
// }
86+
//
87+
// /**
88+
// * Issue 19
89+
// */
90+
// @Test
91+
// public void testDiffWithHeaderLineInText() throws DiffException {
92+
// List<String> original = new ArrayList<>();
93+
// List<String> revised = new ArrayList<>();
94+
//
95+
// original.add("test line1");
96+
// original.add("test line2");
97+
// original.add("test line 4");
98+
// original.add("test line 5");
99+
//
100+
// revised.add("test line1");
101+
// revised.add("test line2");
102+
// revised.add("@@ -2,6 +2,7 @@");
103+
// revised.add("test line 4");
104+
// revised.add("test line 5");
105+
//
106+
// Patch<String> patch = DiffUtils.diff(original, revised);
107+
// List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff("original", "revised",
108+
// original, patch, 10);
109+
// UnifiedDiffUtils.parseUnifiedDiff(udiff);
110+
// }
111+
//
112+
// private void verify(List<String> origLines, List<String> revLines,
113+
// String originalFile, String revisedFile) throws DiffException {
114+
// Patch<String> patch = DiffUtils.diff(origLines, revLines);
115+
// List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
116+
// origLines, patch, 10);
117+
//
118+
// Patch<String> fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
119+
// List<String> patchedLines;
120+
// try {
121+
// patchedLines = fromUnifiedPatch.applyTo(origLines);
122+
// assertEquals(revLines.size(), patchedLines.size());
123+
// for (int i = 0; i < revLines.size(); i++) {
124+
// String l1 = revLines.get(i);
125+
// String l2 = patchedLines.get(i);
126+
// if (!l1.equals(l2)) {
127+
// fail("Line " + (i + 1) + " of the patched file did not match the revised original");
128+
// }
129+
// }
130+
// } catch (PatchFailedException e) {
131+
// fail(e.getMessage());
132+
// }
133+
// }
134+
}

‎src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public void testWrite() throws URISyntaxException, IOException {
4040
UnifiedDiffdiff =UnifiedDiffReader.parseUnifiedDiff(newByteArrayInputStream(str.getBytes()));
4141

4242
StringWriterwriter =newStringWriter();
43-
UnifiedDiffWriter.write(diff,writer);
44-
System.out.println(writer.toString());
43+
// UnifiedDiffWriter.write(diff, writer);
44+
// System.out.println(writer.toString());
4545
}
4646

4747
staticStringreadFile(URIpath,Charsetencoding)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp