Make annotation process a little clearer

This commit is contained in:
Ivan Cilcic
2019-08-12 15:29:55 +03:00
committed by Mikhail Glukhikh
parent e23e662ba7
commit feec5b1a7c
2 changed files with 37 additions and 81 deletions
@@ -6,103 +6,56 @@
package org.jetbrains.kotlin.compiler.visualizer package org.jetbrains.kotlin.compiler.visualizer
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import kotlin.math.max
object Annotator { object Annotator {
class AnnotationInfo(val text: String, val globalRange: TextRange) private const val verticalLine = ""
private const val comment = "//"
class RangeSet<T> { class AnnotationInfo(val text: String, val range: TextRange)
data class Rec<T>(val value: T, val rangeInLine: TextRange)
val localRanges = ArrayList<Rec<T>>()
private inline fun <U> binarySearch(range: TextRange, found: (Int, Rec<T>?) -> U): U { private fun putAnnotationToLines(annotations: List<AnnotationInfo>, lineStart: Int, lineSize: Int): Map<Int, StringBuilder> {
var high = localRanges.size - 1 val annotationLines = mutableMapOf(0 to StringBuilder(comment + " ".repeat(lineSize - comment.length)))
var low = 0
var median = 0
println("search: $low, $high, ins: $range")
while (low <= high) {
median = (high + low) / 2
val element = localRanges[median]
println("cmp ${element.rangeInLine}") var prevAnnStart = Int.MAX_VALUE
when { var lastLevel = 1
range.endOffset < element.rangeInLine.startOffset -> { for (ann in annotations) {
println("take low, $low, $high") if (ann.range.startOffset + ann.text.length >= prevAnnStart) {
high = median - 1 lastLevel++
} } else {
range.startOffset > element.rangeInLine.endOffset -> { lastLevel = 1
println("take high, $low, $high")
low = median + 1
}
else -> {
println("conflict $median")
return found(median, element)
}
}
} }
println("empty at $median")
return found(median, null) if (!annotationLines.containsKey(lastLevel)) {
annotationLines[lastLevel] = StringBuilder(comment + " ".repeat(lineSize - comment.length))
}
val startReplace = max(comment.length, ann.range.startOffset - lineStart)
annotationLines[lastLevel] = annotationLines[lastLevel]!!.replace(startReplace, startReplace + ann.text.length, ann.text)
for (i in 0 until lastLevel) {
annotationLines[i] = annotationLines[i]!!.replace(startReplace, startReplace + 1, verticalLine)
}
prevAnnStart = ann.range.startOffset
} }
fun put(range: TextRange, element: T): Boolean { return annotationLines
binarySearch(range) { index, conflicting ->
return if (conflicting == null) {
localRanges.add(index, Rec(element, range))
true
} else {
false
}
}
}
}
private fun tryPutToAnnotationLines(annotationLines: MutableList<RangeSet<String>>, annotation: Pair<AnnotationInfo, TextRange>) {
val range = annotation.second
fun buildArrow(from: RangeSet<String>) {
for (line in annotationLines) {
if (line == from) {
break
}
line.put(TextRange(range.startOffset, range.startOffset + 1), "|")
}
}
for (line in annotationLines.drop(1)) {
if (line.put(range, annotation.first.text)) {
buildArrow(line)
return
}
}
annotationLines.add(RangeSet())
tryPutToAnnotationLines(annotationLines, annotation)
} }
fun annotate(text: String, annotation: List<AnnotationInfo>): List<String> { fun annotate(text: String, annotation: List<AnnotationInfo>): List<String> {
val lines = text.lines() val lines = text.lines()
val resultLines = mutableListOf<String>() val resultLines = mutableListOf<String>()
val annotationLines = mutableListOf<RangeSet<String>>()
var lineStartOffset = 0 var lineStartOffset = 0
for (line in lines) { for (line in lines) {
annotationLines.clear()
val lineEndOffset = lineStartOffset + line.length val lineEndOffset = lineStartOffset + line.length
val annotations = val annotations = annotation
annotation.filter { it.globalRange.startOffset in lineStartOffset until lineEndOffset } .filter { it.range.startOffset in lineStartOffset until lineEndOffset }
.sortedByDescending { it.globalRange.startOffset } .sortedByDescending { it.range.startOffset }
.map {
val startInLine = it.globalRange.startOffset - lineStartOffset if (annotations.isNotEmpty()) {
it to TextRange(startInLine, startInLine + it.text.length) val annotationLines = putAnnotationToLines(annotations, lineStartOffset, line.length)
} annotationLines.toSortedMap(Comparator.reverseOrder()).mapTo(resultLines) {
for (ann in annotations) { it.value.toString()
tryPutToAnnotationLines(annotationLines, ann)
}
annotationLines.asReversed().mapTo(resultLines) {
var prevOffset = 2
println(it.localRanges)
it.localRanges.fold("//") { acc, element ->
var res = acc
res += " ".repeat(element.rangeInLine.startOffset - prevOffset)
prevOffset = element.rangeInLine.endOffset
res += element.value
res
} }
} }
resultLines.add(line) resultLines.add(line)
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.renderer.ParameterNameRenderingPolicy
import org.jetbrains.kotlin.renderer.render import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext.* import org.jetbrains.kotlin.resolve.BindingContext.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
class PsiRenderer(private val file: KtFile, analysisResult: AnalysisResult) : BaseRenderer { class PsiRenderer(private val file: KtFile, analysisResult: AnalysisResult) : BaseRenderer {
@@ -41,6 +42,7 @@ class PsiRenderer(private val file: KtFile, analysisResult: AnalysisResult) : Ba
} }
private fun addAnnotation(text: String, range: TextRange) { private fun addAnnotation(text: String, range: TextRange) {
annotations.removeIf { it.range.startOffset == range.startOffset }
annotations.add(Annotator.AnnotationInfo(text, range)) annotations.add(Annotator.AnnotationInfo(text, range))
} }
@@ -85,6 +87,7 @@ class PsiRenderer(private val file: KtFile, analysisResult: AnalysisResult) : Ba
override fun visitTypeReference(typeReference: KtTypeReference) { override fun visitTypeReference(typeReference: KtTypeReference) {
val type = bindingContext[TYPE, typeReference] val type = bindingContext[TYPE, typeReference]
addAnnotation(renderType(type), typeReference.textRange) addAnnotation(renderType(type), typeReference.textRange)
super.visitTypeReference(typeReference)
} }
override fun visitConstantExpression(expression: KtConstantExpression) { override fun visitConstantExpression(expression: KtConstantExpression) {