From a3eae3d5c1fd8f5ff118a46a77129c8db6feaa5b Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Tue, 26 Feb 2013 17:00:50 +0400 Subject: [PATCH] Added quick fix which removes val/var from parameters all over the project. --- .../jetbrains/jet/plugin/JetBundle.properties | 1 + .../jet/plugin/quickfix/QuickFixes.java | 5 ++ .../RemoveValVarFromParametersFix.java | 89 +++++++++++++++++++ .../migration/afterValVarFromParameters.kt | 18 ++++ .../migration/beforeValVarFromParameters.kt | 18 ++++ .../quickfix/QuickFixTestGenerated.java | 5 ++ 6 files changed, 136 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/RemoveValVarFromParametersFix.java create mode 100644 idea/testData/quickfix/migration/afterValVarFromParameters.kt create mode 100644 idea/testData/quickfix/migration/beforeValVarFromParameters.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 8d2266dfaa0..93cd759287c 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -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 diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index f3f05f6a98d..5a8a56af89a 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -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()); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveValVarFromParametersFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveValVarFromParametersFix.java new file mode 100644 index 00000000000..33aa7ca40fa --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveValVarFromParametersFix.java @@ -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 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; + } +} diff --git a/idea/testData/quickfix/migration/afterValVarFromParameters.kt b/idea/testData/quickfix/migration/afterValVarFromParameters.kt new file mode 100644 index 00000000000..2ebcc27260e --- /dev/null +++ b/idea/testData/quickfix/migration/afterValVarFromParameters.kt @@ -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) { + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/beforeValVarFromParameters.kt b/idea/testData/quickfix/migration/beforeValVarFromParameters.kt new file mode 100644 index 00000000000..65273b9740e --- /dev/null +++ b/idea/testData/quickfix/migration/beforeValVarFromParameters.kt @@ -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, 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) { + } +} \ 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 6271df80345..19bc2a6a1cd 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -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")