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

Commit1107f43

Browse files
committed
fixes#11
1 parent6c91ef6 commit1107f43

File tree

2 files changed

+87
-32
lines changed

2 files changed

+87
-32
lines changed

‎src/main/java/com/github/difflib/text/DiffRowGenerator.java‎

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@
3535
importjava.util.regex.Pattern;
3636

3737
/**
38-
* This class for generating DiffRows for side-by-sidy view. You can customize the way of
39-
* generating. For example, show inline diffs on not, ignoring white spaces or/and blank lines and
40-
* so on. All parameters for generating are optional. If you do not specify them, the class will use
41-
* the default values.
38+
* This class for generating DiffRows for side-by-sidy view. You can customize the way of generating. For example, show
39+
* inline diffs on not, ignoring white spaces or/and blank lines and so on. All parameters for generating are optional.
40+
* If you do not specify them, the class will use the default values.
4241
*
4342
* These values are: showInlineDiffs = false; ignoreWhiteSpaces = true; ignoreBlankLines = true; ...
4443
*
@@ -49,9 +48,9 @@
4948
*/
5049
publicclassDiffRowGenerator {
5150

52-
publicstaticfinalBiPredicate<String,String>IGNORE_WHITESPACE_EQUALIZER = (original,revised)
51+
publicstaticfinalBiPredicate<String,String>IGNORE_WHITESPACE_EQUALIZER = (original,revised)
5352
->original.trim().replaceAll("\\s+"," ").equals(revised.trim().replaceAll("\\s+"," "));
54-
publicstaticfinalBiPredicate<String,String>DEFAULT_EQUALIZER =Object::equals;
53+
publicstaticfinalBiPredicate<String,String>DEFAULT_EQUALIZER =Object::equals;
5554
privatestaticfinalPatternSPLIT_PATTERN =Pattern.compile("\\s+|[,.\\[\\](){}/\\\\*+\\-#]");
5655
privatefinalbooleanshowInlineDiffs;
5756
privatefinalbooleanignoreWhiteSpaces;
@@ -61,6 +60,7 @@ public class DiffRowGenerator {
6160
privatefinalintcolumnWidth;
6261
privatefinalBiPredicate<String,String>equalizer;
6362
privatefinalbooleanmergeOriginalRevised;
63+
privatefinalbooleanreportLinesUnchanged;
6464

6565
/**
6666
* This class used for building the DiffRowGenerator.
@@ -79,6 +79,7 @@ public static class Builder {
7979
privateintcolumnWidth =80;
8080
privatebooleanmergeOriginalRevised =false;
8181
privatebooleaninlineDiffByWord =false;
82+
privatebooleanreportLinesUnchanged =false;
8283

8384
privateBuilder() {
8485
}
@@ -105,6 +106,17 @@ public Builder ignoreWhiteSpaces(boolean val) {
105106
returnthis;
106107
}
107108

109+
/**
110+
* Give the originial old and new text lines to Diffrow without any additional processing.
111+
*
112+
* @param val the value to set. Default: false.
113+
* @return builder with configured reportLinesUnWrapped parameter
114+
*/
115+
publicBuilderreportLinesUnchanged(finalbooleanval) {
116+
reportLinesUnchanged =val;
117+
returnthis;
118+
}
119+
108120
/**
109121
* Generator for Old-Text-Tags.
110122
*
@@ -130,8 +142,8 @@ public Builder newTag(Function<Boolean, String> generator) {
130142
/**
131143
* Set the column with of generated lines of original and revised texts.
132144
*
133-
* @param width the width to set. Making it < 0 doesn't have any sense. Default 80. @return
134-
*builder with configured ignoreBlankLines parameter
145+
* @param width the width to set. Making it < 0 doesn't have any sense. Default 80. @return builder with config
146+
* ured ignoreBlankLines parameter
135147
*/
136148
publicBuildercolumnWidth(intwidth) {
137149
if (width >0) {
@@ -150,8 +162,7 @@ public DiffRowGenerator build() {
150162
}
151163

152164
/**
153-
* Merge the complete result within the original text. This makes sense for one line
154-
* display.
165+
* Merge the complete result within the original text. This makes sense for one line display.
155166
*
156167
* @param mergeOriginalRevised
157168
* @return
@@ -162,8 +173,8 @@ public Builder mergeOriginalRevised(boolean mergeOriginalRevised) {
162173
}
163174

164175
/**
165-
* Per default each character is separatly processed. This variant introduces processing by
166-
*word, which shoulddeliver no in word changes.
176+
* Per default each character is separatly processed. This variant introduces processing by word, which should
177+
* deliver no in word changes.
167178
*/
168179
publicBuilderinlineDiffByWord(booleaninlineDiffByWord) {
169180
this.inlineDiffByWord =inlineDiffByWord;
@@ -174,7 +185,7 @@ public Builder inlineDiffByWord(boolean inlineDiffByWord) {
174185
publicstaticBuildercreate() {
175186
returnnewBuilder();
176187
}
177-
188+
178189
privateDiffRowGenerator(Builderbuilder) {
179190
showInlineDiffs =builder.showInlineDiffs;
180191
ignoreWhiteSpaces =builder.ignoreWhiteSpaces;
@@ -183,12 +194,13 @@ private DiffRowGenerator(Builder builder) {
183194
columnWidth =builder.columnWidth;
184195
mergeOriginalRevised =builder.mergeOriginalRevised;
185196
inlineDiffByWord =builder.inlineDiffByWord;
186-
equalizer =ignoreWhiteSpaces?IGNORE_WHITESPACE_EQUALIZER:DEFAULT_EQUALIZER;
197+
equalizer =ignoreWhiteSpaces ?IGNORE_WHITESPACE_EQUALIZER :DEFAULT_EQUALIZER;
198+
reportLinesUnchanged =builder.reportLinesUnchanged;
187199
}
188200

189201
/**
190-
* Get the DiffRows describing the difference between original and revised texts using the given
191-
*patch. Useful fordisplaying side-by-side diff.
202+
* Get the DiffRows describing the difference between original and revised texts using the given patch. Useful for
203+
* displaying side-by-side diff.
192204
*
193205
* @param original the original text
194206
* @param revised the revised text
@@ -198,22 +210,34 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
198210
returngenerateDiffRows(original,DiffUtils.diff(original,revised,equalizer));
199211
}
200212

213+
privateStringpreprocessLine(Stringline) {
214+
if (columnWidth ==0) {
215+
returnStringUtils.normalize(line);
216+
}else {
217+
returnStringUtils.wrapText(StringUtils.normalize(line),columnWidth);
218+
}
219+
}
220+
201221
privateDiffRowbuildDiffRow(Tagtype,Stringorgline,Stringnewline) {
202-
StringwrapOrg =StringUtils.wrapText(StringUtils.normalize(orgline),columnWidth);
203-
if (Tag.DELETE ==type) {
204-
if (mergeOriginalRevised ||showInlineDiffs) {
205-
wrapOrg =oldTag.apply(true) +wrapOrg +oldTag.apply(false);
222+
if (reportLinesUnchanged) {
223+
returnnewDiffRow(type,orgline,newline);
224+
}else {
225+
StringwrapOrg =preprocessLine(orgline);
226+
if (Tag.DELETE ==type) {
227+
if (mergeOriginalRevised ||showInlineDiffs) {
228+
wrapOrg =oldTag.apply(true) +wrapOrg +oldTag.apply(false);
229+
}
206230
}
207-
}
208-
StringwrapNew =StringUtils.wrapText(StringUtils.normalize(newline),columnWidth);
209-
if (Tag.INSERT ==type) {
210-
if (mergeOriginalRevised) {
211-
wrapOrg =newTag.apply(true) +wrapNew +newTag.apply(false);
212-
}elseif (showInlineDiffs) {
213-
wrapNew =newTag.apply(true) +wrapNew +newTag.apply(false);
231+
StringwrapNew =preprocessLine(newline);
232+
if (Tag.INSERT ==type) {
233+
if (mergeOriginalRevised) {
234+
wrapOrg =newTag.apply(true) +wrapNew +newTag.apply(false);
235+
}elseif (showInlineDiffs) {
236+
wrapNew =newTag.apply(true) +wrapNew +newTag.apply(false);
237+
}
214238
}
239+
returnnewDiffRow(type,wrapOrg,wrapNew);
215240
}
216-
returnnewDiffRow(type,wrapOrg,wrapNew);
217241
}
218242

219243
privateDiffRowbuildDiffRowWithoutNormalizing(Tagtype,Stringorgline,Stringnewline) {
@@ -223,8 +247,8 @@ private DiffRow buildDiffRowWithoutNormalizing(Tag type, String orgline, String
223247
}
224248

225249
/**
226-
* Generates the DiffRows describing the difference between original and revised texts using the
227-
*given patch. Usefulfor displaying side-by-side diff.
250+
* Generates the DiffRows describing the difference between original and revised texts using the given patch. Useful
251+
* for displaying side-by-side diff.
228252
*
229253
* @param original the original text
230254
* @param revised the revised text
@@ -367,8 +391,7 @@ private List<DiffRow> generateInlineDiffs(Delta<String> delta) throws DiffExcept
367391
/**
368392
* Wrap the elements in the sequence with the given tag
369393
*
370-
* @param startPosition the position from which tag should start. The counting start from a
371-
* zero.
394+
* @param startPosition the position from which tag should start. The counting start from a zero.
372395
* @param endPosition the position before which tag should should be closed.
373396
* @param tag the tag name without angle brackets, just a word
374397
* @param cssClass the optional css class

‎src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ public void testGenerator_Default() throws DiffException {
2424

2525
assertEquals(3,rows.size());
2626
}
27+
28+
@Test
29+
publicvoidtestGenerator_Default2()throwsDiffException {
30+
Stringfirst ="anything\n\nother";
31+
Stringsecond ="anything\n\nother";
32+
33+
DiffRowGeneratorgenerator =DiffRowGenerator.create()
34+
.columnWidth(0)// do not wrap
35+
.build();
36+
List<DiffRow>rows =generator.generateDiffRows(split(first),split(second));
37+
print(rows);
38+
39+
assertEquals(3,rows.size());
40+
}
2741

2842
@Test
2943
publicvoidtestGenerator_InlineDiff()throwsDiffException {
@@ -232,4 +246,22 @@ public void testGeneratorExample2() throws DiffException {
232246
assertEquals("This is a test ~senctence~.",rows.get(0).getOldLine());
233247
assertEquals("This is a test **for diffutils**.",rows.get(0).getNewLine());
234248
}
249+
250+
@Test
251+
publicvoidtestGeneratorUnchanged()throwsDiffException {
252+
Stringfirst ="anything\n\nother";
253+
Stringsecond ="anything\n\nother";
254+
255+
DiffRowGeneratorgenerator =DiffRowGenerator.create()
256+
.columnWidth(5)
257+
.reportLinesUnchanged(true)
258+
.build();
259+
List<DiffRow>rows =generator.generateDiffRows(split(first),split(second));
260+
print(rows);
261+
262+
assertEquals(3,rows.size());
263+
assertEquals("[CHANGE,anything ,anything]",rows.get(0).toString());
264+
assertEquals("[CHANGE, ,]",rows.get(1).toString());
265+
assertEquals("[EQUAL,other,other]",rows.get(2).toString());
266+
}
235267
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp