172: AbstractSlicerTest.kt reverted to previous rebase
This commit is contained in:
committed by
Nikolay Krasko
parent
8c7269ea9a
commit
302bdebd3a
@@ -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<TreeStructureProvider>? = emptyList()
|
||||
|
||||
override fun getRootElement() = rootNode
|
||||
|
||||
override fun commit() {
|
||||
|
||||
object SliceUsageHashingStrategy : TObjectHashingStrategy<KotlinSliceUsage> {
|
||||
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<SliceNode>
|
||||
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 = "<bold>$text</bold>"
|
||||
val element = runReadAction { element }
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val uniqueProcessor = CommonProcessors.UniqueProcessor(
|
||||
{
|
||||
processor(it as KotlinSliceUsage)
|
||||
true
|
||||
},
|
||||
SliceUsageHashingStrategy
|
||||
) as Processor<SliceUsage>
|
||||
|
||||
runReadAction {
|
||||
if (params.dataFlowToThis) {
|
||||
processUsagesFlownDownTo(element, uniqueProcessor)
|
||||
}
|
||||
else {
|
||||
processUsagesFlownFromThe(element, uniqueProcessor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildTreeRepresentation(rootUsage: KotlinSliceUsage): String {
|
||||
val visitedUsages = THashSet<KotlinSliceUsage>(SliceUsageHashingStrategy)
|
||||
|
||||
fun TextChunk.render(): String {
|
||||
var text = text
|
||||
if (attributes.fontType == Font.BOLD) {
|
||||
text = "<bold>$text</bold>"
|
||||
}
|
||||
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("</bold><bold>"), "")
|
||||
}
|
||||
|
||||
return process(rootNode, 0)
|
||||
}
|
||||
}.replace(Regex("</bold><bold>"), "")
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user