Skip to content

Commit

Permalink
Do not delete blank lines in KDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dingemans committed Feb 24, 2022
1 parent e1674f8 commit 40f9caa
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Added

### Fixed
- Do not delete blank lines in KDoc (no-trailing-spaces) ([#1376](https://github.com/pinterest/ktlint/issues/1376))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ class NoTrailingSpacesRule : Rule("no-trailing-spaces") {
) {
if (node.isPartOfKDoc()) {
if (node.elementType == WHITE_SPACE && node.hasTrailingSpacesBeforeNewline()) {
emit(node.startOffset, "Trailing space(s)", true)
val offsetOfSpaceBeforeNewlineInText = node.text.indexOf(" \n")
val offsetOfFirstSpaceBeforeNewlineInText =
node
.text
.take(offsetOfSpaceBeforeNewlineInText)
.dropLastWhile { it == ' ' }
.length
emit(node.startOffset + offsetOfFirstSpaceBeforeNewlineInText, "Trailing space(s)", true)
if (autoCorrect) {
node.removeTrailingSpacesBeforeNewline()
}
Expand Down Expand Up @@ -57,18 +64,20 @@ class NoTrailingSpacesRule : Rule("no-trailing-spaces") {
private fun ASTNode.isPartOfKDoc() = parent({ it.psi is KDoc }, strict = false) != null

private fun ASTNode.hasTrailingSpacesBeforeNewline() =
text.contains(
regex = Regex("\\s+\\n")
)
text.contains(SPACE_OR_TAB_BEFORE_NEWLINE_REGEX)

private fun ASTNode.removeTrailingSpacesBeforeNewline() {
val newText = text.replace(
regex = Regex("\\s+\\n"),
regex = SPACE_OR_TAB_BEFORE_NEWLINE_REGEX,
replacement = "\n"
)
(this as LeafPsiElement).replaceWithText(newText)
}

private fun String.hasTrailingSpace() =
takeLast(1) == " "

private companion object {
val SPACE_OR_TAB_BEFORE_NEWLINE_REGEX = Regex("[ ]+\\n")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,52 @@ class NoTrailingSpacesRuleTest {
)
)
}

@Test
fun `Issue 1376 - trailing spaces should not delete blank line inside kdoc`() {
val code =
"""
/**
Paragraph 1 which should be followed by a blank line.
Paragraph 2 which should have a blank line before it.
*/
class MyClass
""".trimIndent()
assertThat(NoTrailingSpacesRule().lint(code)).isEmpty()
assertThat(NoTrailingSpacesRule().format(code)).isEqualTo(code)
}

@Test
fun `Issue 1376 - trailing spaces should be removed from blank line inside kdoc`() {
val code =
"""
/**
Paragraph 1 which should be followed by a blank line.
${" "}
${" "}
Paragraph 2 which should have a blank line before it.
*/
class MyClass
""".trimIndent()
val formattedCode =
"""
/**
Paragraph 1 which should be followed by a blank line.
Paragraph 2 which should have a blank line before it.
*/
class MyClass
""".trimIndent()
assertThat(
NoTrailingSpacesRule().format(code)
).isEqualTo(formattedCode)
assertThat(
NoTrailingSpacesRule().lint(code)
).containsExactly(
LintError(3, 1, "no-trailing-spaces", "Trailing space(s)")
)
}
}

0 comments on commit 40f9caa

Please sign in to comment.