From 93e3bd930e478cbd430c829700d3276935e39f1b Mon Sep 17 00:00:00 2001 From: Jack Zhou Date: Thu, 31 Jan 2013 17:05:46 -0500 Subject: [PATCH] Added quickfix for USELESS_SIMPLE_IMPORT. --- .../jetbrains/jet/plugin/JetBundle.properties | 2 + .../jet/plugin/quickfix/QuickFixes.java | 2 + .../jet/plugin/quickfix/RemoveImportFix.java | 63 +++++++++++++++++++ .../afterRemoveUselessImport.kt | 8 +++ .../beforeRemoveUselessImport.kt | 9 +++ 5 files changed, 84 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/RemoveImportFix.java create mode 100644 idea/testData/quickfix/uselessImports/afterRemoveUselessImport.kt create mode 100644 idea/testData/quickfix/uselessImports/beforeRemoveUselessImport.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index a57637f659a..f622e917cce 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -23,6 +23,8 @@ remove.parts.from.property.family=Remove parts from property remove.right.part.of.binary.expression=Remove right part of a binary expression remove.cast=Remove cast remove.elvis.operator=Remove elvis operator +remove.useless.import=Remove useless import for ''{0}'' +remove.useless.import.family=Remove useless import replace.operation.in.binary.expression=Replace operation in a binary expression replace.cast.with.static.assert=Replace a cast with a static assert replace.with.dot.call=Replace with dot call diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index fdd19d2d5ff..d485c8d8c59 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -120,6 +120,8 @@ public class QuickFixes { JetIntentionActionFactory unresolvedReferenceFactory = ImportClassAndFunFix.createFactory(); factories.put(UNRESOLVED_REFERENCE, unresolvedReferenceFactory); + factories.put(USELESS_SIMPLE_IMPORT, RemoveImportFix.createFactory()); + factories.put(SUPERTYPE_NOT_INITIALIZED_DEFAULT, ChangeToConstructorInvocationFix.createFactory()); factories.put(FUNCTION_CALL_EXPECTED, ChangeToFunctionInvocationFix.createFactory()); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveImportFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveImportFix.java new file mode 100644 index 00000000000..3142b50fbce --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveImportFix.java @@ -0,0 +1,63 @@ +/* + * Copyright 2010-2013 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.jet.plugin.quickfix; + +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.psi.JetImportDirective; +import org.jetbrains.jet.plugin.JetBundle; + +public class RemoveImportFix extends JetIntentionAction { + private final JetImportDirective directive; + + public RemoveImportFix(JetImportDirective directive) { + super(directive); + this.directive = directive; + } + + @NotNull + @Override + public String getText() { + return JetBundle.message("remove.useless.import", directive.getImportedReference().getText()); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("remove.useless.import.family"); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + directive.delete(); + } + + public static JetIntentionActionFactory createFactory() { + return new JetIntentionActionFactory() { + @Nullable + @Override + public JetIntentionAction createAction(Diagnostic diagnostic) { + return new RemoveImportFix(QuickFixUtil.getParentElementOfType(diagnostic, JetImportDirective.class)); + } + }; + } +} diff --git a/idea/testData/quickfix/uselessImports/afterRemoveUselessImport.kt b/idea/testData/quickfix/uselessImports/afterRemoveUselessImport.kt new file mode 100644 index 00000000000..9c0a6f2e8fa --- /dev/null +++ b/idea/testData/quickfix/uselessImports/afterRemoveUselessImport.kt @@ -0,0 +1,8 @@ +// "Remove useless import for 'test'" "true" +package test + +import org.jetbrains + +fun main(args : Array) { + +} \ No newline at end of file diff --git a/idea/testData/quickfix/uselessImports/beforeRemoveUselessImport.kt b/idea/testData/quickfix/uselessImports/beforeRemoveUselessImport.kt new file mode 100644 index 00000000000..a3b732f0c36 --- /dev/null +++ b/idea/testData/quickfix/uselessImports/beforeRemoveUselessImport.kt @@ -0,0 +1,9 @@ +// "Remove useless import for 'test'" "true" +package test + +import org.jetbrains +import test + +fun main(args : Array) { + +} \ No newline at end of file