Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(resolving): prefer local definition when find definitions #439

Merged
merged 2 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion server/src/e2e/suite/testcases/references/contracts.test
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ contract Test(
}
------------------------------------------------------------------------
References: [7:13]
Scope: GlobalSearchScope
Scope: LocalSearchScope:
{
fun foo() {
self.some;
self.other;
}
}
18 changes: 18 additions & 0 deletions server/src/indexes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ export class GlobalIndex {
return true
}

public processElsByKeyAndFile(
key: IndexKey,
file: File,
processor: ScopeProcessor,
state: ResolveState,
): boolean {
const fileIndex = this.files.get(file.uri)
if (fileIndex !== undefined) {
if (!fileIndex.processElementsByKey(key, processor, state)) return false
}

for (const [k, value] of this.files) {
if (k === file.uri) continue
if (!value.processElementsByKey(key, processor, state)) return false
}
return true
}

public elementByName<K extends IndexKey>(key: K, name: string): IndexKeyToType[K] | null {
for (const value of this.files.values()) {
const result = value.elementByName(key, name)
Expand Down
20 changes: 11 additions & 9 deletions server/src/psi/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,10 @@ export class Reference {
}

private processAllEntities(proc: ScopeProcessor, state: ResolveState): boolean {
const file = this.element.file

if (state.get("completion")) {
const intermediateProc = new (class implements ScopeProcessor {
const processor = new (class implements ScopeProcessor {
public execute(node: Node, state: ResolveState): boolean {
if (!(node instanceof Fun)) return true
if (node.withSelf()) return true // don't add methods to unqualified completion
Expand All @@ -564,17 +566,17 @@ export class Reference {
}
})()

if (!index.processElementsByKey(IndexKey.Funs, intermediateProc, state)) return false
if (!index.processElsByKeyAndFile(IndexKey.Funs, file, processor, state)) return false
} else {
if (!index.processElementsByKey(IndexKey.Funs, proc, state)) return false
if (!index.processElsByKeyAndFile(IndexKey.Funs, file, proc, state)) return false
}

if (!index.processElementsByKey(IndexKey.Primitives, proc, state)) return false
if (!index.processElementsByKey(IndexKey.Structs, proc, state)) return false
if (!index.processElementsByKey(IndexKey.Messages, proc, state)) return false
if (!index.processElementsByKey(IndexKey.Traits, proc, state)) return false
if (!index.processElementsByKey(IndexKey.Constants, proc, state)) return false
return index.processElementsByKey(IndexKey.Contracts, proc, state)
if (!index.processElsByKeyAndFile(IndexKey.Primitives, file, proc, state)) return false
if (!index.processElsByKeyAndFile(IndexKey.Structs, file, proc, state)) return false
if (!index.processElsByKeyAndFile(IndexKey.Messages, file, proc, state)) return false
if (!index.processElsByKeyAndFile(IndexKey.Traits, file, proc, state)) return false
if (!index.processElsByKeyAndFile(IndexKey.Constants, file, proc, state)) return false
return index.processElsByKeyAndFile(IndexKey.Contracts, file, proc, state)
}

public processBlock(proc: ScopeProcessor, state: ResolveState): boolean {
Expand Down
4 changes: 3 additions & 1 deletion server/src/psi/Referent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ export class Referent {
// fast path, identifier name doesn't equal to definition name
// self can refer to enclosing trait or contract
const nodeName = node.text
if (nodeName !== resolved.name() && nodeName !== "self" && nodeName !== "initOf")
if (nodeName !== resolved.name() && nodeName !== "self" && nodeName !== "initOf") {
return true
}
if (nodeName === "self" && !includeSelf) return true

const parent = node.parent
Expand Down Expand Up @@ -217,6 +218,7 @@ export class Referent {

if (
res.node.type === resolved.node.type &&
res.node.startPosition.row === resolved.node.startPosition.row &&
(identifier.text === resolved.name() || identifier.text === "self")
) {
// found new reference
Expand Down