diff --git a/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java b/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java --- a/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java +++ b/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java @@ -2306,9 +2306,49 @@ } return true; } + + /** + * Finds the end of the line (for brace insertion) after the statement Tree, provided + * the statement is followed nu whitespace only. + * @param statement + * @return + */ + private int findNewlineAfterStatement(Tree statement) { + int pos = (int)sp.getEndPosition(root, statement); + if (pos < 0) { + return pos; + } + int index = tokens.index(); + try { + tokens.move(pos); + while (tokens.moveNext()) { + Token tukac = tokens.token(); + switch (tukac.id()) { + case WHITESPACE: { + int nl = tukac.text().toString().indexOf('\n'); + if (nl != -1) { + return tokens.offset() + nl + 1; + } + break; + } + case LINE_COMMENT: + // up to and including EOL: + return tokens.offset() + tukac.length(); + case BLOCK_COMMENT: + break; + default: + return pos; + } + } + } finally { + tokens.moveIndex(index); + tokens.moveNext(); + } + return pos; + } @Override - public Boolean visitIf(IfTree node, Void p) { + public Boolean visitIf(final IfTree node, Void p) { accept(IF); boolean old = continuationIndent; try { @@ -2320,9 +2360,11 @@ } StatementTree elseStat = node.getElseStatement(); CodeStyle.BracesGenerationStyle redundantIfBraces = cs.redundantIfBraces(); + int eoln = findNewlineAfterStatement(node); if ((elseStat != null && redundantIfBraces == CodeStyle.BracesGenerationStyle.ELIMINATE && danglingElseChecker.hasDanglingElse(node.getThenStatement())) || - (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node) || node.getCondition().getKind() == Tree.Kind.ERRONEOUS))) + (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS))) { redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE; + } lastIndent = indent; boolean prevblock = wrapStatement(cs.wrapIfStatement(), redundantIfBraces, cs.spaceBeforeIfLeftBrace() ? 1 : 0, node.getThenStatement()); if (elseStat != null) { @@ -2351,8 +2393,9 @@ lastIndent -= indentSize; } else { redundantIfBraces = cs.redundantIfBraces(); - if (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node))) + if (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln)) { redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE; + } wrapElse = cs.wrapIfStatement(); } wrapStatement(wrapElse, redundantIfBraces, cs.spaceBeforeElseLeftBrace() ? 1 : 0, preserveNewLine, elseStat); @@ -2366,9 +2409,11 @@ lastIndent = indent; boolean old = continuationIndent; try { + int eoln = findNewlineAfterStatement(node); CodeStyle.BracesGenerationStyle redundantDoWhileBraces = cs.redundantDoWhileBraces(); - if (redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node) || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) + if (redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) { redundantDoWhileBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE; + } boolean isBlock = node.getStatement().getKind() == Tree.Kind.BLOCK || redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE; if (isBlock && redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.ELIMINATE) { Iterator stats = ((BlockTree)node.getStatement()).getStatements().iterator(); @@ -2408,9 +2453,11 @@ continuationIndent = old; } lastIndent = indent; + int eoln = findNewlineAfterStatement(node); CodeStyle.BracesGenerationStyle redundantWhileBraces = cs.redundantWhileBraces(); - if (redundantWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node) || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) + if (redundantWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) { redundantWhileBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE; + } wrapStatement(cs.wrapWhileStatement(), redundantWhileBraces, cs.spaceBeforeWhileLeftBrace() ? 1 : 0, node.getStatement()); return true; } @@ -2471,8 +2518,10 @@ } lastIndent = indent; CodeStyle.BracesGenerationStyle redundantForBraces = cs.redundantForBraces(); - if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node))) + int eoln = findNewlineAfterStatement(node); + if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) { redundantForBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE; + } wrapStatement(cs.wrapForStatement(), redundantForBraces, cs.spaceBeforeForLeftBrace() ? 1 : 0, node.getStatement()); return true; } @@ -2496,8 +2545,10 @@ } lastIndent = indent; CodeStyle.BracesGenerationStyle redundantForBraces = cs.redundantForBraces(); - if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node))) + int eoln = findNewlineAfterStatement(node); + if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln)) { redundantForBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE; + } wrapStatement(cs.wrapForStatement(), redundantForBraces, cs.spaceBeforeForLeftBrace() ? 1 : 0, node.getStatement()); return true; } diff --git a/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java b/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java --- a/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java +++ b/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java @@ -4205,8 +4205,19 @@ + " i++;\n" + " }\n" + "}\n"; + String golden2 = + "package hierbas.del.litoral;\n" + + "public class Test{\n" + + " public void test() {\n" + + " int i = 5;\n" + + " if (i > 0)\n" + + " i++;\n" + + " if (i > 0)\n" + + " i++;\n" + + " }\n" + + "}\n"; reformat(doc, content, golden, 92, 128); - reformat(doc, content, golden, 92, 127); + reformat(doc, content, golden2, 92, 127); golden = "package hierbas.del.litoral;\n" @@ -4220,8 +4231,19 @@ + " }\n" + " }\n" + "}\n"; - reformat(doc, content, golden, 128, 163); - reformat(doc, content, golden, 127, 163); + golden2 = + "package hierbas.del.litoral;\n" + + "public class Test{\n" + + " public void test() {\n" + + " int i = 5;\n" + + " if (i > 0)\n" + + " i++;\n" + + " if (i > 0)\n" + + " i++;\n" + + " }\n" + + "}\n"; + reformat(doc, content, golden, 128, 164); + reformat(doc, content, golden2, 127, 163); } public void test177858() throws Exception {