From 3897f51f8eac10eac9d9fbeab108fc51eba1cbf6 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Mon, 10 Feb 2020 11:26:24 +0200 Subject: [PATCH] Refactoring code --- .../kotlin/idea/slicer/InflowSlicer.kt | 19 +++++++++----- .../kotlin/idea/slicer/OutflowSlicer.kt | 26 +++++++++---------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/slicer/InflowSlicer.kt b/idea/src/org/jetbrains/kotlin/idea/slicer/InflowSlicer.kt index 3ade489e873..610050a10e8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/slicer/InflowSlicer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/slicer/InflowSlicer.kt @@ -6,6 +6,7 @@ import com.intellij.psi.search.LocalSearchScope import com.intellij.psi.search.SearchScope import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.slicer.SliceUsage +import com.intellij.usageView.UsageInfo import com.intellij.util.Processor import org.jetbrains.kotlin.asJava.namedUnwrappedElement import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor @@ -107,16 +108,16 @@ class InflowSlicer( val parameterDescriptor = parameter.resolveToParameterDescriptorIfAny(BodyResolveMode.FULL) ?: return - (function as? KtFunction)?.processCalls(analysisScope, includeOverriders) body@{ - val refElement = it.element ?: return@body + fun processCall(usageInfo: UsageInfo) { + val refElement = usageInfo.element ?: return val refParent = refElement.parent val argumentExpression = when { refElement is KtExpression -> { - val callElement = refElement.getParentOfTypeAndBranch { calleeExpression } ?: return@body - val resolvedCall = callElement.resolveToCall() ?: return@body + val callElement = refElement.getParentOfTypeAndBranch { calleeExpression } ?: return + val resolvedCall = callElement.resolveToCall() ?: return val callParameterDescriptor = resolvedCall.resultingDescriptor.valueParameters[parameterDescriptor.index] - val resolvedArgument = resolvedCall.valueArguments[callParameterDescriptor] ?: return@body + val resolvedArgument = resolvedCall.valueArguments[callParameterDescriptor] ?: return when (resolvedArgument) { is DefaultValueArgument -> parameter.defaultValue is ExpressionValueArgument -> resolvedArgument.valueArgument?.getArgumentExpression() @@ -131,6 +132,8 @@ class InflowSlicer( argumentExpression?.passToProcessorAsValue() } + (function as? KtFunction)?.processCalls(analysisScope, includeOverriders, ::processCall) + if (parameter.valOrVarKeyword.toValVar() == KotlinValVar.Var) { processAssignments(parameter, analysisScope) } @@ -211,8 +214,8 @@ class InflowSlicer( } private fun processAssignments(variable: KtCallableDeclaration, accessSearchScope: SearchScope) { - processVariableAccesses(variable, accessSearchScope, AccessKind.WRITE_WITH_OPTIONAL_READ) body@{ - val refElement = it.element ?: return@body + fun processVariableAccess(usageInfo: UsageInfo) { + val refElement = usageInfo.element ?: return val refParent = refElement.parent val rhsValue = when { @@ -231,6 +234,8 @@ class InflowSlicer( } rhsValue?.passToProcessorAsValue() } + + processVariableAccesses(variable, accessSearchScope, AccessKind.WRITE_WITH_OPTIONAL_READ, ::processVariableAccess) } private fun KtPropertyAccessor.processBackingFieldAssignments() { diff --git a/idea/src/org/jetbrains/kotlin/idea/slicer/OutflowSlicer.kt b/idea/src/org/jetbrains/kotlin/idea/slicer/OutflowSlicer.kt index e176144950f..d1dc1b2c702 100644 --- a/idea/src/org/jetbrains/kotlin/idea/slicer/OutflowSlicer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/slicer/OutflowSlicer.kt @@ -4,6 +4,7 @@ import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector import com.intellij.psi.PsiElement import com.intellij.psi.impl.light.LightMemberReference import com.intellij.slicer.SliceUsage +import com.intellij.usageView.UsageInfo import com.intellij.util.Processor import org.jetbrains.kotlin.cfg.pseudocode.PseudoValue import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction @@ -39,15 +40,12 @@ class OutflowSlicer( val withDereferences = parentUsage.params.showInstanceDereferences val accessKind = if (withDereferences) AccessKind.READ_OR_WRITE else AccessKind.READ_ONLY - processVariableAccesses( - variable, - analysisScope, - accessKind - ) body@{ - val refElement = it.element + + fun processVariableAccess(usageInfo: UsageInfo) { + val refElement = usageInfo.element ?: return if (refElement !is KtExpression) { - refElement?.passToProcessor() - return@body + refElement.passToProcessor() + return } val refExpression = KtPsiUtil.safeDeparenthesize(refElement) @@ -58,13 +56,15 @@ class OutflowSlicer( refExpression.passToProcessor() } } + + processVariableAccesses(variable, analysisScope, accessKind, ::processVariableAccess) } private fun processFunction(function: KtFunction) { if (function is KtConstructor<*> || function is KtNamedFunction && function.name != null) { - function.processCalls(analysisScope, includeOverriders = false) { - when (val refElement = it.element) { - null -> (it.reference as? LightMemberReference)?.element?.passToProcessor() + function.processCalls(analysisScope, includeOverriders = false) { usageInfo -> + when (val refElement = usageInfo.element) { + null -> (usageInfo.reference as? LightMemberReference)?.element?.passToProcessor() is KtExpression -> { refElement.getCallElementForExactCallee()?.passToProcessor() refElement.getCallableReferenceForExactCallee()?.passToProcessor(parentUsage.lambdaLevel + 1) @@ -75,12 +75,12 @@ class OutflowSlicer( return } - val funExpression = when (function) { + val funExpression: PsiElement = when (function) { is KtFunctionLiteral -> function.parent as? KtLambdaExpression is KtNamedFunction -> function else -> null } ?: return - (funExpression as PsiElement).passToProcessor(parentUsage.lambdaLevel + 1, true) + funExpression.passToProcessor(parentUsage.lambdaLevel + 1, true) } private fun processExpression(expression: KtExpression) {