Cleanup: KotlinSafeDeleteProcessor

This commit is contained in:
Mikhail Glukhikh
2018-06-26 13:11:59 +03:00
parent 8848d7a043
commit 5bdaef4983
@@ -51,7 +51,6 @@ import org.jetbrains.kotlin.idea.refactoring.isTrueJavaMethod
import org.jetbrains.kotlin.idea.references.KtReference import org.jetbrains.kotlin.idea.references.KtReference
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.usagesSearch.constructor
import org.jetbrains.kotlin.idea.search.usagesSearch.processDelegationCallConstructorUsages import org.jetbrains.kotlin.idea.search.usagesSearch.processDelegationCallConstructorUsages
import org.jetbrains.kotlin.idea.util.actualsForExpected import org.jetbrains.kotlin.idea.util.actualsForExpected
import org.jetbrains.kotlin.idea.util.liftToExpected import org.jetbrains.kotlin.idea.util.liftToExpected
@@ -91,15 +90,17 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
fun getSearchInfo(element: PsiElement) = NonCodeUsageSearchInfo(getIgnoranceCondition(), element) fun getSearchInfo(element: PsiElement) = NonCodeUsageSearchInfo(getIgnoranceCondition(), element)
fun searchKotlinDeclarationReferences(declaration: KtDeclaration): Sequence<PsiReference> { fun searchKotlinDeclarationReferences(declaration: KtDeclaration): Sequence<PsiReference> {
val elementsToSearch = if (declaration is KtParameter) declaration.withExpectedActuals() else listOf(declaration) val elementsToSearch = when (declaration) {
is KtParameter -> declaration.withExpectedActuals()
else -> listOf(declaration)
}
return elementsToSearch.asSequence().flatMap { return elementsToSearch.asSequence().flatMap {
val searchParameters = KotlinReferencesSearchParameters( val searchParameters = KotlinReferencesSearchParameters(
it, it,
it.useScope, it.useScope,
kotlinOptions = KotlinReferencesSearchOptions(acceptCallableOverrides = true) kotlinOptions = KotlinReferencesSearchOptions(acceptCallableOverrides = true)
) )
ReferencesSearch.search(searchParameters) ReferencesSearch.search(searchParameters).asSequence()
.asSequence()
.filterNot { reference -> getIgnoranceCondition().value(reference.element) } .filterNot { reference -> getIgnoranceCondition().value(reference.element) }
} }
} }
@@ -143,7 +144,7 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
val elementToPassToJava = when (element) { val elementToPassToJava = when (element) {
is KtLightFieldImpl<*> -> object : KtLightField by element { is KtLightFieldImpl<*> -> object : KtLightField by element {
// Suppress walking through initializer compiled PSI (it doesn't contain any reference expressions anyway) // Suppress walking through initializer compiled PSI (it doesn't contain any reference expressions anyway)
override fun getInitializer() = null override fun getInitializer(): PsiExpression? = null
} }
else -> element else -> element
} }
@@ -180,7 +181,11 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
usageElement.getNonStrictParentOfType<KtImportDirective>()?.let { importDirective -> usageElement.getNonStrictParentOfType<KtImportDirective>()?.let { importDirective ->
SafeDeleteImportDirectiveUsageInfo(importDirective, element) SafeDeleteImportDirectiveUsageInfo(importDirective, element)
} ?: usageElement.getParentOfTypeAndBranch<KtSuperTypeEntry> { typeReference }?.let { } ?: usageElement.getParentOfTypeAndBranch<KtSuperTypeEntry> { typeReference }?.let {
if (element is PsiClass && element.isInterface) SafeDeleteSuperTypeUsageInfo(it, element) else usageInfo if (element is PsiClass && element.isInterface) {
SafeDeleteSuperTypeUsageInfo(it, element)
} else {
usageInfo
}
} ?: if (forceReferencedElementUnwrapping) { } ?: if (forceReferencedElementUnwrapping) {
SafeDeleteReferenceJavaDeleteUsageInfo(usageElement, element.unwrapped, usageInfo.isSafeDelete) SafeDeleteReferenceJavaDeleteUsageInfo(usageElement, element.unwrapped, usageInfo.isSafeDelete)
} else usageInfo } else usageInfo
@@ -196,8 +201,7 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
} }
fun findUsagesByJavaProcessor(elements: Sequence<PsiElement>, insideDeleted: Condition<PsiElement>): Condition<PsiElement> = fun findUsagesByJavaProcessor(elements: Sequence<PsiElement>, insideDeleted: Condition<PsiElement>): Condition<PsiElement> =
elements elements.mapNotNull { element -> findUsagesByJavaProcessor(element, true)?.insideDeletedCondition }
.mapNotNull { element -> findUsagesByJavaProcessor(element, true)?.insideDeletedCondition }
.fold(insideDeleted) { condition1, condition2 -> Conditions.or(condition1, condition2) } .fold(insideDeleted) { condition1, condition2 -> Conditions.or(condition1, condition2) }
fun findUsagesByJavaProcessor(ktDeclaration: KtDeclaration): NonCodeUsageSearchInfo { fun findUsagesByJavaProcessor(ktDeclaration: KtDeclaration): NonCodeUsageSearchInfo {
@@ -270,13 +274,11 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
is KtNamedFunction -> { is KtNamedFunction -> {
if (element.isLocal) { if (element.isLocal) {
findKotlinDeclarationUsages(element) findKotlinDeclarationUsages(element)
} } else {
else {
val lightMethods = element.toLightMethods() val lightMethods = element.toLightMethods()
if (lightMethods.isNotEmpty()) { if (lightMethods.isNotEmpty()) {
lightMethods.map { method -> findUsagesByJavaProcessor(method, false) }.firstOrNull() lightMethods.map { method -> findUsagesByJavaProcessor(method, false) }.firstOrNull()
} } else {
else {
findKotlinDeclarationUsages(element) findKotlinDeclarationUsages(element)
} }
} }
@@ -293,8 +295,7 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
is KtProperty -> { is KtProperty -> {
if (element.isLocal) { if (element.isLocal) {
findKotlinDeclarationUsages(element) findKotlinDeclarationUsages(element)
} } else {
else {
findUsagesByJavaProcessor(element) findUsagesByJavaProcessor(element)
} }
} }
@@ -383,8 +384,7 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
element.actualsForExpected().forEach { element.actualsForExpected().forEach {
if (it is KtParameter) { if (it is KtParameter) {
(it.parent as? KtParameterList)?.removeParameter(it) (it.parent as? KtParameterList)?.removeParameter(it)
} } else {
else {
it.removeModifier(KtTokens.IMPL_KEYWORD) it.removeModifier(KtTokens.IMPL_KEYWORD)
it.removeModifier(KtTokens.ACTUAL_KEYWORD) it.removeModifier(KtTokens.ACTUAL_KEYWORD)
} }
@@ -437,11 +437,11 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
is KtParameter -> { is KtParameter -> {
val expectParameter = element.liftToExpected() as? KtParameter val expectParameter = element.liftToExpected() as? KtParameter
if (expectParameter != null && expectParameter != element) { if (expectParameter != null && expectParameter != element) {
if (shouldAllowPropagationToExpected(element)) { return if (shouldAllowPropagationToExpected(element)) {
return listOf(expectParameter) listOf(expectParameter)
} else { } else {
element.ownerFunction?.dropActualModifier = true element.ownerFunction?.dropActualModifier = true
return listOf(element) listOf(element)
} }
} }
@@ -457,8 +457,10 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
if (ApplicationManager.getApplication()!!.isUnitTestMode) return Collections.singletonList(element) if (ApplicationManager.getApplication()!!.isUnitTestMode) return Collections.singletonList(element)
return when (element) { return when (element) {
is KtNamedFunction, is KtProperty -> checkSuperMethods(element as KtDeclaration, allElementsToDelete, "delete (with usage search)") is KtNamedFunction, is KtProperty ->
else -> super.getElementsToSearch(element, module, allElementsToDelete) checkSuperMethods(element as KtDeclaration, allElementsToDelete, "delete (with usage search)")
else ->
super.getElementsToSearch(element, module, allElementsToDelete)
} }
} }
} }