Skip to content

Commit

Permalink
Allow optional break after string-literals (#396)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #396

Reviewed By: hick209

Differential Revision: D45043457

Pulled By: cgrushko

fbshipit-source-id: 63122fdf63723af8b6c4324e7d291755e5bd3fa8
  • Loading branch information
nreid260 authored and facebook-github-bot committed Apr 23, 2023
1 parent 8bde75c commit 1b076d1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,9 @@ class KotlinInputAstVisitor(
}
receiver is KtStringTemplateExpression -> {
val isMultiline = receiver.text.contains('\n')
builder.block(if (isMultiline) expressionBreakIndent else ZERO) {
builder.block(expressionBreakIndent) {
visit(receiver)
if (isMultiline) {
builder.forcedBreak()
}
builder.breakOp(if (isMultiline) Doc.FillMode.FORCED else Doc.FillMode.UNIFIED, "", ZERO)
builder.token(expression.operationSign.value)
visit(expression.selectorExpression)
}
Expand Down
16 changes: 13 additions & 3 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,8 @@ class FormatterTest {
assertFormatted(
"""
|-------------------------
|"Hello %s".format(
| someLongExpression)
|"Hello %s"
| .format(expression)
|"""
.trimMargin(),
deduceMaxWidth = true)
Expand Down Expand Up @@ -6462,7 +6462,17 @@ class FormatterTest {
assertFormatted(
"""
|--------------------------------
|fun f() {
|fun stringFitsButNotMethod() {
| val str1 =
| $TQ Some string $TQ
| .trimIndent()
|
| val str2 =
| $TQ Some string $TQ
| .trimIndent(someArg)
|}
|
|fun stringTooLong() {
| val str1 =
| $TQ
| Some very long string that might mess things up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,8 @@ class GoogleStyleFormatterKtTest {
assertFormatted(
"""
|-------------------------
|"Hello %s".format(
| someLongExpression
|)
|"Hello %s"
| .format(expression)
|"""
.trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
Expand Down
4 changes: 3 additions & 1 deletion core/src/test/java/com/facebook/ktfmt/testutil/KtfmtTruth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ fun assertFormatted(

fun assertThatFormatting(code: String): FormattedCodeSubject {
fun codes(): Subject.Factory<FormattedCodeSubject, String> {
return Subject.Factory { metadata, subject -> FormattedCodeSubject(metadata, subject) }
return Subject.Factory { metadata, subject ->
FormattedCodeSubject(metadata, checkNotNull(subject))
}
}
return Truth.assertAbout(codes()).that(code)
}
Expand Down

0 comments on commit 1b076d1

Please sign in to comment.