From 338829a293ac3eb0329c475695b4be82827e1bcd Mon Sep 17 00:00:00 2001 From: fwcd Date: Wed, 4 Oct 2023 13:28:33 +0100 Subject: [PATCH 1/2] Filter by path when querying `BindingContext` --- server/src/main/kotlin/org/javacs/kt/CompiledFile.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt b/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt index e3df3f7a1..4c9ae8ecc 100644 --- a/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt +++ b/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.types.KotlinType import org.eclipse.lsp4j.Location +import java.nio.file.Path import java.nio.file.Paths class CompiledFile( @@ -32,6 +33,8 @@ 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` */ @@ -86,7 +89,7 @@ class CompiledFile( private fun referenceFromContext(cursor: Int, context: BindingContext): Pair? { val targets = context.getSliceContents(BindingContext.REFERENCE_TARGET) return targets.asSequence() - .filter { cursor in it.key.textRange } + .filter { cursor in it.key.textRange && it.key.containingFile.toPath() == path } .sortedBy { it.key.textRange.length } .map { it.toPair() } .firstOrNull() @@ -222,7 +225,11 @@ class CompiledFile( fun scopeAtPoint(cursor: Int): LexicalScope? { val oldCursor = oldOffset(cursor) return compile.getSliceContents(BindingContext.LEXICAL_SCOPE).asSequence() - .filter { it.key.textRange.startOffset <= oldCursor && oldCursor <= it.key.textRange.endOffset } + .filter { + it.key.textRange.startOffset <= oldCursor + && oldCursor <= it.key.textRange.endOffset + && it.key.containingFile.toPath() == path + } .sortedBy { it.key.textRange.length } .map { it.value } .firstOrNull() From 5f355c600042c78ae974197a88c5f6dbaca39018 Mon Sep 17 00:00:00 2001 From: fwcd Date: Wed, 4 Oct 2023 14:01:02 +0100 Subject: [PATCH 2/2] Handle tiny-fake-file mechanism in BindingContext paths --- server/src/main/kotlin/org/javacs/kt/CompiledFile.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt b/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt index 4c9ae8ecc..273099e52 100644 --- a/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt +++ b/server/src/main/kotlin/org/javacs/kt/CompiledFile.kt @@ -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` */ @@ -72,9 +70,11 @@ class CompiledFile( val cursorExpr = element?.findParent() ?: 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) } /** @@ -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? { - return referenceFromContext(cursor, compile) + val path = parse.containingFile.toPath() + return referenceFromContext(cursor, path, compile) } - private fun referenceFromContext(cursor: Int, context: BindingContext): Pair? { + private fun referenceFromContext(cursor: Int, path: Path, context: BindingContext): Pair? { val targets = context.getSliceContents(BindingContext.REFERENCE_TARGET) return targets.asSequence() .filter { cursor in it.key.textRange && it.key.containingFile.toPath() == path } @@ -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