Skip to content

Commit

Permalink
Process consecutive line comments in groups, as if they were one comment
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Oct 2, 2024
1 parent c0b5519 commit b7502b6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/gr/uom/java/xmi/UMLCommentGroup.java
Original file line number Diff line number Diff line change
@@ -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<UMLComment> group;

public UMLCommentGroup() {
this.group = new ArrayList<UMLComment>();
}

public UMLCommentGroup(List<UMLComment> group) {
this.group = group;
}

public void addComment(UMLComment c) {
group.add(c);
}

public List<UMLComment> getGroup() {
return group;
}

public boolean sameText(UMLCommentGroup other) {
if(this.group.size() == other.group.size()) {
int matches = 0;
for(int i=0; i<this.group.size(); i++) {
if(this.group.get(i).getText().equals(other.group.get(i).getText())) {
matches++;
}
}
return matches == this.group.size();
}
return false;
}
}
57 changes: 57 additions & 0 deletions src/main/java/gr/uom/java/xmi/diff/UMLCommentListDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.apache.commons.lang3.tuple.Pair;

import gr.uom.java.xmi.UMLComment;
import gr.uom.java.xmi.UMLCommentGroup;
import gr.uom.java.xmi.LocationInfo.CodeElementType;

public class UMLCommentListDiff {
private List<Pair<UMLComment, UMLComment>> commonComments;
Expand All @@ -21,6 +23,30 @@ public UMLCommentListDiff(List<UMLComment> commentsBefore, List<UMLComment> comm
this.commonComments = new ArrayList<Pair<UMLComment,UMLComment>>();
this.deletedComments = new ArrayList<UMLComment>();
this.addedComments = new ArrayList<UMLComment>();
List<UMLComment> deletedComments = new ArrayList<UMLComment>(commentsBefore);
List<UMLComment> addedComments = new ArrayList<UMLComment>(commentsAfter);
//check if there exist comment groups, consecutive line comments
List<UMLCommentGroup> groupsBefore = createCommentGroups(commentsBefore);
List<UMLCommentGroup> groupsAfter = createCommentGroups(commentsAfter);
for(UMLCommentGroup groupBefore : groupsBefore) {
for(UMLCommentGroup groupAfter : groupsAfter) {
if(groupBefore.sameText(groupAfter)) {
for(int i=0; i<groupBefore.getGroup().size(); i++) {
UMLComment commentBefore = groupBefore.getGroup().get(i);
UMLComment commentAfter = groupAfter.getGroup().get(i);
Pair<UMLComment, UMLComment> pair = Pair.of(commentBefore, commentAfter);
commonComments.add(pair);
deletedComments.remove(commentBefore);
addedComments.remove(commentAfter);
}
break;
}
}
}
processRemainingComments(deletedComments, addedComments);
}

private void processRemainingComments(List<UMLComment> commentsBefore, List<UMLComment> commentsAfter) {
List<UMLComment> deletedComments = new ArrayList<UMLComment>(commentsBefore);
List<UMLComment> addedComments = new ArrayList<UMLComment>(commentsAfter);
if(commentsBefore.size() <= commentsAfter.size()) {
Expand Down Expand Up @@ -54,6 +80,37 @@ public UMLCommentListDiff(List<UMLComment> commentsBefore, List<UMLComment> comm
processModifiedComments(deletedComments, addedComments);
}

private List<UMLCommentGroup> createCommentGroups(List<UMLComment> commentsBefore) {
List<UMLCommentGroup> groups = new ArrayList<UMLCommentGroup>();
UMLCommentGroup currentGroup = new UMLCommentGroup();
for(int i=0; i<commentsBefore.size(); i++) {
UMLComment current = commentsBefore.get(i);
if(current.getLocationInfo().getCodeElementType().equals(CodeElementType.LINE_COMMENT)) {
if(i-1 >= 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<Integer> findAllMatchingIndices(List<UMLComment> fragments, UMLComment comment) {
List<Integer> matchingIndices = new ArrayList<>();
for(int i=0; i<fragments.size(); i++) {
Expand Down

0 comments on commit b7502b6

Please sign in to comment.