Got rid of special methods "applyTo" without editor parameter in intentions
This commit is contained in:
@@ -33,10 +33,6 @@ class AddOperatorModifierIntention : SelfTargetingRangeIntention<KtNamedFunction
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
|
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
|
||||||
applyTo(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtNamedFunction) {
|
|
||||||
element.addModifier(KtTokens.OPERATOR_KEYWORD)
|
element.addModifier(KtTokens.OPERATOR_KEYWORD)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,10 +48,6 @@ class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention<KtNamedFu
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
|
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
|
||||||
applyTo(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtNamedFunction) {
|
|
||||||
val commentSaver = CommentSaver(element)
|
val commentSaver = CommentSaver(element)
|
||||||
val returnSaver = ReturnSaver(element)
|
val returnSaver = ReturnSaver(element)
|
||||||
|
|
||||||
@@ -95,7 +91,7 @@ class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention<KtNamedFu
|
|||||||
|
|
||||||
val moveLambdaOutsideParenthesesIntention = MoveLambdaOutsideParenthesesIntention()
|
val moveLambdaOutsideParenthesesIntention = MoveLambdaOutsideParenthesesIntention()
|
||||||
if (moveLambdaOutsideParenthesesIntention.isApplicableTo(callExpression, replaced.textOffset)) {
|
if (moveLambdaOutsideParenthesesIntention.isApplicableTo(callExpression, replaced.textOffset)) {
|
||||||
moveLambdaOutsideParenthesesIntention.applyTo(callExpression)
|
moveLambdaOutsideParenthesesIntention.applyTo(callExpression, editor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-3
@@ -90,7 +90,7 @@ class ConvertAssertToIfWithThrowIntention : SelfTargetingIntention<KtCallExpress
|
|||||||
)
|
)
|
||||||
assertionErrorCall.valueArguments.single().getArgumentExpression()!!.replace(message)
|
assertionErrorCall.valueArguments.single().getArgumentExpression()!!.replace(message)
|
||||||
|
|
||||||
simplifyConditionIfPossible(ifExpression)
|
simplifyConditionIfPossible(ifExpression, editor)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun extractMessageSingleExpression(functionLiteral: KtLambdaExpression, bindingContext: BindingContext): KtExpression? {
|
private fun extractMessageSingleExpression(functionLiteral: KtLambdaExpression, bindingContext: BindingContext): KtExpression? {
|
||||||
@@ -105,11 +105,11 @@ class ConvertAssertToIfWithThrowIntention : SelfTargetingIntention<KtCallExpress
|
|||||||
return valParameters.size > 1 && !KotlinBuiltIns.isAny(valParameters[1].type)
|
return valParameters.size > 1 && !KotlinBuiltIns.isAny(valParameters[1].type)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun simplifyConditionIfPossible(ifExpression: KtIfExpression) {
|
private fun simplifyConditionIfPossible(ifExpression: KtIfExpression, editor: Editor?) {
|
||||||
val condition = ifExpression.condition as KtPrefixExpression
|
val condition = ifExpression.condition as KtPrefixExpression
|
||||||
val simplifier = SimplifyNegatedBinaryExpressionIntention()
|
val simplifier = SimplifyNegatedBinaryExpressionIntention()
|
||||||
if (simplifier.isApplicableTo(condition)) {
|
if (simplifier.isApplicableTo(condition)) {
|
||||||
simplifier.applyTo(condition)
|
simplifier.applyTo(condition, editor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,11 +43,7 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
||||||
applyTo(element)
|
element.replaced(buildReplacement(element))
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtBinaryExpression): KtStringTemplateExpression {
|
|
||||||
return element.replaced(buildReplacement(element))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shouldSuggestToConvert(expression: KtBinaryExpression): Boolean {
|
fun shouldSuggestToConvert(expression: KtBinaryExpression): Boolean {
|
||||||
|
|||||||
@@ -48,11 +48,6 @@ class IfNullToElvisIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfE
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||||
val newElvis = applyTo(element)
|
|
||||||
editor?.caretModel?.moveToOffset(newElvis.right!!.textOffset)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtIfExpression): KtBinaryExpression {
|
|
||||||
val (initializer, declaration, ifNullExpr) = calcData(element)!!
|
val (initializer, declaration, ifNullExpr) = calcData(element)!!
|
||||||
val factory = KtPsiFactory(element)
|
val factory = KtPsiFactory(element)
|
||||||
|
|
||||||
@@ -77,7 +72,7 @@ class IfNullToElvisIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfE
|
|||||||
declaration.setType(explicitTypeToAdd)
|
declaration.setType(explicitTypeToAdd)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newElvis
|
editor?.caretModel?.moveToOffset(newElvis.right!!.textOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class Data(
|
private data class Data(
|
||||||
|
|||||||
@@ -60,8 +60,4 @@ class MoveLambdaOutsideParenthesesIntention : SelfTargetingIntention<KtCallExpre
|
|||||||
override fun applyTo(element: KtCallExpression, editor: Editor?) {
|
override fun applyTo(element: KtCallExpression, editor: Editor?) {
|
||||||
element.moveFunctionLiteralOutsideParentheses()
|
element.moveFunctionLiteralOutsideParentheses()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun applyTo(element: KtCallExpression) {
|
|
||||||
element.moveFunctionLiteralOutsideParentheses()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,10 +82,6 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtObjectLiteralExpression, editor: Editor?) {
|
override fun applyTo(element: KtObjectLiteralExpression, editor: Editor?) {
|
||||||
applyTo(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtObjectLiteralExpression) {
|
|
||||||
val commentSaver = CommentSaver(element)
|
val commentSaver = CommentSaver(element)
|
||||||
|
|
||||||
val (@Suppress("UNUSED_VARIABLE") baseTypeRef, baseType, singleFunction) = extractData(element)!!
|
val (@Suppress("UNUSED_VARIABLE") baseTypeRef, baseType, singleFunction) = extractData(element)!!
|
||||||
|
|||||||
@@ -29,10 +29,6 @@ class RemoveUnnecessaryParenthesesIntention : SelfTargetingRangeIntention<KtPare
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtParenthesizedExpression, editor: Editor?) {
|
override fun applyTo(element: KtParenthesizedExpression, editor: Editor?) {
|
||||||
applyTo(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtParenthesizedExpression) {
|
|
||||||
element.replace(element.expression!!)
|
element.replace(element.expression!!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,10 +40,6 @@ class SimplifyForIntention : SelfTargetingRangeIntention<KtForExpression>(
|
|||||||
"Simplify 'for'"
|
"Simplify 'for'"
|
||||||
) {
|
) {
|
||||||
override fun applyTo(element: KtForExpression, editor: Editor?) {
|
override fun applyTo(element: KtForExpression, editor: Editor?) {
|
||||||
applyTo(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtForExpression) {
|
|
||||||
val (propertiesToRemove, removeSelectorInLoopRange) = collectPropertiesToRemove(element) ?: return
|
val (propertiesToRemove, removeSelectorInLoopRange) = collectPropertiesToRemove(element) ?: return
|
||||||
|
|
||||||
val loopRange = element.loopRange ?: return
|
val loopRange = element.loopRange ?: return
|
||||||
|
|||||||
-4
@@ -69,10 +69,6 @@ class SimplifyNegatedBinaryExpressionIntention : SelfTargetingRangeIntention<KtP
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtPrefixExpression, editor: Editor?) {
|
override fun applyTo(element: KtPrefixExpression, editor: Editor?) {
|
||||||
applyTo(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtPrefixExpression) {
|
|
||||||
val expression = KtPsiUtil.deparenthesize(element.baseExpression)!!
|
val expression = KtPsiUtil.deparenthesize(element.baseExpression)!!
|
||||||
val operation = (expression as KtOperationExpression).operationReference.getReferencedNameElementType().negate()!!.value
|
val operation = (expression as KtOperationExpression).operationReference.getReferencedNameElementType().negate()!!.value
|
||||||
|
|
||||||
|
|||||||
+5
-6
@@ -57,11 +57,6 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||||
val elvis = applyTo(element)
|
|
||||||
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtIfExpression): KtBinaryExpression {
|
|
||||||
val condition = element.condition as KtBinaryExpression
|
val condition = element.condition as KtBinaryExpression
|
||||||
|
|
||||||
val thenClause = element.then!!
|
val thenClause = element.then!!
|
||||||
@@ -77,6 +72,10 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
}
|
}
|
||||||
|
|
||||||
val newExpr = element.replaced(KtPsiFactory(element).createExpressionByPattern("$0 ?: $1", left, right))
|
val newExpr = element.replaced(KtPsiFactory(element).createExpressionByPattern("$0 ?: $1", left, right))
|
||||||
return KtPsiUtil.deparenthesize(newExpr) as KtBinaryExpression
|
val elvis = KtPsiUtil.deparenthesize(newExpr) as KtBinaryExpression
|
||||||
|
|
||||||
|
if (editor != null) {
|
||||||
|
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -51,11 +51,6 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||||
val safeAccessExpr = applyTo(element)
|
|
||||||
safeAccessExpr.inlineReceiverIfApplicableWithPrompt(editor)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtIfExpression): KtSafeQualifiedExpression {
|
|
||||||
val condition = element.condition as KtBinaryExpression
|
val condition = element.condition as KtBinaryExpression
|
||||||
val receiverExpression = condition.expressionComparedToNull()!!
|
val receiverExpression = condition.expressionComparedToNull()!!
|
||||||
|
|
||||||
@@ -69,7 +64,11 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
|
|||||||
}
|
}
|
||||||
|
|
||||||
val newExpr = KtPsiFactory(element).createExpressionByPattern("$0?.$1", receiverExpression, selectorExpression) as KtSafeQualifiedExpression
|
val newExpr = KtPsiFactory(element).createExpressionByPattern("$0?.$1", receiverExpression, selectorExpression) as KtSafeQualifiedExpression
|
||||||
return element.replaced(newExpr)
|
val safeAccessExpr = element.replaced(newExpr)
|
||||||
|
|
||||||
|
if (editor != null) {
|
||||||
|
safeAccessExpr.inlineReceiverIfApplicableWithPrompt(editor)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clauseContainsAppropriateDotQualifiedExpression(clause: KtExpression, receiverExpression: KtExpression)
|
private fun clauseContainsAppropriateDotQualifiedExpression(clause: KtExpression, receiverExpression: KtExpression)
|
||||||
|
|||||||
-4
@@ -87,10 +87,6 @@ class ReplaceGetOrSetIntention : SelfTargetingRangeIntention<KtDotQualifiedExpre
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
|
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
|
||||||
applyTo(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun applyTo(element: KtDotQualifiedExpression) {
|
|
||||||
val isSet = element.toResolvedCall(BodyResolveMode.PARTIAL)!!.resultingDescriptor.name == OperatorNameConventions.SET
|
val isSet = element.toResolvedCall(BodyResolveMode.PARTIAL)!!.resultingDescriptor.name == OperatorNameConventions.SET
|
||||||
val allArguments = element.callExpression!!.valueArguments
|
val allArguments = element.callExpression!!.valueArguments
|
||||||
assert(allArguments.isNotEmpty())
|
assert(allArguments.isNotEmpty())
|
||||||
|
|||||||
@@ -56,17 +56,17 @@ object J2KPostProcessingRegistrar {
|
|||||||
_processings.add(RemoveRedundantSamAdaptersProcessing())
|
_processings.add(RemoveRedundantSamAdaptersProcessing())
|
||||||
_processings.add(AccessorBodyToExpression())
|
_processings.add(AccessorBodyToExpression())
|
||||||
|
|
||||||
registerIntentionBasedProcessing(IfThenToSafeAccessIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(IfThenToSafeAccessIntention())
|
||||||
registerIntentionBasedProcessing(IfThenToElvisIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(IfThenToElvisIntention())
|
||||||
registerIntentionBasedProcessing(IfNullToElvisIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(IfNullToElvisIntention())
|
||||||
registerIntentionBasedProcessing(SimplifyNegatedBinaryExpressionIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(SimplifyNegatedBinaryExpressionIntention())
|
||||||
registerIntentionBasedProcessing(ReplaceGetOrSetIntention(), additionalChecker = ReplaceGetOrSetInspection.additionalChecker) { applyTo(it) }
|
registerIntentionBasedProcessing(ReplaceGetOrSetIntention(), additionalChecker = ReplaceGetOrSetInspection.additionalChecker)
|
||||||
registerIntentionBasedProcessing(AddOperatorModifierIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(AddOperatorModifierIntention())
|
||||||
registerIntentionBasedProcessing(ObjectLiteralToLambdaIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(ObjectLiteralToLambdaIntention())
|
||||||
registerIntentionBasedProcessing(AnonymousFunctionToLambdaIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(AnonymousFunctionToLambdaIntention())
|
||||||
registerIntentionBasedProcessing(RemoveUnnecessaryParenthesesIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(RemoveUnnecessaryParenthesesIntention())
|
||||||
registerIntentionBasedProcessing(SimplifyForIntention()) { applyTo(it) }
|
registerIntentionBasedProcessing(SimplifyForIntention())
|
||||||
registerIntentionBasedProcessing(SimplifyAssertNotNullIntention()) { applyTo(it, null) }
|
registerIntentionBasedProcessing(SimplifyAssertNotNullIntention())
|
||||||
|
|
||||||
registerDiagnosticBasedProcessing<KtBinaryExpressionWithTypeRHS>(Errors.USELESS_CAST) { element, diagnostic ->
|
registerDiagnosticBasedProcessing<KtBinaryExpressionWithTypeRHS>(Errors.USELESS_CAST) { element, diagnostic ->
|
||||||
val expression = RemoveUselessCastFix.invoke(element)
|
val expression = RemoveUselessCastFix.invoke(element)
|
||||||
@@ -106,17 +106,15 @@ object J2KPostProcessingRegistrar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified TElement : KtElement, TIntention: SelfTargetingRangeIntention<TElement>> registerIntentionBasedProcessing(
|
private inline fun <reified TElement : KtElement, TIntention: SelfTargetingRangeIntention<TElement>> registerIntentionBasedProcessing(
|
||||||
intention: TIntention,
|
intention: TIntention
|
||||||
crossinline apply: TIntention.(TElement) -> Unit
|
|
||||||
) {
|
) {
|
||||||
//TODO: replace with optional argument when supported for inline functions
|
//TODO: replace with optional argument when supported for inline functions
|
||||||
return registerIntentionBasedProcessing<TElement, TIntention>(intention, { true }, apply)
|
return registerIntentionBasedProcessing<TElement, TIntention>(intention, { true })
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified TElement : KtElement, TIntention: SelfTargetingRangeIntention<TElement>> registerIntentionBasedProcessing(
|
private inline fun <reified TElement : KtElement, TIntention: SelfTargetingRangeIntention<TElement>> registerIntentionBasedProcessing(
|
||||||
intention: TIntention,
|
intention: TIntention,
|
||||||
noinline additionalChecker: (TElement) -> Boolean,
|
noinline additionalChecker: (TElement) -> Boolean
|
||||||
crossinline apply: TIntention.(TElement) -> Unit
|
|
||||||
) {
|
) {
|
||||||
_processings.add(object : J2kPostProcessing {
|
_processings.add(object : J2kPostProcessing {
|
||||||
override fun createAction(element: KtElement, diagnostics: Diagnostics): (() -> Unit)? {
|
override fun createAction(element: KtElement, diagnostics: Diagnostics): (() -> Unit)? {
|
||||||
@@ -124,7 +122,7 @@ object J2KPostProcessingRegistrar {
|
|||||||
val tElement = element as TElement
|
val tElement = element as TElement
|
||||||
if (intention.applicabilityRange(tElement) == null) return null
|
if (intention.applicabilityRange(tElement) == null) return null
|
||||||
if (!additionalChecker(tElement)) return null
|
if (!additionalChecker(tElement)) return null
|
||||||
return { intention.apply(element) }
|
return { intention.applyTo(element, null) }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -172,7 +170,7 @@ object J2KPostProcessingRegistrar {
|
|||||||
if (element !is KtCallExpression) return null
|
if (element !is KtCallExpression) return null
|
||||||
val literalArgument = element.valueArguments.lastOrNull()?.getArgumentExpression()?.unpackFunctionLiteral() ?: return null
|
val literalArgument = element.valueArguments.lastOrNull()?.getArgumentExpression()?.unpackFunctionLiteral() ?: return null
|
||||||
if (!intention.isApplicableTo(element, literalArgument.textOffset)) return null
|
if (!intention.isApplicableTo(element, literalArgument.textOffset)) return null
|
||||||
return { intention.applyTo(element) }
|
return { intention.applyTo(element, null) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +179,7 @@ object J2KPostProcessingRegistrar {
|
|||||||
|
|
||||||
override fun createAction(element: KtElement, diagnostics: Diagnostics): (() -> Unit)? {
|
override fun createAction(element: KtElement, diagnostics: Diagnostics): (() -> Unit)? {
|
||||||
if (element is KtBinaryExpression && intention.isApplicableTo(element) && intention.shouldSuggestToConvert(element)) {
|
if (element is KtBinaryExpression && intention.isApplicableTo(element) && intention.shouldSuggestToConvert(element)) {
|
||||||
return { intention.applyTo(element) }
|
return { intention.applyTo(element, null) }
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return null
|
return null
|
||||||
|
|||||||
Reference in New Issue
Block a user