From 7c59592212d7d78e96360e84a8a52ac864ad0c0b Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 27 Jan 2016 19:15:21 +0100 Subject: [PATCH] Quickfix for "Expression cannot be invoked as a function" with no arguments to remove parentheses #KT-10825 Fixed --- .../quickfix/ChangeToPropertyAccessFix.kt | 44 +++++++++++++++++++ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 1 + .../changeToPropertyAccess/nonSimpleName.kt | 5 +++ .../nonSimpleName.kt.after | 5 +++ .../changeToPropertyAccess/propertyCall.kt | 6 +++ .../propertyCall.kt.after | 6 +++ .../propertyCallWithArguments.kt | 7 +++ .../idea/quickfix/QuickFixTestGenerated.java | 27 ++++++++++++ 8 files changed, 101 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToPropertyAccessFix.kt create mode 100644 idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt create mode 100644 idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt.after create mode 100644 idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt create mode 100644 idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt.after create mode 100644 idea/testData/quickfix/variables/changeToPropertyAccess/propertyCallWithArguments.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToPropertyAccessFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToPropertyAccessFix.kt new file mode 100644 index 00000000000..d65c3185b7b --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToPropertyAccessFix.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2016 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.kotlin.idea.quickfix + +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.psi.KtCallExpression +import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.psi.KtFile + +class ChangeToPropertyAccessFix(element: KtCallExpression) : KotlinQuickFixAction(element) { + override fun getFamilyName() = "Change to property access" + + override fun getText() = familyName + + public override fun invoke(project: Project, editor: Editor?, file: KtFile) { + element.replace(element.calleeExpression as KtExpression) + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val expression = diagnostic.psiElement.parent as? KtCallExpression ?: return null + if (expression is KtExpression && expression.valueArguments.isEmpty()) { + return ChangeToPropertyAccessFix(expression) + } + return null + } + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index cd4a95eae9a..61431274bc2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -142,6 +142,7 @@ class QuickFixRegistrar : QuickFixContributor { SUPERTYPE_NOT_INITIALIZED.registerFactory(SuperClassNotInitialized) FUNCTION_CALL_EXPECTED.registerFactory(ChangeToFunctionInvocationFix) + FUNCTION_EXPECTED.registerFactory(ChangeToPropertyAccessFix) CANNOT_CHANGE_ACCESS_PRIVILEGE.registerFactory(ChangeVisibilityModifierFix) CANNOT_WEAKEN_ACCESS_PRIVILEGE.registerFactory(ChangeVisibilityModifierFix) diff --git a/idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt b/idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt new file mode 100644 index 00000000000..78162bd472f --- /dev/null +++ b/idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt @@ -0,0 +1,5 @@ +// "Change to property access" "true" + +fun x() { + val y = (1+2)() +} diff --git a/idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt.after b/idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt.after new file mode 100644 index 00000000000..b5039e3eefc --- /dev/null +++ b/idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt.after @@ -0,0 +1,5 @@ +// "Change to property access" "true" + +fun x() { + val y = (1+2) +} diff --git a/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt b/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt new file mode 100644 index 00000000000..25e1faf3cb8 --- /dev/null +++ b/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt @@ -0,0 +1,6 @@ +// "Change to property access" "true" +class A(val ff: String) + +fun x() { + val y = A("").ff() +} diff --git a/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt.after b/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt.after new file mode 100644 index 00000000000..3dd855d76c2 --- /dev/null +++ b/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt.after @@ -0,0 +1,6 @@ +// "Change to property access" "true" +class A(val ff: String) + +fun x() { + val y = A("").ff +} diff --git a/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCallWithArguments.kt b/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCallWithArguments.kt new file mode 100644 index 00000000000..90c4d202ada --- /dev/null +++ b/idea/testData/quickfix/variables/changeToPropertyAccess/propertyCallWithArguments.kt @@ -0,0 +1,7 @@ +// "Change to property access" "false" +// ERROR: Expression 'fd' of type 'kotlin.String' cannot be invoked as a function. The function invoke() is not found +class A(val fd: String) + +fun x() { + val y = A("").fd("") +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 650acfd6f4d..94e64d9525f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -7488,6 +7488,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/variables/changeToPropertyAccess") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ChangeToPropertyAccess extends AbstractQuickFixTest { + public void testAllFilesPresentInChangeToPropertyAccess() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/variables/changeToPropertyAccess"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("nonSimpleName.kt") + public void testNonSimpleName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/changeToPropertyAccess/nonSimpleName.kt"); + doTest(fileName); + } + + @TestMetadata("propertyCall.kt") + public void testPropertyCall() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/changeToPropertyAccess/propertyCall.kt"); + doTest(fileName); + } + + @TestMetadata("propertyCallWithArguments.kt") + public void testPropertyCallWithArguments() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/changeToPropertyAccess/propertyCallWithArguments.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/variables/removeValVarFromParameter") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)