Skip to content

Commit f0f5d24

Browse files
authored
fixes #89 to get exact add/remove lines'positions (#90)
1 parent e11c159 commit f0f5d24

File tree

5 files changed

+148
-4
lines changed

5 files changed

+148
-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: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ 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;
5053
}
5154

5255
/**
@@ -55,9 +58,31 @@ public Chunk(int position, List<T> lines) {
5558
* @param position the start position
5659
* @param lines the affected lines
5760
*/
58-
public Chunk(int position, T[] lines) {
61+
public Chunk(int position, List<T> lines) {
62+
this(position, lines, null);
63+
}
64+
65+
/**
66+
* Creates a chunk and saves a copy of affected lines
67+
*
68+
* @param position the start position
69+
* @param lines the affected lines
70+
* @param changePosition the positions of changed lines
71+
*/
72+
public Chunk(int position, T[] lines, List<Integer> changePosition) {
5973
this.position = position;
6074
this.lines = Arrays.asList(lines);
75+
this.changePosition = changePosition;
76+
}
77+
78+
/**
79+
* Creates a chunk and saves a copy of affected lines
80+
*
81+
* @param position the start position
82+
* @param lines the affected lines
83+
*/
84+
public Chunk(int position, T[] lines) {
85+
this(position, lines, null);
6186
}
6287

6388
/**
@@ -96,6 +121,13 @@ public List<T> getLines() {
96121
return lines;
97122
}
98123

124+
/**
125+
* @return the positions of changed lines of chunk in the text
126+
*/
127+
public List<Integer> getChangePosition() {
128+
return changePosition;
129+
}
130+
99131
public int size() {
100132
return lines.size();
101133
}

java-diff-utils/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.difflib;
22

3+
import com.github.difflib.patch.Chunk;
34
import com.github.difflib.patch.Patch;
45
import com.github.difflib.patch.PatchFailedException;
56
import java.io.BufferedReader;
@@ -8,9 +9,11 @@
89
import java.io.IOException;
910
import java.util.ArrayList;
1011
import java.util.Arrays;
12+
import java.util.Collections;
1113
import java.util.List;
1214
import static java.util.stream.Collectors.joining;
1315
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1417
import static org.junit.jupiter.api.Assertions.fail;
1518
import org.junit.jupiter.api.Test;
1619

@@ -126,6 +129,44 @@ public void testNewFileCreation() {
126129
UnifiedDiffUtils.parseUnifiedDiff(udiff);
127130
}
128131

132+
/**
133+
* Issue 89
134+
*/
135+
@Test
136+
public void testChagngePosition() throws IOException {
137+
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue89_patch.txt");
138+
final Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
139+
List<Integer> realRemoveListOne = Collections.singletonList(3);
140+
List<Integer> realAddListOne = Arrays.asList(3, 7, 8, 9, 10, 11, 12, 13, 14);
141+
validateChangePosition(patch, 0, realRemoveListOne, realAddListOne);
142+
List<Integer> realRemoveListTwo = new ArrayList<>();
143+
List<Integer> realAddListTwo = Arrays.asList(27, 28);
144+
validateChangePosition(patch, 1, realRemoveListTwo, realAddListTwo);
145+
146+
}
147+
148+
private void validateChangePosition(Patch<String> patch, int index, List<Integer> realRemoveList,
149+
List<Integer> realAddList ) {
150+
final Chunk originChunk = patch.getDeltas().get(index).getSource();
151+
List<Integer> removeList = originChunk.getChangePosition();
152+
assertEquals(realRemoveList.size(), removeList.size());
153+
for (Integer ele: realRemoveList) {
154+
assertTrue(realRemoveList.contains(ele));
155+
}
156+
for (Integer ele: removeList) {
157+
assertTrue(realAddList.contains(ele));
158+
}
159+
final Chunk targetChunk = patch.getDeltas().get(index).getTarget();
160+
List<Integer> addList = targetChunk.getChangePosition();
161+
assertEquals(realAddList.size(), addList.size());
162+
for (Integer ele: realAddList) {
163+
assertTrue(addList.contains(ele));
164+
}
165+
for (Integer ele: addList) {
166+
assertTrue(realAddList.contains(ele));
167+
}
168+
}
169+
129170
private void verify(List<String> origLines, List<String> revLines,
130171
String originalFile, String revisedFile) {
131172
Patch<String> patch = DiffUtils.diff(origLines, revLines);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--- Origin.java 2020-06-11 11:06:21.000000000 +0800
2+
+++ Update.java 2020-06-11 10:59:48.000000000 +0800
3+
@@ -1,9 +1,17 @@
4+
package checkstyle_demo.PatchSuppression.MultiChangeInOneFile;
5+
6+
-public class Origin {
7+
+public class Update {
8+
public void test1() {
9+
10+
}
11+
+
12+
+ public void test2() {
13+
+
14+
+ }
15+
+
16+
+ public void test3() {
17+
+
18+
+ }
19+
}
20+
21+
class BasicTest {
22+
@@ -16,5 +24,7 @@
23+
class Test2 {
24+
public void test1() {
25+
System.out.println();
26+
+ System.out.println();
27+
+ System.out.println();
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package checkstyle_demo.PatchSuppression.MultiChangeInOneFile;
2+
3+
public class issue89_revised {
4+
public void test1() {
5+
6+
}
7+
8+
public void test2() {
9+
10+
}
11+
12+
public void test3() {
13+
14+
}
15+
}
16+
17+
class Test {
18+
private int i;
19+
void foo() {
20+
i++;
21+
}
22+
}
23+
24+
class Test2 {
25+
public void test1() {
26+
System.out.println();
27+
System.out.println();
28+
System.out.println();
29+
}
30+
}

0 commit comments

Comments
 (0)