[groovy] highlight unnecessary import aliases

This commit is contained in:
Daniil Ovchinnikov
2017-12-03 19:05:55 +03:00
parent e506d3c538
commit fb39701de0
5 changed files with 60 additions and 16 deletions
@@ -0,0 +1,8 @@
<html>
<body>
Reports unnecessary import aliases
<p>
<small>New in 2018.1</small>
</p>
</body>
</html>
@@ -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
unnecessary.def.explicitly.typed.only=Only report in explicitly typed declarations
unnecessary.alias.fix=Remove unnecessary alias
unnecessary.alias.description=Alias is unnecessary
@@ -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<PsiElement, PsiElement> myElementFunction;
public RemoveElementQuickFix(@NotNull String name) {
this(name, Functions.identity());
}
public RemoveElementQuickFix(@NotNull String name, @NotNull Function<PsiElement, PsiElement> function) {
myName = name;
myElementFunction = function;
@@ -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
)
}
}
}
}
+3
View File
@@ -886,6 +886,9 @@
<localInspection language="Groovy" groupPath="Groovy" groupKey="inspection.style"
displayName="Unnecessary semicolon" cleanupTool="true" enabledByDefault="true"
implementationClass="org.jetbrains.plugins.groovy.codeInspection.style.GrUnnecessarySemicolonInspection"/>
<localInspection language="Groovy" groupPath="Groovy" groupKey="inspection.style"
displayName="Unnecessary import alias" cleanupTool="true" enabledByDefault="true" level="WEAK WARNING"
implementationClass="org.jetbrains.plugins.groovy.codeInspection.style.GrUnnecessaryAliasInspection"/>
<localInspection language="Groovy" groupPath="Groovy" shortName="ChangeToOperator"
displayName="Change to operator"
groupKey="inspection.style" enabledByDefault="true" level="WEAK WARNING"