Added quick fix which removes val/var from parameters all over the project.

This commit is contained in:
Evgeny Gerashchenko
2013-02-26 17:00:50 +04:00
parent 998b74aa0a
commit a3eae3d5c1
6 changed files with 136 additions and 0 deletions
@@ -112,6 +112,7 @@ options.jet.attribute.descriptor.label=Label
change.to.function.invocation=Change to function invocation
migrate.tuple=Migrate tuples in project\: e.g., \#(,) and \#(,,) will be replaced by Pair and Triple
migrate.sure=Replace sure() calls by !! in project
remove.val.var.from.parameter=Remove val/var from function, loop and catch parameters in project
add.when.else.branch.action.family.name=Add Else Branch
add.when.else.branch.action=Add else branch
move.when.else.branch.to.the.end.action=Move else branch to the end
@@ -155,6 +155,11 @@ public class QuickFixes {
actions.put(VAL_REASSIGNMENT, changeVariableMutabilityFix);
actions.put(VAR_OVERRIDDEN_BY_VAL, changeVariableMutabilityFix);
RemoveValVarFromParametersFix removeValVarFromParametersFix = new RemoveValVarFromParametersFix();
actions.put(VAL_OR_VAR_ON_FUN_PARAMETER, removeValVarFromParametersFix);
actions.put(VAL_OR_VAR_ON_LOOP_PARAMETER, removeValVarFromParametersFix);
actions.put(VAL_OR_VAR_ON_CATCH_PARAMETER, removeValVarFromParametersFix);
factories.put(UNUSED_VARIABLE, RemoveVariableFix.createRemoveVariableFactory());
actions.put(UNNECESSARY_SAFE_CALL, ReplaceCallFix.toDotCallFromSafeCall());
@@ -0,0 +1,89 @@
/*
* 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.codeInsight.intention.IntentionAction;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.TokenType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.project.PluginJetFilesProvider;
import java.util.Collection;
public class RemoveValVarFromParametersFix implements IntentionAction {
@NotNull
@Override
public String getText() {
return JetBundle.message("remove.val.var.from.parameter");
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("remove.val.var.from.parameter");
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return file.getManager().isInProject(file);
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
// TODO after M6, this quick fix should remove val/var only for current parameter
JetFile initialFile = (JetFile) file;
Collection<JetFile> files = PluginJetFilesProvider.WHOLE_PROJECT_DECLARATION_PROVIDER.fun(initialFile);
for (JetFile jetFile : files) {
jetFile.acceptChildren(new JetVisitorVoid() {
@Override
public void visitParameter(JetParameter parameter) {
visitJetElement(parameter); // run recursively for children
PsiElement parent = parameter.getParent();
if (parent != null && parent.getParent() instanceof JetClass) {
return; // constructor parameter
}
ASTNode valOrVarNode = parameter.getValOrVarNode();
if (valOrVarNode != null) {
ASTNode whitespace = valOrVarNode.getTreeNext();
assert whitespace.getElementType() == TokenType.WHITE_SPACE;
parameter.getNode().removeRange(valOrVarNode, whitespace.getTreeNext());
}
}
@Override
public void visitJetElement(JetElement element) {
element.acceptChildren(this);
}
});
}
}
@Override
public boolean startInWriteAction() {
return true;
}
}
@@ -0,0 +1,18 @@
// "Remove val/var from function, loop and catch parameters in project" "true"
class Class(a: Int, val b: Int, var c: Int, vararg val d: Int) {
}
fun f(a: Int, b: Int, c: Int, vararg d: Int) {
for (i in d) {
}
for (i in d) {
}
try {
} catch (e: Exception) {
} catch (e: Exception) {
} catch (e: Exception) {
}
}
@@ -0,0 +1,18 @@
// "Remove val/var from function, loop and catch parameters in project" "true"
class Class(a: Int, val b: Int, var c: Int, vararg val d: Int) {
}
fun f(a: Int, <caret>val b: Int, var c: Int, vararg val d: Int) {
for (val i in d) {
}
for (var i in d) {
}
try {
} catch (val e: Exception) {
} catch (var e: Exception) {
} catch (e: Exception) {
}
}
@@ -407,6 +407,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/migration/beforeTuplesWithRuntime.kt");
}
@TestMetadata("beforeValVarFromParameters.kt")
public void testValVarFromParameters() throws Exception {
doTest("idea/testData/quickfix/migration/beforeValVarFromParameters.kt");
}
}
@TestMetadata("idea/testData/quickfix/modifiers")