From 0f441760c2ee39e5810586fa1978a5ac8d7ce529 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 22 Mar 2018 13:49:35 +0300 Subject: [PATCH] Refactoring: remove usages of `analyzeFully` Function isn't removed because it has usages from other plugins --- .../idea/caches/resolve/resolutionApi.kt | 43 +++++++++++++------ .../KotlinFrameExtraVariablesProvider.kt | 5 ++- .../stepping/KotlinSmartStepIntoHandler.kt | 5 ++- .../idea/inspections/LeakingThisInspection.kt | 7 +-- .../idea/quickfix/InlineTypeParameterFix.kt | 1 - .../kotlin/idea/refactoring/move/moveUtils.kt | 5 ++- .../kotlin/idea/ResolveElementCacheTest.kt | 14 ++++-- 7 files changed, 53 insertions(+), 27 deletions(-) diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt index 281cd2d0ddc..3f80376e445 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt @@ -128,21 +128,30 @@ fun KtDeclaration.analyzeWithContent(): BindingContext = inline fun T.analyzeWithContent(): BindingContext where T : KtDeclarationContainer, T : KtElement = getResolutionFacade().analyzeWithAllCompilerChecks(listOf(this)).bindingContext -// NB: for statements / expressions, usually should be replaced with analyze(), -// for declarations, analyzeWithContent() will do what you want. -@Deprecated( - "Use analyzeWithContent() instead, or use just analyze()", - ReplaceWith("analyze()") -) -fun KtElement.analyzeFully(): BindingContext = getResolutionFacade().analyzeWithAllCompilerChecks(listOf(this)).bindingContext - -// This and next function are expected to produce the same result as compiler -// for the given element and its children (including diagnostics, trace slices, descriptors, etc.) -// Not recommended to call both of them without real need -// See also KotlinResolveCache, KotlinResolveDataProvider +/** + * This function is expected to produce the same result as compiler for the whole file content (including diagnostics, + * trace slices, descriptors, etc.). + * + * It's not recommended to call this function without real need. + * + * @ref [KotlinCacheService] + * @ref [org.jetbrains.kotlin.idea.caches.resolve.PerFileAnalysisCache] + */ fun KtFile.analyzeWithAllCompilerChecks(vararg extraFiles: KtFile): AnalysisResult = KotlinCacheService.getInstance(project).getResolutionFacade(listOf(this) + extraFiles.toList()).analyzeWithAllCompilerChecks(listOf(this)) +/** + * This function is expected to produce the same result as compiler for the given element and its children (including diagnostics, + * trace slices, descriptors, etc.). For some expression element it actually performs analyze for some parent (usually declaration). + * + * It's not recommended to call this function without real need. + * + * NB: for statements / expressions, usually should be replaced with analyze(), + * for declarations, analyzeWithContent() will do what you want. + * + * @ref [KotlinCacheService] + * @ref [org.jetbrains.kotlin.idea.caches.resolve.PerFileAnalysisCache] + */ @Deprecated( "Use either KtFile.analyzeWithAllCompilerChecks() or KtElement.analyzeAndGetResult()", ReplaceWith("analyzeAndGetResult()") @@ -158,4 +167,12 @@ fun ResolutionFacade.resolveImportReference( val qualifiedExpressionResolver = this.getFrontendService(moduleDescriptor, QualifiedExpressionResolver::class.java) return qualifiedExpressionResolver.processImportReference( importDirective, moduleDescriptor, BindingTraceContext(), excludedImportNames = emptyList(), packageFragmentForVisibilityCheck = null)?.getContributedDescriptors() ?: emptyList() -} \ No newline at end of file +} + +@Suppress("DEPRECATION") +@Deprecated( + "This method is going to be removed in 1.3.0 release", + ReplaceWith("analyzeWithAllCompilerChecks().bindingContext"), + DeprecationLevel.ERROR +) +fun KtElement.analyzeFully(): BindingContext = analyzeWithAllCompilerChecks().bindingContext \ No newline at end of file diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/KotlinFrameExtraVariablesProvider.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/KotlinFrameExtraVariablesProvider.kt index cf5cec11742..503a6673418 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/KotlinFrameExtraVariablesProvider.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/KotlinFrameExtraVariablesProvider.kt @@ -32,7 +32,7 @@ import com.intellij.psi.util.PsiTreeUtil import com.intellij.util.text.CharArrayUtil import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.idea.KotlinFileType -import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully +import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithAllCompilerChecks import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils import org.jetbrains.kotlin.idea.refactoring.getLineEndOffset import org.jetbrains.kotlin.idea.refactoring.getLineStartOffset @@ -153,7 +153,8 @@ private class VariablesCollector( private fun isRefToProperty(expression: KtReferenceExpression): Boolean { // NB: analyze() cannot be called here, because DELEGATED_PROPERTY_RESOLVED_CALL will be always null // Looks like a bug - val context = expression.analyzeFully() + @Suppress("DEPRECATION") + val context = expression.analyzeWithAllCompilerChecks().bindingContext val descriptor = context[BindingContext.REFERENCE_TARGET, expression] if (descriptor is PropertyDescriptor) { val getter = descriptor.getter diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/stepping/KotlinSmartStepIntoHandler.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/stepping/KotlinSmartStepIntoHandler.kt index 49f8623648f..8df1932dd0f 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/stepping/KotlinSmartStepIntoHandler.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/stepping/KotlinSmartStepIntoHandler.kt @@ -34,7 +34,7 @@ import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully +import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithAllCompilerChecks import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde import org.jetbrains.kotlin.idea.util.application.runReadAction @@ -62,7 +62,8 @@ class KotlinSmartStepIntoHandler : JvmSmartStepIntoHandler() { val doc = PsiDocumentManager.getInstance(file.project).getDocument(file) ?: return emptyList() val lines = Range(doc.getLineNumber(elementTextRange.startOffset), doc.getLineNumber(elementTextRange.endOffset)) - val bindingContext = element.analyzeFully() + @Suppress("DEPRECATION") + val bindingContext = element.analyzeWithAllCompilerChecks().bindingContext val result = OrderedSet() // TODO support class initializers, local functions, delegated properties with specified type, setter for properties diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt index 84947be7bd1..1b39946a2cf 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt @@ -14,7 +14,7 @@ import com.intellij.psi.PsiElementVisitor import com.intellij.psi.search.searches.DefinitionsScopedSearch import org.jetbrains.kotlin.cfg.LeakingThisDescriptor.* import org.jetbrains.kotlin.descriptors.Modality -import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully +import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithAllCompilerChecks import org.jetbrains.kotlin.idea.quickfix.AddModifierFix import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtClass @@ -28,11 +28,12 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils class LeakingThisInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor { return expressionVisitor { expression -> - // We still use analyzeFully() here. + // We still use analyzeWithAllCompilerChecks() here. // It's possible to use analyze(), but then we should repeat class constructor consistency check // for different class internal elements, like KtProperty and KtClassInitializer. // It can affect performance, so yet we want to avoid this. - val context = expression.analyzeFully() + @Suppress("DEPRECATION") + val context = expression.analyzeWithAllCompilerChecks().bindingContext val leakingThisDescriptor = context.get(LEAKING_THIS, expression) ?: return@expressionVisitor val description = when (leakingThisDescriptor) { is NonFinalClass -> diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/InlineTypeParameterFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/InlineTypeParameterFix.kt index e6763912f19..b7a1523a480 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/InlineTypeParameterFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/InlineTypeParameterFix.kt @@ -20,7 +20,6 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Errors -import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt index 3dce77513f0..38d24935157 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt @@ -33,7 +33,7 @@ import org.jetbrains.kotlin.asJava.unwrapped import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.KotlinFileType import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully +import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithAllCompilerChecks import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde @@ -191,7 +191,8 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange( return null } - val bindingContext = analyzeFully() + @Suppress("DEPRECATION") + val bindingContext = analyzeWithAllCompilerChecks().bindingContext forEachDescendantOfType { refExpr -> if (refExpr !is KtSimpleNameExpression || refExpr.parent is KtThisExpression) return@forEachDescendantOfType diff --git a/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt b/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt index 5812125b069..198d073a132 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt @@ -20,7 +20,10 @@ import com.intellij.psi.PsiDocumentManager import junit.framework.TestCase import org.intellij.lang.annotations.Language import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor -import org.jetbrains.kotlin.idea.caches.resolve.* +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithAllCompilerChecks +import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade +import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor import org.jetbrains.kotlin.idea.imports.importableFqName import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor @@ -387,8 +390,9 @@ class C(param1: String = "", param2: Int = 0) { val defaultValue = ((file.declarations[0]) as KtClass).getPrimaryConstructor()!!.valueParameters[0].defaultValue!! // Kept to preserve correct behaviour of analyzeFully() on class internal elements - // TODO: delete after removal of KtElement.analyzeFully() - defaultValue.analyzeFully() + + @Suppress("DEPRECATION") + defaultValue.analyzeWithAllCompilerChecks() } fun testPrimaryConstructorAnnotationFullAnalysis() { @@ -397,7 +401,9 @@ class C(param1: String = "", param2: Int = 0) { """) as KtFile val annotationArguments = ((file.declarations[0]) as KtClass).getPrimaryConstructor()!!.annotationEntries[0].valueArgumentList!! - annotationArguments.analyzeFully() + + @Suppress("DEPRECATION") + annotationArguments.analyzeWithAllCompilerChecks() } fun testFunctionParameterAnnotation() {