Skip to content

Commit

Permalink
Handle tiny-fake-file mechanism in BindingContext paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Oct 4, 2023
1 parent 338829a commit 5f355c6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions server/src/main/kotlin/org/javacs/kt/CompiledFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class CompiledFile(
val isScript: Boolean = false,
val kind: CompilationKind = CompilationKind.DEFAULT
) {
val path: Path by lazy { parse.containingFile.toPath() }

/**
* Find the type of the expression at `cursor`
*/
Expand Down Expand Up @@ -72,9 +70,11 @@ class CompiledFile(
val cursorExpr = element?.findParent<KtExpression>() ?: return nullResult("Couldn't find expression at ${describePosition(cursor)} (only found $element)")
val surroundingExpr = expandForReference(cursor, cursorExpr)
val scope = scopeAtPoint(cursor) ?: return nullResult("Couldn't find scope at ${describePosition(cursor)}")
// NOTE: Due to our tiny-fake-file mechanism, we may have `path == /dummy.virtual.kt != parse.containingFile.toPath`
val path = surroundingExpr.containingFile.toPath()
val context = bindingContextOf(surroundingExpr, scope)
LOG.info("Hovering {}", surroundingExpr)
return referenceFromContext(cursor, context)
return referenceFromContext(cursor, path, context)
}

/**
Expand All @@ -83,10 +83,11 @@ class CompiledFile(
* This method should not be used for anything other than finding definitions (at least for now).
*/
fun referenceExpressionAtPoint(cursor: Int): Pair<KtExpression, DeclarationDescriptor>? {
return referenceFromContext(cursor, compile)
val path = parse.containingFile.toPath()
return referenceFromContext(cursor, path, compile)
}

private fun referenceFromContext(cursor: Int, context: BindingContext): Pair<KtExpression, DeclarationDescriptor>? {
private fun referenceFromContext(cursor: Int, path: Path, context: BindingContext): Pair<KtExpression, DeclarationDescriptor>? {
val targets = context.getSliceContents(BindingContext.REFERENCE_TARGET)
return targets.asSequence()
.filter { cursor in it.key.textRange && it.key.containingFile.toPath() == path }
Expand Down Expand Up @@ -224,6 +225,7 @@ class CompiledFile(
*/
fun scopeAtPoint(cursor: Int): LexicalScope? {
val oldCursor = oldOffset(cursor)
val path = parse.containingFile.toPath()
return compile.getSliceContents(BindingContext.LEXICAL_SCOPE).asSequence()
.filter {
it.key.textRange.startOffset <= oldCursor
Expand Down

0 comments on commit 5f355c6

Please sign in to comment.