Quickfix for UNUSED_VARIABLE

This commit is contained in:
Michał Sapalski
2013-02-18 12:48:55 +01:00
committed by Nikolay Krasko
parent 881de132f7
commit bb9c65de4a
8 changed files with 106 additions and 1 deletions
@@ -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={ }
surround.with.function.template={ }
remove.variable.family.name=Remove variable
remove.variable.action=Remove variable ''{0}''
@@ -153,6 +153,8 @@ public class 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());
@@ -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<JetProperty> {
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);
}
};
}
}
@@ -0,0 +1,6 @@
// "Remove variable 'a'" "true"
var cnt = 5
fun getCnt() = cnt++
fun f() {
getCnt()
}
@@ -0,0 +1,3 @@
// "Remove variable 'test'" "true"
fun f() {
}
@@ -0,0 +1,6 @@
// "Remove variable 'a'" "true"
var cnt = 5
fun getCnt() = cnt++
fun f() {
var <caret>a = getCnt()
}
@@ -0,0 +1,4 @@
// "Remove variable 'test'" "true"
fun f() {
val <caret>test: Int
}
@@ -775,6 +775,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
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 {