Refactoring: remove usages of analyzeFully

Function isn't removed because it has usages from other plugins
This commit is contained in:
Nikolay Krasko
2018-03-22 13:49:35 +03:00
parent 059013a8d3
commit 0f441760c2
7 changed files with 53 additions and 27 deletions
@@ -128,21 +128,30 @@ fun KtDeclaration.analyzeWithContent(): BindingContext =
inline fun <reified T> 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()
}
}
@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
@@ -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
@@ -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<SmartStepTarget>()
// TODO support class initializers, local functions, delegated properties with specified type, setter for properties
@@ -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 ->
@@ -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
@@ -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<KtReferenceExpression> { refExpr ->
if (refExpr !is KtSimpleNameExpression || refExpr.parent is KtThisExpression) return@forEachDescendantOfType
@@ -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() {