Skip to content

Commit c1d0101

Browse files
authored
Merge pull request #509 from fwcd/binding-context-filter-by-path
Filter by path when querying `BindingContext`
2 parents 5aa94da + 5f355c6 commit c1d0101

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

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

Lines changed: 14 additions & 5 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(
@@ -69,9 +70,11 @@ class CompiledFile(
6970
val cursorExpr = element?.findParent<KtExpression>() ?: return nullResult("Couldn't find expression at ${describePosition(cursor)} (only found $element)")
7071
val surroundingExpr = expandForReference(cursor, cursorExpr)
7172
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()
7275
val context = bindingContextOf(surroundingExpr, scope)
7376
LOG.info("Hovering {}", surroundingExpr)
74-
return referenceFromContext(cursor, context)
77+
return referenceFromContext(cursor, path, context)
7578
}
7679

7780
/**
@@ -80,13 +83,14 @@ class CompiledFile(
8083
* This method should not be used for anything other than finding definitions (at least for now).
8184
*/
8285
fun referenceExpressionAtPoint(cursor: Int): Pair<KtExpression, DeclarationDescriptor>? {
83-
return referenceFromContext(cursor, compile)
86+
val path = parse.containingFile.toPath()
87+
return referenceFromContext(cursor, path, compile)
8488
}
8589

86-
private fun referenceFromContext(cursor: Int, context: BindingContext): Pair<KtExpression, DeclarationDescriptor>? {
90+
private fun referenceFromContext(cursor: Int, path: Path, context: BindingContext): Pair<KtExpression, DeclarationDescriptor>? {
8791
val targets = context.getSliceContents(BindingContext.REFERENCE_TARGET)
8892
return targets.asSequence()
89-
.filter { cursor in it.key.textRange }
93+
.filter { cursor in it.key.textRange && it.key.containingFile.toPath() == path }
9094
.sortedBy { it.key.textRange.length }
9195
.map { it.toPair() }
9296
.firstOrNull()
@@ -221,8 +225,13 @@ class CompiledFile(
221225
*/
222226
fun scopeAtPoint(cursor: Int): LexicalScope? {
223227
val oldCursor = oldOffset(cursor)
228+
val path = parse.containingFile.toPath()
224229
return compile.getSliceContents(BindingContext.LEXICAL_SCOPE).asSequence()
225-
.filter { it.key.textRange.startOffset <= oldCursor && oldCursor <= it.key.textRange.endOffset }
230+
.filter {
231+
it.key.textRange.startOffset <= oldCursor
232+
&& oldCursor <= it.key.textRange.endOffset
233+
&& it.key.containingFile.toPath() == path
234+
}
226235
.sortedBy { it.key.textRange.length }
227236
.map { it.value }
228237
.firstOrNull()

0 commit comments

Comments
 (0)