From 2364f1613d8b809c9c65ae4810e059bece20798c Mon Sep 17 00:00:00 2001 From: nickreid Date: Mon, 22 Apr 2024 06:05:25 -0700 Subject: [PATCH] Update google-java-format to 1.22.0 (#452) Summary: This update adds support for multiline tokens, which fixes several bugs related to multiline strings. Pull Request resolved: https://github.com/facebook/ktfmt/pull/452 Reviewed By: cortinico Differential Revision: D55641796 Pulled By: hick209 fbshipit-source-id: ec0782c0d91ababe537866627c719cd3df7c6b54 --- core/pom.xml | 2 +- .../ktfmt/format/KotlinInputAstVisitor.kt | 3 +- .../facebook/ktfmt/format/FormatterTest.kt | 33 ++++++++++++------- .../format/GoogleStyleFormatterKtTest.kt | 20 +++++++++++ 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 76297494..c57177a3 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -179,7 +179,7 @@ com.google.googlejavaformat google-java-format - 1.8 + 1.22.0 junit diff --git a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt index da62912e..21d93e87 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -479,10 +479,9 @@ class KotlinInputAstVisitor( } } receiver is KtStringTemplateExpression -> { - val isMultiline = receiver.text.contains('\n') builder.block(expressionBreakIndent) { visit(receiver) - builder.breakOp(if (isMultiline) Doc.FillMode.FORCED else Doc.FillMode.UNIFIED, "", ZERO) + builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO) builder.token(expression.operationSign.value) visit(expression.selectorExpression) } diff --git a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt index 203c464a..4e8b5878 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -2795,8 +2795,14 @@ class FormatterTest { assertFormatted( """ |fun doIt(world: String) { - | println(${"\"".repeat(3)}Hello - | world!${"\"".repeat(3)}) + | println( + | ${TQ}Hello + | world!${TQ}) + | println( + | ${TQ}Hello + | world!${TQ}, + | ${TQ}Goodbye + | world!${TQ}) |} |""" .trimMargin()) @@ -2806,14 +2812,18 @@ class FormatterTest { val code = listOf( "fun doIt(world: String) {", - " println(\"\"\"This line has trailing whitespace ", - " world!\"\"\")", - " println(\"\"\"This line has trailing whitespace \$s ", - " world!\"\"\")", - " println(\"\"\"This line has trailing whitespace \${s} ", - " world!\"\"\")", - " println(\"\"\"This line has trailing whitespace \$ ", - " world!\"\"\")", + " println(", + " ${TQ}This line has trailing whitespace ", + " world!${TQ})", + " println(", + " ${TQ}This line has trailing whitespace \$s ", + " world!${TQ})", + " println(", + " ${TQ}This line has trailing whitespace \${s} ", + " world!${TQ})", + " println(", + " ${TQ}This line has trailing whitespace \$ ", + " world!${TQ})", "}", "") .joinToString("\n") @@ -2824,7 +2834,8 @@ class FormatterTest { fun `Consecutive line breaks in multiline strings are preserved`() = assertFormatted( """ - |val x = $TQ + |val x = + | $TQ | | | diff --git a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt index 14bfd386..48ca5d1c 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt @@ -1548,6 +1548,26 @@ class GoogleStyleFormatterKtTest { formattingOptions = Formatter.GOOGLE_FORMAT, deduceMaxWidth = true) + @Test + fun `multiline string literals as function params`() = + assertFormatted( + """ + |fun doIt(world: String) { + | println( + | ${TQ}Hello + | world!${TQ} + | ) + | println( + | ${TQ}Hello + | world!${TQ}, + | ${TQ}Goodbye + | world!${TQ}, + | ) + |} + |""" + .trimMargin(), + formattingOptions = Formatter.GOOGLE_FORMAT) + @Test fun `array-literal in annotation`() = assertFormatted(