From 225d354604690d763a23e70ac5036651bcd2efe8 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Sat, 9 May 2020 03:50:32 +0300 Subject: [PATCH] KT-38829 Add additional check to RemoveRedundantBackticksQuickFix - Identifier's name can change between reporting of inspection and applying a quickfix - ^KT-38829 Fixed --- .../RemoveRedundantBackticksInspection.kt | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt index 67e2d4648a3..e75130307aa 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt @@ -19,7 +19,6 @@ import com.intellij.codeInspection.LocalQuickFix import com.intellij.codeInspection.ProblemDescriptor import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ProblemsHolder -import com.intellij.lang.ASTNode import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import com.intellij.psi.PsiElementVisitor @@ -38,7 +37,7 @@ class RemoveRedundantBackticksInspection : AbstractKotlinInspection() { override fun visitKtElement(element: KtElement) { super.visitKtElement(element) SharedImplUtil.getChildrenOfType(element.node, KtTokens.IDENTIFIER).forEach { - if (isRedundantBackticks(it)) { + if (isRedundantBackticks(it.text)) { registerProblem(holder, it.psi) } } @@ -46,16 +45,6 @@ class RemoveRedundantBackticksInspection : AbstractKotlinInspection() { } } - private fun isKeyword(text: String): Boolean = - text == "yield" || text.all { it == '_' } || (KtTokens.KEYWORDS.types + KtTokens.SOFT_KEYWORDS.types).any { it.toString() == text } - - private fun isRedundantBackticks(node: ASTNode): Boolean { - val text = node.text - if (!(text.startsWith("`") && text.endsWith("`"))) return false - val unquotedText = text.unquote() - return unquotedText.isIdentifier() && !isKeyword(unquotedText) - } - private fun registerProblem(holder: ProblemsHolder, element: PsiElement) { holder.registerProblem( element, @@ -72,7 +61,17 @@ class RemoveRedundantBackticksQuickFix : LocalQuickFix { override fun applyFix(project: Project, descriptor: ProblemDescriptor) { val element = descriptor.psiElement + if (!isRedundantBackticks(element.text)) return val factory = KtPsiFactory(project) - element.replace(factory.createIdentifier(element.text.removePrefix("`").removeSuffix("`"))) + element.replace(factory.createIdentifier(element.text.unquote())) } +} + +private fun isKeyword(text: String): Boolean = + text == "yield" || text.all { it == '_' } || (KtTokens.KEYWORDS.types + KtTokens.SOFT_KEYWORDS.types).any { it.toString() == text } + +private fun isRedundantBackticks(identifier: String): Boolean { + if (!(identifier.startsWith("`") && identifier.endsWith("`"))) return false + val unquotedText = identifier.unquote() + return unquotedText.isIdentifier() && !isKeyword(unquotedText) } \ No newline at end of file