From a80930bf1599e72d1a389d8995b199de93e081c4 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 13 Nov 2015 20:05:15 +0100 Subject: [PATCH] code cleanup can replace infix calls to library functions with ordinary calls #KT-9976 Fixed --- .../kotlin/idea/inspections/InfixCallFix.kt | 58 +++++++++++++++++++ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 + idea/testData/inspections/cleanup/cleanup.kt | 4 ++ .../inspections/cleanup/cleanup.kt.after | 4 ++ 4 files changed, 68 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/inspections/InfixCallFix.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/InfixCallFix.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/InfixCallFix.kt new file mode 100644 index 00000000000..e219bdf8777 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/InfixCallFix.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2010-2015 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.inspections + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters2 +import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde +import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil +import org.jetbrains.kotlin.idea.intentions.InfixCallToOrdinaryIntention +import org.jetbrains.kotlin.idea.quickfix.CleanupFix +import org.jetbrains.kotlin.idea.quickfix.KotlinQuickFixAction +import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory +import org.jetbrains.kotlin.psi.KtBinaryExpression +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtModifierListOwner +import org.jetbrains.kotlin.psi.KtOperationReferenceExpression + +class InfixCallFix(expression: KtBinaryExpression) : KotlinQuickFixAction(expression), CleanupFix { + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + InfixCallToOrdinaryIntention.convert(element) + } + + override fun getFamilyName(): String = text + override fun getText(): String = "Replace infix call with ordinary call" + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val functionDescriptor = (diagnostic as? DiagnosticWithParameters2<*, *, *>)?.a as? FunctionDescriptor ?: return null + val target = DescriptorToSourceUtilsIde.getAnyDeclaration(diagnostic.psiFile.project, functionDescriptor) + as? KtModifierListOwner + if (target == null || QuickFixUtil.canModifyElement(target)) { + // we'll fix the problem by adding the 'infix' modifier to the target + return null + } + + val infixCall = (diagnostic.psiElement as? KtOperationReferenceExpression)?.parent as? KtBinaryExpression ?: return null + return InfixCallFix(infixCall) + } + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 5c6241eafdd..330220affa0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.diagnostics.Errors.* import org.jetbrains.kotlin.idea.core.overrideImplement.ImplementMembersHandler import org.jetbrains.kotlin.idea.inspections.AddModifierFixFactory import org.jetbrains.kotlin.idea.inspections.AddReflectionQuickFix +import org.jetbrains.kotlin.idea.inspections.InfixCallFix import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable.* import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.CreateClassFromCallWithConstructorCalleeActionFactory import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.CreateClassFromConstructorCallActionFactory @@ -333,6 +334,7 @@ public class QuickFixRegistrar : QuickFixContributor { OPERATOR_MODIFIER_REQUIRED.registerFactory(AutoImportForMissingOperatorFactory) INFIX_MODIFIER_REQUIRED.registerFactory(AddModifierFixFactory(KtTokens.INFIX_KEYWORD)) + INFIX_MODIFIER_REQUIRED.registerFactory(InfixCallFix) UNDERSCORE_IS_RESERVED.registerFactory(RenameUnderscoreFix) diff --git a/idea/testData/inspections/cleanup/cleanup.kt b/idea/testData/inspections/cleanup/cleanup.kt index 55917724884..6f38337bca0 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt +++ b/idea/testData/inspections/cleanup/cleanup.kt @@ -83,3 +83,7 @@ fun test(a: AAA, b: BBB) { a.foo() } } + +fun infixTest() { + arrayListOf(1, 2, 3) map { it } +} diff --git a/idea/testData/inspections/cleanup/cleanup.kt.after b/idea/testData/inspections/cleanup/cleanup.kt.after index 9d5fbb09a4c..87888468d65 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt.after +++ b/idea/testData/inspections/cleanup/cleanup.kt.after @@ -83,3 +83,7 @@ fun test(a: AAA, b: BBB) { (a.foo)() } } + +fun infixTest() { + arrayListOf(1, 2, 3).map { it } +}