Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing replaceWithText() method with rawReplaceWithText() #939

Merged
merged 3 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
if (node.isConstant()) {
if (!variableName.text.isUpperSnakeCase() && variableName.text.length > 1) {
CONSTANT_UPPERCASE.warnAndFix(configRules, emitWarn, isFix, variableName.text, variableName.startOffset, node) {
(variableName as LeafPsiElement).replaceWithText(variableName.text.toUpperSnakeCase())
(variableName as LeafPsiElement).rawReplaceWithText(variableName.text.toUpperSnakeCase())
}
}
} else if (variableName.text != "_" && !variableName.text.isLowerCamelCase()) {
Expand All @@ -194,21 +194,21 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
.parent({ it.elementType == FILE })
?.findAllVariablesWithUsages { it.name == variableName.text }
?.flatMap { it.value.toList() }
?.forEach { (it.node.firstChildNode as LeafPsiElement).replaceWithText(correctVariableName) }
(variableName as LeafPsiElement).replaceWithText(correctVariableName)
?.forEach { (it.node.firstChildNode as LeafPsiElement).rawReplaceWithText(correctVariableName) }
(variableName as LeafPsiElement).rawReplaceWithText(correctVariableName)
}
}
}

// need to get new node in case we have already converted the case before (and replaced the child node)
// we need to recalculate it twice, because nodes could have been changed by "replaceWithText" function
// we need to recalculate it twice, because nodes could have been changed by "rawReplaceWithText" function
namesOfVariables = extractVariableIdentifiers(node)
namesOfVariables
.forEach { variableName ->
// generally, variables with prefixes are not allowed (like mVariable, xCode, iValue)
if (variableName.text.hasPrefix()) {
VARIABLE_HAS_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, variableName.text, variableName.startOffset, node) {
(variableName as LeafPsiElement).replaceWithText(variableName.text.removePrefix())
(variableName as LeafPsiElement).rawReplaceWithText(variableName.text.removePrefix())
}
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
val className: ASTNode = node.getIdentifierName() ?: return emptyList()
if (!(className.text.isPascalCase())) {
CLASS_NAME_INCORRECT.warnAndFix(configRules, emitWarn, isFixMode, className.text, className.startOffset, className) {
(className as LeafPsiElement).replaceWithText(className.text.toPascalCase())
(className as LeafPsiElement).rawReplaceWithText(className.text.toPascalCase())
}
}

Expand All @@ -296,7 +296,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
if (superClassName != null && hasExceptionSuffix(superClassName) && !hasExceptionSuffix(classNameNode.text)) {
EXCEPTION_SUFFIX.warnAndFix(configRules, emitWarn, isFixMode, classNameNode.text, classNameNode.startOffset, classNameNode) {
// FixMe: need to add tests for this
(classNameNode as LeafPsiElement).replaceWithText(classNameNode.text + "Exception")
(classNameNode as LeafPsiElement).rawReplaceWithText(classNameNode.text + "Exception")
}
}
}
Expand All @@ -312,7 +312,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
val objectName: ASTNode = node.getIdentifierName() ?: return emptyList()
if (!objectName.text.isPascalCase()) {
OBJECT_NAME_INCORRECT.warnAndFix(configRules, emitWarn, isFixMode, objectName.text, objectName.startOffset, objectName) {
(objectName as LeafPsiElement).replaceWithText(objectName.text.toPascalCase())
(objectName as LeafPsiElement).rawReplaceWithText(objectName.text.toPascalCase())
}
}
return listOf(objectName)
Expand Down Expand Up @@ -341,7 +341,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
if (!validator(value.text)) {
ENUM_VALUE.warnAndFix(configRules, emitWarn, isFixMode, value.text, value.startOffset, value) {
// FixMe: add tests for this
(value as LeafPsiElement).replaceWithText(autofix(value.text))
(value as LeafPsiElement).rawReplaceWithText(autofix(value.text))
}
}

Expand All @@ -368,7 +368,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
if (!functionName.text.isLowerCamelCase()) {
FUNCTION_NAME_INCORRECT_CASE.warnAndFix(configRules, emitWarn, isFixMode, functionName.text, functionName.startOffset, functionName) {
// FixMe: add tests for this
(functionName as LeafPsiElement).replaceWithText(functionName.text.toLowerCamelCase())
(functionName as LeafPsiElement).rawReplaceWithText(functionName.text.toLowerCamelCase())
}
}

