|
| 1 | +diff --git a/ide/editor.document/src/org/netbeans/api/editor/document/LineDocumentUtils.java b/ide/editor.document/src/org/netbeans/api/editor/document/LineDocumentUtils.java |
| 2 | +index 5fb34c754a..247dda8b82 100644 |
| 3 | +--- a/ide/editor.document/src/org/netbeans/api/editor/document/LineDocumentUtils.java |
| 4 | ++++ b/ide/editor.document/src/org/netbeans/api/editor/document/LineDocumentUtils.java |
| 5 | +@@ -30,7 +30,6 @@ import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
| 6 | + import org.netbeans.modules.editor.document.DocumentServices; |
| 7 | + import org.netbeans.modules.editor.document.TextSearchUtils; |
| 8 | + import org.netbeans.modules.editor.document.implspi.CharClassifier; |
| 9 | +-import org.netbeans.modules.editor.lib2.AcceptorFactory; |
| 10 | + import org.netbeans.spi.editor.document.DocumentFactory; |
| 11 | + import org.openide.util.Lookup; |
| 12 | + |
| 13 | +diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java b/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java |
| 14 | +index 0e44783168..3202b2ac97 100644 |
| 15 | +--- a/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java |
| 16 | ++++ b/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java |
| 17 | +@@ -2565,7 +2565,36 @@ public class Reformatter implements ReformatTask { |
| 18 | + StatementTree elseStat = node.getElseStatement(); |
| 19 | + CodeStyle.BracesGenerationStyle redundantIfBraces = cs.redundantIfBraces(); |
| 20 | + int eoln = findNewlineAfterStatement(node); |
| 21 | +- if ((elseStat != null && redundantIfBraces == CodeStyle.BracesGenerationStyle.ELIMINATE && danglingElseChecker.hasDanglingElse(node.getThenStatement())) || |
| 22 | ++ Boolean hasErrThenStatement = node.getThenStatement() instanceof ExpressionStatementTree thenStatement |
| 23 | ++ && thenStatement.getExpression() instanceof ErroneousTree; |
| 24 | ++ if (hasErrThenStatement) { |
| 25 | ++ if (getCurrentPath().getParentPath().getLeaf() instanceof BlockTree parentStTree) { |
| 26 | ++ boolean isPreviousIfTree = false; |
| 27 | ++ int endPositionOfErrThenStatement = endPos; |
| 28 | ++ for (StatementTree statement : parentStTree.getStatements()) { |
| 29 | ++ if (isPreviousIfTree) { |
| 30 | ++ int startPositionOfNextErrorStatement = (int) sp.getStartPosition(getCurrentPath().getCompilationUnit(), statement); |
| 31 | ++ endPositionOfErrThenStatement = startPositionOfNextErrorStatement; |
| 32 | ++ break; |
| 33 | ++ } else if (statement == node) { |
| 34 | ++ isPreviousIfTree = true; |
| 35 | ++ endPositionOfErrThenStatement = (int) sp.getEndPosition(getCurrentPath().getCompilationUnit(), parentStTree) - 1; |
| 36 | ++ } |
| 37 | ++ |
| 38 | ++ } |
| 39 | ++ if (isPreviousIfTree) { |
| 40 | ++ while (tokens.offset() <= endPositionOfErrThenStatement && endPositionOfErrThenStatement != -1) { |
| 41 | ++ tokens.moveNext(); |
| 42 | ++ } |
| 43 | ++ tokens.movePrevious(); |
| 44 | ++ if (endPositionOfErrThenStatement != -1) { |
| 45 | ++ endPos = endPositionOfErrThenStatement; |
| 46 | ++ } |
| 47 | ++ } |
| 48 | ++ } |
| 49 | ++ } |
| 50 | ++ |
| 51 | ++ if (hasErrThenStatement || (elseStat != null && redundantIfBraces == CodeStyle.BracesGenerationStyle.ELIMINATE && danglingElseChecker.hasDanglingElse(node.getThenStatement())) || |
| 52 | + (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS))) { |
| 53 | + redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE; |
| 54 | + } |
| 55 | +diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java |
| 56 | +index 8797b9111b..d5962a9ba3 100644 |
| 57 | +--- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java |
| 58 | ++++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java |
| 59 | +@@ -1556,6 +1556,76 @@ public class FormatingTest extends NbTestCase { |
| 60 | + preferences.putBoolean("specialElseIf", false); |
| 61 | + reformat(doc, content, golden); |
| 62 | + preferences.putBoolean("specialElseIf", true); |
| 63 | ++ |
| 64 | ++ content = """ |
| 65 | ++ package hierbas.del.litoral; |
| 66 | ++ class Test extends Integer implements Runnable, Serializable{ |
| 67 | ++ public void run(){ |
| 68 | ++ if ("foo".contains("bar"))))) ))) { |
| 69 | ++ System.out.println("bar"); |
| 70 | ++ } |
| 71 | ++ } |
| 72 | ++ } |
| 73 | ++ """; |
| 74 | ++ golden = """ |
| 75 | ++ package hierbas.del.litoral; |
| 76 | ++ |
| 77 | ++ class Test extends Integer implements Runnable, Serializable { |
| 78 | ++ |
| 79 | ++ public void run() { |
| 80 | ++ if ("foo".contains("bar"))))) ))) { |
| 81 | ++ System.out.println("bar"); |
| 82 | ++ } |
| 83 | ++ } |
| 84 | ++ } |
| 85 | ++ """; |
| 86 | ++ reformat(doc, content, golden); |
| 87 | ++ |
| 88 | ++ content = """ |
| 89 | ++ package hierbas.del.litoral; |
| 90 | ++ class Test extends Integer implements Runnable, Serializable{ |
| 91 | ++ public void run(){ |
| 92 | ++ if ("foo".contains("bar"))))) ))) |
| 93 | ++ } |
| 94 | ++ } |
| 95 | ++ """; |
| 96 | ++ golden = """ |
| 97 | ++ package hierbas.del.litoral; |
| 98 | ++ |
| 99 | ++ class Test extends Integer implements Runnable, Serializable { |
| 100 | ++ |
| 101 | ++ public void run() { |
| 102 | ++ if ("foo".contains("bar"))))) ))) |
| 103 | ++ } |
| 104 | ++ } |
| 105 | ++ """; |
| 106 | ++ reformat(doc, content, golden); |
| 107 | ++ |
| 108 | ++ content = """ |
| 109 | ++ package hierbas.del.litoral; |
| 110 | ++ class Test extends Integer implements Runnable, Serializable{ |
| 111 | ++ public void run(){ |
| 112 | ++ if ("foo".contains("bar"))))) ))) |
| 113 | ++ else { |
| 114 | ++ System.out.println("bar2") |
| 115 | ++ } |
| 116 | ++ } |
| 117 | ++ } |
| 118 | ++ """; |
| 119 | ++ golden = """ |
| 120 | ++ package hierbas.del.litoral; |
| 121 | ++ |
| 122 | ++ class Test extends Integer implements Runnable, Serializable { |
| 123 | ++ |
| 124 | ++ public void run() { |
| 125 | ++ if ("foo".contains("bar"))))) ))) |
| 126 | ++ else { |
| 127 | ++ System.out.println("bar2") |
| 128 | ++ } |
| 129 | ++ } |
| 130 | ++ } |
| 131 | ++ """; |
| 132 | ++ reformat(doc, content, golden); |
| 133 | + } |
| 134 | + |
| 135 | + public void testWhile() throws Exception { |
0 commit comments