Inspection to get rid of unnecessary ticks in references #KT-18124 Fixed
This commit is contained in:
committed by
Alexey Sedunov
parent
9e500831dd
commit
1875d129ea
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports the redundant backticks in references
|
||||
</body>
|
||||
</html>
|
||||
@@ -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("`")))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RemoveRedundantBackticksInspection
|
||||
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(<caret>`a`: Int) {
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(<caret>a: Int) {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo() {
|
||||
<caret>`print`("I don't want to use ticks in Kotlin anymore")
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo() {
|
||||
<caret>print("I don't want to use ticks in Kotlin anymore")
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// PROBLEM: none
|
||||
fun foo() {
|
||||
<caret>val ` two words ` = "two words"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
fun foo() {
|
||||
<caret>`is`("bar")
|
||||
}
|
||||
|
||||
fun `is`(x: String) {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo() {
|
||||
val <caret>`a` = 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo() {
|
||||
val <caret>a = 1
|
||||
}
|
||||
+38
@@ -2949,6 +2949,44 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
@TestMetadata("sameVisibility3.kt")
|
||||
public void testSameVisibility3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/sameVisibility3.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/removeRedundantBackticks")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RemoveRedundantBackticks extends AbstractLocalInspectionTest {
|
||||
public void testAllFilesPresentInRemoveRedundantBackticks() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/removeRedundantBackticks"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("functionArgument.kt")
|
||||
public void testFunctionArgument() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantBackticks/functionArgument.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("functionCall.kt")
|
||||
public void testFunctionCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantBackticks/functionCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("identifierContainingSpaces.kt")
|
||||
public void testIdentifierContainingSpaces() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantBackticks/identifierContainingSpaces.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("keyword.kt")
|
||||
public void testKeyword() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantBackticks/keyword.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("property.kt")
|
||||
public void testProperty() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantBackticks/property.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user