summaryrefslogtreecommitdiff
path: root/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/DecompiledText.kt
blob: c7daf97dbfbc2c8dbc544f62039a3cd14407f93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.idea.decompiler.textBuilder

import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.utils.keysToMap

data class DecompiledText(val text: String, val index: DecompiledTextIndex)

interface DecompiledTextIndexer<out T : Any> {
    fun indexDescriptor(descriptor: DeclarationDescriptor): Collection<T>
}

// In-memory HashMap-based index of decompiled text
// allows navigation features
class DecompiledTextIndex(private val indexers: Collection<DecompiledTextIndexer<*>>) {
    private val indexerToMap: Map<DecompiledTextIndexer<*>, MutableMap<Any, TextRange>> = indexers.keysToMap { hashMapOf<Any, TextRange>() }

    fun addToIndex(descriptor: DeclarationDescriptor, textRange: TextRange) {
        indexers.forEach { mapper ->
            val correspondingMap = indexerToMap.getValue(mapper)
            mapper.indexDescriptor(descriptor).forEach { key ->
                correspondingMap[key] = textRange
            }
        }
    }

    fun <T : Any> getRange(mapper: DecompiledTextIndexer<T>, key: T): TextRange? = indexerToMap[mapper]?.get(key)

    companion object {
        val Empty = DecompiledTextIndex(listOf())
    }
}