Do not hold PsiFile in PsiFileEntry.
This commit is contained in:
committed by
Dmitry Petrov
parent
513ef0c1c8
commit
e29b8e356c
@@ -23,18 +23,30 @@ import org.jetbrains.kotlin.ir.declarations.IrFile
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class PsiSourceManager : SourceLocationManager {
|
class PsiSourceManager : SourceLocationManager {
|
||||||
class PsiFileEntry(val psiFile: PsiFile) : SourceLocationManager.FileEntry {
|
class PsiFileEntry(psiFile: PsiFile) : SourceLocationManager.FileEntry {
|
||||||
private val document = psiFile.viewProvider.document
|
private val psiFileName = psiFile.virtualFile?.path ?: psiFile.name
|
||||||
|
|
||||||
override val maxOffset: Int
|
override val maxOffset: Int
|
||||||
get() = document?.textLength ?: Int.MAX_VALUE
|
private val lineStartOffsets: IntArray
|
||||||
|
|
||||||
override fun getLineNumber(offset: Int): Int =
|
init {
|
||||||
document?.getLineNumber(offset) ?: -1
|
val document = psiFile.viewProvider.document ?: throw AssertionError("No document for $psiFile")
|
||||||
|
|
||||||
|
maxOffset = document.textLength
|
||||||
|
|
||||||
|
lineStartOffsets = (0 .. document.lineCount - 1)
|
||||||
|
.map { document.getLineStartOffset(it) }
|
||||||
|
.toIntArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getLineNumber(offset: Int): Int {
|
||||||
|
val index = lineStartOffsets.binarySearch(offset)
|
||||||
|
return if (index >= 0) index else -index - 1
|
||||||
|
}
|
||||||
|
|
||||||
override fun getColumnNumber(offset: Int): Int {
|
override fun getColumnNumber(offset: Int): Int {
|
||||||
val startOffset = document?.getLineStartOffset(offset) ?: return -1
|
val lineNumber = getLineNumber(offset)
|
||||||
return offset - startOffset
|
return offset - lineStartOffsets[lineNumber]
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getSourceRangeInfo(beginOffset: Int, endOffset: Int): SourceRangeInfo =
|
override fun getSourceRangeInfo(beginOffset: Int, endOffset: Int): SourceRangeInfo =
|
||||||
@@ -48,9 +60,9 @@ class PsiSourceManager : SourceLocationManager {
|
|||||||
endColumnNumber = getColumnNumber(endOffset)
|
endColumnNumber = getColumnNumber(endOffset)
|
||||||
)
|
)
|
||||||
|
|
||||||
fun getRecognizableName(): String = psiFile.virtualFile?.path ?: psiFile.name
|
fun getRecognizableName(): String = psiFileName
|
||||||
|
|
||||||
override fun toString(): String = "${getRecognizableName()}"
|
override fun toString(): String = getRecognizableName()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val fileEntriesByPsiFile = HashMap<PsiFile, PsiFileEntry>()
|
private val fileEntriesByPsiFile = HashMap<PsiFile, PsiFileEntry>()
|
||||||
|
|||||||
Reference in New Issue
Block a user