Skip to content

Commit

Permalink
Fallback to declaration text range
Browse files Browse the repository at this point in the history
Not all named declarations have a name identifier. In those cases
fallback to using the range that encloses the whole named declaration
rather than returning nothing.
  • Loading branch information
Lorenz Bateman committed Feb 20, 2024
1 parent 4056a8b commit ec4e302
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions server/src/main/kotlin/org/javacs/kt/symbols/Symbols.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import org.eclipse.lsp4j.WorkspaceSymbolLocation
import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.javacs.kt.SourcePath
import org.javacs.kt.position.range
import org.javacs.kt.position.toURIString
import org.javacs.kt.util.containsCharactersInOrder
import org.javacs.kt.util.preOrderTraversal
import org.javacs.kt.util.toPath
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.parents

Expand Down Expand Up @@ -82,20 +82,22 @@ private fun symbolKind(d: KtNamedDeclaration): SymbolKind =
else -> throw IllegalArgumentException("Unexpected symbol $d")
}

private fun location(d: KtNamedDeclaration): Location? {
val uri = d.containingFile.toPath().toUri().toString()
val (content, textRange) = try { d.containingFile?.text to d.nameIdentifier?.textRange } catch (e: Exception) { null to null }
return if (content != null && textRange != null) {
Location(uri, range(content, textRange))
} else {
private fun location(d: KtNamedDeclaration): Location? =
try {
val content = d.containingFile?.text
val locationInContent = (d.nameIdentifier?.textRange ?: d.textRange)
if (content != null && locationInContent != null) {
Location(d.containingFile.toURIString(), range(content, locationInContent))
} else {
null
}
} catch (e: Exception) {
null
}
}

private fun workspaceSymbolLocation(d: KtNamedDeclaration): WorkspaceSymbolLocation {
val uri = d.containingFile.toPath().toUri().toString()
return WorkspaceSymbolLocation(uri)
}

private fun workspaceSymbolLocation(d: KtNamedDeclaration): WorkspaceSymbolLocation =
WorkspaceSymbolLocation(d.containingFile.toURIString())

private fun symbolContainer(d: KtNamedDeclaration): String? =
d.parents
Expand Down

0 comments on commit ec4e302

Please sign in to comment.