Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for formatting on tagged code samples in KDoc. #238

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1862,6 +1862,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 @@ -3716,6 +3731,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 @@ -4318,7 +4347,7 @@ class FormatterKtTest {
|
|
|class C {}
|
|
|
|class C {}
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,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