Skip to content

Commit

Permalink
Fix explicit ctor formatting; and KDoc's ``` followed by more text (#238
Browse files Browse the repository at this point in the history
)

Summary:
Fix for #235 and #221

Pull Request resolved: #238

Test Plan: ktfmt-diff: D28778163

Differential Revision: D28778136

Pulled By: cgrushko

fbshipit-source-id: 67ab94fd876a0030cff8f9969c819882da590bf2
  • Loading branch information
bethcutler authored and facebook-github-bot committed Jun 6, 2021
1 parent 57a2a81 commit e62ef21
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,11 @@ open class KotlinInputAstVisitorBase(
builder.token(":")
if (parameterList?.parameters.isNullOrEmpty()) {
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
builder.block(expressionBreakIndent) { typeOrDelegationCall.accept(this) }
} else {
builder.space()
builder.block(expressionBreakNegativeIndent) { typeOrDelegationCall.accept(this) }
}
builder.block(expressionBreakNegativeIndent) { typeOrDelegationCall.accept(this) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object KDocFormatter {
// END_KDOC properly. We want to recover in such cases
if (word == "*/") {
tokens.add(Token(END_KDOC, word))
} else if (word == "```") {
} else if (word.startsWith("```")) {
tokens.add(Token(CODE_BLOCK_MARKER, word))
} else {
tokens.add(Token(LITERAL, word))
Expand Down
31 changes: 30 additions & 1 deletion core/src/test/java/com/facebook/ktfmt/FormatterKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,21 @@ class FormatterKtTest {
|""".trimMargin(),
deduceMaxWidth = true)

@Test
fun `a secondary constructor with no arguments passed to delegate`() =
assertFormatted(
"""
|--------------------------------------------------
|data class Foo {
| constructor() :
| this(
| Foo.createSpeciallyDesignedParameter(),
| Foo.createSpeciallyDesignedParameter(),
| )
|}
|""".trimMargin(),
deduceMaxWidth = true)

@Test
fun `secondary constructor with param list that fits in one line, with delegate`() =
assertFormatted(
Expand Down Expand Up @@ -3744,6 +3759,20 @@ class FormatterKtTest {
|class MyClass {}
|""".trimMargin())

@Test
fun `handle KDoc with tagged code examples`() =
assertFormatted(
"""
|/**
| * ```kotlin
| * fun main(args: Array<String>) {
| * println("Hello World!")
| * }
| * ```
| */
|class MyClass {}
|""".trimMargin())

@Test
fun `handle stray code markers in lines and produce stable output`() {
val code =
Expand Down Expand Up @@ -4346,7 +4375,7 @@ class FormatterKtTest {
|
|
|class C {}
|
|
|
|class C {}
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,22 @@ class GoogleStyleFormatterKtTest {
formattingOptions = GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `a secondary constructor with no arguments passed to delegate`() =
assertFormatted(
"""
|--------------------------------------------------
|data class Foo {
| constructor() :
| this(
| Foo.createSpeciallyDesignedParameter(),
| Foo.createSpeciallyDesignedParameter(),
| )
|}
|""".trimMargin(),
formattingOptions = GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `handle trailing commas (function calls)`() =
assertFormatted(
Expand Down

0 comments on commit e62ef21

Please sign in to comment.