From 808a855abd7b4488639196a38a1e27fccaf8baf3 Mon Sep 17 00:00:00 2001 From: John Ayad Date: Fri, 5 Apr 2024 18:25:13 +0100 Subject: [PATCH 1/2] Fix: Properly format switch expressions --- .../javaformat/gradle/FormatDiffTest.java | 1 + .../java/java14/Java14InputAstVisitor.java | 1 + .../java/testdata/ExpressionSwitch.input | 17 +++++++++++++++++ .../java/testdata/ExpressionSwitch.output | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java index 07f4b8e98..08b7ab20e 100644 --- a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java +++ b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java @@ -63,6 +63,7 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines() throws IOExc runCommandInRepo("git", "init"); runCommandInRepo("git", "config", "user.name", "Test User"); runCommandInRepo("git", "config", "user.email", "test-user@palantir.com"); + runCommandInRepo("git", "config", "commit.gpgsign", "false"); runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init"); Path subdir = repo.resolve("subdir"); diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java index db2599b7e..d2ebe1d71 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java @@ -303,6 +303,7 @@ public Void visitCase(CaseTree node, Void unused) { scan(node.getBody(), null); } builder.guessToken(";"); + builder.forcedBreak(minusTwo); break; default: throw new IllegalArgumentException(node.getCaseKind().name()); diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input index bdc4d2b8c..9599cfc4c 100644 --- a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input @@ -41,4 +41,21 @@ class ExpressionSwitch { default -> false; }; } + + public void test1(int y) { + int x = + switch (y) { + case 1 -> 1; + case 2 -> throw new IllegalArgumentException(); + default -> throw new IllegalStateException(); + }; + } + + public void test2(int y) { + int x; + x = switch (y) { + case 1 -> 1; + case 2 -> throw new IllegalArgumentException(); + default -> throw new IllegalStateException();}; + } } diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output index a3c701d27..e8f80b1b2 100644 --- a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output @@ -47,4 +47,22 @@ class ExpressionSwitch { default -> false; }; } + + public void test1(int y) { + int x = + switch (y) { + case 1 -> 1; + case 2 -> throw new IllegalArgumentException(); + default -> throw new IllegalStateException(); + }; + } + + public void test2(int y) { + int x; + x = switch (y) { + case 1 -> 1; + case 2 -> throw new IllegalArgumentException(); + default -> throw new IllegalStateException(); + }; + } } From bf9ae9e50c80c20716c4ca7f2671bf9e89804eb5 Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Fri, 5 Apr 2024 17:26:45 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-1069.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-1069.v2.yml diff --git a/changelog/@unreleased/pr-1069.v2.yml b/changelog/@unreleased/pr-1069.v2.yml new file mode 100644 index 000000000..d391467b2 --- /dev/null +++ b/changelog/@unreleased/pr-1069.v2.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: Properly format switch expressions + links: + - https://github.com/palantir/palantir-java-format/pull/1069