Skip to content

Commit cc50a1e

Browse files
committed
ASTDiff: Fix the issue related to the comments offset
1 parent 50c9618 commit cc50a1e

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/main/java/org/refactoringminer/astDiff/matchers/wrappers/CommentMatcher.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.commons.lang3.tuple.Pair;
99
import org.refactoringminer.astDiff.models.ExtendedMultiMappingStore;
1010
import org.refactoringminer.astDiff.models.OptimizationData;
11+
import org.refactoringminer.astDiff.utils.Constants;
1112
import org.refactoringminer.astDiff.utils.TreeUtilFunctions;
1213

1314
import java.util.Optional;
@@ -37,8 +38,8 @@ void matchAndUpdateOptimizationStore(Tree src, Tree dst, ExtendedMultiMappingSto
3738
matchAndUpdateOptimizationStore(src, dst, mappingStore);
3839
}
3940
else {
40-
Tree srcComment = TreeUtilFunctions.findByLocationInfo(src, commonComment.getLeft().getLocationInfo());
41-
Tree dstComment = TreeUtilFunctions.findByLocationInfo(dst, commonComment.getRight().getLocationInfo());
41+
Tree srcComment = TreeUtilFunctions.findByLocationInfo(src, commonComment.getLeft().getLocationInfo(), Constants.LINE_COMMENT, Constants.BLOCK_COMMENT);
42+
Tree dstComment = TreeUtilFunctions.findByLocationInfo(dst, commonComment.getRight().getLocationInfo(), Constants.LINE_COMMENT, Constants.BLOCK_COMMENT);
4243
if (srcComment != null && dstComment != null) {
4344
mappingStore.addMapping(srcComment, dstComment);
4445
}

src/main/java/org/refactoringminer/astDiff/utils/TreeUtilFunctions.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static Tree findByLocationInfoNoLookAhead(Tree tree, LocationInfo locatio
5757
return treeBetweenPositions;
5858
}
5959

60-
public static Tree findByLocationInfo(Tree tree, LocationInfo locationInfo, String type){
60+
public static Tree findByLocationInfo(Tree tree, LocationInfo locationInfo, String... type){
6161
int startoffset = locationInfo.getStartOffset();
6262
int endoffset = locationInfo.getEndOffset();
6363
return getTreeBetweenPositions(tree, startoffset, endoffset,type);
@@ -71,7 +71,7 @@ public static Tree getTreeBetweenPositions(Tree tree, int position, int endPosit
7171
return null;
7272
}
7373

74-
public static Tree getTreeBetweenPositions(Tree tree, int position, int endPosition,String type) {
74+
public static Tree getTreeBetweenPositions(Tree tree, int position, int endPosition,String... type) {
7575
for (Tree t: tree.preOrder()) {
7676
if (t.getPos() >= position && t.getEndPos() <= endPosition)
7777
if (isFromType(t, type))
@@ -301,8 +301,12 @@ public static boolean areBothFromThisType(Tree t1, Tree t2, String type) {
301301
&& isFromType(t2, type);
302302
}
303303

304-
public static boolean isFromType(Tree t1, String type) {
305-
return t1.getType().name.equals(type);
304+
public static boolean isFromType(Tree t1, String... type) {
305+
if (t1 == null) return false;
306+
for (String t: type)
307+
if (t1.getType().name.equals(t))
308+
return true;
309+
return false;
306310
}
307311

308312
public static Tree findFirstByType(Tree inputTree, String typeName) {

0 commit comments

Comments
 (0)