Inspection to get rid of unnecessary ticks in references #KT-18124 Fixed

This commit is contained in:
kenji tomita
2017-08-22 08:38:54 +09:00
committed by Alexey Sedunov
parent 9e500831dd
commit 1875d129ea
13 changed files with 156 additions and 2 deletions
+9 -2
View File
@@ -2497,8 +2497,15 @@
enabledByDefault="true"
cleanupTool="true"
level="WEAK WARNING"
language="kotlin"
/>
language="kotlin"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RemoveRedundantBackticksInspection"
displayName="Remove redundant backticks"
groupPath="Kotlin"
groupName="Redundant constructs"
enabledByDefault="true"
level="WARNING"
language="kotlin"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantSetterInspection"
displayName="Redundant property setter"
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.*
import com.intellij.lang.ASTNode
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.impl.source.tree.SharedImplUtil
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
class RemoveRedundantBackticksInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : KtVisitorVoid() {
override fun visitKtElement(element: KtElement) {
super.visitKtElement(element)
SharedImplUtil.getChildrenOfType(element.node, KtTokens.IDENTIFIER).forEach {
if (isRedundantBackticks(it)) {
registerProblem(holder, it.psi)
}
}
}
}
}
private fun isKeyword(text: String): Boolean {
return (KtTokens.KEYWORDS.types + KtTokens.SOFT_KEYWORDS.types).any { it.toString() == text }
}
private fun isRedundantBackticks(node: ASTNode): Boolean {
return (node.text.startsWith("`") &&
node.text.endsWith("`") &&
KotlinNameSuggester.isIdentifier(node.text) &&
!isKeyword(node.text.removePrefix("`").removeSuffix("`")))
}
private fun registerProblem(holder: ProblemsHolder, element: PsiElement) {
holder.registerProblem(element,
"Remove redundant backticks",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
RemoveRedundantBackticksQuickFix())
}
}
class RemoveRedundantBackticksQuickFix : LocalQuickFix {
override fun getName() = "Remove redundant backticks"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement
val factory = KtPsiFactory(project)
element.replace(factory.createIdentifier(element.text.removePrefix("`").removeSuffix("`")))
}
}