From 327b471eaf6224a28bd495985abb9c9b33fd74ae Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 23 May 2012 18:53:22 +0400 Subject: [PATCH] "Change to function invocation" fix added --- .../jetbrains/jet/plugin/JetBundle.properties | 1 + ... => ChangeToConstructorInvocationFix.java} | 6 +- .../ChangeToFunctionInvocationFix.java | 68 +++++++++++++++++++ .../jet/plugin/quickfix/QuickFixes.java | 3 +- .../afterFunInvWithoutParentheses.kt | 8 +++ .../beforeFunInvWithoutParentheses.kt | 8 +++ 6 files changed, 90 insertions(+), 4 deletions(-) rename idea/src/org/jetbrains/jet/plugin/quickfix/{ChangeToInvocationFix.java => ChangeToConstructorInvocationFix.java} (89%) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java create mode 100644 idea/testData/quickfix/variables/changeToFunctionInvocation/afterFunInvWithoutParentheses.kt create mode 100644 idea/testData/quickfix/variables/changeToFunctionInvocation/beforeFunInvWithoutParentheses.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 14ae9e857cf..2a2386cd858 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -86,3 +86,4 @@ options.jet.attribute.descriptor.extension.fun.call=Extension function call options.jet.attribute.descriptor.constructor.call=Constructor call options.jet.attribute.descriptor.auto.casted=Smart-cast value options.jet.attribute.descriptor.label=Label +change.to.function.invocation=Change to function invocation diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToInvocationFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToConstructorInvocationFix.java similarity index 89% rename from idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToInvocationFix.java rename to idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToConstructorInvocationFix.java index 1982499ac82..163ff567cf0 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToInvocationFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToConstructorInvocationFix.java @@ -33,9 +33,9 @@ import java.util.List; /** * @author svtk */ -public class ChangeToInvocationFix extends JetIntentionAction { +public class ChangeToConstructorInvocationFix extends JetIntentionAction { - public ChangeToInvocationFix(@NotNull JetDelegatorToSuperClass element) { + public ChangeToConstructorInvocationFix(@NotNull JetDelegatorToSuperClass element) { super(element); } @@ -66,7 +66,7 @@ public class ChangeToInvocationFix extends JetIntentionAction createAction(Diagnostic diagnostic) { if (diagnostic.getPsiElement() instanceof JetDelegatorToSuperClass) { - return new ChangeToInvocationFix((JetDelegatorToSuperClass) diagnostic.getPsiElement()); + return new ChangeToConstructorInvocationFix((JetDelegatorToSuperClass) diagnostic.getPsiElement()); } return null; } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java new file mode 100644 index 00000000000..339ad6e1cf1 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java @@ -0,0 +1,68 @@ +/* + * Copyright 2010-2012 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.*; +import org.jetbrains.jet.plugin.JetBundle; + +import java.util.List; + +/** + * @author svtk + */ +public class ChangeToFunctionInvocationFix extends JetIntentionAction { + + public ChangeToFunctionInvocationFix(@NotNull JetReferenceExpression element) { + super(element); + } + + @NotNull + @Override + public String getText() { + return JetBundle.message("change.to.function.invocation"); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("change.to.function.invocation"); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + JetReferenceExpression reference = (JetReferenceExpression) element.copy(); + element.replace(JetPsiFactory.createExpression(project, reference.getText() + "()")); + } + + public static JetIntentionActionFactory createFactory() { + return new JetIntentionActionFactory() { + @Override + public JetIntentionAction createAction(Diagnostic diagnostic) { + if (diagnostic.getPsiElement() instanceof JetReferenceExpression) { + return new ChangeToFunctionInvocationFix((JetReferenceExpression) diagnostic.getPsiElement()); + } + return null; + } + }; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 0935e9e22db..ee3e4874220 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -119,7 +119,8 @@ public class QuickFixes { JetIntentionActionFactory unresolvedReferenceFactory = ImportClassAndFunFix.createFactory(); factories.put(UNRESOLVED_REFERENCE, unresolvedReferenceFactory); - factories.put(SUPERTYPE_NOT_INITIALIZED_DEFAULT, ChangeToInvocationFix.createFactory()); + factories.put(SUPERTYPE_NOT_INITIALIZED_DEFAULT, ChangeToConstructorInvocationFix.createFactory()); + factories.put(FUNCTION_CALL_EXPECTED, ChangeToFunctionInvocationFix.createFactory()); factories.put(CANNOT_CHANGE_ACCESS_PRIVILEGE, ChangeVisibilityModifierFix.createFactory()); factories.put(CANNOT_WEAKEN_ACCESS_PRIVILEGE, ChangeVisibilityModifierFix.createFactory()); diff --git a/idea/testData/quickfix/variables/changeToFunctionInvocation/afterFunInvWithoutParentheses.kt b/idea/testData/quickfix/variables/changeToFunctionInvocation/afterFunInvWithoutParentheses.kt new file mode 100644 index 00000000000..6be3b1ed961 --- /dev/null +++ b/idea/testData/quickfix/variables/changeToFunctionInvocation/afterFunInvWithoutParentheses.kt @@ -0,0 +1,8 @@ +// "Change to function invocation" "true" +package a + +fun foo() {} + +fun test() { + foo() +} diff --git a/idea/testData/quickfix/variables/changeToFunctionInvocation/beforeFunInvWithoutParentheses.kt b/idea/testData/quickfix/variables/changeToFunctionInvocation/beforeFunInvWithoutParentheses.kt new file mode 100644 index 00000000000..45679736045 --- /dev/null +++ b/idea/testData/quickfix/variables/changeToFunctionInvocation/beforeFunInvWithoutParentheses.kt @@ -0,0 +1,8 @@ +// "Change to function invocation" "true" +package a + +fun foo() {} + +fun test() { + foo +}