From 52891c269f1f4fb78bb6031d4deb135ed1c7b92c Mon Sep 17 00:00:00 2001 From: nickreid Date: Tue, 2 Apr 2024 05:47:51 -0700 Subject: [PATCH] Update google-java-format to 1.22 This update adds support for multiline tokens, which fixes several bugs related to multiline strings. --- 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..ba861e2d 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -179,7 +179,7 @@ com.google.googlejavaformat google-java-format - 1.8 + 1.22 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 b01bdc3b..b5c2eaed 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -481,10 +481,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 6ab3c853..e4bc2a10 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 7b6c6321..f2a8670e 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(