Fix extract refactoring for android extensions declarations (KT-11048)

Allow any target declarations in marking references. Otherwise conflicts
for references resolved to xml are not considered broken.

This also fix evaluate for extension fields.

 #KT-11048 Fixed
This commit is contained in:
Nikolay Krasko
2017-07-05 14:55:40 +03:00
parent a09b31b568
commit 72611d1337
11 changed files with 265 additions and 106 deletions
@@ -22,8 +22,7 @@ import com.intellij.openapi.util.Key
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
@@ -66,7 +65,7 @@ data class ExtractionOptions(
data class ResolveResult(
val originalRefExpr: KtSimpleNameExpression,
val declaration: PsiNameIdentifierOwner,
val declaration: PsiElement,
val descriptor: DeclarationDescriptor,
val resolvedCall: ResolvedCall<*>?
)
@@ -128,14 +127,17 @@ data class ExtractionData(
return function == null || !function.isInsideOf(physicalElements)
}
private tailrec fun getDeclaration(descriptor: DeclarationDescriptor, context: BindingContext): PsiNameIdentifierOwner? {
(DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor) as? PsiNameIdentifierOwner)?.let { return it }
private tailrec fun getDeclaration(descriptor: DeclarationDescriptor, context: BindingContext): PsiElement? {
val declaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor)
if (declaration is PsiNameIdentifierOwner) {
return declaration
}
return when {
isExtractableIt(descriptor, context) -> itFakeDeclaration
isSynthesizedInvoke(descriptor) -> synthesizedInvokeDeclaration
descriptor is SyntheticJavaPropertyDescriptor -> getDeclaration(descriptor.getMethod, context)
else -> null
else -> declaration
}
}
@@ -770,8 +770,9 @@ internal fun KtNamedDeclaration.getGeneratedBody() =
@JvmOverloads
fun ExtractableCodeDescriptor.validate(target: ExtractionTarget = ExtractionTarget.FUNCTION): ExtractableCodeDescriptorWithConflicts {
fun getDeclarationMessage(declaration: PsiNamedElement, messageKey: String, capitalize: Boolean = true): String {
val message = KotlinRefactoringBundle.message(messageKey, RefactoringUIUtil.getDescription(declaration, true))
fun getDeclarationMessage(declaration: PsiElement, messageKey: String, capitalize: Boolean = true): String {
val declarationStr = RefactoringUIUtil.getDescription(declaration, true)
val message = KotlinRefactoringBundle.message(messageKey, declarationStr)
return if (capitalize) message.capitalize() else message
}
@@ -94,7 +94,7 @@ private fun buildSignature(config: ExtractionGeneratorConfiguration, renderer: D
config.descriptor.parameters.forEach { parameter ->
param(parameter.name,
parameter.getParameterType(config.descriptor.extractionData.options.allowSpecialClassNames).typeAsString())
parameter.getParameterType(config.descriptor.extractionData.options.allowSpecialClassNames).typeAsString())
}
with(config.descriptor.returnType) {
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.util.containers.MultiMap
import org.jetbrains.kotlin.builtins.createFunctionType
import org.jetbrains.kotlin.cfg.pseudocode.Pseudocode
@@ -94,7 +95,7 @@ internal fun ExtractionData.inferParametersInfo(
}
else {
extensionReceiver
}) as? ReceiverValue
})
val twoReceivers = resolvedCall != null && resolvedCall.hasBothReceivers()
val dispatchReceiverDescriptor = (resolvedCall?.dispatchReceiver as? ImplicitReceiver)?.declarationDescriptor
@@ -136,7 +137,7 @@ internal fun ExtractionData.inferParametersInfo(
}
}
for (typeToCheck in info.typeParameters.flatMapTo(HashSet<KotlinType>()) { it.collectReferencedTypes(bindingContext) }) {
for (typeToCheck in info.typeParameters.flatMapTo(HashSet()) { it.collectReferencedTypes(bindingContext) }) {
typeToCheck.processTypeIfExtractable(info.typeParameters, info.nonDenotableTypes, options, targetScope)
}
@@ -179,7 +180,7 @@ private fun ExtractionData.extractReceiver(
is ConstructorDescriptor -> it.containingDeclaration
else -> null
} as? ClassifierDescriptor
}
}
if (referencedClassifierDescriptor != null) {
@@ -254,7 +255,10 @@ private fun ExtractionData.extractReceiver(
}
if (!extractThis) {
parameter.currentName = originalDeclaration.nameIdentifier?.text
parameter.currentName = when (originalDeclaration) {
is PsiNameIdentifierOwner -> originalDeclaration.nameIdentifier?.text
else -> null
}
}
parameter.refCount++