Got rid of special methods "applyTo" without editor parameter in intentions

This commit is contained in:
Valentin Kipyatkov
2016-01-14 17:24:57 +03:00
parent 552359ce03
commit 65f5dd1dd5
14 changed files with 33 additions and 78 deletions
@@ -33,10 +33,6 @@ class AddOperatorModifierIntention : SelfTargetingRangeIntention<KtNamedFunction
}
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
applyTo(element)
}
fun applyTo(element: KtNamedFunction) {
element.addModifier(KtTokens.OPERATOR_KEYWORD)
}
}
@@ -48,10 +48,6 @@ class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention<KtNamedFu
}
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
applyTo(element)
}
fun applyTo(element: KtNamedFunction) {
val commentSaver = CommentSaver(element)
val returnSaver = ReturnSaver(element)
@@ -95,7 +91,7 @@ class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention<KtNamedFu
val moveLambdaOutsideParenthesesIntention = MoveLambdaOutsideParenthesesIntention()
if (moveLambdaOutsideParenthesesIntention.isApplicableTo(callExpression, replaced.textOffset)) {
moveLambdaOutsideParenthesesIntention.applyTo(callExpression)
moveLambdaOutsideParenthesesIntention.applyTo(callExpression, editor)
}
}
}
@@ -90,7 +90,7 @@ class ConvertAssertToIfWithThrowIntention : SelfTargetingIntention<KtCallExpress
)
assertionErrorCall.valueArguments.single().getArgumentExpression()!!.replace(message)
simplifyConditionIfPossible(ifExpression)
simplifyConditionIfPossible(ifExpression, editor)
}
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)
}
private fun simplifyConditionIfPossible(ifExpression: KtIfExpression) {
private fun simplifyConditionIfPossible(ifExpression: KtIfExpression, editor: Editor?) {
val condition = ifExpression.condition as KtPrefixExpression
val simplifier = SimplifyNegatedBinaryExpressionIntention()
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?) {
applyTo(element)
}
fun applyTo(element: KtBinaryExpression): KtStringTemplateExpression {
return element.replaced(buildReplacement(element))
element.replaced(buildReplacement(element))
}
fun shouldSuggestToConvert(expression: KtBinaryExpression): Boolean {
@@ -48,11 +48,6 @@ class IfNullToElvisIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfE
}
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 factory = KtPsiFactory(element)
@@ -77,7 +72,7 @@ class IfNullToElvisIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfE
declaration.setType(explicitTypeToAdd)
}
return newElvis
editor?.caretModel?.moveToOffset(newElvis.right!!.textOffset)
}
private data class Data(
@@ -60,8 +60,4 @@ class MoveLambdaOutsideParenthesesIntention : SelfTargetingIntention<KtCallExpre
override fun applyTo(element: KtCallExpression, editor: Editor?) {
element.moveFunctionLiteralOutsideParentheses()
}
fun applyTo(element: KtCallExpression) {
element.moveFunctionLiteralOutsideParentheses()
}
}
@@ -82,10 +82,6 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
}
override fun applyTo(element: KtObjectLiteralExpression, editor: Editor?) {
applyTo(element)
}
fun applyTo(element: KtObjectLiteralExpression) {
val commentSaver = CommentSaver(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?) {
applyTo(element)
}
fun applyTo(element: KtParenthesizedExpression) {
element.replace(element.expression!!)
}
}
@@ -40,10 +40,6 @@ class SimplifyForIntention : SelfTargetingRangeIntention<KtForExpression>(
"Simplify 'for'"
) {
override fun applyTo(element: KtForExpression, editor: Editor?) {
applyTo(element)
}
fun applyTo(element: KtForExpression) {
val (propertiesToRemove, removeSelectorInLoopRange) = collectPropertiesToRemove(element) ?: return
val loopRange = element.loopRange ?: return
@@ -69,10 +69,6 @@ class SimplifyNegatedBinaryExpressionIntention : SelfTargetingRangeIntention<KtP
}
override fun applyTo(element: KtPrefixExpression, editor: Editor?) {
applyTo(element)
}
fun applyTo(element: KtPrefixExpression) {
val expression = KtPsiUtil.deparenthesize(element.baseExpression)!!
val operation = (expression as KtOperationExpression).operationReference.getReferencedNameElementType().negate()!!.value
@@ -57,11 +57,6 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
}
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 thenClause = element.then!!
@@ -77,6 +72,10 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
}
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)
}
}
}
@@ -51,11 +51,6 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
}
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 receiverExpression = condition.expressionComparedToNull()!!
@@ -69,7 +64,11 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
}
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)
@@ -87,10 +87,6 @@ class ReplaceGetOrSetIntention : SelfTargetingRangeIntention<KtDotQualifiedExpre
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
applyTo(element)
}
fun applyTo(element: KtDotQualifiedExpression) {
val isSet = element.toResolvedCall(BodyResolveMode.PARTIAL)!!.resultingDescriptor.name == OperatorNameConventions.SET
val allArguments = element.callExpression!!.valueArguments
assert(allArguments.isNotEmpty())
@@ -56,17 +56,17 @@ object J2KPostProcessingRegistrar {
_processings.add(RemoveRedundantSamAdaptersProcessing())
_processings.add(AccessorBodyToExpression())
registerIntentionBasedProcessing(IfThenToSafeAccessIntention()) { applyTo(it) }
registerIntentionBasedProcessing(IfThenToElvisIntention()) { applyTo(it) }
registerIntentionBasedProcessing(IfNullToElvisIntention()) { applyTo(it) }
registerIntentionBasedProcessing(SimplifyNegatedBinaryExpressionIntention()) { applyTo(it) }
registerIntentionBasedProcessing(ReplaceGetOrSetIntention(), additionalChecker = ReplaceGetOrSetInspection.additionalChecker) { applyTo(it) }
registerIntentionBasedProcessing(AddOperatorModifierIntention()) { applyTo(it) }
registerIntentionBasedProcessing(ObjectLiteralToLambdaIntention()) { applyTo(it) }
registerIntentionBasedProcessing(AnonymousFunctionToLambdaIntention()) { applyTo(it) }
registerIntentionBasedProcessing(RemoveUnnecessaryParenthesesIntention()) { applyTo(it) }
registerIntentionBasedProcessing(SimplifyForIntention()) { applyTo(it) }
registerIntentionBasedProcessing(SimplifyAssertNotNullIntention()) { applyTo(it, null) }
registerIntentionBasedProcessing(IfThenToSafeAccessIntention())
registerIntentionBasedProcessing(IfThenToElvisIntention())
registerIntentionBasedProcessing(IfNullToElvisIntention())
registerIntentionBasedProcessing(SimplifyNegatedBinaryExpressionIntention())
registerIntentionBasedProcessing(ReplaceGetOrSetIntention(), additionalChecker = ReplaceGetOrSetInspection.additionalChecker)
registerIntentionBasedProcessing(AddOperatorModifierIntention())
registerIntentionBasedProcessing(ObjectLiteralToLambdaIntention())
registerIntentionBasedProcessing(AnonymousFunctionToLambdaIntention())
registerIntentionBasedProcessing(RemoveUnnecessaryParenthesesIntention())
registerIntentionBasedProcessing(SimplifyForIntention())
registerIntentionBasedProcessing(SimplifyAssertNotNullIntention())
registerDiagnosticBasedProcessing<KtBinaryExpressionWithTypeRHS>(Errors.USELESS_CAST) { element, diagnostic ->
val expression = RemoveUselessCastFix.invoke(element)
@@ -106,17 +106,15 @@ object J2KPostProcessingRegistrar {
}
private inline fun <reified TElement : KtElement, TIntention: SelfTargetingRangeIntention<TElement>> registerIntentionBasedProcessing(
intention: TIntention,
crossinline apply: TIntention.(TElement) -> Unit
intention: TIntention
) {
//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(
intention: TIntention,
noinline additionalChecker: (TElement) -> Boolean,
crossinline apply: TIntention.(TElement) -> Unit
noinline additionalChecker: (TElement) -> Boolean
) {
_processings.add(object : J2kPostProcessing {
override fun createAction(element: KtElement, diagnostics: Diagnostics): (() -> Unit)? {
@@ -124,7 +122,7 @@ object J2KPostProcessingRegistrar {
val tElement = element as TElement
if (intention.applicabilityRange(tElement) == null) 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
val literalArgument = element.valueArguments.lastOrNull()?.getArgumentExpression()?.unpackFunctionLiteral() ?: 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)? {
if (element is KtBinaryExpression && intention.isApplicableTo(element) && intention.shouldSuggestToConvert(element)) {
return { intention.applyTo(element) }
return { intention.applyTo(element, null) }
}
else {
return null