Skip to content

Commit

Permalink
fixed issue with indentation and added more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ThLeu committed Jun 7, 2018
1 parent b8035a7 commit 5b277fe
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ public void replacingFieldWithAnotherField() {
}

// Annotations
@Test
public void removingAnnotations() {
ClassOrInterfaceDeclaration cid = consider(
"@Value" + EOL +
"public class A {}");
cid.getAnnotationByName("Value").get().remove();
assertTransformedToString("public class A {}", cid);
}

@Test
public void removingAnnotationsWithSpaces() {
ClassOrInterfaceDeclaration cid = consider(
" @Value " + EOL +
"public class A {}");
cid.getAnnotationByName("Value").get().remove();
assertTransformedToString("public class A {}", cid);
}

// Javadoc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.io.IOException;
import java.util.EnumSet;

import static com.github.javaparser.utils.Utils.EOL;

/**
* Transforming FieldDeclaration and verifying the LexicalPreservation works as expected.
*/
Expand Down Expand Up @@ -73,4 +75,23 @@ public void changingTypes() {
it.getVariable(1).setType("Xyz");
assertTransformedToString("Xyz a, b;", it);
}

// Annotations
@Test
public void removingAnnotations() {
FieldDeclaration it = consider( EOL +
"@Annotation" + EOL +
"public int A;");
it.getAnnotationByName("Annotation").get().remove();
assertTransformedToString("public int A;", it);
}

@Test
public void removingAnnotationsWithSpaces() {
FieldDeclaration it = consider( EOL +
" @Annotation " + EOL +
"public int A;");
it.getAnnotationByName("Annotation").get().remove();
assertTransformedToString("public int A;", it);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.github.javaparser.ast.type.PrimitiveType;
import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import org.junit.Ignore;
import org.junit.Test;

import java.util.EnumSet;
Expand Down Expand Up @@ -272,4 +273,25 @@ public void removingAnnotations() {
" }\n" +
"}\n", result);
}

@Ignore
@Test
public void removingAnnotationsWithSpaces() {
considerCode(
"class X {" + EOL +
" @Override " + EOL +
" public void testCase() {" + EOL +
" }" + EOL +
"}" + EOL
);

cu.getType(0).getMethods().get(0).getAnnotationByName("Override").get().remove();

String result = LexicalPreservingPrinter.print(cu.findCompilationUnit().get());
assertEqualsNoEol(
"class X {\n" +
" public void testCase() {\n" +
" }\n" +
"}\n", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ private void cleanTheLineOfLeftOverSpace(RemovedGroup removedGroup, Removed remo
return;
}

if (removedGroup.isACompleteLine() && removedGroup.getLastElement() == removed && !removedGroup.isProcessed()) {
if (!removedGroup.isProcessed()
&& removedGroup.getLastElement() == removed
&& removedGroup.isACompleteLine()) {
Integer lastElementIndex = removedGroup.getLastElementIndex();
Optional<Integer> indentation = removedGroup.getIndentation();

Expand All @@ -397,7 +399,7 @@ private void cleanTheLineOfLeftOverSpace(RemovedGroup removedGroup, Removed remo
if (originalElements.get(originalIndex).isSpaceOrTab()) {
// If the current element is a space, remove it
nodeText.removeElement(originalIndex);
} else if (originalIndex >= 1) {
} else if (originalIndex >= 1 && originalElements.get(originalIndex - 1).isSpaceOrTab()) {
// If the current element is not a space itself we remove the space in front of it
nodeText.removeElement(originalIndex - 1);
originalIndex--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.printer.concretesyntaxmodel.CsmToken;

import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -148,10 +150,12 @@ private boolean hasOnlyWhitespaceInFront(JavaToken token) {
return hasOnlyWhitespaceInFront(previousToken.get());
} else if (TokenTypes.isEndOfLineToken(previousToken.get().getKind())) {
return true;
} else {
return false;
}
}

return false;
return true;
}

private boolean hasOnlyWhitespaceBehind(JavaToken token) {
Expand All @@ -162,10 +166,12 @@ private boolean hasOnlyWhitespaceBehind(JavaToken token) {
return hasOnlyWhitespaceInFront(nextToken.get());
} else if (TokenTypes.isEndOfLineToken(nextToken.get().getKind())) {
return true;
} else {
return false;
}
}

return false;
return true;
}

/**
Expand Down Expand Up @@ -195,10 +201,14 @@ Optional<Integer> getIndentation() {
previousToken = previousToken.get().getPreviousToken();
}

if (previousToken.isPresent() && TokenTypes.isEndOfLineToken(previousToken.get().getKind())) {
return Optional.of(Integer.valueOf(indentation));
if (previousToken.isPresent()) {
if (TokenTypes.isEndOfLineToken(previousToken.get().getKind())) {
return Optional.of(Integer.valueOf(indentation));
} else {
return Optional.empty();
}
} else {
return Optional.empty();
return Optional.of(Integer.valueOf(indentation));
}
}
}
Expand All @@ -221,6 +231,7 @@ public boolean hasNext() {
public Removed next() {
return removedList.get(currentIndex++);
}

};
}
}
}

0 comments on commit 5b277fe

Please sign in to comment.