Skip to content

Commit

Permalink
Quickfix for UNUSED_VARIABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
sapal authored and goodwinnk committed Feb 21, 2013
1 parent 881de13 commit bb9c65d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 1 deletion.
4 changes: 3 additions & 1 deletion idea/src/org/jetbrains/jet/plugin/JetBundle.properties
Expand Up @@ -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}''
2 changes: 2 additions & 0 deletions idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java
Expand Up @@ -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());

Expand Down
72 changes: 72 additions & 0 deletions 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<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
}
Expand Up @@ -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 {
Expand Down

0 comments on commit bb9c65d

Please sign in to comment.