Replace some KtDeclaration.analyze() with resolveToDescriptorIfAny()
Also, enhance comment on KtElement.analyze()
This commit is contained in:
@@ -72,14 +72,26 @@ fun KtFile.resolveImportReference(fqName: FqName): Collection<DeclarationDescrip
|
||||
|
||||
|
||||
// This and next function are used for 'normal' element analysis
|
||||
// Their exact semantics is a bit unclear and depends on 'bodyResolveMode'
|
||||
// They are expected to provide correct descriptors for the element
|
||||
// but not diagnostics, trace slices are provided only partially
|
||||
// Element body analysis, if any, is not guaranteed
|
||||
// For compiler-compatible analysis, analyzeFully is recommended
|
||||
// See ResolveSessionForBodies, ResolveElementCache
|
||||
@JvmOverloads fun KtElement.analyze(bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL): BindingContext =
|
||||
getResolutionFacade().analyze(this, bodyResolveMode)
|
||||
// This analysis *should* provide all information extractable from this KtElement except:
|
||||
// - it does not analyze bodies of functions
|
||||
// - it does not analyze content of classes
|
||||
// - it does not analyze initializers / accessors for member / top-level properties
|
||||
// This information includes related descriptors, resolved calls (but not inside body, see above!)
|
||||
// and many other binding context slices.
|
||||
// Normally, the function is used on local declarations or statements / expressions
|
||||
// Any usage on non-local declaration is a bit suspicious,
|
||||
// consider replacing it with resolveToDescriptorIfAny and
|
||||
// remember that body / content is not analyzed;
|
||||
// if it's necessary, use analyzeWithContent() / analyzeWithDeclarations().
|
||||
//
|
||||
// If you need diagnostics in result context, use BodyResolveMode.PARTIAL_WITH_DIAGNOSTICS.
|
||||
// BodyResolveMode.FULL analyzes all statements on the level of KtElement and above.
|
||||
// BodyResolveMode.PARTIAL analyzes only statements necessary for this KtElement precise analysis.
|
||||
//
|
||||
// See also: ResolveSessionForBodies, ResolveElementCache
|
||||
@JvmOverloads
|
||||
fun KtElement.analyze(bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL): BindingContext =
|
||||
getResolutionFacade().analyze(this, bodyResolveMode)
|
||||
|
||||
fun KtElement.analyzeAndGetResult(): AnalysisResult {
|
||||
val resolutionFacade = getResolutionFacade()
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.asJava.unwrapped
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getJavaMethodDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.references.unwrappedTargets
|
||||
import org.jetbrains.kotlin.idea.search.declarationsSearch.HierarchySearchRequest
|
||||
import org.jetbrains.kotlin.idea.search.declarationsSearch.searchInheritors
|
||||
@@ -43,9 +44,10 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.OverridingUtil
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
val KtDeclaration.descriptor: DeclarationDescriptor?
|
||||
get() = this.analyze().get(BindingContext.DECLARATION_TO_DESCRIPTOR, this)
|
||||
get() = this.resolveToDescriptorIfAny(BodyResolveMode.FULL)
|
||||
|
||||
val KtDeclaration.constructor: ConstructorDescriptor?
|
||||
get() {
|
||||
|
||||
+4
-8
@@ -21,25 +21,21 @@ import com.intellij.ide.util.treeView.smartTree.TreeElement
|
||||
import com.intellij.psi.NavigatablePsiElement
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import java.util.*
|
||||
|
||||
class KotlinInheritedMembersNodeProvider: InheritedMembersNodeProvider<TreeElement>() {
|
||||
override fun provideNodes(node: TreeElement): Collection<TreeElement> {
|
||||
if (node !is KotlinStructureViewElement) return listOf()
|
||||
|
||||
val element = node.element
|
||||
if (element !is KtClassOrObject) return listOf()
|
||||
val element = node.element as? KtClassOrObject ?: return listOf()
|
||||
|
||||
val project = element.project
|
||||
|
||||
val context = element.analyze()
|
||||
val descriptor = context[BindingContext.DECLARATION_TO_DESCRIPTOR, element]
|
||||
|
||||
if (descriptor !is ClassifierDescriptor) return listOf()
|
||||
val descriptor = element.resolveToDescriptorIfAny(BodyResolveMode.FULL) as? ClassifierDescriptor ?: return listOf()
|
||||
|
||||
val children = ArrayList<TreeElement>()
|
||||
|
||||
|
||||
+2
-2
@@ -37,6 +37,7 @@ import org.jetbrains.android.util.AndroidUtils
|
||||
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingIntention
|
||||
@@ -231,8 +232,7 @@ class KotlinAndroidAddStringResource : SelfTargetingIntention<KtLiteralStringTem
|
||||
}
|
||||
|
||||
private fun KtClassOrObject.isSubclassOfAny(baseClasses: Collection<String>): Boolean {
|
||||
val bindingContext = analyze(BodyResolveMode.PARTIAL)
|
||||
val declarationDescriptor = bindingContext.get(BindingContext.CLASS, this)
|
||||
val declarationDescriptor = resolveToDescriptorIfAny() as? ClassDescriptor
|
||||
return baseClasses.any { declarationDescriptor?.isSubclassOf(it) ?: false }
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.facet.implementingDescriptors
|
||||
import org.jetbrains.kotlin.idea.project.targetPlatform
|
||||
import org.jetbrains.kotlin.idea.util.module
|
||||
@@ -46,8 +47,7 @@ class KotlinMultiplatformJUnitRecognizer : JUnitRecognizer() {
|
||||
val implModules = moduleDescriptor.implementingDescriptors
|
||||
if (implModules.isEmpty()) return false
|
||||
|
||||
val bindingContext = origin.analyze(BodyResolveMode.PARTIAL)
|
||||
val methodDescriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, origin] ?: return false
|
||||
val methodDescriptor = origin.resolveToDescriptorIfAny() ?: return false
|
||||
return methodDescriptor.annotations.getAllAnnotations().any { it.isExpectOfAnnotation("org.junit.Test", implModules) }
|
||||
|
||||
}
|
||||
|
||||
+4
-6
@@ -25,13 +25,13 @@ import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor;
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils;
|
||||
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils;
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences;
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
|
||||
import org.jetbrains.kotlin.types.KotlinType;
|
||||
import org.jetbrains.kotlin.types.KotlinTypeKt;
|
||||
@@ -117,11 +117,9 @@ public class MoveDeclarationsOutHelper {
|
||||
|
||||
@NotNull
|
||||
private static KotlinType getPropertyType(@NotNull KtProperty property) {
|
||||
BindingContext bindingContext = ResolutionUtils.analyze(property, BodyResolveMode.PARTIAL);
|
||||
|
||||
VariableDescriptor propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property);
|
||||
assert propertyDescriptor != null : "Couldn't resolve property to property descriptor " + property.getText();
|
||||
return propertyDescriptor.getType();
|
||||
DeclarationDescriptor variableDescriptor = ResolutionUtils.resolveToDescriptorIfAny(property, BodyResolveMode.PARTIAL);
|
||||
assert variableDescriptor instanceof VariableDescriptor : "Couldn't resolve property to property descriptor " + property.getText();
|
||||
return ((VariableDescriptor) variableDescriptor).getType();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -28,8 +28,9 @@ import com.intellij.psi.*
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.asJava.toLightClass
|
||||
import org.jetbrains.kotlin.config.TargetPlatformKind
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.lightClasses.KtFakeLightClass
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.project.targetPlatform
|
||||
import org.jetbrains.kotlin.idea.search.allScope
|
||||
import org.jetbrains.kotlin.idea.stubindex.KotlinClassShortNameIndex
|
||||
@@ -40,7 +41,7 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class KotlinTypeHierarchyProvider : JavaTypeHierarchyProvider() {
|
||||
private fun getOriginalPsiClassOrCreateLightClass(classOrObject: KtClassOrObject, module: Module?): PsiClass? {
|
||||
@@ -69,7 +70,7 @@ class KotlinTypeHierarchyProvider : JavaTypeHierarchyProvider() {
|
||||
is KtClassOrObject -> getOriginalPsiClassOrCreateLightClass(target, module)
|
||||
is KtNamedFunction -> { // Factory methods
|
||||
val functionName = target.name
|
||||
val functionDescriptor = target.analyze()[BindingContext.FUNCTION, target] ?: return null
|
||||
val functionDescriptor = target.resolveToDescriptorIfAny(BodyResolveMode.FULL) as? FunctionDescriptor ?: return null
|
||||
val type = functionDescriptor.returnType ?: return null
|
||||
val returnTypeText = DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(type)
|
||||
if (returnTypeText != functionName) return null
|
||||
|
||||
@@ -10,8 +10,10 @@ import com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateEqualsAndHashcodeAction
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtFunction
|
||||
@@ -48,9 +50,7 @@ class ArrayInDataClassInspection : AbstractKotlinInspection() {
|
||||
if (declaration !is KtFunction) continue
|
||||
if (!declaration.hasModifier(KtTokens.OVERRIDE_KEYWORD)) continue
|
||||
if (declaration.nameAsName == OperatorNameConventions.EQUALS && declaration.valueParameters.size == 1) {
|
||||
val parameter = declaration.valueParameters.single()
|
||||
val context = declaration.analyze(BodyResolveMode.PARTIAL)
|
||||
val type = context.get(BindingContext.TYPE, parameter.typeReference)
|
||||
val type = (declaration.resolveToDescriptorIfAny() as? FunctionDescriptor)?.valueParameters?.singleOrNull()?.type
|
||||
if (type != null && KotlinBuiltIns.isNullableAny(type)) {
|
||||
overriddenEquals = true
|
||||
}
|
||||
|
||||
@@ -10,11 +10,10 @@ import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class RedundantExplicitTypeInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) =
|
||||
@@ -23,7 +22,7 @@ class RedundantExplicitTypeInspection : AbstractKotlinInspection() {
|
||||
val typeReference = property.typeReference ?: return
|
||||
val initializer = property.initializer ?: return
|
||||
|
||||
val type = property.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference] ?: return
|
||||
val type = (property.resolveToDescriptorIfAny() as? VariableDescriptor)?.type ?: return
|
||||
when (initializer) {
|
||||
is KtConstantExpression -> {
|
||||
when (initializer.node.elementType) {
|
||||
|
||||
@@ -10,12 +10,11 @@ import com.intellij.codeInspection.IntentionWrapper
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention
|
||||
import org.jetbrains.kotlin.psi.KtCodeFragment
|
||||
import org.jetbrains.kotlin.psi.namedFunctionVisitor
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||
|
||||
class RedundantUnitReturnTypeInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
||||
@@ -23,8 +22,7 @@ class RedundantUnitReturnTypeInspection : AbstractKotlinInspection(), CleanupLoc
|
||||
return namedFunctionVisitor(fun(function) {
|
||||
if (function.containingFile is KtCodeFragment) return
|
||||
val typeElement = function.typeReference?.typeElement ?: return
|
||||
val context = function.analyze(BodyResolveMode.PARTIAL)
|
||||
val descriptor = context[BindingContext.FUNCTION, function] ?: return
|
||||
val descriptor = function.resolveToDescriptorIfAny() as? FunctionDescriptor ?: return
|
||||
if (descriptor.returnType?.isUnit() == true) {
|
||||
if (!function.hasBlockBody()) {
|
||||
return
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.MainFunctionDetector
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.core.isOverridable
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatureConfiguration
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinMethodDescriptor
|
||||
@@ -40,6 +41,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class UnusedReceiverParameterInspection : AbstractKotlinInspection() {
|
||||
override val suppressionKey: String get() = "unused"
|
||||
@@ -130,7 +132,7 @@ class UnusedReceiverParameterInspection : AbstractKotlinInspection() {
|
||||
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return
|
||||
|
||||
val function = element.parent as? KtCallableDeclaration ?: return
|
||||
val callableDescriptor = function.analyze()[BindingContext.DECLARATION_TO_DESCRIPTOR, function] as? CallableDescriptor ?: return
|
||||
val callableDescriptor = function.resolveToDescriptorIfAny(BodyResolveMode.FULL) as? CallableDescriptor ?: return
|
||||
runChangeSignature(project, callableDescriptor, configureChangeSignature(), element, name)
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.jetbrains.kotlin.asJava.namedUnwrappedElement
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
@@ -50,7 +50,6 @@ import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypeAndBranch
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStartOffsetIn
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.findVariable
|
||||
@@ -211,15 +210,13 @@ class ConvertFunctionToPropertyIntention : SelfTargetingIntention<KtNamedFunctio
|
||||
return false
|
||||
}
|
||||
|
||||
val descriptor = element.analyze(BodyResolveMode.PARTIAL)[BindingContext.DECLARATION_TO_DESCRIPTOR, element] as? FunctionDescriptor
|
||||
?: return false
|
||||
val descriptor = element.resolveToDescriptorIfAny() as? FunctionDescriptor ?: return false
|
||||
val returnType = descriptor.returnType ?: return false
|
||||
return !KotlinBuiltIns.isUnit(returnType) && !KotlinBuiltIns.isNothing(returnType)
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
|
||||
val context = element.analyze(BodyResolveMode.PARTIAL)
|
||||
val descriptor = context[BindingContext.DECLARATION_TO_DESCRIPTOR, element] as FunctionDescriptor
|
||||
val descriptor = element.unsafeResolveToDescriptor(BodyResolveMode.PARTIAL) as FunctionDescriptor
|
||||
Converter(element.project, editor, descriptor).run()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.intellij.util.containers.MultiMap
|
||||
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
||||
import org.jetbrains.kotlin.idea.refactoring.*
|
||||
import org.jetbrains.kotlin.idea.references.KtReference
|
||||
@@ -43,7 +44,6 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.findFunction
|
||||
@@ -195,8 +195,7 @@ class ConvertPropertyToFunctionIntention : SelfTargetingIntention<KtProperty>(Kt
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtProperty, editor: Editor?) {
|
||||
val context = element.analyze()
|
||||
val descriptor = context[BindingContext.DECLARATION_TO_DESCRIPTOR, element] as? CallableDescriptor ?: return
|
||||
val descriptor = element.resolveToDescriptorIfAny(BodyResolveMode.FULL) as? CallableDescriptor ?: return
|
||||
Converter(element.project, descriptor).run()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.core.setType
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.isError
|
||||
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||
@@ -102,7 +100,7 @@ class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody
|
||||
}
|
||||
|
||||
private fun KtNamedFunction.returnType(): KotlinType? {
|
||||
val descriptor = analyze(BodyResolveMode.PARTIAL)[BindingContext.DECLARATION_TO_DESCRIPTOR, this] ?: return null
|
||||
val descriptor = resolveToDescriptorIfAny() ?: return null
|
||||
return (descriptor as FunctionDescriptor).returnType
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.idea.core.moveCaret
|
||||
import org.jetbrains.kotlin.idea.core.unblockDocument
|
||||
@@ -37,9 +38,9 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.ImportPath
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument
|
||||
import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||
import java.util.*
|
||||
|
||||
@@ -197,7 +198,7 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
|
||||
if (!hasBlockBody()) return body
|
||||
val block = body as? KtBlockExpression ?: return null
|
||||
val statement = block.statements.singleOrNull() ?: return null
|
||||
val returnsUnit = (analyze()[BindingContext.DECLARATION_TO_DESCRIPTOR, this] as? FunctionDescriptor)?.returnType?.isUnit() ?: return null
|
||||
val returnsUnit = (resolveToDescriptorIfAny(BodyResolveMode.FULL) as? FunctionDescriptor)?.returnType?.isUnit() ?: return null
|
||||
return when (statement) {
|
||||
is KtReturnExpression -> statement.returnedExpression
|
||||
else -> if (returnsUnit) statement else null
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiNameIdentifierOwner
|
||||
@@ -27,6 +26,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil
|
||||
import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix
|
||||
import org.jetbrains.kotlin.idea.refactoring.canRefactor
|
||||
@@ -150,8 +150,7 @@ open class AddModifierFix(
|
||||
val property = Errors.MUST_BE_INITIALIZED_OR_BE_ABSTRACT.cast(diagnostic).psiElement
|
||||
if (!property.isVar) return null
|
||||
|
||||
val context = property.analyze()
|
||||
val descriptor = context[BindingContext.DECLARATION_TO_DESCRIPTOR, property] ?: return null
|
||||
val descriptor = property.resolveToDescriptorIfAny(BodyResolveMode.FULL) ?: return null
|
||||
val type = (descriptor as? PropertyDescriptor)?.type ?: return null
|
||||
|
||||
if (TypeUtils.isNullableType(type)) return null
|
||||
|
||||
@@ -22,7 +22,7 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatureConfiguration
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinMethodDescriptor
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinTypeInfo
|
||||
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.idea.refactoring.changeSignature.runChangeSignature
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
class ChangeParameterTypeFix(element: KtParameter, type: KotlinType) : KotlinQuickFixAction<KtParameter>(element) {
|
||||
@@ -66,8 +66,7 @@ class ChangeParameterTypeFix(element: KtParameter, type: KotlinType) : KotlinQui
|
||||
val element = element ?: return
|
||||
val function = element.getStrictParentOfType<KtFunction>() ?: return
|
||||
val parameterIndex = function.valueParameters.indexOf(element)
|
||||
val context = function.analyze()
|
||||
val descriptor = context[BindingContext.DECLARATION_TO_DESCRIPTOR, function] as? FunctionDescriptor ?: return
|
||||
val descriptor = function.resolveToDescriptorIfAny(BodyResolveMode.FULL) as? FunctionDescriptor ?: return
|
||||
val configuration = object : KotlinChangeSignatureConfiguration {
|
||||
override fun configure(originalDescriptor: KotlinMethodDescriptor) = originalDescriptor.apply {
|
||||
parameters[if (receiver != null) parameterIndex + 1 else parameterIndex].currentTypeInfo = typeInfo
|
||||
|
||||
@@ -21,13 +21,13 @@ import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.intentions.RemoveEmptyPrimaryConstructorIntention
|
||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixAction<KtParameter>(parameter) {
|
||||
override fun getFamilyName() = ChangeFunctionSignatureFix.FAMILY_NAME
|
||||
@@ -40,8 +40,7 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
|
||||
val element = element ?: return
|
||||
val primaryConstructor = element.parent?.parent as? KtPrimaryConstructor
|
||||
val parameterList = element.parent as? KtParameterList
|
||||
val context = element.analyze()
|
||||
val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, element] as? ValueParameterDescriptor ?: return
|
||||
val parameterDescriptor = element.resolveToDescriptorIfAny(BodyResolveMode.FULL) as? ValueParameterDescriptor ?: return
|
||||
ChangeFunctionSignatureFix.runRemoveParameter(parameterDescriptor, element)
|
||||
val nextParameter = parameterList?.parameters?.getOrNull(parameterDescriptor.index)
|
||||
if (nextParameter != null) {
|
||||
|
||||
@@ -45,10 +45,7 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors
|
||||
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.*
|
||||
import org.jetbrains.kotlin.idea.core.isOverridable
|
||||
import org.jetbrains.kotlin.idea.findUsages.KotlinFunctionFindUsagesOptions
|
||||
import org.jetbrains.kotlin.idea.findUsages.KotlinPropertyFindUsagesOptions
|
||||
@@ -69,6 +66,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
|
||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
@@ -260,7 +258,7 @@ class InflowSlicer(
|
||||
.forEach { (it.element?.parent as? KtProperty)?.processPropertyAssignments() }
|
||||
}
|
||||
|
||||
val parameterDescriptor = analyze()[BindingContext.VALUE_PARAMETER, this] ?: return
|
||||
val parameterDescriptor = resolveToDescriptorIfAny(BodyResolveMode.FULL) ?: return
|
||||
|
||||
(function as? KtFunction)?.processCalls(parentUsage.scope.toSearchScope()) body@ {
|
||||
val refElement = it.element ?: return@body
|
||||
|
||||
+2
-2
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.core.getOrCreateCompanionObject
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.unwrapBlockOrParenthesis
|
||||
import org.jetbrains.kotlin.idea.util.findAnnotation
|
||||
@@ -54,8 +55,7 @@ class ParcelMigrateToParcelizeQuickFix(function: KtClass) : AbstractParcelableQu
|
||||
|
||||
private fun KtClass.findParcelerCompanionObject(): Pair<KtObjectDeclaration, ClassDescriptor>? {
|
||||
for (obj in companionObjects) {
|
||||
val bindingContext = obj.analyze(BodyResolveMode.PARTIAL)
|
||||
val objDescriptor = bindingContext[BindingContext.CLASS, obj] ?: continue
|
||||
val objDescriptor = obj.resolveToDescriptorIfAny() as? ClassDescriptor ?: continue
|
||||
for (superClassifier in objDescriptor.getAllSuperClassifiers()) {
|
||||
val superClass = superClassifier as? ClassDescriptor ?: continue
|
||||
if (superClass.fqNameSafe == PARCELER_FQNAME) return Pair(obj, objDescriptor)
|
||||
|
||||
Reference in New Issue
Block a user