@@ -73,6 +73,9 @@ public static class Builder {
7373 private String inlineNewCssClass = "editNewInline" ;
7474 private int columnWidth = 80 ;
7575
76+ private Builder () {
77+ }
78+
7679 /**
7780 * Show inline diffs in generating diff rows or not.
7881 *
@@ -161,7 +164,7 @@ public DiffRowGenerator build() {
161164 return new DiffRowGenerator (this );
162165 }
163166 }
164-
167+
165168 public static Builder create () {
166169 return new Builder ();
167170 }
@@ -195,7 +198,13 @@ public boolean equals(String original, String revised) {
195198 * @return the DiffRows between original and revised texts
196199 */
197200 public List <DiffRow > generateDiffRows (List <String > original , List <String > revised ) throws DiffException {
198- return generateDiffRows (original , revised , DiffUtils .diff (original , revised , equalizer ));
201+ return generateDiffRows (original , DiffUtils .diff (original , revised , equalizer ));
202+ }
203+
204+ private DiffRow buildDiffRow (Tag type , String orgline , String newline ) {
205+ return new DiffRow (type ,
206+ StringUtils .wrapText (StringUtils .normalize (orgline ), columnWidth ),
207+ StringUtils .wrapText (StringUtils .normalize (newline ), columnWidth ));
199208 }
200209
201210 /**
@@ -207,15 +216,15 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
207216 * @param patch the given patch
208217 * @return the DiffRows between original and revised texts
209218 */
210- public List <DiffRow > generateDiffRows (final List <String > originalText , final List < String > revisedText , Patch <String > patch ) throws DiffException {
219+ public List <DiffRow > generateDiffRows (final List <String > original , Patch <String > patch ) throws DiffException {
211220 // normalize the lines (expand tabs, escape html entities)
212- List <String > original = StringUtils . normalize ( originalText ) ;
213- List <String > revised = StringUtils .normalize (revisedText );
221+ // List<String> original = originalText;
222+ // List<String> revised = StringUtils.normalize(revisedText);
214223
215224 // wrap to the column width
216- original = StringUtils . wrapText ( original , this . columnWidth );
217- revised = StringUtils .wrapText (revised , this .columnWidth );
218-
225+ // TODO: we want to process original text und not wrapped text
226+ //original = StringUtils.wrapText(original , this.columnWidth);
227+ //revised = StringUtils.wrapText(revised, this.columnWidth);
219228 List <DiffRow > diffRows = new ArrayList <>();
220229 int endPos = 0 ;
221230 final List <Delta <String >> deltaList = patch .getDeltas ();
@@ -225,22 +234,23 @@ public List<DiffRow> generateDiffRows(final List<String> originalText, final Lis
225234 Chunk <String > rev = delta .getRevised ();
226235
227236 // We should normalize and wrap lines in deltas too.
228- orig .setLines (StringUtils .normalize ((List <String >) orig .getLines ()));
229- rev .setLines (StringUtils .normalize ((List <String >) rev .getLines ()));
230-
231- orig .setLines (StringUtils .wrapText ((List <String >) orig .getLines (), this .columnWidth ));
232- rev .setLines (StringUtils .wrapText ((List <String >) rev .getLines (), this .columnWidth ));
237+ // TODO: not in Deltas
238+ //orig.setLines(StringUtils.normalize((List<String>) orig.getLines()));
239+ //rev.setLines(StringUtils.normalize((List<String>) rev.getLines()));
233240
241+ //TODO: no we shouldnt: Deltas should not be used for display purposes
242+ //orig.setLines(StringUtils.wrapText((List<String>) orig.getLines(), this.columnWidth));
243+ //rev.setLines(StringUtils.wrapText((List<String>) rev.getLines(), this.columnWidth));
234244 // catch the equal prefix for each chunk
235245 for (String line : original .subList (endPos , orig .getPosition ())) {
236- diffRows .add (new DiffRow (Tag .EQUAL , line , line ));
246+ diffRows .add (buildDiffRow (Tag .EQUAL , line , line ));
237247 }
238248
239249 // Inserted DiffRow
240250 if (delta instanceof InsertDelta ) {
241251 endPos = orig .last () + 1 ;
242252 for (String line : (List <String >) rev .getLines ()) {
243- diffRows .add (new DiffRow (Tag .INSERT , "" , line ));
253+ diffRows .add (buildDiffRow (Tag .INSERT , "" , line ));
244254 }
245255 continue ;
246256 }
@@ -249,88 +259,100 @@ public List<DiffRow> generateDiffRows(final List<String> originalText, final Lis
249259 if (delta instanceof DeleteDelta ) {
250260 endPos = orig .last () + 1 ;
251261 for (String line : (List <String >) orig .getLines ()) {
252- diffRows .add (new DiffRow (Tag .DELETE , line , "" ));
262+ diffRows .add (buildDiffRow (Tag .DELETE , line , "" ));
253263 }
254264 continue ;
255265 }
256266
257267 if (showInlineDiffs ) {
258- addInlineDiffs (delta );
259- }
260- // the changed size is match
261- if (orig .size () == rev .size ()) {
262- for (int j = 0 ; j < orig .size (); j ++) {
263- diffRows .add (new DiffRow (Tag .CHANGE , (String ) orig .getLines ().get (j ),
264- (String ) rev .getLines ().get (j )));
265- }
266- } else if (orig .size () > rev .size ()) {
267- for (int j = 0 ; j < orig .size (); j ++) {
268- diffRows .add (new DiffRow (Tag .CHANGE , (String ) orig .getLines ().get (j ), rev
269- .getLines ().size () > j ? (String ) rev .getLines ().get (j ) : "" ));
270- }
268+ diffRows .addAll (generateInlineDiffs (delta , false ));
271269 } else {
272- for (int j = 0 ; j < rev .size (); j ++) {
273- diffRows .add (new DiffRow (Tag .CHANGE , orig .getLines ().size () > j ? (String ) orig
274- .getLines ().get (j ) : "" , (String ) rev .getLines ().get (j )));
270+ if (orig .size () == rev .size ()) {
271+ for (int j = 0 ; j < orig .size (); j ++) {
272+ diffRows .add (buildDiffRow (Tag .CHANGE , (String ) orig .getLines ().get (j ),
273+ (String ) rev .getLines ().get (j )));
274+ }
275+ } else if (orig .size () > rev .size ()) {
276+ for (int j = 0 ; j < orig .size (); j ++) {
277+ diffRows .add (buildDiffRow (Tag .CHANGE , (String ) orig .getLines ().get (j ), rev
278+ .getLines ().size () > j ? (String ) rev .getLines ().get (j ) : "" ));
279+ }
280+ } else {
281+ for (int j = 0 ; j < rev .size (); j ++) {
282+ diffRows .add (buildDiffRow (Tag .CHANGE , orig .getLines ().size () > j ? (String ) orig
283+ .getLines ().get (j ) : "" , (String ) rev .getLines ().get (j )));
284+ }
275285 }
276286 }
277287 endPos = orig .last () + 1 ;
278288 }
279289
280290 // Copy the final matching chunk if any.
281291 for (String line : original .subList (endPos , original .size ())) {
282- diffRows .add (new DiffRow (Tag .EQUAL , line , line ));
292+ diffRows .add (buildDiffRow (Tag .EQUAL , line , line ));
283293 }
284294 return diffRows ;
285295 }
286-
296+
287297 /**
288298 * Add the inline diffs for given delta
289299 *
290300 * @param delta the given delta
291301 */
292- private void addInlineDiffs (Delta <String > delta ) throws DiffException {
293- List <String > orig = (List <String >) delta .getOriginal ().getLines ();
294- List <String > rev = (List <String >) delta .getRevised ().getLines ();
295- LinkedList <String > origList = new LinkedList <>();
296- for (Character character : String .join ("\n " , orig ).toCharArray ()) {
297- origList .add (character .toString ());
298- }
299- LinkedList <String > revList = new LinkedList <>();
300- for (Character character : String .join ("\n " , rev ).toCharArray ()) {
301- revList .add (character .toString ());
302- }
303- List <Delta <String >> inlineDeltas = DiffUtils .diff (origList , revList ).getDeltas ();
304- if (inlineDeltas .size () < 3 ) {
305- Collections .reverse (inlineDeltas );
306- for (Delta <String > inlineDelta : inlineDeltas ) {
307- Chunk <String > inlineOrig = inlineDelta .getOriginal ();
308- Chunk <String > inlineRev = inlineDelta .getRevised ();
309- if (inlineDelta instanceof DeleteDelta ) {
310- origList = wrapInTag (origList , inlineOrig .getPosition (), inlineOrig
311- .getPosition ()
312- + inlineOrig .size () + 1 , this .inlineOldTag , this .inlineOldCssClass );
313- } else if (inlineDelta instanceof InsertDelta ) {
314- revList = wrapInTag (revList , inlineRev .getPosition (), inlineRev .getPosition ()
315- + inlineRev .size () + 1 , this .inlineNewTag , this .inlineNewCssClass );
316- } else if (inlineDelta instanceof ChangeDelta ) {
317- origList = wrapInTag (origList , inlineOrig .getPosition (), inlineOrig
318- .getPosition ()
319- + inlineOrig .size () + 1 , this .inlineOldTag , this .inlineOldCssClass );
320- revList = wrapInTag (revList , inlineRev .getPosition (), inlineRev .getPosition ()
321- + inlineRev .size () + 1 , this .inlineNewTag , this .inlineNewCssClass );
322- }
323- }
324- StringBuilder origResult = new StringBuilder (), revResult = new StringBuilder ();
325- for (String character : origList ) {
326- origResult .append (character );
302+ private List <DiffRow > generateInlineDiffs (Delta <String > delta , boolean mergeToTarget ) throws DiffException {
303+ if (delta .getType ()!=Delta .DeltaType .CHANGE )
304+ throw new IllegalArgumentException ("only for change deltas allowed" );
305+ // List<String> orig = delta.getOriginal().getLines();
306+ // List<String> rev = delta.getRevised().getLines();
307+ // LinkedList<String> origList = new LinkedList<>();
308+ // for (Character character : String.join("\n", orig).toCharArray()) {
309+ // origList.add(character.toString());
310+ // }
311+ // LinkedList<String> revList = new LinkedList<>();
312+ // for (Character character : String.join("\n", rev).toCharArray()) {
313+ // revList.add(character.toString());
314+ // }
315+ // List<Delta<String>> inlineDeltas = DiffUtils.diff(origList, revList).getDeltas();
316+ Patch <String > patch = DiffUtils .diffInline (String .join ("\n " , delta .getOriginal ().getLines ()), String .join ("\n " , delta .getRevised ().getLines ()));
317+ if (patch .getDeltas ().size () < 3 ) {
318+ List <Delta <String >> deltas = new ArrayList <>(patch .getDeltas ());
319+ Collections .reverse (deltas );
320+
321+ for (Delta <String > inlineDelta : deltas ) {
322+ System .out .println (inlineDelta );
327323 }
328- for (String character : revList ) {
329- revResult .append (character );
330- }
331- delta .getOriginal ().setLines (Arrays .asList (origResult .toString ().split ("\n " )));
332- delta .getRevised ().setLines (Arrays .asList (revResult .toString ().split ("\n " )));
333- }
324+
325+
326+ return Collections .EMPTY_LIST ;
327+ // Collections.reverse(inlineDeltas);
328+ // for (Delta<String> inlineDelta : inlineDeltas) {
329+ // Chunk<String> inlineOrig = inlineDelta.getOriginal();
330+ // Chunk<String> inlineRev = inlineDelta.getRevised();
331+ // if (inlineDelta instanceof DeleteDelta) {
332+ // origList = wrapInTag(origList, inlineOrig.getPosition(), inlineOrig
333+ // .getPosition()
334+ // + inlineOrig.size() + 1, this.inlineOldTag, this.inlineOldCssClass);
335+ // } else if (inlineDelta instanceof InsertDelta) {
336+ // revList = wrapInTag(revList, inlineRev.getPosition(), inlineRev.getPosition()
337+ // + inlineRev.size() + 1, this.inlineNewTag, this.inlineNewCssClass);
338+ // } else if (inlineDelta instanceof ChangeDelta) {
339+ // origList = wrapInTag(origList, inlineOrig.getPosition(), inlineOrig
340+ // .getPosition()
341+ // + inlineOrig.size() + 1, this.inlineOldTag, this.inlineOldCssClass);
342+ // revList = wrapInTag(revList, inlineRev.getPosition(), inlineRev.getPosition()
343+ // + inlineRev.size() + 1, this.inlineNewTag, this.inlineNewCssClass);
344+ // }
345+ // }
346+ // StringBuilder origResult = new StringBuilder(), revResult = new StringBuilder();
347+ // for (String character : origList) {
348+ // origResult.append(character);
349+ // }
350+ // for (String character : revList) {
351+ // revResult.append(character);
352+ // }
353+ // delta.getOriginal().setLines(Arrays.asList(origResult.toString().split("\n")));
354+ // delta.getRevised().setLines(Arrays.asList(revResult.toString().split("\n")));
355+ } else return Collections .emptyList ();
334356 }
335357
336358 /**
0 commit comments