Refactored code to avoid duplication

This commit is contained in:
Valentin Kipyatkov
2020-02-09 23:36:09 +02:00
parent b406d85ca3
commit e76eaa4b5b
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.idea.slicer
import com.intellij.analysis.AnalysisScope import com.intellij.analysis.AnalysisScope
import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector.Access import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector.Access
import com.intellij.lang.java.JavaLanguage
import com.intellij.psi.PsiCall import com.intellij.psi.PsiCall
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod import com.intellij.psi.PsiMethod
@@ -132,23 +131,39 @@ abstract class Slicer(
abstract fun processChildren() abstract fun processChildren()
protected fun KtFunction.processCalls(scope: SearchScope, exactFunctionOnly: Boolean, usageProcessor: (UsageInfo) -> Unit) { protected fun KtFunction.processCalls(scope: SearchScope, includeOverriders: Boolean, usageProcessor: (UsageInfo) -> Unit) {
val options = KotlinFunctionFindUsagesOptions(project).apply { val options = KotlinFunctionFindUsagesOptions(project).apply {
isSearchForTextOccurrences = false isSearchForTextOccurrences = false
isSkipImportStatements = true isSkipImportStatements = true
searchScope = scope.intersectWith(useScope) searchScope = scope.intersectWith(useScope)
} }
if (exactFunctionOnly) {
processAllExactUsages(options, usageProcessor) val descriptor = unsafeResolveToDescriptor() as? CallableMemberDescriptor ?: return
} else { val superDescriptors = if (includeOverriders)
val descriptor = unsafeResolveToDescriptor() as? CallableMemberDescriptor ?: return descriptor.getDeepestSuperDeclarations()
for (superDescriptor in descriptor.getDeepestSuperDeclarations()) { else
val declaration = superDescriptor.originalSource.getPsi() ?: continue listOf(descriptor) + DescriptorUtils.getAllOverriddenDeclarations(descriptor)
when (declaration) {
is KtDeclaration -> declaration.processAllUsages(options, usageProcessor) for (superDescriptor in superDescriptors) {
val declaration = superDescriptor.originalSource.getPsi() ?: continue
when (declaration) {
is KtDeclaration -> {
if (includeOverriders) {
declaration.processAllUsages(options, usageProcessor)
} else {
declaration.processAllExactUsages(options, usageProcessor)
}
}
is PsiMethod -> {
// todo: work around the bug in JavaSliceProvider.transform() // todo: work around the bug in JavaSliceProvider.transform()
is PsiMethod -> processor.process(JavaSliceUsage.createRootUsage(declaration, parentUsage.params)) processor.process(JavaSliceUsage.createRootUsage(declaration, parentUsage.params))
else -> declaration.passToProcessor() }
else -> {
if (declaration in scope) {
declaration.passToProcessor()
}
} }
} }
} }
@@ -257,7 +272,7 @@ class InflowSlicer(
val parameterDescriptor = resolveToParameterDescriptorIfAny(BodyResolveMode.FULL) ?: return val parameterDescriptor = resolveToParameterDescriptorIfAny(BodyResolveMode.FULL) ?: return
(function as? KtFunction)?.processCalls(parentUsage.scope.toSearchScope(), exactFunctionOnly = false) body@{ (function as? KtFunction)?.processCalls(parentUsage.scope.toSearchScope(), includeOverriders = true) body@{
val refElement = it.element ?: return@body val refElement = it.element ?: return@body
val refParent = refElement.parent val refParent = refElement.parent
@@ -436,23 +451,14 @@ class OutflowSlicer(
private fun KtFunction.processFunction() { private fun KtFunction.processFunction() {
if (this is KtConstructor<*> || this is KtNamedFunction && name != null) { if (this is KtConstructor<*> || this is KtNamedFunction && name != null) {
processHierarchyUpward(parentUsage.scope) { processCalls(parentUsage.scope.toSearchScope(), includeOverriders = false) {
if (this is KtFunction) { when (val refElement = it.element) {
processCalls(parentUsage.scope.toSearchScope(), exactFunctionOnly = true) { null -> (it.reference as? LightMemberReference)?.element?.passToProcessor()
when (val refElement = it.element) { is KtExpression -> {
null -> (it.reference as? LightMemberReference)?.element?.passToProcessor() refElement.getCallElementForExactCallee()?.passToProcessor()
is KtExpression -> { refElement.getCallableReferenceForExactCallee()?.passToProcessor(parentUsage.lambdaLevel + 1)
refElement.getCallElementForExactCallee()?.passToProcessor()
refElement.getCallableReferenceForExactCallee()?.passToProcessor(parentUsage.lambdaLevel + 1)
}
else -> refElement.passToProcessor()
}
} }
} else if (this is PsiMethod && language == JavaLanguage.INSTANCE) { else -> refElement.passToProcessor()
// todo: work around the bug in JavaSliceProvider.transform()
processor.process(JavaSliceUsage.createRootUsage(this, parentUsage.params))
} else {
passToProcessor()
} }
} }
return return