From 72710c0318a6f883ad1fdb63e7d524547edff456 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 6 Aug 2015 15:28:32 +0300 Subject: [PATCH] Fix usages of AnalyzerUtil Sadly contains some changes that hard to merge into appropriate commits introduced after rebasing repeatedly --- .../kotlin/idea/analysis/AnalyzerUtil.kt | 26 +++++++++++++---- .../caches/resolve/CodeFragmentAnalyzer.kt | 4 +-- .../kotlin/idea/util/ShortenReferences.kt | 8 +++--- .../intentions/AddForLoopIndicesIntention.kt | 2 +- .../ConvertToExpressionBodyIntention.kt | 6 ++-- .../UsePropertyAccessSyntaxIntention.kt | 9 +++++- .../quickfix/DeprecatedSymbolUsageFixBase.kt | 5 ++-- .../quickfix/ReplaceWithAnnotationAnalyzer.kt | 28 +++++++++++++------ .../JetChangeSignatureUsageProcessor.java | 6 +--- .../KotlinIntroduceVariableHandler.java | 10 ++++--- 10 files changed, 69 insertions(+), 35 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/analysis/AnalyzerUtil.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/analysis/AnalyzerUtil.kt index c9531f59617..d1b38b88d86 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/analysis/AnalyzerUtil.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/analysis/AnalyzerUtil.kt @@ -32,20 +32,36 @@ import org.jetbrains.kotlin.types.expressions.JetTypeInfo jvmOverloads public fun JetExpression.computeTypeInfoInContext( scope: JetScope, + contextExpression: JetExpression = this, trace: BindingTrace = BindingTraceContext(), dataFlowInfo: DataFlowInfo = DataFlowInfo.EMPTY, - expectedType: JetType = TypeUtils.NO_EXPECTED_TYPE + expectedType: JetType = TypeUtils.NO_EXPECTED_TYPE, + isStatement: Boolean = false ): JetTypeInfo { - return getResolutionFacade().frontendService(this).getTypeInfo(scope, this, expectedType, dataFlowInfo, trace) + return contextExpression.getResolutionFacade().frontendService(contextExpression) + .getTypeInfo(scope, this, expectedType, dataFlowInfo, trace, isStatement) } jvmOverloads public fun JetExpression.analyzeInContext( scope: JetScope, + contextExpression: JetExpression = this, + trace: BindingTrace = BindingTraceContext(), + dataFlowInfo: DataFlowInfo = DataFlowInfo.EMPTY, + expectedType: JetType = TypeUtils.NO_EXPECTED_TYPE, + isStatement: Boolean = false +): BindingContext { + computeTypeInfoInContext(scope, contextExpression, trace, dataFlowInfo, expectedType, isStatement) + return trace.getBindingContext() +} + +jvmOverloads +public fun JetExpression.computeTypeInContext( + scope: JetScope, + contextExpression: JetExpression = this, trace: BindingTrace = BindingTraceContext(), dataFlowInfo: DataFlowInfo = DataFlowInfo.EMPTY, expectedType: JetType = TypeUtils.NO_EXPECTED_TYPE -): BindingContext { - computeTypeInfoInContext(scope, trace, dataFlowInfo, expectedType) - return trace.getBindingContext() +): JetType? { + return computeTypeInfoInContext(scope, contextExpression, trace, dataFlowInfo, expectedType).type } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt index 3c0a79b5a99..4ab7d6ad3a7 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt @@ -33,7 +33,6 @@ import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import javax.inject.Inject -import kotlin.properties.Delegates public class CodeFragmentAnalyzer( private val resolveSession: ResolveSession, @@ -57,7 +56,8 @@ public class CodeFragmentAnalyzer( codeFragmentExpression, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, - trace + trace, + false ) } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt index ad64e8837f7..9caabd79a44 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt @@ -20,15 +20,15 @@ import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiElement import com.intellij.psi.util.PsiTreeUtil -import org.jetbrains.kotlin.idea.analysis.analyzeInContext import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.PackageViewDescriptor -import org.jetbrains.kotlin.idea.resolve.ResolutionFacade +import org.jetbrains.kotlin.idea.analysis.analyzeInContext import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport import org.jetbrains.kotlin.idea.imports.getImportableTargets +import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.util.ShortenReferences.Options import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.psi.* @@ -339,7 +339,7 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options. val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, qualifiedExpression] ?: return false val selectorCopy = selector.copy() as JetReferenceExpression - val newContext = selectorCopy.analyzeInContext(scope) + val newContext = selectorCopy.analyzeInContext(scope, selector) val targetsWhenShort = (selectorCopy.getCalleeExpressionIfAny() as JetReferenceExpression).targets(newContext) val targetsMatch = targetsWhenShort.singleOrNull()?.asString() == target.asString() @@ -386,7 +386,7 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options. val targetBefore = thisExpression.getInstanceReference().targets(bindingContext).singleOrNull() ?: return val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, thisExpression] ?: return - val newContext = simpleThis.analyzeInContext(scope) + val newContext = simpleThis.analyzeInContext(scope, thisExpression) val targetAfter = simpleThis.getInstanceReference().targets(newContext).singleOrNull() if (targetBefore == targetAfter) { addElementToShorten(thisExpression) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 1b1a1127635..bc22fdd27b0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -47,7 +47,7 @@ public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention>() expression.forEachDescendantOfType { @@ -112,7 +112,7 @@ object ReplaceWithAnnotationAnalyzer { } // reanalyze expression - new usages of type parameters may be added - bindingContext = expression.analyzeInContext(scope) + bindingContext = analyzeInContext(expression, symbolDescriptor, scope, resolutionFacade) } val receiversToAdd = ArrayList>() @@ -160,6 +160,18 @@ object ReplaceWithAnnotationAnalyzer { return ReplacementExpression(expression, importFqNames) } + private fun analyzeInContext( + expression: JetExpression, + symbolDescriptor: CallableDescriptor, + scope: ChainedScope, + resolutionFacade: ResolutionFacade + ): BindingContext { + val traceContext = BindingTraceContext() + resolutionFacade.frontendService(symbolDescriptor.module) + .getTypeInfo(scope, expression, TypeUtils.NO_EXPECTED_TYPE, DataFlowInfo.EMPTY, traceContext, false) + return traceContext.bindingContext + } + private fun getResolutionScope(descriptor: DeclarationDescriptor): JetScope { return when (descriptor) { is PackageFragmentDescriptor -> { diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureUsageProcessor.java b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureUsageProcessor.java index 25e2aaef8da..d2ea6387006 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureUsageProcessor.java +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureUsageProcessor.java @@ -66,19 +66,15 @@ import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage; import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersPackage; import org.jetbrains.kotlin.renderer.DescriptorRenderer; import org.jetbrains.kotlin.resolve.BindingContext; -import org.jetbrains.kotlin.resolve.BindingTraceContext; import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; -import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; -import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind; import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode; import org.jetbrains.kotlin.resolve.scopes.JetScope; import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue; import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver; import org.jetbrains.kotlin.types.JetType; -import org.jetbrains.kotlin.types.TypeUtils; import java.util.*; @@ -741,7 +737,7 @@ public class JetChangeSignatureUsageProcessor implements ChangeSignatureUsagePro JetSimpleNameExpression labelExpr = newExpr.getTargetLabel(); if (labelExpr == null) continue; - BindingContext newContext = AnalysisPackage.analyzeInContext(newExpr, scope); + BindingContext newContext = AnalysisPackage.analyzeInContext(newExpr, scope, originalExpr); if (newContext.get(BindingContext.AMBIGUOUS_LABEL_TARGET, labelExpr) != null) { result.putValue( diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java index 640aaeb30b7..021094d2fab 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java @@ -42,9 +42,9 @@ import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage; import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils; import org.jetbrains.kotlin.idea.core.CorePackage; import org.jetbrains.kotlin.idea.core.KotlinNameSuggester; +import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator; import org.jetbrains.kotlin.idea.intentions.ConvertToBlockBodyIntention; import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention; -import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator; import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle; import org.jetbrains.kotlin.idea.refactoring.JetRefactoringUtil; import org.jetbrains.kotlin.idea.refactoring.introduce.IntroducePackage; @@ -65,10 +65,12 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode; import org.jetbrains.kotlin.resolve.scopes.JetScope; import org.jetbrains.kotlin.types.JetType; -import org.jetbrains.kotlin.types.TypeUtils; import org.jetbrains.kotlin.types.checker.JetTypeChecker; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; @@ -137,7 +139,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase { ObservableBindingTrace bindingTrace = new ObservableBindingTrace(new BindingTraceContext()); JetType typeNoExpectedType = AnalysisPackage.computeTypeInfoInContext( - expression, scope, bindingTrace, dataFlowInfo + expression, scope, expression, bindingTrace, dataFlowInfo ).getType(); if (expressionType != null && typeNoExpectedType != null && !JetTypeChecker.DEFAULT.equalTypes(expressionType, typeNoExpectedType)) {