Skip to content

Commit 56a6d53

Browse files
committed
fixes #89 to get exact add/remove lines'positions
1 parent e11c159 commit 56a6d53

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,31 @@ private static void processLinesInPrevChunk(List<String[]> rawChunk, Patch<Strin
9797
List<String> oldChunkLines = new ArrayList<>();
9898
List<String> newChunkLines = new ArrayList<>();
9999

100+
List<Integer> removePosition = new ArrayList<>();
101+
List<Integer> addPosition = new ArrayList<>();
102+
int removeNum = 0;
103+
int addNum = 0;
100104
for (String[] raw_line : rawChunk) {
101105
tag = raw_line[0];
102106
rest = raw_line[1];
103107
if (" ".equals(tag) || "-".equals(tag)) {
108+
removeNum++;
104109
oldChunkLines.add(rest);
110+
if ("-".equals(tag)) {
111+
removePosition.add(old_ln - 1 + removeNum);
112+
}
105113
}
106114
if (" ".equals(tag) || "+".equals(tag)) {
115+
addNum++;
107116
newChunkLines.add(rest);
117+
if ("+".equals(tag)) {
118+
addPosition.add(new_ln - 1 + addNum);
119+
}
108120
}
109121
}
110122
patch.addDelta(new ChangeDelta<>(new Chunk<>(
111-
old_ln - 1, oldChunkLines), new Chunk<>(
112-
new_ln - 1, newChunkLines)));
123+
old_ln - 1, oldChunkLines, removePosition), new Chunk<>(
124+
new_ln - 1, newChunkLines, addPosition)));
113125
rawChunk.clear();
114126
}
115127
}

java-diff-utils/src/main/java/com/github/difflib/patch/Chunk.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,42 @@ public final class Chunk<T> {
3737

3838
private final int position;
3939
private List<T> lines;
40+
private final List<Integer> changePosition;
4041

4142
/**
4243
* Creates a chunk and saves a copy of affected lines
4344
*
4445
* @param position the start position
4546
* @param lines the affected lines
47+
* @param changePosition the positions of changed lines
4648
*/
47-
public Chunk(int position, List<T> lines) {
49+
public Chunk(int position, List<T> lines, List<Integer> changePosition) {
4850
this.position = position;
4951
this.lines = new ArrayList<>(lines);
52+
this.changePosition = changePosition;
53+
}
54+
55+
/**
56+
* Creates a chunk and saves a copy of affected lines
57+
*
58+
* @param position the start position
59+
* @param lines the affected lines
60+
*/
61+
public Chunk(int position, List<T> lines) {
62+
this(position, lines, null);
5063
}
5164

5265
/**
5366
* Creates a chunk and saves a copy of affected lines
5467
*
5568
* @param position the start position
5669
* @param lines the affected lines
70+
* @param changePosition the positions of changed lines
5771
*/
58-
public Chunk(int position, T[] lines) {
72+
public Chunk(int position, T[] lines, List<Integer> changePosition) {
5973
this.position = position;
6074
this.lines = Arrays.asList(lines);
75+
this.changePosition = changePosition;
6176
}
6277

6378
/**
@@ -96,6 +111,13 @@ public List<T> getLines() {
96111
return lines;
97112
}
98113

114+
/**
115+
* @return the positions of changed lines of chunk in the text
116+
*/
117+
public List<Integer> getChangePosition() {
118+
return changePosition;
119+
}
120+
99121
public int size() {
100122
return lines.size();
101123
}

0 commit comments

Comments
 (0)