Skip to content

Commit

Permalink
- conversation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DrAlexD committed Jun 19, 2024
1 parent dc3de66 commit f4eaa14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class KdocComments(configRules: List<RulesConfig>) : 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 {
Expand All @@ -218,29 +218,6 @@ class KdocComments(configRules: List<RulesConfig>) : 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,
Expand Down Expand Up @@ -371,7 +348,7 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
appendKdocTagContent(parameterInClassKdoc, parameterName, commentText)

if (isHasWrongTag) {
replaceWrongTagInClassKdoc(kdocBeforeClass, parameterName, isParamTagNeeded)
kdocBeforeClass.replaceWrongTagInClassKdoc(parameterName, isParamTagNeeded)
}

node.removeWithWhiteSpace(prevComment)
Expand Down Expand Up @@ -413,7 +390,7 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
appendKdocTagContent(parameterInClassKdoc, parameterName, createClassKdocTextFromEolComment(prevComment))

if (isHasWrongTag) {
replaceWrongTagInClassKdoc(kdocBeforeClass, parameterName, isParamTagNeeded)
kdocBeforeClass.replaceWrongTagInClassKdoc(parameterName, isParamTagNeeded)
}

node.treeParent.removeChildMergingSurroundingWhitespaces(prevComment.treePrev, prevComment, prevComment.treeNext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)!!)
}

0 comments on commit f4eaa14

Please sign in to comment.