Refactoring: introduce resolve to property descriptor

This commit is contained in:
Mikhail Glukhikh
2018-10-05 11:23:11 +03:00
parent b08f966428
commit 688fc386b6
7 changed files with 12 additions and 14 deletions
@@ -75,6 +75,10 @@ fun KtNamedFunction.resolveToDescriptorIfAny(bodyResolveMode: BodyResolveMode =
return (this as KtDeclaration).resolveToDescriptorIfAny(bodyResolveMode) as? FunctionDescriptor return (this as KtDeclaration).resolveToDescriptorIfAny(bodyResolveMode) as? FunctionDescriptor
} }
fun KtProperty.resolveToDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL): VariableDescriptor? {
return (this as KtDeclaration).resolveToDescriptorIfAny(bodyResolveMode) as? VariableDescriptor
}
fun KtParameter.resolveToParameterDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL): ValueParameterDescriptor? { fun KtParameter.resolveToParameterDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL): ValueParameterDescriptor? {
val context = analyze(bodyResolveMode) val context = analyze(bodyResolveMode)
return context.get(BindingContext.VALUE_PARAMETER, this) as? ValueParameterDescriptor return context.get(BindingContext.VALUE_PARAMETER, this) as? ValueParameterDescriptor
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
@@ -255,7 +254,7 @@ fun KtModifierListOwner.setVisibility(visibilityModifier: KtModifierKeywordToken
fun KtDeclaration.implicitVisibility(): KtModifierKeywordToken? { fun KtDeclaration.implicitVisibility(): KtModifierKeywordToken? {
return when { return when {
this is KtPropertyAccessor && isSetter && property.hasModifier(KtTokens.OVERRIDE_KEYWORD) -> { this is KtPropertyAccessor && isSetter && property.hasModifier(KtTokens.OVERRIDE_KEYWORD) -> {
(property.descriptor as? PropertyDescriptor)?.overriddenDescriptors?.forEach { (property.resolveToDescriptorIfAny() as? PropertyDescriptor)?.overriddenDescriptors?.forEach {
val visibility = it.setter?.visibility?.toKeywordToken() val visibility = it.setter?.visibility?.toKeywordToken()
if (visibility != null) return visibility if (visibility != null) return visibility
} }
@@ -25,7 +25,6 @@ import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiUtilCore; import com.intellij.psi.util.PsiUtilCore;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.VariableDescriptor; import org.jetbrains.kotlin.descriptors.VariableDescriptor;
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils; import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils;
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils; import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils;
@@ -117,9 +116,9 @@ public class MoveDeclarationsOutHelper {
@NotNull @NotNull
private static KotlinType getPropertyType(@NotNull KtProperty property) { private static KotlinType getPropertyType(@NotNull KtProperty property) {
DeclarationDescriptor variableDescriptor = ResolutionUtils.resolveToDescriptorIfAny(property, BodyResolveMode.PARTIAL); VariableDescriptor variableDescriptor = ResolutionUtils.resolveToDescriptorIfAny(property, BodyResolveMode.PARTIAL);
assert variableDescriptor instanceof VariableDescriptor : "Couldn't resolve property to property descriptor " + property.getText(); assert variableDescriptor != null : "Couldn't resolve property to property descriptor " + property.getText();
return ((VariableDescriptor) variableDescriptor).getType(); return variableDescriptor.getType();
} }
@NotNull @NotNull
@@ -10,7 +10,6 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder import com.intellij.codeInspection.ProblemsHolder
import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
@@ -22,7 +21,7 @@ class RedundantExplicitTypeInspection : AbstractKotlinInspection() {
val typeReference = property.typeReference ?: return val typeReference = property.typeReference ?: return
val initializer = property.initializer ?: return val initializer = property.initializer ?: return
val type = (property.resolveToDescriptorIfAny() as? VariableDescriptor)?.type ?: return val type = property.resolveToDescriptorIfAny()?.type ?: return
when (initializer) { when (initializer) {
is KtConstantExpression -> { is KtConstantExpression -> {
when (initializer.node.elementType) { when (initializer.node.elementType) {
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.* import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.FilterTransformationBase import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.FilterTransformationBase
@@ -83,7 +82,7 @@ class CountTransformation(
if (initialization.variable.countUsages(state.outerLoop) != 1) return null // this should be the only usage of this variable inside the loop if (initialization.variable.countUsages(state.outerLoop) != 1) return null // this should be the only usage of this variable inside the loop
val variableType = (initialization.variable.resolveToDescriptorIfAny() as? VariableDescriptor)?.type ?: return null val variableType = initialization.variable.resolveToDescriptorIfAny()?.type ?: return null
if (!KotlinBuiltIns.isInt(variableType)) return null if (!KotlinBuiltIns.isInt(variableType)) return null
val transformation = CountTransformation(state.outerLoop, state.inputVariable, initialization, null) val transformation = CountTransformation(state.outerLoop, state.inputVariable, initialization, null)
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.* import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
@@ -71,7 +70,7 @@ abstract class SumTransformationBase(
val value = statement.right ?: return null val value = statement.right ?: return null
val valueType = value.typeWithSmartCast()?.toSupportedType() ?: return null val valueType = value.typeWithSmartCast()?.toSupportedType() ?: return null
val sumType = (variableInitialization.variable.resolveToDescriptorIfAny() as? VariableDescriptor)?.type?.toSupportedType() ?: return null val sumType = variableInitialization.variable.resolveToDescriptorIfAny()?.type?.toSupportedType() ?: return null
val conversionFunctionName = when (sumType) { val conversionFunctionName = when (sumType) {
SupportedType.INT -> { SupportedType.INT -> {
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.ShortenReferences
@@ -33,7 +32,7 @@ import org.jetbrains.kotlin.types.isError
class ChangeAccessorTypeFix(element: KtPropertyAccessor) : KotlinQuickFixAction<KtPropertyAccessor>(element) { class ChangeAccessorTypeFix(element: KtPropertyAccessor) : KotlinQuickFixAction<KtPropertyAccessor>(element) {
private fun getType(): KotlinType? = private fun getType(): KotlinType? =
(element!!.property.resolveToDescriptorIfAny() as? VariableDescriptor)?.type?.takeUnless(KotlinType::isError) element!!.property.resolveToDescriptorIfAny()?.type?.takeUnless(KotlinType::isError)
override fun isAvailable(project: Project, editor: Editor?, file: KtFile) = getType() != null override fun isAvailable(project: Project, editor: Editor?, file: KtFile) = getType() != null