Skip to content

Commit

Permalink
Update google-java-format to 1.22
Browse files Browse the repository at this point in the history
This update adds support for multiline tokens, which fixes several bugs related to multiline strings.
  • Loading branch information
nreid260 committed Apr 2, 2024
1 parent 7016dc7 commit 52891c2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
<version>1.8</version>
<version>1.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
33 changes: 22 additions & 11 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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")
Expand All @@ -2824,7 +2834,8 @@ class FormatterTest {
fun `Consecutive line breaks in multiline strings are preserved`() =
assertFormatted(
"""
|val x = $TQ
|val x =
| $TQ
|
|
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 52891c2

Please sign in to comment.