Extract function for debugger: add block code fragment as list of statement, not as block expression

This commit is contained in:
Natalia Ukhorskaya
2015-06-15 16:37:47 +03:00
parent 9a09a25df5
commit 0f108eee2f
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.refactoring.createTempCopy import org.jetbrains.kotlin.idea.core.refactoring.createTempCopy
import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.* import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.*
import java.util.*
fun getFunctionForExtractedFragment( fun getFunctionForExtractedFragment(
codeFragment: JetCodeFragment, codeFragment: JetCodeFragment,
@@ -83,17 +84,17 @@ fun getFunctionForExtractedFragment(
addImportsToFile(codeFragment.importsAsImportList(), tmpFile) addImportsToFile(codeFragment.importsAsImportList(), tmpFile)
val newDebugExpression = addDebugExpressionBeforeContextElement(codeFragment, contextElement) val newDebugExpressions = addDebugExpressionBeforeContextElement(codeFragment, contextElement)
if (newDebugExpression == null) return null if (newDebugExpressions.isEmpty()) return null
val targetSibling = tmpFile.getDeclarations().firstOrNull() val targetSibling = tmpFile.getDeclarations().firstOrNull()
if (targetSibling == null) return null if (targetSibling == null) return null
val options = ExtractionOptions(inferUnitTypeForUnusedValues = false, val options = ExtractionOptions(inferUnitTypeForUnusedValues = false,
enableListBoxing = true, enableListBoxing = true,
allowSpecialClassNames = true, allowSpecialClassNames = true,
captureLocalFunctions = true) captureLocalFunctions = true)
val analysisResult = ExtractionData(tmpFile, newDebugExpression.toRange(), targetSibling, null, options).performAnalysis() val analysisResult = ExtractionData(tmpFile, newDebugExpressions.toRange(), targetSibling, null, options).performAnalysis()
if (analysisResult.status != Status.SUCCESS) { if (analysisResult.status != Status.SUCCESS) {
throw EvaluateExceptionUtil.createEvaluateException(getErrorMessageForExtractFunctionResult(analysisResult)) throw EvaluateExceptionUtil.createEvaluateException(getErrorMessageForExtractFunctionResult(analysisResult))
} }
@@ -175,7 +176,7 @@ private fun getExpressionToAddDebugExpressionBefore(tmpFile: JetFile, contextEle
return parent return parent
} }
private fun addDebugExpressionBeforeContextElement(codeFragment: JetCodeFragment, contextElement: PsiElement): JetExpression? { private fun addDebugExpressionBeforeContextElement(codeFragment: JetCodeFragment, contextElement: PsiElement): List<JetExpression> {
val psiFactory = JetPsiFactory(codeFragment) val psiFactory = JetPsiFactory(codeFragment)
fun insertNewInitializer(classBody: JetClassBody): PsiElement? { fun insertNewInitializer(classBody: JetClassBody): PsiElement? {
@@ -234,19 +235,32 @@ private fun addDebugExpressionBeforeContextElement(codeFragment: JetCodeFragment
} }
val parent = elementBefore?.getParent() val parent = elementBefore?.getParent()
if (parent == null || elementBefore == null) return null if (parent == null || elementBefore == null) return emptyList()
parent.addBefore(psiFactory.createNewLine(), elementBefore) parent.addBefore(psiFactory.createNewLine(), elementBefore)
val debugExpression = codeFragment.getContentElement() val debugExpression = codeFragment.getContentElement() ?: return emptyList()
if (debugExpression == null) return null
val newDebugExpression = parent.addBefore(debugExpression, elementBefore) val expressions = ArrayList<JetExpression>()
if (newDebugExpression == null) return null
parent.addBefore(psiFactory.createNewLine(), elementBefore) fun insertExpression(expr: JetExpression) {
val newDebugExpression = parent.addBefore(expr, elementBefore) ?: return
return newDebugExpression as JetExpression expressions.add(newDebugExpression as JetExpression)
parent.addBefore(psiFactory.createNewLine(), elementBefore)
}
when (debugExpression) {
is JetBlockExpression -> {
for (statement in debugExpression.getStatements()) {
insertExpression(statement)
}
}
is JetExpression -> insertExpression(debugExpression)
}
return expressions
} }
private fun replaceByRunFunction(expression: JetExpression): JetCallExpression { private fun replaceByRunFunction(expression: JetExpression): JetCallExpression {