Skip to content

Commit

Permalink
fix(comment): fix bug in comments in array initialization (#1088)
Browse files Browse the repository at this point in the history
Closes #1073
  • Loading branch information
surli authored and monperrus committed Jan 5, 2017
1 parent ac4988a commit ce55bc4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ public <T> void visitCtAnnotationMethod(CtAnnotationMethod<T> annotationMethod)
@SuppressWarnings("rawtypes")
public <T> void visitCtNewArray(CtNewArray<T> newArray) {
enterCtExpression(newArray);

elementPrinterHelper.writeComment(newArray, CommentOffset.BEFORE);
boolean isNotInAnnotation;
try {
isNotInAnnotation = (newArray.getParent(CtAnnotationType.class) == null) && (newArray.getParent(CtAnnotation.class) == null);
Expand Down Expand Up @@ -1244,8 +1244,11 @@ public <T> void visitCtNewArray(CtNewArray<T> newArray) {
elementPrinterHelper.writeComment(e, CommentOffset.AFTER);
}
}

elementPrinterHelper.writeComment(newArray, CommentOffset.INSIDE);
printer.write(" }");
}
elementPrinterHelper.writeComment(newArray, CommentOffset.AFTER);
exitCtExpression(newArray);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ public List<CtComment> getComments(CtElement element, CommentOffset offset) {
}
final int line = element.getPosition().getLine();
final int sourceEnd = element.getPosition().getSourceEnd();
if (offset == CommentOffset.BEFORE && (comment.getPosition().getLine() < line || sourceEnd >= comment.getPosition().getSourceEnd())) {
final int sourceStart = element.getPosition().getSourceStart();
if (offset == CommentOffset.BEFORE && (comment.getPosition().getLine() < line || (sourceStart <= comment.getPosition().getSourceStart() && sourceEnd >= comment.getPosition().getSourceEnd()))) {
commentsToPrint.add(comment);
} else if (offset == CommentOffset.AFTER && comment.getPosition().getSourceStart() > sourceEnd) {
commentsToPrint.add(comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void buildComment(int[] positions) {
private CtElement addCommentToNear(final CtComment comment, final Collection<CtElement> elements) {
CtElement best = null;
int smallDistance = Integer.MAX_VALUE;

for (CtElement element : elements) {
if (element.getPosition() == null) {
continue;
Expand All @@ -157,7 +158,11 @@ private CtElement addCommentToNear(final CtComment comment, final Collection<CtE
if (isAfter) {
distance = Math.abs(element.getPosition().getSourceEnd() - comment.getPosition().getSourceStart());
}
if (distance < smallDistance && (!isAfter || element.getPosition().getEndLine() == comment.getPosition().getLine())) {

int elementEndLine = element.getPosition().getEndLine();
int commentLine = comment.getPosition().getLine();

if (distance < smallDistance && (!isAfter || elementEndLine == commentLine)) {
best = element;
smallDistance = distance;
}
Expand Down
13 changes: 10 additions & 3 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testInLineComment() {

List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
// verify that the number of comment present in the AST is correct
assertEquals(59, comments.size());
assertEquals(61, comments.size());

// verify that all comments present in the AST is printed
for (CtComment comment : comments) {
Expand All @@ -128,9 +128,10 @@ public void testInLineComment() {
assertEquals(createFakeComment(f, "comment class"), type.getComments().get(1));

CtField<?> field = type.getField("field");
assertEquals(2, field.getComments().size());
assertEquals(3, field.getComments().size());
assertEquals(createFakeComment(f, "Comment Field"), field.getComments().get(0));
assertEquals("// Comment Field" + newLine
+ "// comment field 2" + newLine
+ "// comment in field" + newLine
+ "private int field = 10;", field.toString());

Expand Down Expand Up @@ -256,10 +257,13 @@ public void testInLineComment() {
+ "new java.lang.Double((j / ((double) (i - 1))))", ctLocalVariable1.toString());

CtNewArray ctNewArray = (CtNewArray) ((CtLocalVariable) m1.getBody().getStatement(11)).getDefaultExpression();
assertEquals(createFakeComment(f, "last comment at the end of array"), ctNewArray.getComments().get(0));

CtElement arrayValue = (CtElement) ctNewArray.getElements().get(0);
assertEquals(createFakeComment(f, "comment before array value"), arrayValue.getComments().get(0));
assertEquals(createFakeComment(f, "comment after array value"), arrayValue.getComments().get(1));


CtReturn ctReturn = m1.getBody().getStatement(12);
assertEquals(createFakeComment(f, "comment return"), ctReturn.getComments().get(0));
assertEquals("// comment return" + newLine
Expand Down Expand Up @@ -634,6 +638,9 @@ public void testCommentsInComment1And2() {

type = (CtClass<?>) f.Type().get(Comment2.class);
comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
assertEquals(1, comments.size());
assertEquals(2, comments.size());

CtComment commentD = comments.get(1);
assertEquals("D", commentD.getContent());
}
}
6 changes: 6 additions & 0 deletions src/test/java/spoon/test/comment/testclasses/Comment2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ public class Comment2 {
// C
@interface Code_2{}

public void code_3()
{
int[] myArray = {
3, 5 // D
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// comment class
public class InlineComment {
// Comment Field
// comment field 2
private int field // comment in field
= 10;

Expand Down Expand Up @@ -91,6 +92,7 @@ public void m1() {
1, // comment after array value
2,
3
// last comment at the end of array
};
// comment return
return;
Expand Down

0 comments on commit ce55bc4

Please sign in to comment.