From b7502b6e398d39a5651e5ecd0aaa8a06a93fa8ce Mon Sep 17 00:00:00 2001 From: tsantalis Date: Wed, 2 Oct 2024 11:49:32 -0400 Subject: [PATCH] Process consecutive line comments in groups, as if they were one comment --- .../java/gr/uom/java/xmi/UMLCommentGroup.java | 38 +++++++++++++ .../uom/java/xmi/diff/UMLCommentListDiff.java | 57 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/main/java/gr/uom/java/xmi/UMLCommentGroup.java diff --git a/src/main/java/gr/uom/java/xmi/UMLCommentGroup.java b/src/main/java/gr/uom/java/xmi/UMLCommentGroup.java new file mode 100644 index 000000000..824eeb505 --- /dev/null +++ b/src/main/java/gr/uom/java/xmi/UMLCommentGroup.java @@ -0,0 +1,38 @@ +package gr.uom.java.xmi; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class UMLCommentGroup { + private List group; + + public UMLCommentGroup() { + this.group = new ArrayList(); + } + + public UMLCommentGroup(List group) { + this.group = group; + } + + public void addComment(UMLComment c) { + group.add(c); + } + + public List getGroup() { + return group; + } + + public boolean sameText(UMLCommentGroup other) { + if(this.group.size() == other.group.size()) { + int matches = 0; + for(int i=0; i> commonComments; @@ -21,6 +23,30 @@ public UMLCommentListDiff(List commentsBefore, List comm this.commonComments = new ArrayList>(); this.deletedComments = new ArrayList(); this.addedComments = new ArrayList(); + List deletedComments = new ArrayList(commentsBefore); + List addedComments = new ArrayList(commentsAfter); + //check if there exist comment groups, consecutive line comments + List groupsBefore = createCommentGroups(commentsBefore); + List groupsAfter = createCommentGroups(commentsAfter); + for(UMLCommentGroup groupBefore : groupsBefore) { + for(UMLCommentGroup groupAfter : groupsAfter) { + if(groupBefore.sameText(groupAfter)) { + for(int i=0; i pair = Pair.of(commentBefore, commentAfter); + commonComments.add(pair); + deletedComments.remove(commentBefore); + addedComments.remove(commentAfter); + } + break; + } + } + } + processRemainingComments(deletedComments, addedComments); + } + + private void processRemainingComments(List commentsBefore, List commentsAfter) { List deletedComments = new ArrayList(commentsBefore); List addedComments = new ArrayList(commentsAfter); if(commentsBefore.size() <= commentsAfter.size()) { @@ -54,6 +80,37 @@ public UMLCommentListDiff(List commentsBefore, List comm processModifiedComments(deletedComments, addedComments); } + private List createCommentGroups(List commentsBefore) { + List groups = new ArrayList(); + UMLCommentGroup currentGroup = new UMLCommentGroup(); + for(int i=0; i= 0) { + UMLComment previous = commentsBefore.get(i-1); + if(previous.getLocationInfo().getCodeElementType().equals(CodeElementType.LINE_COMMENT)) { + if(previous.getLocationInfo().getStartLine() + 1 == current.getLocationInfo().getStartLine()) { + //consecutive line comments + currentGroup.addComment(current); + } + else { + //make new group + groups.add(new UMLCommentGroup(currentGroup.getGroup())); + currentGroup = new UMLCommentGroup(); + currentGroup.addComment(current); + } + } + } + else { + //this is the first line comment + currentGroup.addComment(current); + } + } + } + groups.add(currentGroup); + return groups; + } + private List findAllMatchingIndices(List fragments, UMLComment comment) { List matchingIndices = new ArrayList<>(); for(int i=0; i