|
| 1 | +package io.github.amichne.kast.common |
| 2 | + |
| 3 | +import com.intellij.openapi.util.TextRange |
| 4 | +import com.intellij.psi.PsiClass |
| 5 | +import com.intellij.psi.PsiElement |
| 6 | +import com.intellij.psi.PsiField |
| 7 | +import com.intellij.psi.PsiMethod |
| 8 | +import com.intellij.psi.PsiNameIdentifierOwner |
| 9 | +import com.intellij.psi.PsiNamedElement |
| 10 | +import io.github.amichne.kast.api.Location |
| 11 | +import io.github.amichne.kast.api.NotFoundException |
| 12 | +import io.github.amichne.kast.api.Symbol |
| 13 | +import io.github.amichne.kast.api.SymbolKind |
| 14 | +import io.github.amichne.kast.api.TextEdit |
| 15 | +import org.jetbrains.kotlin.psi.KtClass |
| 16 | +import org.jetbrains.kotlin.psi.KtNamedDeclaration |
| 17 | +import org.jetbrains.kotlin.psi.KtNamedFunction |
| 18 | +import org.jetbrains.kotlin.psi.KtObjectDeclaration |
| 19 | +import org.jetbrains.kotlin.psi.KtParameter |
| 20 | +import org.jetbrains.kotlin.psi.KtProperty |
| 21 | + |
| 22 | +/** |
| 23 | + * Walks the PSI element hierarchy up from [offset] until it finds a resolvable reference |
| 24 | + * or a named element, then returns it. Works in both IntelliJ-hosted and standalone modes. |
| 25 | + */ |
| 26 | +fun resolveTarget(file: com.intellij.psi.PsiFile, offset: Int): PsiElement { |
| 27 | + val leaf = file.findElementAt(offset) |
| 28 | + ?: throw NotFoundException( |
| 29 | + message = "No PSI element was found at the requested offset", |
| 30 | + details = mapOf("offset" to offset.toString()), |
| 31 | + ) |
| 32 | + |
| 33 | + generateSequence(leaf as PsiElement?) { it.parent }.forEach { element -> |
| 34 | + element.references.firstNotNullOfOrNull { it.resolve() }?.let { return it } |
| 35 | + |
| 36 | + if (element is PsiNamedElement && !element.name.isNullOrBlank()) { |
| 37 | + return element |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + throw NotFoundException("No resolvable symbol was found at the requested offset") |
| 42 | +} |
| 43 | + |
| 44 | +fun PsiElement.toSymbolModel(containingDeclaration: String?): Symbol = Symbol( |
| 45 | + fqName = fqName(), |
| 46 | + kind = kind(), |
| 47 | + location = toKastLocation(nameRange()), |
| 48 | + type = typeDescription(), |
| 49 | + containingDeclaration = containingDeclaration, |
| 50 | +) |
| 51 | + |
| 52 | +fun PsiElement.nameRange(): TextRange = when (this) { |
| 53 | + is KtNamedDeclaration -> nameIdentifier?.textRange ?: textRange |
| 54 | + is PsiNameIdentifierOwner -> nameIdentifier?.textRange ?: textRange |
| 55 | + else -> textRange |
| 56 | +} |
| 57 | + |
| 58 | +fun PsiElement.declarationEdit(newName: String): TextEdit { |
| 59 | + val range = nameRange() |
| 60 | + return TextEdit( |
| 61 | + filePath = containingFile.virtualFile.path, |
| 62 | + startOffset = range.startOffset, |
| 63 | + endOffset = range.endOffset, |
| 64 | + newText = newName, |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +fun PsiElement.fqName(): String = when (this) { |
| 69 | + is KtNamedDeclaration -> fqName?.asString() ?: name ?: "<anonymous>" |
| 70 | + is PsiClass -> qualifiedName ?: name ?: "<anonymous>" |
| 71 | + is PsiMethod -> "${containingClass?.qualifiedName ?: "<local>"}#$name" |
| 72 | + is PsiField -> "${containingClass?.qualifiedName ?: "<local>"}.$name" |
| 73 | + is PsiNamedElement -> name ?: "<anonymous>" |
| 74 | + else -> text |
| 75 | +} |
| 76 | + |
| 77 | +fun PsiElement.kind(): SymbolKind = when (this) { |
| 78 | + is KtClass -> if (isInterface()) SymbolKind.INTERFACE else SymbolKind.CLASS |
| 79 | + is KtObjectDeclaration -> SymbolKind.OBJECT |
| 80 | + is KtNamedFunction -> SymbolKind.FUNCTION |
| 81 | + is KtProperty -> SymbolKind.PROPERTY |
| 82 | + is KtParameter -> SymbolKind.PARAMETER |
| 83 | + is PsiClass -> if (isInterface) SymbolKind.INTERFACE else SymbolKind.CLASS |
| 84 | + is PsiMethod -> SymbolKind.FUNCTION |
| 85 | + is PsiField -> SymbolKind.PROPERTY |
| 86 | + else -> SymbolKind.UNKNOWN |
| 87 | +} |
| 88 | + |
| 89 | +fun PsiElement.typeDescription(): String? = when (this) { |
| 90 | + is KtNamedFunction -> typeReference?.text |
| 91 | + is KtProperty -> typeReference?.text |
| 92 | + is KtParameter -> typeReference?.text |
| 93 | + is PsiMethod -> returnType?.presentableText |
| 94 | + is PsiField -> type.presentableText |
| 95 | + else -> null |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Converts a PSI element and text range to a [Location] using raw file text. |
| 100 | + * Works in both IntelliJ-hosted and standalone modes without requiring a Document. |
| 101 | + */ |
| 102 | +fun PsiElement.toKastLocation(range: TextRange = nameRange()): Location { |
| 103 | + val content = containingFile.text |
| 104 | + val startOffset = range.startOffset.coerceIn(0, content.length) |
| 105 | + val endOffset = range.endOffset.coerceIn(startOffset, content.length) |
| 106 | + val lineStart = content.lastIndexOf('\n', startOffset - 1).let { if (it == -1) 0 else it + 1 } |
| 107 | + val lineEnd = content.indexOf('\n', startOffset).let { if (it == -1) content.length else it } |
| 108 | + |
| 109 | + return Location( |
| 110 | + filePath = containingFile.virtualFile?.path |
| 111 | + ?: containingFile.viewProvider.virtualFile.path, |
| 112 | + startOffset = startOffset, |
| 113 | + endOffset = endOffset, |
| 114 | + startLine = content.take(startOffset).count { it == '\n' } + 1, |
| 115 | + startColumn = startOffset - lineStart + 1, |
| 116 | + preview = content.substring(lineStart, lineEnd).trimEnd(), |
| 117 | + ) |
| 118 | +} |
0 commit comments