Expand Down Expand Up @@ -398,7 +398,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(

if (!aliasName.text.isPascalCase()) {
TYPEALIAS_NAME_INCORRECT_CASE.warnAndFix(configRules, emitWarn, isFixMode, aliasName.text, aliasName.startOffset, aliasName) {
(aliasName as LeafPsiElement).replaceWithText(aliasName.text.toPascalCase())
(aliasName as LeafPsiElement).rawReplaceWithText(aliasName.text.toPascalCase())
}
}
return listOf(aliasName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
private fun correctPackageWordSeparatorsUsed(word: ASTNode) {
if (word.text.contains("_") && !isExceptionForUnderscore(word.text)) {
INCORRECT_PACKAGE_SEPARATOR.warnAndFix(configRules, emitWarn, isFixMode, word.text, word.startOffset, word) {
(word as LeafPsiElement).replaceWithText(word.text.replace("_", ""))
(word as LeafPsiElement).rawReplaceWithText(word.text.replace("_", ""))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
// Triggers when there is a copyright, but its year is not updated.
if (!isMissingCopyright && copyrightWithCorrectYear.isNotEmpty()) {
WRONG_COPYRIGHT_YEAR.warnAndFix(configRules, emitWarn, isFixMode, "year should be $curYear", node.startOffset, node) {
(headerComment as LeafElement).replaceWithText(headerComment.text.replace(copyrightText, copyrightWithCorrectYear))
(headerComment as LeafElement).rawReplaceWithText(headerComment.text.replace(copyrightText, copyrightWithCorrectYear))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
} else {
if (node.treePrev.numNewLines() == 1 || node.treePrev.numNewLines() > 2) {
WRONG_NEWLINES_AROUND_KDOC.warnAndFix(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) {
(node.treePrev as LeafPsiElement).replaceWithText("\n\n")
(node.treePrev as LeafPsiElement).rawReplaceWithText("\n\n")
}
}
}
Expand All @@ -198,7 +198,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
"There should be 0 space(s) before comment text, but are ${node.treePrev.text.count { it == ' ' }} in ${node.text}",
node.startOffset, node) {
if (node.treePrev.elementType == WHITE_SPACE) {
(node.treePrev as LeafPsiElement).replaceWithText("\n")
(node.treePrev as LeafPsiElement).rawReplaceWithText("\n")
} else {
node.treeParent.addChild(PsiWhiteSpaceImpl("\n"), node)
}
Expand All @@ -214,7 +214,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
// if there are too many spaces before comment
COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode,
"There should be ${configuration.maxSpacesBeforeComment} space(s) before comment text, but there are too many in ${node.text}", node.startOffset, node) {
(node.treePrev as LeafPsiElement).replaceWithText(" ".repeat(configuration.maxSpacesBeforeComment))
(node.treePrev as LeafPsiElement).rawReplaceWithText(" ".repeat(configuration.maxSpacesBeforeComment))
}
}
}
Expand Down Expand Up @@ -265,8 +265,8 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
val commentText = node.text.drop(2).trim()

when (node.elementType) {
EOL_COMMENT -> (node as LeafPsiElement).replaceWithText("// $commentText")
BLOCK_COMMENT -> (node as LeafPsiElement).replaceWithText("/* $commentText")
EOL_COMMENT -> (node as LeafPsiElement).rawReplaceWithText("// $commentText")
BLOCK_COMMENT -> (node as LeafPsiElement).rawReplaceWithText("/* $commentText")
KDOC -> {
node.findAllDescendantsWithSpecificType(KDOC_TEXT).forEach {
modifyKdocText(it, configuration)
Expand All @@ -283,7 +283,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
if (!node.text.startsWith(" ".repeat(configuration.maxSpacesInComment))) {
val commentText = node.text.trim()
val indent = " ".repeat(configuration.maxSpacesInComment)
(node as LeafPsiElement).replaceWithText("$indent$commentText")
(node as LeafPsiElement).rawReplaceWithText("$indent$commentText")
}
}

Expand All @@ -303,7 +303,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
} else if (node.treeParent.elementType != FILE &&
(node.treeParent.treePrev.numNewLines() == 1 || node.treeParent.treePrev.numNewLines() > 2)) {
WRONG_NEWLINES_AROUND_KDOC.warnAndFix(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) {
(node.treeParent.treePrev as LeafPsiElement).replaceWithText("\n\n")
(node.treeParent.treePrev as LeafPsiElement).rawReplaceWithText("\n\n")
}
}
}
Expand All @@ -312,7 +312,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
if (node.treePrev.isWhiteSpace() &&
(node.treePrev.numNewLines() > 1 || node.treePrev.numNewLines() == 0)) {
FIRST_COMMENT_NO_BLANK_LINE.warnAndFix(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) {
(node.treePrev as LeafPsiElement).replaceWithText("\n")
(node.treePrev as LeafPsiElement).rawReplaceWithText("\n")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
propertyInClassKdoc?.let {
if (propertyInClassKdoc.hasChildOfType(KDOC_TEXT)) {
val kdocText = propertyInClassKdoc.findChildByType(KDOC_TEXT)!!
(kdocText as LeafPsiElement).replaceWithText("${kdocText.text} ${prevComment.text}")
(kdocText as LeafPsiElement).rawReplaceWithText("${kdocText.text} ${prevComment.text}")
} else {
propertyInClassKdoc.addChild(LeafPsiElement(KDOC_TEXT, prevComment.text), null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AnnotationNewLineRule(configRules: List<RulesConfig>) : DiktatRule(
.treePrev
.isWhiteSpace()
if (hasSpaceBeforeGrandparent) {
(node.treeParent.treeParent.treePrev as LeafPsiElement).replaceWithText("\n")
(node.treeParent.treeParent.treePrev as LeafPsiElement).rawReplaceWithText("\n")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class BlockStructureBraces(configRules: List<RulesConfig>) : DiktatRule(
if (newNode.elementType != WHITE_SPACE) {
newNode.treeParent.addChild(PsiWhiteSpaceImpl("\n"), newNode)
} else {
(newNode as LeafPsiElement).replaceWithText("\n")
(newNode as LeafPsiElement).rawReplaceWithText("\n")
}
}
}
Expand All @@ -221,7 +221,7 @@ class BlockStructureBraces(configRules: List<RulesConfig>) : DiktatRule(
if (space.elementType != WHITE_SPACE) {
node.addChild(PsiWhiteSpaceImpl(" "), node.findChildByType(keyword))
} else {
(space as LeafPsiElement).replaceWithText(" ")
(space as LeafPsiElement).rawReplaceWithText(" ")
}
}
}
Expand All @@ -245,7 +245,7 @@ class BlockStructureBraces(configRules: List<RulesConfig>) : DiktatRule(
if (space.elementType != WHITE_SPACE) {
node.addChild(PsiWhiteSpaceImpl("\n"), node.findChildByType(RBRACE))
} else {
(space as LeafPsiElement).replaceWithText("\n")
(space as LeafPsiElement).rawReplaceWithText("\n")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ConsecutiveSpacesRule(configRules: List<RulesConfig>) : DiktatRule(

private fun ASTNode.hasEolComment(): Boolean = this.treeNext.elementType == EOL_COMMENT

private fun ASTNode.squeezeSpaces() = (this as LeafElement).replaceWithText(" ")
private fun ASTNode.squeezeSpaces() = (this as LeafElement).rawReplaceWithText(" ")

/**
* [RuleConfiguration] for consecutive spaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "different style for empty block",
node.startOffset, node) {
if (space.elementType == WHITE_SPACE) {
(space.treeNext as LeafPsiElement).replaceWithText("\n")
(space.treeNext as LeafPsiElement).rawReplaceWithText("\n")
} else {
node.addChild(PsiWhiteSpaceImpl("\n"), space.treeNext)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LongNumericalValuesSeparatedRule(configRules: List<RulesConfig>) : DiktatR
resultRealPart.append(chunks.joinToString(separator = "_") { it.reversed() })

resultRealPart.append(nodeSuffix(node.text))
(node as LeafPsiElement).replaceWithText(resultRealPart.toString())
(node as LeafPsiElement).rawReplaceWithText(resultRealPart.toString())
}

private fun fixFloatConstantPart(
Expand Down Expand Up @@ -82,7 +82,7 @@ class LongNumericalValuesSeparatedRule(configRules: List<RulesConfig>) : DiktatR
resultFractionalPart.append(fractionalNumber).append(nodeSuffix(fractionalPart))
}

(node as LeafPsiElement).replaceWithText(resultRealPart.append(resultFractionalPart).toString())
(node as LeafPsiElement).rawReplaceWithText(resultRealPart.append(resultFractionalPart).toString())
}

private fun nodePrefix(nodeText: String) = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class FileStructureRule(configRules: List<RulesConfig>) : DiktatRule(
if (elementType == WHITE_SPACE && text.count { it == '\n' } != 2) {
FILE_NO_BLANK_LINE_BETWEEN_BLOCKS.warnAndFix(configRules, emitWarn, isFixMode, astNode.text.lines().first(),
astNode.startOffset, astNode) {
(this as LeafPsiElement).parent.node.replaceChild(this, PsiWhiteSpaceImpl("\n\n${text.replace("\n", "")}"))
(this as LeafPsiElement).rawReplaceWithText("\n\n${text.replace("\n", "")}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}
.forEach {
WRONG_INDENTATION.warnAndFix(configRules, emitWarn, isFixMode, "tabs are not allowed for indentation", it.startOffset + it.text.indexOf('\t'), it) {
(it as LeafPsiElement).replaceWithText(it.text.replace("\t", " ".repeat(configuration.indentationSize)))
(it as LeafPsiElement).rawReplaceWithText(it.text.replace("\t", " ".repeat(configuration.indentationSize)))
}
}
return isFixMode // true if we changed all tabs to spaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.nextCodeSibling
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevCodeSibling
import com.pinterest.ktlint.core.ast.prevSibling
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(
val isEol = node.textContains('\n') || node.psi.parentsWithSelf.all { it.nextSibling == null }
if (hasSpaces && isEol) {
WRONG_WHITESPACE.warnAndFix(configRules, emitWarn, isFixMode, "there should be no spaces in the end of line", node.startOffset, node) {
(node as LeafElement).replaceWithText(node.text.trimStart(' '))
(node as LeafElement).rawReplaceWithText(node.text.trimStart(' '))
}
}
}
Expand Down Expand Up @@ -395,7 +395,7 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(

private fun ASTNode.leaveSingleWhiteSpace() {
if (treeNext.elementType == WHITE_SPACE) {
(treeNext as LeafElement).replaceWithText(" ")
(treeNext as LeafElement).rawReplaceWithText(" ")
} else {
treeParent.addChild(PsiWhiteSpaceImpl(" "), treeNext)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ fun ASTNode.isVarProperty() =
* Replaces text of [this] node with lowercase text
*/
fun ASTNode.toLower() {
(this as LeafPsiElement).replaceWithText(this.text.lowercase(Locale.getDefault()))
(this as LeafPsiElement).rawReplaceWithText(this.text.lowercase(Locale.getDefault()))
}

/**
Expand Down Expand Up @@ -537,7 +537,7 @@ fun ASTNode.leaveOnlyOneNewLine() = leaveExactlyNumNewLines(1)
*/
fun ASTNode.leaveExactlyNumNewLines(num: Int) {
require(this.elementType == WHITE_SPACE)
(this as LeafPsiElement).replaceWithText("${"\n".repeat(num)}${this.text.replace("\n", "")}")
(this as LeafPsiElement).rawReplaceWithText("${"\n".repeat(num)}${this.text.replace("\n", "")}")
}

/**
Expand All @@ -549,7 +549,7 @@ fun ASTNode.leaveExactlyNumNewLines(num: Int) {
*/
fun ASTNode.appendNewlineMergingWhiteSpace(whiteSpaceNode: ASTNode?, beforeNode: ASTNode?) {
if (whiteSpaceNode != null && whiteSpaceNode.elementType == WHITE_SPACE) {
(whiteSpaceNode as LeafPsiElement).replaceWithText("\n${whiteSpaceNode.text}")
(whiteSpaceNode as LeafPsiElement).rawReplaceWithText("\n${whiteSpaceNode.text}")
} else {
addChild(PsiWhiteSpaceImpl("\n"), beforeNode)
}
Expand Down