From bb9c65de4adb09db1f0a3ebd98658379fe166acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sapalski?= Date: Mon, 18 Feb 2013 12:48:55 +0100 Subject: [PATCH] Quickfix for UNUSED_VARIABLE --- .../jetbrains/jet/plugin/JetBundle.properties | 4 +- .../jet/plugin/quickfix/QuickFixes.java | 2 + .../plugin/quickfix/RemoveVariableFix.java | 72 +++++++++++++++++++ .../afterUnusedVariableWithInitializer.kt | 6 ++ .../afterUnusedVariableWithoutInitializer.kt | 3 + .../beforeUnusedVariableWithInitializer.kt | 6 ++ .../beforeUnusedVariableWithoutInitializer.kt | 4 ++ .../quickfix/QuickFixTestGenerated.java | 10 +++ 8 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/RemoveVariableFix.java create mode 100644 idea/testData/quickfix/variables/afterUnusedVariableWithInitializer.kt create mode 100644 idea/testData/quickfix/variables/afterUnusedVariableWithoutInitializer.kt create mode 100644 idea/testData/quickfix/variables/beforeUnusedVariableWithInitializer.kt create mode 100644 idea/testData/quickfix/variables/beforeUnusedVariableWithoutInitializer.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 4d815943e01b2..d1410342a6bff 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -118,4 +118,6 @@ change.to.property.name.action=Change ''{0}'' to ''{1}'' surround.with.string.template="${expr}" surround.with.when.template=when (expr) {} -surround.with.function.template={ } \ No newline at end of file +surround.with.function.template={ } +remove.variable.family.name=Remove variable +remove.variable.action=Remove variable ''{0}'' diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 92674fccc8938..17153b859d375 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -153,6 +153,8 @@ private QuickFixes() {} actions.put(VAL_REASSIGNMENT, changeVariableMutabilityFix); actions.put(VAR_OVERRIDDEN_BY_VAL, changeVariableMutabilityFix); + factories.put(UNUSED_VARIABLE, RemoveVariableFix.createRemoveVariableFactory()); + actions.put(UNNECESSARY_SAFE_CALL, ReplaceCallFix.toDotCallFromSafeCall()); actions.put(UNSAFE_CALL, ReplaceCallFix.toSafeCall()); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveVariableFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveVariableFix.java new file mode 100644 index 0000000000000..10cc523824338 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveVariableFix.java @@ -0,0 +1,72 @@ +/* + * 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.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.psi.JetExpression; +import org.jetbrains.jet.lang.psi.JetProperty; +import org.jetbrains.jet.plugin.JetBundle; + +public class RemoveVariableFix extends JetIntentionAction { + public RemoveVariableFix(@NotNull JetProperty element) { + super(element); + } + + private String getVariableName() { + return element.getName(); + } + + @NotNull + @Override + public String getText() { + return JetBundle.message("remove.variable.action", getVariableName()); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("remove.variable.family.name"); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + JetExpression initializer = element.getInitializer(); + if (initializer != null) { + element.replace(initializer); + } + else { + element.delete(); + } + } + + public static JetIntentionActionFactory createRemoveVariableFactory() { + return new JetIntentionActionFactory() { + @Override + public JetIntentionAction createAction(Diagnostic diagnostic) { + JetProperty expression = QuickFixUtil.getParentElementOfType(diagnostic, JetProperty.class); + if (expression == null) return null; + return new RemoveVariableFix(expression); + } + }; + } +} + diff --git a/idea/testData/quickfix/variables/afterUnusedVariableWithInitializer.kt b/idea/testData/quickfix/variables/afterUnusedVariableWithInitializer.kt new file mode 100644 index 0000000000000..d534789df10a4 --- /dev/null +++ b/idea/testData/quickfix/variables/afterUnusedVariableWithInitializer.kt @@ -0,0 +1,6 @@ +// "Remove variable 'a'" "true" +var cnt = 5 +fun getCnt() = cnt++ +fun f() { + getCnt() +} \ No newline at end of file diff --git a/idea/testData/quickfix/variables/afterUnusedVariableWithoutInitializer.kt b/idea/testData/quickfix/variables/afterUnusedVariableWithoutInitializer.kt new file mode 100644 index 0000000000000..66a1e40cd42c3 --- /dev/null +++ b/idea/testData/quickfix/variables/afterUnusedVariableWithoutInitializer.kt @@ -0,0 +1,3 @@ +// "Remove variable 'test'" "true" +fun f() { +} \ No newline at end of file diff --git a/idea/testData/quickfix/variables/beforeUnusedVariableWithInitializer.kt b/idea/testData/quickfix/variables/beforeUnusedVariableWithInitializer.kt new file mode 100644 index 0000000000000..96ac565a88fad --- /dev/null +++ b/idea/testData/quickfix/variables/beforeUnusedVariableWithInitializer.kt @@ -0,0 +1,6 @@ +// "Remove variable 'a'" "true" +var cnt = 5 +fun getCnt() = cnt++ +fun f() { + var a = getCnt() +} \ No newline at end of file diff --git a/idea/testData/quickfix/variables/beforeUnusedVariableWithoutInitializer.kt b/idea/testData/quickfix/variables/beforeUnusedVariableWithoutInitializer.kt new file mode 100644 index 0000000000000..567d5b35dde74 --- /dev/null +++ b/idea/testData/quickfix/variables/beforeUnusedVariableWithoutInitializer.kt @@ -0,0 +1,4 @@ +// "Remove variable 'test'" "true" +fun f() { + val test: Int +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 0db217b690786..51c2cdc1e2bc7 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -775,6 +775,16 @@ public void testAllFilesPresentInVariables() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/variables"), Pattern.compile("^before(\\w+)\\.kt$"), true); } + @TestMetadata("beforeUnusedVariableWithInitializer.kt") + public void testUnusedVariableWithInitializer() throws Exception { + doTest("idea/testData/quickfix/variables/beforeUnusedVariableWithInitializer.kt"); + } + + @TestMetadata("beforeUnusedVariableWithoutInitializer.kt") + public void testUnusedVariableWithoutInitializer() throws Exception { + doTest("idea/testData/quickfix/variables/beforeUnusedVariableWithoutInitializer.kt"); + } + @TestMetadata("idea/testData/quickfix/variables/changeMutability") public static class ChangeMutability extends AbstractQuickFixTest { public void testAllFilesPresentInChangeMutability() throws Exception {