MoveLambdaOutsideParentheses: intention -> inspection #KT-21413 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
83573ed517
commit
decf9939fe
@@ -973,11 +973,6 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.MoveLambdaOutsideParenthesesIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.declarations.SplitPropertyDeclarationIntention</className>
|
||||
<category>Kotlin</category>
|
||||
@@ -2774,6 +2769,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.MoveLambdaOutsideParenthesesInspection"
|
||||
displayName="Lambda argument inside parentheses"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.idea.core.canMoveLambdaOutsideParentheses
|
||||
import org.jetbrains.kotlin.idea.core.getLastLambdaExpression
|
||||
import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParentheses
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
|
||||
class MoveLambdaOutsideParenthesesInspection : AbstractApplicabilityBasedInspection<KtCallExpression>(
|
||||
KtCallExpression::class.java
|
||||
) {
|
||||
override fun isApplicable(element: KtCallExpression) = element.canMoveLambdaOutsideParentheses()
|
||||
|
||||
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
|
||||
val expression = element.getParentOfType<KtCallExpression>(strict = false) ?: return
|
||||
|
||||
if (expression.canMoveLambdaOutsideParentheses()) {
|
||||
expression.moveFunctionLiteralOutsideParentheses()
|
||||
}
|
||||
}
|
||||
|
||||
override fun inspectionText(element: KtCallExpression) = "Should be moved lambda argument out of parentheses"
|
||||
|
||||
override fun inspectionTarget(element: KtCallExpression): KtElement {
|
||||
return element.getLastLambdaExpression()?.getStrictParentOfType<KtValueArgument>()?.asElement() ?: element
|
||||
}
|
||||
|
||||
override val defaultFixText = "Move lambda argument out of parentheses"
|
||||
}
|
||||
@@ -20,6 +20,8 @@ import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.search.LocalSearchScope
|
||||
import com.intellij.psi.search.searches.ReferencesSearch
|
||||
import org.jetbrains.kotlin.idea.core.getLastLambdaExpression
|
||||
import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParenthesesIfPossible
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -89,9 +91,6 @@ class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention<KtNamedFu
|
||||
val returnLabel = callee.getReferencedNameAsName()
|
||||
returnSaver.restore(replaced, returnLabel)
|
||||
|
||||
val moveLambdaOutsideParenthesesIntention = MoveLambdaOutsideParenthesesIntention()
|
||||
if (moveLambdaOutsideParenthesesIntention.isApplicableTo(callExpression, replaced.textOffset)) {
|
||||
moveLambdaOutsideParenthesesIntention.applyTo(callExpression, editor)
|
||||
}
|
||||
callExpression.getLastLambdaExpression()?.moveFunctionLiteralOutsideParenthesesIfPossible()
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -97,7 +97,7 @@ class ConvertFunctionTypeParameterToReceiverIntention : SelfTargetingRangeIntent
|
||||
"$receiver.${expression.text}(${arguments.joinToString()})"
|
||||
)
|
||||
expression.replaced(adapterLambda).let {
|
||||
MoveLambdaOutsideParenthesesIntention.moveFunctionLiteralOutsideParenthesesIfPossible(it)
|
||||
it.moveFunctionLiteralOutsideParenthesesIfPossible()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ class ConvertFunctionTypeParameterToReceiverIntention : SelfTargetingRangeIntent
|
||||
} as KtLambdaExpression
|
||||
|
||||
expression.replaced(replacingLambda).let {
|
||||
MoveLambdaOutsideParenthesesIntention.moveFunctionLiteralOutsideParenthesesIfPossible(it)
|
||||
it.moveFunctionLiteralOutsideParenthesesIfPossible()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,13 +24,10 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.core.*
|
||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.DOUBLE_COLON_LHS
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
|
||||
@@ -123,10 +120,7 @@ class ConvertReferenceToLambdaIntention : SelfTargetingOffsetIndependentIntentio
|
||||
ShortenReferences.DEFAULT.process(element.replaced(wrappedExpression))
|
||||
|
||||
if (valueArgumentParent != null && callGrandParent != null) {
|
||||
val moveOutOfParenthesis = MoveLambdaOutsideParenthesesIntention()
|
||||
if (moveOutOfParenthesis.isApplicableTo(callGrandParent, valueArgumentParent.startOffset)) {
|
||||
moveOutOfParenthesis.applyTo(callGrandParent, editor)
|
||||
}
|
||||
callGrandParent.getLastLambdaExpression()?.moveFunctionLiteralOutsideParenthesesIfPossible()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.core.canMoveLambdaOutsideParentheses
|
||||
import org.jetbrains.kotlin.idea.core.getLastLambdaExpression
|
||||
import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParentheses
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.psi.KtLambdaExpression
|
||||
import org.jetbrains.kotlin.psi.KtValueArgument
|
||||
import org.jetbrains.kotlin.psi.KtValueArgumentList
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containsInside
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
|
||||
class MoveLambdaOutsideParenthesesIntention : SelfTargetingIntention<KtCallExpression>(KtCallExpression::class.java, "Move lambda argument out of parentheses") {
|
||||
companion object {
|
||||
fun moveFunctionLiteralOutsideParenthesesIfPossible(expression: KtLambdaExpression) {
|
||||
val call = ((expression.parent as? KtValueArgument)?.parent as? KtValueArgumentList)?.parent as? KtCallExpression ?: return
|
||||
if (call.canMoveLambdaOutsideParentheses()) {
|
||||
call.moveFunctionLiteralOutsideParentheses()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun isApplicableTo(element: KtCallExpression, caretOffset: Int): Boolean {
|
||||
if (!element.canMoveLambdaOutsideParentheses()) return false
|
||||
|
||||
val lambdaExpression = element.getLastLambdaExpression() ?: return false
|
||||
val argument = lambdaExpression.getStrictParentOfType<KtValueArgument>() ?: return false
|
||||
if (caretOffset < argument.startOffset) return false
|
||||
val bodyRange = lambdaExpression.bodyExpression?.textRange ?: return true
|
||||
return !bodyRange.containsInside(caretOffset)
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtCallExpression, editor: Editor?) {
|
||||
element.moveFunctionLiteralOutsideParentheses()
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParenthesesIfPossible
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.core.setVisibility
|
||||
import org.jetbrains.kotlin.idea.inspections.*
|
||||
@@ -69,7 +70,7 @@ object J2KPostProcessingRegistrar {
|
||||
init {
|
||||
_processings.add(RemoveExplicitTypeArgumentsProcessing())
|
||||
_processings.add(RemoveRedundantOverrideVisibilityProcessing())
|
||||
_processings.add(MoveLambdaOutsideParenthesesProcessing())
|
||||
registerInspectionBasedProcessing(MoveLambdaOutsideParenthesesInspection())
|
||||
_processings.add(FixObjectStringConcatenationProcessing())
|
||||
_processings.add(ConvertToStringTemplateProcessing())
|
||||
_processings.add(UsePropertyAccessSyntaxProcessing())
|
||||
@@ -243,19 +244,6 @@ object J2KPostProcessingRegistrar {
|
||||
}
|
||||
}
|
||||
|
||||
private class MoveLambdaOutsideParenthesesProcessing : J2kPostProcessing {
|
||||
override val writeActionNeeded = true
|
||||
|
||||
private val intention = MoveLambdaOutsideParenthesesIntention()
|
||||
|
||||
override fun createAction(element: KtElement, diagnostics: Diagnostics): (() -> Unit)? {
|
||||
if (element !is KtCallExpression) return null
|
||||
val literalArgument = element.valueArguments.lastOrNull()?.getArgumentExpression()?.unpackFunctionLiteral() ?: return null
|
||||
if (!intention.isApplicableTo(element, literalArgument.textOffset)) return null
|
||||
return { intention.applyTo(element, null) }
|
||||
}
|
||||
}
|
||||
|
||||
private class ConvertToStringTemplateProcessing : J2kPostProcessing {
|
||||
override val writeActionNeeded = true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user