KT-38829 Add additional check to RemoveRedundantBackticksQuickFix
- Identifier's name can change between reporting of inspection and applying a quickfix - ^KT-38829 Fixed
This commit is contained in:
committed by
Roman Golyshev
parent
6034fcdc46
commit
225d354604
+12
-13
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user