Skip to content

Commit 338829a

Browse files
committed
Filter by path when querying BindingContext
1 parent 5aa94da commit 338829a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
2020
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
2121
import org.jetbrains.kotlin.types.KotlinType
2222
import org.eclipse.lsp4j.Location
23+
import java.nio.file.Path
2324
import java.nio.file.Paths
2425

2526
class CompiledFile(
@@ -32,6 +33,8 @@ class CompiledFile(
3233
val isScript: Boolean = false,
3334
val kind: CompilationKind = CompilationKind.DEFAULT
3435
) {
36+
val path: Path by lazy { parse.containingFile.toPath() }
37+
3538
/**
3639
* Find the type of the expression at `cursor`
3740
*/
@@ -86,7 +89,7 @@ class CompiledFile(
8689
private fun referenceFromContext(cursor: Int, context: BindingContext): Pair<KtExpression, DeclarationDescriptor>? {
8790
val targets = context.getSliceContents(BindingContext.REFERENCE_TARGET)
8891
return targets.asSequence()
89-
.filter { cursor in it.key.textRange }
92+
.filter { cursor in it.key.textRange && it.key.containingFile.toPath() == path }
9093
.sortedBy { it.key.textRange.length }
9194
.map { it.toPair() }
9295
.firstOrNull()
@@ -222,7 +225,11 @@ class CompiledFile(
222225
fun scopeAtPoint(cursor: Int): LexicalScope? {
223226
val oldCursor = oldOffset(cursor)
224227
return compile.getSliceContents(BindingContext.LEXICAL_SCOPE).asSequence()
225-
.filter { it.key.textRange.startOffset <= oldCursor && oldCursor <= it.key.textRange.endOffset }
228+
.filter {
229+
it.key.textRange.startOffset <= oldCursor
230+
&& oldCursor <= it.key.textRange.endOffset
231+
&& it.key.containingFile.toPath() == path
232+
}
226233
.sortedBy { it.key.textRange.length }
227234
.map { it.value }
228235
.firstOrNull()

0 commit comments

Comments
 (0)