Quickfix for "Expression cannot be invoked as a function" with no arguments to remove parentheses

#KT-10825 Fixed
This commit is contained in:
Dmitry Jemerov
2016-01-27 19:15:21 +01:00
parent 6d7e1c69dc
commit 7c59592212
8 changed files with 101 additions and 0 deletions
@@ -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<KtCallExpression>(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<KtCallExpression>? {
val expression = diagnostic.psiElement.parent as? KtCallExpression ?: return null
if (expression is KtExpression && expression.valueArguments.isEmpty()) {
return ChangeToPropertyAccessFix(expression)
}
return null
}
}
}
@@ -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)
@@ -0,0 +1,5 @@
// "Change to property access" "true"
fun x() {
val y = (1+2<caret>)()
}
@@ -0,0 +1,5 @@
// "Change to property access" "true"
fun x() {
val y = (1+2)
}
@@ -0,0 +1,6 @@
// "Change to property access" "true"
class A(val ff: String)
fun x() {
val y = A("").f<caret>f()
}
@@ -0,0 +1,6 @@
// "Change to property access" "true"
class A(val ff: String)
fun x() {
val y = A("").ff
}
@@ -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("").f<caret>d("")
}
@@ -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)