summaryrefslogtreecommitdiff
path: root/plugins/kotlin/jvm-debugger/sequence/src/org/jetbrains/kotlin/idea/debugger/sequence/psi/KotlinPsiUtil.kt
blob: 3b5df694d1d6227d0cd839dbf1c4ccaafc29a568 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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.debugger.sequence.psi

import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.safeAnalyzeNonSourceRootCode
import org.jetbrains.kotlin.idea.util.approximateFlexibleTypes
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.FlexibleType
import org.jetbrains.kotlin.types.KotlinType

object KotlinPsiUtil {
    fun getTypeName(type: KotlinType): String {
        if (type is FlexibleType) {
            return DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(type.approximateFlexibleTypes())
        }

        return DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(type)
    }

    fun getTypeWithoutTypeParameters(type: KotlinType): String {
        val descriptor = type.constructor.declarationDescriptor ?: return getTypeName(type)
        return descriptor.fqNameSafe.asString()
    }
}

fun KtCallExpression.callName(): String = this.calleeExpression!!.text

fun KtCallExpression.previousCall(): KtCallExpression? {
    val parent = this.parent as? KtDotQualifiedExpression ?: return null
    val receiverExpression = parent.receiverExpression
    if (receiverExpression is KtCallExpression) return receiverExpression
    if (receiverExpression is KtDotQualifiedExpression) return receiverExpression.selectorExpression as? KtCallExpression
    return null
}

@Deprecated(
    "Use org.jetbrains.kotlin.idea.core.analyze() instead.",
    ReplaceWith("resolveType()", "org.jetbrains.kotlin.idea.core.analyze"),
    level = DeprecationLevel.ERROR
)
fun KtExpression.resolveType(): KotlinType =
    this.analyze(BodyResolveMode.PARTIAL).getType(this)!!