CodeToInlineBuilder: cleanup code

This commit is contained in:
Dmitry Gridin
2020-04-29 15:07:56 +07:00
parent bcefee0105
commit aafb4482c3
@@ -1,17 +1,6 @@
/* /*
* Copyright 2010-2016 JetBrains s.r.o. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* 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.codeInliner package org.jetbrains.kotlin.idea.codeInliner
@@ -64,12 +53,11 @@ class CodeToInlineBuilder(
reformat: Boolean reformat: Boolean
): CodeToInline { ): CodeToInline {
var bindingContext = analyze() var bindingContext = analyze()
val alwaysKeepMainExpression = when (val descriptor = mainExpression.getResolvedCall(bindingContext)?.resultingDescriptor) {
val descriptor = mainExpression.getResolvedCall(bindingContext)?.resultingDescriptor
val alwaysKeepMainExpression = when (descriptor) {
is PropertyDescriptor -> descriptor.getter?.isDefault == false is PropertyDescriptor -> descriptor.getter?.isDefault == false
else -> false else -> false
} }
val codeToInline = MutableCodeToInline( val codeToInline = MutableCodeToInline(
mainExpression, mainExpression,
statementsBefore.toMutableList(), statementsBefore.toMutableList(),
@@ -102,6 +90,7 @@ class CodeToInlineBuilder(
ErrorUtils.containsErrorTypeInParameters(lambdaDescriptor) || ErrorUtils.containsErrorTypeInParameters(lambdaDescriptor) ||
ErrorUtils.containsErrorType(lambdaDescriptor.returnType) ErrorUtils.containsErrorType(lambdaDescriptor.returnType)
) return null ) return null
return lambdaDescriptor.valueParameters.joinToString { return lambdaDescriptor.valueParameters.joinToString {
it.name.render() + ": " + IdeDescriptorRenderers.SOURCE_CODE.renderType(it.type) it.name.render() + ": " + IdeDescriptorRenderers.SOURCE_CODE.renderType(it.type)
} }
@@ -110,10 +99,10 @@ class CodeToInlineBuilder(
private fun addFunctionLiteralParameterTypes(parameters: String, inlinedExpression: KtExpression) { private fun addFunctionLiteralParameterTypes(parameters: String, inlinedExpression: KtExpression) {
val containingFile = inlinedExpression.containingKtFile val containingFile = inlinedExpression.containingKtFile
val resolutionFacade = containingFile.getResolutionFacade() val resolutionFacade = containingFile.getResolutionFacade()
val lambdaExpr = inlinedExpression.unpackFunctionLiteral(true).sure { val lambdaExpr = inlinedExpression.unpackFunctionLiteral(true).sure {
"can't find function literal expression for " + inlinedExpression.text "can't find function literal expression for " + inlinedExpression.text
} }
if (!needToAddParameterTypes(lambdaExpr, resolutionFacade)) return if (!needToAddParameterTypes(lambdaExpr, resolutionFacade)) return
SpecifyExplicitLambdaSignatureIntention.applyWithParameters(lambdaExpr, parameters) SpecifyExplicitLambdaSignatureIntention.applyWithParameters(lambdaExpr, parameters)
} }
@@ -127,11 +116,11 @@ class CodeToInlineBuilder(
return context.diagnostics.any { diagnostic -> return context.diagnostics.any { diagnostic ->
val factory = diagnostic.factory val factory = diagnostic.factory
val element = diagnostic.psiElement val element = diagnostic.psiElement
val hasCantInferParameter = factory == Errors.CANNOT_INFER_PARAMETER_TYPE && val hasCantInferParameter = factory == Errors.CANNOT_INFER_PARAMETER_TYPE && element.parent.parent == functionLiteral
element.parent.parent == functionLiteral
val hasUnresolvedItOrThis = factory == Errors.UNRESOLVED_REFERENCE && val hasUnresolvedItOrThis = factory == Errors.UNRESOLVED_REFERENCE &&
element.text == "it" && element.text == "it" &&
element.getStrictParentOfType<KtFunctionLiteral>() == functionLiteral element.getStrictParentOfType<KtFunctionLiteral>() == functionLiteral
hasCantInferParameter || hasUnresolvedItOrThis hasCantInferParameter || hasUnresolvedItOrThis
} }
} }
@@ -173,12 +162,14 @@ class CodeToInlineBuilder(
} else { } else {
target.importableFqName target.importableFqName
} }
if (importableFqName != null) { if (importableFqName != null) {
val lexicalScope = (expression?.containingFile as? KtFile)?.getResolutionScope(bindingContext, resolutionFacade) val lexicalScope = (expression.containingFile as? KtFile)?.getResolutionScope(bindingContext, resolutionFacade)
val lookupName = importableFqName?.let { val lookupName = lexicalScope?.findClassifier(importableFqName.shortName(), NoLookupLocation.FROM_IDE)
lexicalScope?.findClassifier(it.shortName(), NoLookupLocation.FROM_IDE)?.typeConstructor ?.typeConstructor
?.declarationDescriptor?.fqNameOrNull() ?.declarationDescriptor
} ?.fqNameOrNull()
codeToInline.fqNamesToImport.add(lookupName ?: importableFqName) codeToInline.fqNamesToImport.add(lookupName ?: importableFqName)
} }
} }
@@ -189,6 +180,7 @@ class CodeToInlineBuilder(
} else if (target is TypeParameterDescriptor && target.containingDeclaration == targetCallable) { } else if (target is TypeParameterDescriptor && target.containingDeclaration == targetCallable) {
expression.putCopyableUserData(CodeToInline.TYPE_PARAMETER_USAGE_KEY, target.name) expression.putCopyableUserData(CodeToInline.TYPE_PARAMETER_USAGE_KEY, target.name)
} }
if (targetCallable !is ImportedFromObjectCallableDescriptor<*>) { if (targetCallable !is ImportedFromObjectCallableDescriptor<*>) {
val resolvedCall = expression.getResolvedCall(bindingContext) val resolvedCall = expression.getResolvedCall(bindingContext)
if (resolvedCall != null && resolvedCall.isReallySuccess()) { if (resolvedCall != null && resolvedCall.isReallySuccess()) {
@@ -196,6 +188,7 @@ class CodeToInlineBuilder(
resolvedCall.extensionReceiver resolvedCall.extensionReceiver
else else
resolvedCall.dispatchReceiver resolvedCall.dispatchReceiver
if (receiver is ImplicitReceiver) { if (receiver is ImplicitReceiver) {
val resolutionScope = expression.getResolutionScope(bindingContext, resolutionFacade) val resolutionScope = expression.getResolutionScope(bindingContext, resolutionFacade)
val receiverExpression = receiver.asExpression(resolutionScope, psiFactory) val receiverExpression = receiver.asExpression(resolutionScope, psiFactory)