Exctract common code from psi and fir visualizer classes
This commit is contained in:
committed by
Mikhail Glukhikh
parent
d504774527
commit
f0c7aadb20
+26
-2
@@ -5,6 +5,30 @@
|
||||
|
||||
package org.jetbrains.kotlin.compiler.visualizer
|
||||
|
||||
interface BaseRenderer {
|
||||
fun render(): String
|
||||
import com.intellij.psi.PsiElement
|
||||
|
||||
abstract class BaseRenderer {
|
||||
private val annotations = mutableSetOf<Annotator.AnnotationInfo>()
|
||||
private val unnecessaryData = mapOf(
|
||||
"kotlin/" to ""
|
||||
)
|
||||
|
||||
open fun addAnnotation(annotationText: String, element: PsiElement?, deleteDuplicate: Boolean = true) {
|
||||
if (element == null) return
|
||||
annotations.removeIf { it.range.startOffset == element.textRange.startOffset && deleteDuplicate }
|
||||
|
||||
var textWithOutUnnecessaryData = annotationText
|
||||
for ((key, value) in unnecessaryData) {
|
||||
textWithOutUnnecessaryData = textWithOutUnnecessaryData.replace(key, value)
|
||||
}
|
||||
if (textWithOutUnnecessaryData != element.text && textWithOutUnnecessaryData.isNotEmpty()) {
|
||||
annotations.add(Annotator.AnnotationInfo(textWithOutUnnecessaryData, element.textRange))
|
||||
}
|
||||
}
|
||||
|
||||
protected fun getAnnotations(): Set<Annotator.AnnotationInfo> {
|
||||
return annotations
|
||||
}
|
||||
|
||||
abstract fun render(): String
|
||||
}
|
||||
+4
-19
@@ -22,30 +22,15 @@ import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType
|
||||
|
||||
class FirVisualizer(private val firFile: FirFile) : BaseRenderer {
|
||||
private val annotations = mutableSetOf<Annotator.AnnotationInfo>()
|
||||
|
||||
private val unnecessaryData = mapOf(
|
||||
"kotlin/" to ""
|
||||
)
|
||||
|
||||
private fun addAnnotation(annotationText: String, element: PsiElement?, deleteDuplicate: Boolean = false) {
|
||||
if (element == null) return
|
||||
annotations.removeIf { it.range.startOffset == element.textRange.startOffset && deleteDuplicate }
|
||||
|
||||
var textWithOutUnnecessaryData = annotationText
|
||||
for ((key, value) in unnecessaryData) {
|
||||
textWithOutUnnecessaryData = textWithOutUnnecessaryData.replace(key, value)
|
||||
}
|
||||
if (textWithOutUnnecessaryData != element.text && textWithOutUnnecessaryData.isNotEmpty()) {
|
||||
annotations.add(Annotator.AnnotationInfo(textWithOutUnnecessaryData, element.textRange))
|
||||
}
|
||||
class FirVisualizer(private val firFile: FirFile) : BaseRenderer() {
|
||||
override fun addAnnotation(annotationText: String, element: PsiElement?, deleteDuplicate: Boolean) {
|
||||
super.addAnnotation(annotationText, element, false)
|
||||
}
|
||||
|
||||
override fun render(): String {
|
||||
val map = mutableMapOf<PsiElement, MutableList<FirElement>>().apply { Psi2FirMapper(this).visitFile(firFile) }
|
||||
map.keys.firstOrNull { it is KtFile }?.accept(PsiVisitor(map))
|
||||
return Annotator.annotate(firFile.psi!!.text, annotations).joinToString("\n")
|
||||
return Annotator.annotate(firFile.psi!!.text, getAnnotations()).joinToString("\n")
|
||||
}
|
||||
|
||||
inner class PsiVisitor(private val map: Map<PsiElement, MutableList<FirElement>>) : KtVisitorVoid() {
|
||||
|
||||
+2
-19
@@ -35,33 +35,16 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import java.util.ArrayList
|
||||
|
||||
class PsiRenderer(private val file: KtFile, analysisResult: AnalysisResult) : BaseRenderer {
|
||||
class PsiVisualizer(private val file: KtFile, analysisResult: AnalysisResult) : BaseRenderer() {
|
||||
private val bindingContext = analysisResult.bindingContext
|
||||
private val annotations = mutableSetOf<Annotator.AnnotationInfo>()
|
||||
private val filePackage = file.packageFqName.toString().replace(".", "/")
|
||||
private val argumentsLabel = "<PLACE-FOR-ARGUMENTS>"
|
||||
|
||||
val descriptorRenderer = PsiDescriptorRenderer()
|
||||
|
||||
private val unnecessaryData = mapOf(
|
||||
"kotlin/" to ""
|
||||
)
|
||||
|
||||
private fun addAnnotation(annotationText: String, element: PsiElement, deleteDuplicate: Boolean = true) {
|
||||
annotations.removeIf { it.range.startOffset == element.textRange.startOffset && deleteDuplicate }
|
||||
|
||||
var textWithOutUnnecessaryData = annotationText
|
||||
for ((key, value) in unnecessaryData) {
|
||||
textWithOutUnnecessaryData = textWithOutUnnecessaryData.replace(key, value)
|
||||
}
|
||||
if (textWithOutUnnecessaryData != element.text && textWithOutUnnecessaryData.isNotEmpty()) {
|
||||
annotations.add(Annotator.AnnotationInfo(textWithOutUnnecessaryData, element.textRange))
|
||||
}
|
||||
}
|
||||
|
||||
override fun render(): String {
|
||||
file.accept(Renderer())
|
||||
return annotate(file.text, annotations).joinToString("\n")
|
||||
return annotate(file.text, getAnnotations()).joinToString("\n")
|
||||
}
|
||||
|
||||
inner class Renderer : KtVisitorVoid() {
|
||||
+2
-5
@@ -5,13 +5,10 @@
|
||||
|
||||
package org.jetbrains.kotlin.visualizer.psi
|
||||
|
||||
import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithJava
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.compiler.visualizer.PsiRenderer
|
||||
import org.jetbrains.kotlin.compiler.visualizer.PsiVisualizer
|
||||
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.TestJdkKind
|
||||
import org.jetbrains.kotlin.visualizer.AbstractVisualizer
|
||||
import java.io.File
|
||||
|
||||
@@ -20,7 +17,7 @@ abstract class AbstractPsiVisualizer : AbstractVisualizer() {
|
||||
val ktFiles = environment.getSourceFiles()
|
||||
val analysisResult = JvmResolveUtil.analyze(ktFiles, environment)
|
||||
|
||||
val renderer = PsiRenderer(ktFiles.first(), analysisResult)
|
||||
val renderer = PsiVisualizer(ktFiles.first(), analysisResult)
|
||||
val psiRenderResult = renderer.render()
|
||||
|
||||
val expectedPath = file.absolutePath.replace(replacement.first, replacement.second)
|
||||
|
||||
Reference in New Issue
Block a user