Skip to content

Commit

Permalink
Fix issue of missing whitespaces after link tags in KDoc
Browse files Browse the repository at this point in the history
Summary: This is a smarter way to deal with KDoc, ignore exactly one space after an asterisk. Otherwise keep them.

Reviewed By: cgrushko

Differential Revision: D19844836

fbshipit-source-id: 1fefe459354e1f944f68d28af53e0006de2d7a14
  • Loading branch information
strulovich authored and facebook-github-bot committed Feb 12, 2020
1 parent 733c7d9 commit 0ef289e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 9 additions & 8 deletions core/src/main/java/com/facebook/ktfmt/kdoc/KDocFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ object KDocFormatter {
var previousType: IElementType? = null
while (kDocLexer.tokenType != null) {
val tokenType = kDocLexer.tokenType
val tokenText = kDocLexer.tokenText
val tokenText = with(kDocLexer.tokenText) {
if (previousType == KDocTokens.LEADING_ASTERISK && first() == ' ') substring(1) else this
}

if (tokenType === KDocTokens.START) {
newTokensBuilder.add(Token(BEGIN_KDOC, tokenText))
} else if (tokenType === KDocTokens.LEADING_ASTERISK) {
// ignore
// Ignore, no need to output anything
} else if (tokenType === KDocTokens.END) {
newTokensBuilder.add(Token(END_KDOC, tokenText))
} else if (tokenType === KDocTokens.TEXT) {
val trimmedText = tokenText.trim { it <= ' ' }
if (!trimmedText.isEmpty()) {
val words = trimmedText.split(" +".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (!tokenText.isEmpty()) {
val words = tokenText.split(" +".toRegex()).dropLastWhile { it.isEmpty() }
var first = true
for (word in words) {
if (first) {
Expand All @@ -82,10 +84,9 @@ object KDocFormatter {
}
}
} else if (tokenType === KDocTokens.TAG_NAME) {
newTokensBuilder.add(Token(LITERAL, tokenText.trim { it <= ' ' }))
newTokensBuilder.add(Token(LITERAL, tokenText))
} else if (tokenType === KDocTokens.CODE_BLOCK_TEXT) {
val trimmedFirstSpaceText = if (tokenText.first() == ' ') tokenText.substring(1) else tokenText
newTokensBuilder.add(Token(LITERAL, trimmedFirstSpaceText))
newTokensBuilder.add(Token(LITERAL, tokenText))
} else if (tokenType === KDocTokens.MARKDOWN_INLINE_LINK) {
newTokensBuilder.add(Token(LITERAL, tokenText))
} else if (tokenType === KDocTokens.MARKDOWN_LINK) {
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/FormatterKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,13 @@ class FormatterKtTest {
|class MyClass {}
|""".trimMargin())

@Test
fun `handle KDoc with link reference`() = assertFormatted(
"""
|/** Doc line with a reference to [AnotherClass] in the middle of a sentence */
|class MyClass {}
|""".trimMargin())

/** Verifies the given code passes through formatting, and stays the same at the end */
private fun assertFormatted(code: String, maxWidth: Int = DEFAULT_MAX_WIDTH) {
assertThatFormatting(code, maxWidth).isEqualTo(code)
Expand Down

0 comments on commit 0ef289e

Please sign in to comment.