|
15 | 15 | */ |
16 | 16 | package com.github.difflib.unifieddiff; |
17 | 17 |
|
| 18 | +import com.github.difflib.patch.AbstractDelta; |
18 | 19 | import com.github.difflib.patch.ChangeDelta; |
19 | 20 | import com.github.difflib.patch.Chunk; |
| 21 | +import com.github.difflib.patch.DeleteDelta; |
| 22 | +import com.github.difflib.patch.EqualDelta; |
| 23 | +import com.github.difflib.patch.InsertDelta; |
20 | 24 | import java.io.BufferedReader; |
21 | 25 | import java.io.IOException; |
22 | 26 | import java.io.InputStream; |
@@ -298,23 +302,41 @@ private void processSimilarityIndex(MatchResult match, String line) { |
298 | 302 | private int delLineIdx = 0; |
299 | 303 | private int addLineIdx = 0; |
300 | 304 |
|
301 | | - private void finalizeChunk() { |
302 | | - if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) { |
303 | | - actualFile |
304 | | - .getPatch() |
305 | | - .addDelta(new ChangeDelta<>( |
306 | | - new Chunk<>(old_ln - 1, originalTxt, delLineIdxList), |
307 | | - new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList))); |
308 | | - old_ln = 0; |
309 | | - new_ln = 0; |
310 | | - originalTxt.clear(); |
311 | | - revisedTxt.clear(); |
312 | | - addLineIdxList.clear(); |
313 | | - delLineIdxList.clear(); |
314 | | - delLineIdx = 0; |
315 | | - addLineIdx = 0; |
316 | | - } |
317 | | - } |
| 305 | + private void finalizeChunk() { |
| 306 | + if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) { |
| 307 | + boolean hasDeletes = !delLineIdxList.isEmpty(); |
| 308 | + boolean hasInserts = !addLineIdxList.isEmpty(); |
| 309 | + boolean hasContext = originalTxt.size() != delLineIdxList.size() |
| 310 | + || revisedTxt.size() != addLineIdxList.size(); |
| 311 | + AbstractDelta<String> delta; |
| 312 | + if (hasContext || (hasDeletes && hasInserts)) { |
| 313 | + delta = new ChangeDelta<>( |
| 314 | + new Chunk<>(old_ln - 1, originalTxt, delLineIdxList), |
| 315 | + new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList)); |
| 316 | + } else if (hasDeletes) { |
| 317 | + delta = new DeleteDelta<>( |
| 318 | + new Chunk<>(old_ln - 1, originalTxt, delLineIdxList), |
| 319 | + new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList)); |
| 320 | + } else if (hasInserts) { |
| 321 | + delta = new InsertDelta<>( |
| 322 | + new Chunk<>(old_ln - 1, originalTxt, delLineIdxList), |
| 323 | + new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList)); |
| 324 | + } else { |
| 325 | + delta = new EqualDelta<>( |
| 326 | + new Chunk<>(old_ln - 1, originalTxt, delLineIdxList), |
| 327 | + new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList)); |
| 328 | + } |
| 329 | + actualFile.getPatch().addDelta(delta); |
| 330 | + old_ln = 0; |
| 331 | + new_ln = 0; |
| 332 | + originalTxt.clear(); |
| 333 | + revisedTxt.clear(); |
| 334 | + addLineIdxList.clear(); |
| 335 | + delLineIdxList.clear(); |
| 336 | + delLineIdx = 0; |
| 337 | + addLineIdx = 0; |
| 338 | + } |
| 339 | + } |
318 | 340 |
|
319 | 341 | private void processNormalLine(MatchResult match, String line) { |
320 | 342 | String cline = line.substring(1); |
|
0 commit comments