diff --git a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocComments.kt b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocComments.kt index 04007d436f..0fb36399ce 100644 --- a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocComments.kt +++ b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocComments.kt @@ -191,7 +191,7 @@ class KdocComments(configRules: List) : DiktatRule( val warningText = "change `${paramOrPropertySwitchText.first}` tag to `${paramOrPropertySwitchText.second}` tag for <$parameterName> to KDoc" KDOC_NO_CONSTRUCTOR_PROPERTY.warnAndFix(configRules, emitWarn, isFixMode, warningText, node.startOffset, node) { - replaceWrongTagInClassKdoc(kdocBeforeClass, parameterName, isParamTagNeeded) + kdocBeforeClass.replaceWrongTagInClassKdoc(parameterName, isParamTagNeeded) } } } ?: run { @@ -218,29 +218,6 @@ class KdocComments(configRules: List) : DiktatRule( (isTypeParameterNode && configuration.isParamTagsForGenericTypes) } - private fun replaceWrongTagInClassKdoc( - kdocBeforeClass: ASTNode, - parameterName: String, - isParamTagNeeded: Boolean - ) { - val paramOrPropertySwitchText = if (isParamTagNeeded) "@property" to "@param" else "@param" to "@property" - val wrongTagText = "* ${paramOrPropertySwitchText.first} $parameterName" - val replaceText = "* ${paramOrPropertySwitchText.second} $parameterName" - - changeTagInKdoc(kdocBeforeClass, wrongTagText, replaceText) - } - - @Suppress("UnsafeCallOnNullableType") - private fun changeTagInKdoc( - kdocBeforeClass: ASTNode, - wrongTagText: String, - correctTagText: String - ) { - val allKdocText = kdocBeforeClass.text - val newKdocText = allKdocText.replaceFirst(wrongTagText, correctTagText) - kdocBeforeClass.treeParent.replaceChild(kdocBeforeClass, KotlinParser().createNode(newKdocText).findChildByType(KDOC)!!) - } - private fun checkKdocBeforeClass( node: ASTNode, kdocBeforeClass: ASTNode, @@ -371,7 +348,7 @@ class KdocComments(configRules: List) : DiktatRule( appendKdocTagContent(parameterInClassKdoc, parameterName, commentText) if (isHasWrongTag) { - replaceWrongTagInClassKdoc(kdocBeforeClass, parameterName, isParamTagNeeded) + kdocBeforeClass.replaceWrongTagInClassKdoc(parameterName, isParamTagNeeded) } node.removeWithWhiteSpace(prevComment) @@ -413,7 +390,7 @@ class KdocComments(configRules: List) : DiktatRule( appendKdocTagContent(parameterInClassKdoc, parameterName, createClassKdocTextFromEolComment(prevComment)) if (isHasWrongTag) { - replaceWrongTagInClassKdoc(kdocBeforeClass, parameterName, isParamTagNeeded) + kdocBeforeClass.replaceWrongTagInClassKdoc(parameterName, isParamTagNeeded) } node.treeParent.removeChildMergingSurroundingWhitespaces(prevComment.treePrev, prevComment, prevComment.treeNext) diff --git a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/KdocUtils.kt b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/KdocUtils.kt index 6bfa3aeea4..9c6f474d5b 100644 --- a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/KdocUtils.kt +++ b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/KdocUtils.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.kdoc.lexer.KDocTokens +import org.jetbrains.kotlin.kdoc.lexer.KDocTokens.KDOC import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag @@ -77,3 +78,36 @@ inline fun ASTNode.insertTagBefore( kdocSection.addChild(newTag, beforeTagLineStart) consumer(newTag) } + +/** + * This method replaces wrong tag in class KDoc leaving [parameterName] unchanged. + * + * @param parameterName name of class parameter + * @param isParamTagNeeded true, in case we need to change the tag to `@param` + */ +fun ASTNode.replaceWrongTagInClassKdoc( + parameterName: String, + isParamTagNeeded: Boolean +) { + val paramOrPropertySwitchText = if (isParamTagNeeded) "@property" to "@param" else "@param" to "@property" + val wrongTagText = "* ${paramOrPropertySwitchText.first} $parameterName" + val replaceText = "* ${paramOrPropertySwitchText.second} $parameterName" + + this.replaceFirstTextInKdoc(wrongTagText, replaceText) +} + +/** + * This method replaces the first occurrence of text in class KDoc. + * + * @param wrongText text to replace + * @param correctText replacement text + */ +@Suppress("UnsafeCallOnNullableType") +fun ASTNode.replaceFirstTextInKdoc( + wrongText: String, + correctText: String +) { + val allKdocText = this.text + val newKdocText = allKdocText.replaceFirst(wrongText, correctText) + this.treeParent.replaceChild(this, KotlinParser().createNode(newKdocText).findChildByType(KDOC)!!) +}