Skip to content

Commit 5f355c6

Browse files
committed
Handle tiny-fake-file mechanism in BindingContext paths
1 parent 338829a commit 5f355c6

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

server/src/main/kotlin/org/javacs/kt/CompiledFile.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class CompiledFile(
3333
val isScript: Boolean = false,
3434
val kind: CompilationKind = CompilationKind.DEFAULT
3535
) {
36-
val path: Path by lazy { parse.containingFile.toPath() }
37-
3836
/**
3937
* Find the type of the expression at `cursor`
4038
*/
@@ -72,9 +70,11 @@ class CompiledFile(
7270
val cursorExpr = element?.findParent<KtExpression>() ?: return nullResult("Couldn't find expression at ${describePosition(cursor)} (only found $element)")
7371
val surroundingExpr = expandForReference(cursor, cursorExpr)
7472
val scope = scopeAtPoint(cursor) ?: return nullResult("Couldn't find scope at ${describePosition(cursor)}")
73+
// NOTE: Due to our tiny-fake-file mechanism, we may have `path == /dummy.virtual.kt != parse.containingFile.toPath`
74+
val path = surroundingExpr.containingFile.toPath()
7575
val context = bindingContextOf(surroundingExpr, scope)
7676
LOG.info("Hovering {}", surroundingExpr)
77-
return referenceFromContext(cursor, context)
77+
return referenceFromContext(cursor, path, context)
7878
}
7979

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

89-
private fun referenceFromContext(cursor: Int, context: BindingContext): Pair<KtExpression, DeclarationDescriptor>? {
90+
private fun referenceFromContext(cursor: Int, path: Path, context: BindingContext): Pair<KtExpression, DeclarationDescriptor>? {
9091
val targets = context.getSliceContents(BindingContext.REFERENCE_TARGET)
9192
return targets.asSequence()
9293
.filter { cursor in it.key.textRange && it.key.containingFile.toPath() == path }
@@ -224,6 +225,7 @@ class CompiledFile(
224225
*/
225226
fun scopeAtPoint(cursor: Int): LexicalScope? {
226227
val oldCursor = oldOffset(cursor)
228+
val path = parse.containingFile.toPath()
227229
return compile.getSliceContents(BindingContext.LEXICAL_SCOPE).asSequence()
228230
.filter {
229231
it.key.textRange.startOffset <= oldCursor

0 commit comments

Comments
 (0)