Skip to content

Commit

Permalink
Accounting for trailing new lines (should not indent)
Browse files Browse the repository at this point in the history
  • Loading branch information
moraispgsi committed Mar 3, 2019
1 parent 417e41a commit 13177e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Expand Up @@ -200,7 +200,7 @@ void issue2099AddingStatementAfterTraillingComment2() {


@Test
void AddingStatement1() {
void addingStatement1() {
Statement statement = LexicalPreservingPrinter.setup(StaticJavaParser.parseStatement(
" if(value != null) {" + EOL +
" value.value();" + EOL +
Expand Down Expand Up @@ -230,7 +230,7 @@ void AddingStatement1() {
}

@Test
void AddingStatement2() {
void addingStatement2() {
Statement statement = LexicalPreservingPrinter.setup(StaticJavaParser.parseStatement(
" if(value != null) {" + EOL +
" value.value();" + EOL +
Expand Down Expand Up @@ -258,4 +258,34 @@ void AddingStatement2() {
"}";
assertEqualsNoEol(expected, s);
}

@Test
void addingStatement3() {
Statement statement = LexicalPreservingPrinter.setup(StaticJavaParser.parseStatement(
" if(value != null) {" + EOL +
" value.value();" + EOL +
" }"));

CompilationUnit compilationUnit = LexicalPreservingPrinter.setup(StaticJavaParser.parse("public class Test {" + EOL +
" public void method() {" + EOL +
" value1();" + EOL +
" value2();" + EOL + EOL +
" }" + EOL +
"}"));
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration)compilationUnit.getChildNodes().get(0);
MethodDeclaration methodDeclaration = (MethodDeclaration)classOrInterfaceDeclaration.getChildNodes().get(2);
methodDeclaration.getBody().get().addStatement(statement);

String s = LexicalPreservingPrinter.print(compilationUnit);
String expected = "public class Test {\n" +
" public void method() {\n" +
" value1();\n" +
" value2();\n" +
" if(value != null) {\n" +
" value.value();\n" +
" }\n\n" +
" }\n" +
"}";
assertEqualsNoEol(expected, s);
}
}
Expand Up @@ -518,8 +518,8 @@ private int rewindSpace(int index) {
}
}

private boolean nextIsRightBrace() {
List<TextElement> elements = originalElements.subList(originalIndex, originalElements.size());
private boolean nextIsRightBrace(int index) {
List<TextElement> elements = originalElements.subList(index, originalElements.size());
for(TextElement element : elements) {
if (!element.isSpaceOrTab()) {
return element.isToken(RBRACE);
Expand Down Expand Up @@ -550,7 +550,7 @@ private void applyAddedDiffElement(Added added) {
boolean used = false;
if (originalIndex > 0 && originalElements.get(originalIndex - 1).isNewline()) {
List<TextElement> elements = processIndentation(indentation, originalElements.subList(0, originalIndex - 1));
boolean nextIsRightBrace = nextIsRightBrace();
boolean nextIsRightBrace = nextIsRightBrace(originalIndex);
for (TextElement e : elements) {
if (!nextIsRightBrace
&& e instanceof TokenTextElement
Expand Down Expand Up @@ -606,7 +606,11 @@ private void applyAddedDiffElement(Added added) {

if (addedTextElement.isNewline()) {
boolean followedByUnindent = isFollowedByUnindent(diffElements, diffIndex);
originalIndex = adjustIndentation(indentation, nodeText, originalIndex, followedByUnindent);
boolean nextIsRightBrace = nextIsRightBrace(originalIndex);
boolean nextIsNewLine = nodeText.getTextElement(originalIndex).isNewline();
if ((!nextIsNewLine && !nextIsRightBrace) || followedByUnindent) {
originalIndex = adjustIndentation(indentation, nodeText, originalIndex, followedByUnindent);
}
}

diffIndex++;
Expand Down

0 comments on commit 13177e3

Please sign in to comment.