Skip to content

Commit

Permalink
Bugfix. ClassCastException in IndentationRule when blank line is adde…
Browse files Browse the repository at this point in the history
…d after @implNote tag in KDoc (#527)

* bugfix/class-cast-exception-kdoc(#480)
### What's done:
  * Fixed bugs
  • Loading branch information
aktsay6 authored Nov 23, 2020
1 parent 4d17fe0 commit 3888e94
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ class KdocFormatting(private val configRules: List<RulesConfig>) : Rule("kdoc-fo
emitWarn = emit
fileName = node.getRootNode().getFileName()

val declarationTypes = setOf(CLASS, FUN, PROPERTY)

if (node.elementType == KDOC && isKdocNotEmpty(node)) {
checkNoDeprecatedTag(node)
checkEmptyTags(node.kDocTags())
Expand Down Expand Up @@ -278,7 +276,7 @@ class KdocFormatting(private val configRules: List<RulesConfig>) : Rule("kdoc-fo
}
}

@Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION")
@Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION", "ComplexMethod")
private fun checkNewLineAfterSpecialTags(node: ASTNode) {
val presentSpecialTagNodes = node
.getFirstChildWithType(KDOC_SECTION)
Expand All @@ -304,13 +302,13 @@ class KdocFormatting(private val configRules: List<RulesConfig>) : Rule("kdoc-fo
node.removeChild(node.lastChildNode) // KDOC_LEADING_ASTERISK
node.removeChild(node.lastChildNode) // WHITE_SPACE
}
if (node.lastChildNode.elementType != KDOC_LEADING_ASTERISK) {
if (node.treeParent.lastChildNode != node && node.lastChildNode.elementType != KDOC_LEADING_ASTERISK) {
val indent = node
.prevSibling { it.elementType == WHITE_SPACE }
?.text
?.substringAfter('\n')
?.count { it == ' ' } ?: 0
node.addChild(LeafPsiElement(WHITE_SPACE, "\n${" ".repeat(indent)}"), null)
node.addChild(PsiWhiteSpaceImpl("\n${" ".repeat(indent)}"), null)
node.addChild(LeafPsiElement(KDOC_LEADING_ASTERISK, "*"), null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
fixAndCompare("Example3Expected.kt", "Example3Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #4`() {
fixAndCompare("Example4Expected.kt", "Example4Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - should not fail if package is not set`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ class Example {
@Deprecated(message = "Use testNew")
fun test(a: Int): Int = 2 * a
}

class Foo {
/**
* @implNote lorem ipsum
*/
private fun foo() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ class Example {
*/
fun test(a: Int): Int = 2 * a
}

class Foo {
/**
* @implNote lorem ipsum
*/
private fun foo() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class SpecialTagsInKdoc {
* @implSpec bar
*
* @implNote baz
*
*/
fun test() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ class Example {
}
}

class Foo {
/**
* @implNote lorem ipsum
*/
private fun foo() {}
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ class Example {
foo()
}
}


class Foo {
/**
* @implNote lorem ipsum
*/
private fun foo() {}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cqfn.diktat

class SpecialTagsInKdoc {
/**
* Empty function to test KDocs
* @apiNote foo
*
* @implSpec bar
*
* @implNote baz
*
* @return
*/
fun test() = Unit
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cqfn.diktat

class SpecialTagsInKdoc {

/**
* Empty function to test KDocs
* @apiNote foo
* @implSpec bar
*
*
* @implNote baz
*/
fun test() = Unit
}

0 comments on commit 3888e94

Please sign in to comment.