This is an automated email from the ASF dual-hosted git repository.

neilcsmith pushed a commit to branch delivery
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/delivery by this push:
     new 9892495c62 Fix formatting of try/catch as first statement in switch 
case (GH5323).
     new 38dd456033 Merge pull request #6678 from neilcsmith-net/gh5323
9892495c62 is described below

commit 9892495c62015678a512fef685f18b618b9c73a7
Author: Neil C Smith <neilcsm...@apache.org>
AuthorDate: Thu Nov 9 16:46:53 2023 +0000

    Fix formatting of try/catch as first statement in switch case (GH5323).
---
 .../modules/java/source/save/Reformatter.java      |   6 +-
 .../modules/java/source/save/FormatingTest.java    | 127 +++++++++++++++++++++
 2 files changed, 132 insertions(+), 1 deletion(-)

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 361daf7f43..f123913642 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
@@ -2999,7 +2999,11 @@ public class Reformatter implements ReformatTask {
                         if (stat.getKind() == Tree.Kind.BLOCK) {
                             indent = lastIndent;
                         }
-                        wrapStatement(cs.wrapCaseStatements(), 
CodeStyle.BracesGenerationStyle.LEAVE_ALONE, 1, stat);
+                        if (stat.getKind() == Tree.Kind.TRY) {
+                            wrapTree(cs.wrapCaseStatements(), -1, 1, stat);
+                        } else {
+                            wrapStatement(cs.wrapCaseStatements(), 
CodeStyle.BracesGenerationStyle.LEAVE_ALONE, 1, stat);
+                        }
                     } else {
                         newline();
                         scan(stat, p);
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 016b9c9341..c11be84ffc 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
@@ -2205,6 +2205,133 @@ public class FormatingTest extends NbTestCase {
         preferences.putBoolean("indentCasesFromSwitch", true);
 
     }
+
+    public void testSwitchWithTryExpr() throws Exception {
+        testFile = new File(getWorkDir(), "Test.java");
+        TestUtilities.copyStringToFile(testFile, "");
+        FileObject testSourceFO = FileUtil.toFileObject(testFile);
+        DataObject testSourceDO = DataObject.find(testSourceFO);
+        EditorCookie ec = (EditorCookie) 
testSourceDO.getCookie(EditorCookie.class);
+        final Document doc = ec.openDocument();
+        doc.putProperty(Language.class, JavaTokenId.language());
+
+        String content
+                = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public void taragui(int i) {\n"
+                + "        switch (i) {\n"
+                + "        case 0:\n"
+                + "        try {\n"
+                + "        System.out.println(i);\n"
+                + "        } catch (Exception ex) {\n"
+                + "        System.out.println(ex);\n"
+                + "        }\n"
+                + "        break;\n"
+                + "        case 1:\n"
+                + "        try {\n"
+                + "        System.out.println(i);\n"
+                + "        } catch (Exception ex) {\n"
+                + "        System.out.println(ex);\n"
+                + "        } finally {\n"
+                + "        System.out.println(\"FINALLY\");\n"
+                + "        }\n"
+                + "        break;\n"
+                + "        case 2:\n"
+                + "        try (  StringReader r = new StringReader(\"\")  ) 
{\n"
+                + "        System.out.println(i);\n"
+                + "        } catch (Exception ex) {\n"
+                + "        System.out.println(ex);\n"
+                + "        }\n"
+                + "        break;\n"
+                + "        default:\n"
+                + "        try {\n"
+                + "        System.out.println(\"DEFAULT\");\n"
+                + "        } catch (Exception ex) {\n"
+                + "        System.out.println(ex);\n"
+                + "        }\n"
+                + "        }\n"
+                + "    }\n"
+                + "}\n";
+
+        String golden
+                = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public void taragui(int i) {\n"
+                + "        switch (i) {\n"
+                + "            case 0:\n"
+                + "                try {\n"
+                + "                    System.out.println(i);\n"
+                + "                } catch (Exception ex) {\n"
+                + "                    System.out.println(ex);\n"
+                + "                }\n"
+                + "                break;\n"
+                + "            case 1:\n"
+                + "                try {\n"
+                + "                    System.out.println(i);\n"
+                + "                } catch (Exception ex) {\n"
+                + "                    System.out.println(ex);\n"
+                + "                } finally {\n"
+                + "                    System.out.println(\"FINALLY\");\n"
+                + "                }\n"
+                + "                break;\n"
+                + "            case 2:\n"
+                + "                try (StringReader r = new 
StringReader(\"\")) {\n"
+                + "                    System.out.println(i);\n"
+                + "                } catch (Exception ex) {\n"
+                + "                    System.out.println(ex);\n"
+                + "                }\n"
+                + "                break;\n"
+                + "            default:\n"
+                + "                try {\n"
+                + "                    System.out.println(\"DEFAULT\");\n"
+                + "                } catch (Exception ex) {\n"
+                + "                    System.out.println(ex);\n"
+                + "                }\n"
+                + "        }\n"
+                + "    }\n"
+                + "}\n";
+
+        reformat(doc, content, golden);
+
+        content = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public void taragui(int i) {\n"
+                + "        switch (i) {\n"
+                + "        case 0:\n"
+                + "        int x = i*2;\n"
+                + "        try {\n"
+                + "        System.out.println(x);\n"
+                + "        } catch (Exception ex) {\n"
+                + "        System.out.println(ex);\n"
+                + "        }\n"
+                + "        break;\n"
+                + "        default:\n"
+                + "        System.out.println(\"DEFAULT\");\n"
+                + "        }\n"
+                + "    }\n"
+                + "}\n";
+
+        golden = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public void taragui(int i) {\n"
+                + "        switch (i) {\n"
+                + "            case 0:\n"
+                + "                int x = i * 2;\n"
+                + "                try {\n"
+                + "                    System.out.println(x);\n"
+                + "                } catch (Exception ex) {\n"
+                + "                    System.out.println(ex);\n"
+                + "                }\n"
+                + "                break;\n"
+                + "            default:\n"
+                + "                System.out.println(\"DEFAULT\");\n"
+                + "        }\n"
+                + "    }\n"
+                + "}\n";
+        reformat(doc, content, golden);
+
+    }
+
     public void testRuleSwitch() throws Exception {
         testFile = new File(getWorkDir(), "Test.java");
         TestUtilities.copyStringToFile(testFile,


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to