FIR IDE: add tests for building kt symbols by PSI

This commit is contained in:
Ilya Kirillov
2020-06-26 18:08:59 +03:00
parent 8a52954bf8
commit c44756bbfd
14 changed files with 380 additions and 46 deletions
+2
View File
@@ -4,6 +4,8 @@ plugins {
}
dependencies {
compileOnly(project(":kotlin-reflect-api"))
compileOnly(project(":compiler:psi"))
compileOnly(project(":compiler:frontend"))
compileOnly(project(":core:type-system"))
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.frontend.api.symbols
import org.jetbrains.kotlin.idea.frontend.api.TypeInfo
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.javaGetter
object DebugSymbolRenderer {
fun render(symbol: KtSymbol): String = buildString {
val klass = symbol::class
appendLine("${klass.simpleName}:")
klass.members.filterIsInstance<KProperty<*>>().sortedBy { it.name }.forEach { property ->
if (property.name in ignoredPropertyNames) return@forEach
val value = property.javaGetter?.invoke(symbol) ?: return@forEach
val stringValue = renderValue(value)
appendLine(" ${property.name}: $stringValue")
}
}
private fun renderValue(value: Any?): String = when (value) {
null -> "null"
is Boolean -> value.toString()
is Name -> value.asString()
is FqName -> value.asString()
is ClassId -> value.asString()
is Enum<*> -> value.name
is List<*> -> buildString {
append("[")
value.joinTo(this) { renderValue(it) }
append("]")
}
is TypeInfo -> value.asDenotableTypeStringRepresentation()
is KtSymbol -> {
val symbolTag = when (value) {
is KtClassLikeSymbol -> renderValue(value.classId)
is KtFunctionSymbol -> renderValue(value.fqName)
is KtConstructorSymbol -> "<constructor>"
is KtNamedSymbol -> renderValue(value.name)
else -> TODO(value::class.toString())
}
"${value::class.simpleName!!}($symbolTag)"
}
else -> value::class.simpleName!!
}
private val ignoredPropertyNames = setOf("fir", "psi", "token")
}