From 1fe678942551b446d589c0fe0ad5319ccf17ffa3 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 12 Sep 2023 07:03:59 -0700 Subject: [PATCH] Handle `final var` lambda variables The start position of `final` variable is apparently after the token for `final`, this works around that by inlining a call to `visitVariable` and dropping the `sync` call that asserts the start position of the current node matches the next token. Fixes https://github.com/google/google-java-format/issues/959 PiperOrigin-RevId: 564710136 --- .../google/googlejavaformat/java/JavaInputAstVisitor.java | 5 ++++- .../com/google/googlejavaformat/java/testdata/I959.input | 5 +++++ .../com/google/googlejavaformat/java/testdata/I959.output | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output 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 394d396d9..89c944c5b 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -1250,7 +1250,10 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) { token(","); builder.breakOp(" "); } - scan(parameter, null); + visitVariables( + ImmutableList.of(parameter), + DeclarationKind.NONE, + fieldAnnotationDirection(parameter.getModifiers())); first = false; } if (parens) { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input new file mode 100644 index 000000000..0660079d7 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input @@ -0,0 +1,5 @@ +class I959 { + public void test() { + new File(".").listFiles((final var dir, final var name) -> true); + } +} \ No newline at end of file diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output new file mode 100644 index 000000000..76a07f48b --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output @@ -0,0 +1,5 @@ +class I959 { + public void test() { + new File(".").listFiles((final var dir, final var name) -> true); + } +}