Skip to content

Commit dcc151e

Browse files
committed
by word inline diff changes
1 parent ef97a99 commit dcc151e

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/main/java/difflib/text/DiffRowGenerator.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,18 @@ public DiffRowGenerator build() {
173173
/**
174174
* Merge the complete result within the original text. This makes sense for one line
175175
* display.
176+
*
176177
* @param mergeOriginalRevised
177-
* @return
178+
* @return
178179
*/
179180
public Builder mergeOriginalRevised(boolean mergeOriginalRevised) {
180181
this.mergeOriginalRevised = mergeOriginalRevised;
181182
return this;
182183
}
183-
184+
184185
/**
185-
* Per default each character is separatly processed. This variant introduces processing
186-
* by word, which should deliver no in word changes.
186+
* Per default each character is separatly processed. This variant introduces processing by
187+
* word, which should deliver no in word changes.
187188
*/
188189
public Builder inlineDiffByWord(boolean inlineDiffByWord) {
189190
this.inlineDiffByWord = inlineDiffByWord;
@@ -316,7 +317,7 @@ private List<DiffRow> generateInlineDiffs(Delta<String> delta) throws DiffExcept
316317
List<String> rev = StringUtils.normalize(delta.getRevised().getLines());
317318
List<String> origList;
318319
List<String> revList;
319-
320+
320321
if (inlineDiffByWord) {
321322
origList = splitStringPreserveDelimiter(String.join("\n", orig));
322323
revList = splitStringPreserveDelimiter(String.join("\n", rev));
@@ -330,7 +331,7 @@ private List<DiffRow> generateInlineDiffs(Delta<String> delta) throws DiffExcept
330331
revList.add(character.toString());
331332
}
332333
}
333-
334+
334335
List<Delta<String>> inlineDeltas = DiffUtils.diff(origList, revList).getDeltas();
335336

336337
Collections.reverse(inlineDeltas);
@@ -411,20 +412,24 @@ private static String createCloseTag(String tag) {
411412
private static String createOpenTag(String tag, String cssClass) {
412413
return "<" + tag + (cssClass != null ? " class=\"" + cssClass + "\"" : "") + ">";
413414
}
414-
415-
private static final Pattern SPLIT_PATTERN = Pattern.compile("\\s+");
416-
417-
private List<String> splitStringPreserveDelimiter(String str) {
415+
416+
private static final Pattern SPLIT_PATTERN = Pattern.compile("\\s+|[,.\\[\\](){}/\\\\*+\\-#]");
417+
418+
static List<String> splitStringPreserveDelimiter(String str) {
418419
List<String> list = new ArrayList<>();
419420
if (str != null) {
420421
Matcher matcher = SPLIT_PATTERN.matcher(str);
421422
int pos = 0;
422423
while (matcher.find()) {
423-
list.add(str.substring(pos, matcher.start()));
424+
if (pos < matcher.start()) {
425+
list.add(str.substring(pos, matcher.start()));
426+
}
424427
list.add(matcher.group());
425428
pos = matcher.end();
426429
}
427-
list.add(str.substring(pos));
430+
if (pos < str.length()) {
431+
list.add(str.substring(pos));
432+
}
428433
}
429434
return list;
430435
}

src/test/java/difflib/text/DiffRowGeneratorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,27 @@ public void testGeneratorWithMergeByWord5() throws DiffException {
166166
assertEquals(1, rows.size());
167167
assertEquals("[CHANGE,<span class=\"editOldInline\">Test</span><span class=\"editNewInline\">ester</span> <br/>feature<span class=\"editNewInline\"> best</span>,ester feature best]", rows.get(0).toString());
168168
}
169+
170+
@Test
171+
public void testSplitString() {
172+
List<String> list = DiffRowGenerator.splitStringPreserveDelimiter("test,test2");
173+
assertEquals(3, list.size());
174+
assertEquals("[test, ,, test2]", list.toString());
175+
}
176+
177+
@Test
178+
public void testSplitString2() {
179+
List<String> list = DiffRowGenerator.splitStringPreserveDelimiter("test , test2");
180+
System.out.println(list);
181+
assertEquals(5, list.size());
182+
assertEquals("[test, , ,, , test2]", list.toString());
183+
}
184+
185+
@Test
186+
public void testSplitString3() {
187+
List<String> list = DiffRowGenerator.splitStringPreserveDelimiter("test,test2,");
188+
System.out.println(list);
189+
assertEquals(4, list.size());
190+
assertEquals("[test, ,, test2, ,]", list.toString());
191+
}
169192
}

0 commit comments

Comments
 (0)