Safe reporting PSI elements in KEWA

This commit is contained in:
Vladimir Dolzhenko
2021-09-28 22:04:06 +02:00
committed by teamcity
parent 20871dd555
commit 5dc2442872
16 changed files with 63 additions and 52 deletions
@@ -72,7 +72,7 @@ class KtDotQualifiedExpression : KtExpressionImplStub<KotlinPlaceHolderStub<KtDo
LOG.error(
KotlinExceptionWithAttachments(
"Invalid stub structure. DOT_QUALIFIED_EXPRESSION must have one or two children. Was: ${expressions.size}\n"
).withAttachment("file.kt", containingFile.text)
).withPsiAttachment("file.kt", containingFile)
)
return null
}
@@ -316,7 +316,7 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
checkWithAttachment(declarations.size == 1, { "unexpected ${declarations.size} declarations" }) {
it.withAttachment("text.kt", text)
for (d in declarations.withIndex()) {
it.withAttachment("declaration${d.index}.kt", d.value.text)
it.withPsiAttachment("declaration${d.index}.kt", d.value)
}
}
@Suppress("UNCHECKED_CAST")
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.psi.psiUtil
import com.intellij.injected.editor.VirtualFileWindow
import com.intellij.lang.ASTNode
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
@@ -381,29 +380,7 @@ fun PsiElement.startsWithComment(): Boolean = firstChild is PsiComment
// ---------------------------------- Debug/logging ----------------------------------------------------------------------------------------
fun PsiElement.getElementTextWithContext(): String {
if (!isValid) return "<invalid element $this>"
if (this is PsiFile) {
return containingFile.text
}
// Find parent for element among file children
val topLevelElement = PsiTreeUtil.findFirstParent(this, { it.parent is PsiFile })
?: throw AssertionError("For non-file element we should always be able to find parent in file children")
val startContextOffset = topLevelElement.startOffset
val elementContextOffset = textRange.startOffset
val inFileParentOffset = elementContextOffset - startContextOffset
val isInjected = containingFile is VirtualFileWindow
return StringBuilder(topLevelElement.text)
.insert(inFileParentOffset, "<caret>")
.insert(0, "File name: ${containingFile.name} Physical: ${containingFile.isPhysical} Injected: $isInjected\n")
.toString()
}
fun PsiElement.getElementTextWithContext(): String = org.jetbrains.kotlin.utils.getElementTextWithContext(this)
fun PsiElement.getTextWithLocation(): String = "'${this.text}' at ${PsiDiagnosticUtils.atLocation(this)}"