Skip to content

fix for #396 - formatting issue when there is erroneous if tree #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
patches/8442-draft.diff
patches/8460-draft.diff
patches/8470-draft.diff
patches/8484.diff
patches/disable-error-notification.diff
patches/mvn-sh.diff
patches/project-marker-jdk.diff
Expand Down
135 changes: 135 additions & 0 deletions patches/8484.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
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
index 5fb34c754a..247dda8b82 100644
--- a/ide/editor.document/src/org/netbeans/api/editor/document/LineDocumentUtils.java
+++ b/ide/editor.document/src/org/netbeans/api/editor/document/LineDocumentUtils.java
@@ -30,7 +30,6 @@ import org.netbeans.lib.editor.util.swing.DocumentUtilities;
import org.netbeans.modules.editor.document.DocumentServices;
import org.netbeans.modules.editor.document.TextSearchUtils;
import org.netbeans.modules.editor.document.implspi.CharClassifier;
-import org.netbeans.modules.editor.lib2.AcceptorFactory;
import org.netbeans.spi.editor.document.DocumentFactory;
import org.openide.util.Lookup;

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
index 0e44783168..3202b2ac97 100644
--- 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
@@ -2565,7 +2565,36 @@ public class Reformatter implements ReformatTask {
StatementTree elseStat = node.getElseStatement();
CodeStyle.BracesGenerationStyle redundantIfBraces = cs.redundantIfBraces();
int eoln = findNewlineAfterStatement(node);
- if ((elseStat != null && redundantIfBraces == CodeStyle.BracesGenerationStyle.ELIMINATE && danglingElseChecker.hasDanglingElse(node.getThenStatement())) ||
+ Boolean hasErrThenStatement = node.getThenStatement() instanceof ExpressionStatementTree thenStatement
+ && thenStatement.getExpression() instanceof ErroneousTree;
+ if (hasErrThenStatement) {
+ if (getCurrentPath().getParentPath().getLeaf() instanceof BlockTree parentStTree) {
+ boolean isPreviousIfTree = false;
+ int endPositionOfErrThenStatement = endPos;
+ for (StatementTree statement : parentStTree.getStatements()) {
+ if (isPreviousIfTree) {
+ int startPositionOfNextErrorStatement = (int) sp.getStartPosition(getCurrentPath().getCompilationUnit(), statement);
+ endPositionOfErrThenStatement = startPositionOfNextErrorStatement;
+ break;
+ } else if (statement == node) {
+ isPreviousIfTree = true;
+ endPositionOfErrThenStatement = (int) sp.getEndPosition(getCurrentPath().getCompilationUnit(), parentStTree) - 1;
+ }
+
+ }
+ if (isPreviousIfTree) {
+ while (tokens.offset() <= endPositionOfErrThenStatement && endPositionOfErrThenStatement != -1) {
+ tokens.moveNext();
+ }
+ tokens.movePrevious();
+ if (endPositionOfErrThenStatement != -1) {
+ endPos = endPositionOfErrThenStatement;
+ }
+ }
+ }
+ }
+
+ if (hasErrThenStatement || (elseStat != null && redundantIfBraces == CodeStyle.BracesGenerationStyle.ELIMINATE && danglingElseChecker.hasDanglingElse(node.getThenStatement())) ||
(redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS))) {
redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
}
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
index 8797b9111b..d5962a9ba3 100644
--- 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
@@ -1556,6 +1556,76 @@ public class FormatingTest extends NbTestCase {
preferences.putBoolean("specialElseIf", false);
reformat(doc, content, golden);
preferences.putBoolean("specialElseIf", true);
+
+ content = """
+ package hierbas.del.litoral;
+ class Test extends Integer implements Runnable, Serializable{
+ public void run(){
+ if ("foo".contains("bar"))))) ))) {
+ System.out.println("bar");
+ }
+ }
+ }
+ """;
+ golden = """
+ package hierbas.del.litoral;
+
+ class Test extends Integer implements Runnable, Serializable {
+
+ public void run() {
+ if ("foo".contains("bar"))))) ))) {
+ System.out.println("bar");
+ }
+ }
+ }
+ """;
+ reformat(doc, content, golden);
+
+ content = """
+ package hierbas.del.litoral;
+ class Test extends Integer implements Runnable, Serializable{
+ public void run(){
+ if ("foo".contains("bar"))))) )))
+ }
+ }
+ """;
+ golden = """
+ package hierbas.del.litoral;
+
+ class Test extends Integer implements Runnable, Serializable {
+
+ public void run() {
+ if ("foo".contains("bar"))))) )))
+ }
+ }
+ """;
+ reformat(doc, content, golden);
+
+ content = """
+ package hierbas.del.litoral;
+ class Test extends Integer implements Runnable, Serializable{
+ public void run(){
+ if ("foo".contains("bar"))))) )))
+ else {
+ System.out.println("bar2")
+ }
+ }
+ }
+ """;
+ golden = """
+ package hierbas.del.litoral;
+
+ class Test extends Integer implements Runnable, Serializable {
+
+ public void run() {
+ if ("foo".contains("bar"))))) )))
+ else {
+ System.out.println("bar2")
+ }
+ }
+ }
+ """;
+ reformat(doc, content, golden);
}

public void testWhile() throws Exception {