From 302bdebd3a4d7c70d24f07759275e7c531ce017d Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Tue, 6 Feb 2018 20:41:04 +0300 Subject: [PATCH] 172: AbstractSlicerTest.kt reverted to previous rebase --- .../idea/slicer/AbstractSlicerTest.kt.172 | 136 +++++++++--------- 1 file changed, 71 insertions(+), 65 deletions(-) diff --git a/idea/tests/org/jetbrains/kotlin/idea/slicer/AbstractSlicerTest.kt.172 b/idea/tests/org/jetbrains/kotlin/idea/slicer/AbstractSlicerTest.kt.172 index 65245dad778..6194fbc2b55 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/slicer/AbstractSlicerTest.kt.172 +++ b/idea/tests/org/jetbrains/kotlin/idea/slicer/AbstractSlicerTest.kt.172 @@ -17,89 +17,96 @@ package org.jetbrains.kotlin.idea.slicer import com.intellij.analysis.AnalysisScope -import com.intellij.ide.projectView.TreeStructureProvider -import com.intellij.ide.util.treeView.AbstractTreeStructureBase import com.intellij.openapi.util.io.FileUtil -import com.intellij.slicer.DuplicateMap import com.intellij.slicer.SliceAnalysisParams -import com.intellij.slicer.SliceNode -import com.intellij.slicer.SliceRootNode +import com.intellij.slicer.SliceUsage import com.intellij.usages.TextChunk +import com.intellij.util.CommonProcessors +import com.intellij.util.Processor +import gnu.trove.THashSet +import gnu.trove.TObjectHashingStrategy import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.psi.KtFile -import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.KotlinTestUtils import java.awt.Font import java.io.File abstract class AbstractSlicerTest : KotlinLightCodeInsightFixtureTestCase() { - protected class SliceTreeStructure(private val rootNode: SliceNode) : AbstractTreeStructureBase(rootNode.project) { - override fun getProviders(): List? = emptyList() - - override fun getRootElement() = rootNode - - override fun commit() { - + object SliceUsageHashingStrategy : TObjectHashingStrategy { + override fun computeHashCode(`object`: KotlinSliceUsage): Int { + return `object`.usageInfo.hashCode() * 31 + `object`.forcedExpressionMode.hashCode() } - override fun hasSomethingToCommit() = false + override fun equals(o1: KotlinSliceUsage, o2: KotlinSliceUsage): Boolean { + return o1.usageInfo == o2.usageInfo && o1.forcedExpressionMode == o2.forcedExpressionMode + } } - companion object { - private val SliceNode.sortedChildren : List - get() = children.sortedBy { it.value.element?.startOffset ?: -1 } + // Based on SliceUsage.processChildren + private fun KotlinSliceUsage.processChildrenWithoutProgress(processor: (KotlinSliceUsage) -> Unit) { + if (this is KotlinSliceDereferenceUsage) return - @JvmStatic - protected fun buildTreeRepresentation(rootNode: SliceNode): String { - fun TextChunk.render(): String { - var text = text - if (attributes.fontType == Font.BOLD) { - text = "$text" + val element = runReadAction { element } + + @Suppress("UNCHECKED_CAST") + val uniqueProcessor = CommonProcessors.UniqueProcessor( + { + processor(it as KotlinSliceUsage) + true + }, + SliceUsageHashingStrategy + ) as Processor + + runReadAction { + if (params.dataFlowToThis) { + processUsagesFlownDownTo(element, uniqueProcessor) + } + else { + processUsagesFlownFromThe(element, uniqueProcessor) + } + } + } + + private fun buildTreeRepresentation(rootUsage: KotlinSliceUsage): String { + val visitedUsages = THashSet(SliceUsageHashingStrategy) + + fun TextChunk.render(): String { + var text = text + if (attributes.fontType == Font.BOLD) { + text = "$text" + } + return text + } + + fun process(usage: KotlinSliceUsage, indent: Int): String { + val isDuplicated = !visitedUsages.add(usage) + + return buildString { + val chunks = usage.text + append(chunks.first().render() + " ") + append("\t".repeat(indent)) + if (usage is KotlinSliceDereferenceUsage) { + append("DEREFERENCE: ") } - return text - } - - fun process(node: SliceNode, indent: Int): String { - val usage = node.element!!.value as KotlinSliceUsage - - node.calculateDupNode() - val isDuplicated = node.duplicate != null - - return buildString { - when { - node is SliceRootNode && usage.element is KtFile -> { - node.sortedChildren.forEach { append(process(it, indent)) } - return@buildString - } - else -> { - val chunks = usage.text - append(chunks.first().render() + " ") - append("\t".repeat(indent)) - if (usage is KotlinSliceDereferenceUsage) { - append("DEREFERENCE: ") - } - append("[LAMBDA] ".repeat(usage.lambdaLevel)) - chunks.slice(1..chunks.size - 1).joinTo( - this, - separator = "", - prefix = if (isDuplicated) "DUPLICATE: " else "", - postfix = "\n" - ) { it.render() } - } + append("[LAMBDA] ".repeat(usage.lambdaLevel)) + chunks.slice(1..chunks.size - 1).joinTo( + this, + separator = "", + prefix = if (isDuplicated) "DUPLICATE: " else "", + postfix = "\n" + ) { it.render() } + if (!isDuplicated) { + usage.processChildrenWithoutProgress { + append(process(it, indent + 1)) } - - if (!isDuplicated) { - node.sortedChildren.forEach { append(process(it, indent + 1)) } - } - }.replace(Regex(""), "") - } - - return process(rootNode, 0) + } + }.replace(Regex(""), "") } - } - protected abstract fun doTest(path: String, sliceProvider: KotlinSliceProvider, rootNode: SliceRootNode) + return process(rootUsage, 0) + } protected fun doTest(path: String) { val mainFile = File(path) @@ -129,7 +136,6 @@ abstract class AbstractSlicerTest : KotlinLightCodeInsightFixtureTestCase() { val sliceProvider = KotlinSliceProvider() val expression = sliceProvider.getExpressionAtCaret(elementAtCaret, analysisParams.dataFlowToThis)!! val rootUsage = sliceProvider.createRootUsage(expression, analysisParams) - val rootNode = SliceRootNode(project, DuplicateMap(), rootUsage) - doTest(path, sliceProvider, rootNode) + KotlinTestUtils.assertEqualsToFile(File(path.replace(".kt", ".results.txt")), buildTreeRepresentation(rootUsage)) } } \ No newline at end of file