From c757701a8769b8aee0011c15bda2afe28eb86bc6 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 20 May 2015 12:20:20 +0300 Subject: [PATCH] Refactoring: Extract function getContainingPseudocode() --- .../kotlin/cfg/pseudocode/pseudocodeUtil.kt | 23 +++++++++++++++++++ .../extractableAnalysisUtil.kt | 18 +-------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/pseudocodeUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/pseudocodeUtil.kt index 1a441c76f4d..0ee28e44341 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/pseudocodeUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/pseudocodeUtil.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.cfg.pseudocode +import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction import org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.* @@ -25,6 +26,9 @@ import org.jetbrains.kotlin.cfg.pseudocode.instructions.jumps.ThrowExceptionInst import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType +import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.OverridingUtil import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunctionDescriptor @@ -181,6 +185,25 @@ fun Pseudocode.getElementValuesRecursively(element: JetElement): List(), javaClass()) + ?: getNonStrictParentOfType() + ?: return null + + val enclosingPseudocodeDeclaration = if (pseudocodeDeclaration is JetFunctionLiteral) { + parents(withItself = false).firstOrNull { it is JetDeclaration && it !is JetFunctionLiteral } as? JetDeclaration + ?: pseudocodeDeclaration + } + else { + pseudocodeDeclaration + } + + val enclosingPseudocode = PseudocodeUtil.generatePseudocode(enclosingPseudocodeDeclaration, context) + return enclosingPseudocode.getPseudocodeByElement(pseudocodeDeclaration) + ?: throw AssertionError("Can't find nested pseudocode for element: ${pseudocodeDeclaration.getElementTextWithContext()}") +} + public fun Pseudocode.getPseudocodeByElement(element: JetElement): Pseudocode? { if (getCorrespondingElement() == element) return this diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractableAnalysisUtil.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractableAnalysisUtil.kt index 34d4bc907d7..02130481a85 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractableAnalysisUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractableAnalysisUtil.kt @@ -820,23 +820,7 @@ fun ExtractionData.performAnalysis(): AnalysisResult { val targetScope = JetScopeUtils.getResolutionScope(targetSibling, bindingContext) - val pseudocodeDeclaration = - PsiTreeUtil.getParentOfType(commonParent, javaClass(), javaClass()) - ?: commonParent.getNonStrictParentOfType() - ?: return noContainerError - - val enclosingPseudocodeDeclaration = if (pseudocodeDeclaration is JetFunctionLiteral) { - commonParent.parents(withItself = false) - .firstOrNull { it is JetDeclaration && it !is JetFunctionLiteral } as? JetDeclaration - ?: pseudocodeDeclaration - } - else { - pseudocodeDeclaration - } - - val enclosingPseudocode = PseudocodeUtil.generatePseudocode(enclosingPseudocodeDeclaration, bindingContext) - val pseudocode = enclosingPseudocode.getPseudocodeByElement(pseudocodeDeclaration) - ?: throw AssertionError("Can't find nested pseudocode for element: ${pseudocodeDeclaration.getElementTextWithContext()}") + val pseudocode = commonParent.getContainingPseudocode(bindingContext) ?: return noContainerError val localInstructions = getLocalInstructions(pseudocode) val modifiedVarDescriptorsWithExpressions = localInstructions.getModifiedVarDescriptors(bindingContext)