diff --git a/plugins/groovy/groovy-psi/resources/inspectionDescriptions/GrUnnecessaryAlias.html b/plugins/groovy/groovy-psi/resources/inspectionDescriptions/GrUnnecessaryAlias.html new file mode 100644 index 000000000000..b21e003f5ad0 --- /dev/null +++ b/plugins/groovy/groovy-psi/resources/inspectionDescriptions/GrUnnecessaryAlias.html @@ -0,0 +1,8 @@ + + +Reports unnecessary import aliases +

+ New in 2018.1 +

+ + diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties index 641a39f21784..2c98f9bbf701 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties @@ -121,4 +121,6 @@ expected.type.0=Expected ''{0}'', found ''{1}'' declare.explicit.implementations.of.trait=Declare explicit implementations of trait unnecessary.modifier.description=Modifier ''{0}'' is not necessary unnecessary.modifier.remove=Remove unnecessary ''{0}'' -unnecessary.def.explicitly.typed.only=Only report in explicitly typed declarations \ No newline at end of file +unnecessary.def.explicitly.typed.only=Only report in explicitly typed declarations +unnecessary.alias.fix=Remove unnecessary alias +unnecessary.alias.description=Alias is unnecessary \ No newline at end of file diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/fixes/RemoveElementQuickFix.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/fixes/RemoveElementQuickFix.java index b6de7c11b1e2..4c06dfddd29f 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/fixes/RemoveElementQuickFix.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/fixes/RemoveElementQuickFix.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 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. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.plugins.groovy.codeInspection.fixes; import com.intellij.codeInspection.LocalQuickFix; @@ -20,6 +6,7 @@ import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.intellij.util.Function; +import com.intellij.util.Functions; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -28,6 +15,10 @@ public class RemoveElementQuickFix implements LocalQuickFix { private final String myName; private final Function myElementFunction; + public RemoveElementQuickFix(@NotNull String name) { + this(name, Functions.identity()); + } + public RemoveElementQuickFix(@NotNull String name, @NotNull Function function) { myName = name; myElementFunction = function; diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/style/GrUnnecessaryAliasInspection.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/style/GrUnnecessaryAliasInspection.kt new file mode 100644 index 000000000000..7ef3ae2ef6db --- /dev/null +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/style/GrUnnecessaryAliasInspection.kt @@ -0,0 +1,40 @@ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.groovy.codeInspection.style + +import com.intellij.codeInspection.CleanupLocalInspectionTool +import com.intellij.codeInspection.ProblemHighlightType +import com.intellij.codeInspection.ProblemsHolder +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiElementVisitor +import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle.message +import org.jetbrains.plugins.groovy.codeInspection.GroovySuppressableInspectionTool +import org.jetbrains.plugins.groovy.codeInspection.fixes.RemoveElementQuickFix +import org.jetbrains.plugins.groovy.lang.psi.api.GrImportAlias +import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement + +class GrUnnecessaryAliasInspection : GroovySuppressableInspectionTool(), CleanupLocalInspectionTool { + + companion object { + @JvmStatic + private val fix = RemoveElementQuickFix(message("unnecessary.alias.fix")) + } + + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = object : PsiElementVisitor() { + + override fun visitElement(element: PsiElement) { + val alias = element as? GrImportAlias ?: return + val aliasName = alias.name ?: return + val statement = alias.parent as? GrImportStatement ?: return + val reference = statement.importReference ?: return + val name = reference.referenceName ?: return + if (aliasName == name) { + holder.registerProblem( + alias, + message("unnecessary.alias.description"), + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + fix + ) + } + } + } +} \ No newline at end of file diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index e683d3f7d048..f9f22166c8e8 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -886,6 +886,9 @@ +