diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt index e75130307aa..73e6526c0e1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantBackticksInspection.kt @@ -19,6 +19,7 @@ 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 @@ -26,9 +27,9 @@ import com.intellij.psi.impl.source.tree.SharedImplUtil import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.core.unquote import org.jetbrains.kotlin.lexer.KtTokens -import org.jetbrains.kotlin.psi.KtElement -import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.psi.KtVisitorVoid +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.isIdentifier class RemoveRedundantBackticksInspection : AbstractKotlinInspection() { @@ -37,7 +38,7 @@ class RemoveRedundantBackticksInspection : AbstractKotlinInspection() { override fun visitKtElement(element: KtElement) { super.visitKtElement(element) SharedImplUtil.getChildrenOfType(element.node, KtTokens.IDENTIFIER).forEach { - if (isRedundantBackticks(it.text)) { + if (isRedundantBackticks(it)) { registerProblem(holder, it.psi) } } @@ -61,7 +62,7 @@ class RemoveRedundantBackticksQuickFix : LocalQuickFix { override fun applyFix(project: Project, descriptor: ProblemDescriptor) { val element = descriptor.psiElement - if (!isRedundantBackticks(element.text)) return + if (!isRedundantBackticks(element.node)) return val factory = KtPsiFactory(project) element.replace(factory.createIdentifier(element.text.unquote())) } @@ -70,8 +71,11 @@ class RemoveRedundantBackticksQuickFix : LocalQuickFix { 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 { +private fun isRedundantBackticks(node: ASTNode): Boolean { + val identifier = node.text if (!(identifier.startsWith("`") && identifier.endsWith("`"))) return false val unquotedText = identifier.unquote() - return unquotedText.isIdentifier() && !isKeyword(unquotedText) + if (!unquotedText.isIdentifier() || isKeyword(unquotedText)) return false + val simpleNameStringTemplateEntry = node.psi.getStrictParentOfType() + return simpleNameStringTemplateEntry == null || canPlaceAfterSimpleNameEntry(simpleNameStringTemplateEntry.nextSibling) } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate.kt b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate.kt new file mode 100644 index 00000000000..c4a0a02cc01 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate.kt @@ -0,0 +1,5 @@ +// PROBLEM: none +fun main() { + val name = "Kotlin" + val test = "$`name`test" +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate2.kt b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate2.kt new file mode 100644 index 00000000000..661180b3879 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate2.kt @@ -0,0 +1,4 @@ +fun main() { + val name = "Kotlin" + val test = "$`name` test" +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate2.kt.after b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate2.kt.after new file mode 100644 index 00000000000..30e558f8080 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate2.kt.after @@ -0,0 +1,4 @@ +fun main() { + val name = "Kotlin" + val test = "$name test" +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate3.kt b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate3.kt new file mode 100644 index 00000000000..96c140fb037 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate3.kt @@ -0,0 +1,4 @@ +fun main() { + val name = "Kotlin" + val test = "${`name`}test" +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate3.kt.after b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate3.kt.after new file mode 100644 index 00000000000..0e45cc1fa25 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate3.kt.after @@ -0,0 +1,4 @@ +fun main() { + val name = "Kotlin" + val test = "${name}test" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index d35763b5ce9..aa275db46b4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -9545,6 +9545,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/removeRedundantBackticks/identifierContainingSpaces.kt"); } + @TestMetadata("inStringTemplate.kt") + public void testInStringTemplate() throws Exception { + runTest("idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate.kt"); + } + + @TestMetadata("inStringTemplate2.kt") + public void testInStringTemplate2() throws Exception { + runTest("idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate2.kt"); + } + + @TestMetadata("inStringTemplate3.kt") + public void testInStringTemplate3() throws Exception { + runTest("idea/testData/inspectionsLocal/removeRedundantBackticks/inStringTemplate3.kt"); + } + @TestMetadata("keyword.kt") public void testKeyword() throws Exception { runTest("idea/testData/inspectionsLocal/removeRedundantBackticks/keyword.kt");