Skip to content

fixes #89 to get exact add/remove lines' positions #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,31 @@ private static void processLinesInPrevChunk(List<String[]> rawChunk, Patch<Strin
List<String> oldChunkLines = new ArrayList<>();
List<String> newChunkLines = new ArrayList<>();

List<Integer> removePosition = new ArrayList<>();
List<Integer> addPosition = new ArrayList<>();
int removeNum = 0;
int addNum = 0;
for (String[] raw_line : rawChunk) {
tag = raw_line[0];
rest = raw_line[1];
if (" ".equals(tag) || "-".equals(tag)) {
removeNum++;
oldChunkLines.add(rest);
if ("-".equals(tag)) {
removePosition.add(old_ln - 1 + removeNum);
}
}
if (" ".equals(tag) || "+".equals(tag)) {
addNum++;
newChunkLines.add(rest);
if ("+".equals(tag)) {
addPosition.add(new_ln - 1 + addNum);
}
}
}
patch.addDelta(new ChangeDelta<>(new Chunk<>(
old_ln - 1, oldChunkLines), new Chunk<>(
new_ln - 1, newChunkLines)));
old_ln - 1, oldChunkLines, removePosition), new Chunk<>(
new_ln - 1, newChunkLines, addPosition)));
rawChunk.clear();
}
}
Expand Down
36 changes: 34 additions & 2 deletions java-diff-utils/src/main/java/com/github/difflib/patch/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ public final class Chunk<T> {

private final int position;
private List<T> lines;
private final List<Integer> changePosition;

/**
* Creates a chunk and saves a copy of affected lines
*
* @param position the start position
* @param lines the affected lines
* @param changePosition the positions of changed lines
*/
public Chunk(int position, List<T> lines) {
public Chunk(int position, List<T> lines, List<Integer> changePosition) {
this.position = position;
this.lines = new ArrayList<>(lines);
this.changePosition = changePosition;
}

/**
Expand All @@ -55,9 +58,31 @@ public Chunk(int position, List<T> lines) {
* @param position the start position
* @param lines the affected lines
*/
public Chunk(int position, T[] lines) {
public Chunk(int position, List<T> lines) {
this(position, lines, null);
}

/**
* Creates a chunk and saves a copy of affected lines
*
* @param position the start position
* @param lines the affected lines
* @param changePosition the positions of changed lines
*/
public Chunk(int position, T[] lines, List<Integer> changePosition) {
this.position = position;
this.lines = Arrays.asList(lines);
this.changePosition = changePosition;
}

/**
* Creates a chunk and saves a copy of affected lines
*
* @param position the start position
* @param lines the affected lines
*/
public Chunk(int position, T[] lines) {
this(position, lines, null);
}

/**
Expand Down Expand Up @@ -96,6 +121,13 @@ public List<T> getLines() {
return lines;
}

/**
* @return the positions of changed lines of chunk in the text
*/
public List<Integer> getChangePosition() {
return changePosition;
}

public int size() {
return lines.size();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.difflib;

import com.github.difflib.patch.Chunk;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
import java.io.BufferedReader;
Expand All @@ -8,9 +9,11 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.joining;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -126,6 +129,44 @@ public void testNewFileCreation() {
UnifiedDiffUtils.parseUnifiedDiff(udiff);
}

/**
* Issue 89
*/
@Test
public void testChagngePosition() throws IOException {
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue89_patch.txt");
final Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
List<Integer> realRemoveListOne = Collections.singletonList(3);
List<Integer> realAddListOne = Arrays.asList(3, 7, 8, 9, 10, 11, 12, 13, 14);
validateChangePosition(patch, 0, realRemoveListOne, realAddListOne);
List<Integer> realRemoveListTwo = new ArrayList<>();
List<Integer> realAddListTwo = Arrays.asList(27, 28);
validateChangePosition(patch, 1, realRemoveListTwo, realAddListTwo);

}

private void validateChangePosition(Patch<String> patch, int index, List<Integer> realRemoveList,
List<Integer> realAddList ) {
final Chunk originChunk = patch.getDeltas().get(index).getSource();
List<Integer> removeList = originChunk.getChangePosition();
assertEquals(realRemoveList.size(), removeList.size());
for (Integer ele: realRemoveList) {
assertTrue(realRemoveList.contains(ele));
}
for (Integer ele: removeList) {
assertTrue(realAddList.contains(ele));
}
final Chunk targetChunk = patch.getDeltas().get(index).getTarget();
List<Integer> addList = targetChunk.getChangePosition();
assertEquals(realAddList.size(), addList.size());
for (Integer ele: realAddList) {
assertTrue(addList.contains(ele));
}
for (Integer ele: addList) {
assertTrue(realAddList.contains(ele));
}
}

private void verify(List<String> origLines, List<String> revLines,
String originalFile, String revisedFile) {
Patch<String> patch = DiffUtils.diff(origLines, revLines);
Expand Down
29 changes: 29 additions & 0 deletions java-diff-utils/src/test/resources/mocks/issue89_patch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--- Origin.java 2020-06-11 11:06:21.000000000 +0800
+++ Update.java 2020-06-11 10:59:48.000000000 +0800
@@ -1,9 +1,17 @@
package checkstyle_demo.PatchSuppression.MultiChangeInOneFile;

-public class Origin {
+public class Update {
public void test1() {

}
+
+ public void test2() {
+
+ }
+
+ public void test3() {
+
+ }
}

class BasicTest {
@@ -16,5 +24,7 @@
class Test2 {
public void test1() {
System.out.println();
+ System.out.println();
+ System.out.println();
}
}
30 changes: 30 additions & 0 deletions java-diff-utils/src/test/resources/mocks/issue89_revised.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package checkstyle_demo.PatchSuppression.MultiChangeInOneFile;

public class issue89_revised {
public void test1() {

}

public void test2() {

}

public void test3() {

}
}

class Test {
private int i;
void foo() {
i++;
}
}

class Test2 {
public void test1() {
System.out.println();
System.out.println();
System.out.println();
}
}