diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index d138011f0..cb30b8547 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -2091,14 +2091,17 @@ public Void visitTry(TryTree node, Void unused) { builder.space(); if (!node.getResources().isEmpty()) { token("("); - builder.open(node.getResources().size() > 1 ? plusFour : ZERO); + boolean multiVariable = node.getResources().size() > 1; + builder.open(multiVariable ? plusFour : ZERO); + if (multiVariable) { + builder.forcedBreak(); + } boolean afterFirstToken = false; for (Tree resource : node.getResources()) { if (afterFirstToken) { builder.forcedBreak(); } if (resource instanceof VariableTree variableTree) { - declareOne( DeclarationKind.PARAMETER, fieldAnnotationDirection(variableTree.getModifiers()), @@ -2112,7 +2115,6 @@ public Void visitTry(TryTree node, Void unused) { /* receiverExpression= */ Optional.empty(), /* typeWithDims= */ Optional.empty()); } else { - // TODO(cushon): think harder about what to do with `try (resource1; resource2) {}` scan(resource, null); } if (builder.peekToken().equals(Optional.of(";"))) { @@ -2121,12 +2123,11 @@ public Void visitTry(TryTree node, Void unused) { } afterFirstToken = true; } - if (builder.peekToken().equals(Optional.of(";"))) { - token(";"); - builder.space(); + builder.close(); + if (multiVariable) { + builder.forcedBreak(); } token(")"); - builder.close(); builder.space(); } // An empty try-with-resources body can collapse to "{}" if there are no trailing catch or diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java index 71a286f50..5809118d6 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java @@ -670,6 +670,34 @@ void f() { """); } + @Test + public void multivariableTryWithResources() throws Exception { + String input = + "class Test {\n" + + " void m() {\n" + + " try (var input = Files.newInputStream(Path.of(\"./input\"));\n" + + " var output = Files.newOutputStream(Path.of(\"./output\"));) {\n" + + " output.write(input.read());\n" + + " } catch (IOException _) {\n" + + " }\n" + + " }\n" + + "}\n"; + String expected = + "class Test {\n" + + " void m() {\n" + + " try (\n" + + " var input = Files.newInputStream(Path.of(\"./input\"));\n" + + " var output = Files.newOutputStream(Path.of(\"./output\"));\n" + + " ) {\n" + + " output.write(input.read());\n" + + " } catch (IOException _) {\n" + + " }\n" + + " }\n" + + "}\n"; + + assertThat(new Formatter().formatSource(input)).isEqualTo(expected); + } + @Test public void testI1205() throws Exception { String input = diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21465217.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21465217.output index add19d3db..df207a74d 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21465217.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21465217.output @@ -1,9 +1,10 @@ class B21465217 { void m() { - try (JimfsOutputStream out2 = newOutputStream(false); + try ( + JimfsOutputStream out2 = newOutputStream(false); BufferedOutputStream bout = new BufferedOutputStream(out2); - OutputStreamWriter writer = - new OutputStreamWriter(bout, UTF_8___________________________)) {} + OutputStreamWriter writer = new OutputStreamWriter(bout, UTF_8___________________________) + ) {} try (Writer sourceWriter = env.getFiler().createSourceFile(qualifiedNamezzzzzzzz).openWriter()) { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26159561.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26159561.output index d9ae26904..8aa6a4f53 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26159561.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26159561.output @@ -1,7 +1,9 @@ class B26159561 { { - try (A a = a(); - B b = b()) {} + try ( + A a = a(); + B b = b() + ) {} try (A a = a(); ) {} } } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.input index 5d294f94a..01a63b6d0 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.input @@ -1,4 +1,4 @@ -class TryWtihResources { +class TryWithResources { { try (@A C c = c(); ) {} try (final @A C c = c(); ) {} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.output index 5d294f94a..01a63b6d0 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TryWithResources.output @@ -1,4 +1,4 @@ -class TryWtihResources { +class TryWithResources { { try (@A C c = c(); ) {} try (final @A C c = c(); ) {}