Skip to content

Commit 2bf56b7

Browse files
committed
more
1 parent f6f429b commit 2bf56b7

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

internal/ls/findallreferences.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,21 +1199,21 @@ type refSearch struct {
11991199
includes func(symbol *ast.Symbol) bool
12001200
}
12011201

1202-
// type inheritKey struct {
1203-
// symbol *ast.Symbol
1204-
// parent *ast.Symbol
1205-
// }
1202+
type inheritKey struct {
1203+
symbol *ast.Symbol
1204+
parent *ast.Symbol
1205+
}
12061206

12071207
type refState struct {
12081208
sourceFiles []*ast.SourceFile
12091209
sourceFilesSet *collections.Set[string]
1210-
specialSearchKind string // !!! none, constructor, class
1210+
specialSearchKind string // "none", "constructor", or "class"
12111211
checker *checker.Checker
12121212
// cancellationToken CancellationToken
1213-
searchMeaning ast.SemanticMeaning
1214-
options refOptions
1215-
result []*SymbolAndEntries
1216-
// inheritsFromCache map[inheritKey]bool
1213+
searchMeaning ast.SemanticMeaning
1214+
options refOptions
1215+
result []*SymbolAndEntries
1216+
inheritsFromCache map[inheritKey]bool
12171217
seenContainingTypeReferences collections.Set[*ast.Node] // node seen tracker
12181218
// seenReExportRHS *collections.Set[*ast.Node] // node seen tracker
12191219
importTracker ImportTracker
@@ -1229,7 +1229,7 @@ func newState(sourceFiles []*ast.SourceFile, sourceFilesSet *collections.Set[str
12291229
checker: checker,
12301230
searchMeaning: searchMeaning,
12311231
options: options,
1232-
// inheritsFromCache: map[inheritKey]bool{},
1232+
inheritsFromCache: map[inheritKey]bool{},
12331233
// seenReExportRHS: &collections.Set[*ast.Node]{},
12341234
symbolToReferences: map[*ast.Symbol]*SymbolAndEntries{},
12351235
sourceFileToSeenSymbols: map[*ast.SourceFile]*collections.Set[*ast.Symbol]{},
@@ -1281,7 +1281,6 @@ func (state *refState) createSearch(location *ast.Node, symbol *ast.Symbol, comi
12811281
}
12821282

12831283
func (state *refState) referenceAdder(searchSymbol *ast.Symbol) func(*ast.Node, entryKind) {
1284-
// !!! after find all references is fully implemented, rename this to something like 'getReferenceAdder'
12851284
symbolAndEntries := state.symbolToReferences[searchSymbol]
12861285
if symbolAndEntries == nil {
12871286
symbolAndEntries = NewSymbolAndEntries(definitionKindSymbol, nil, searchSymbol, nil)
@@ -1822,9 +1821,7 @@ func (state *refState) getRelatedSymbol(search *refSearch, referenceSymbol *ast.
18221821
},
18231822
func(rootSymbol *ast.Symbol) bool {
18241823
return !(len(search.parents) != 0 && !core.Some(search.parents, func(parent *ast.Symbol) bool {
1825-
return false
1826-
// !!! not implemented
1827-
// return state.explicitlyInheritsFrom(rootSymbol.Parent, parent)
1824+
return state.explicitlyInheritsFrom(rootSymbol.Parent, parent)
18281825
}))
18291826
},
18301827
)
@@ -1976,3 +1973,34 @@ func (state *refState) searchForName(sourceFile *ast.SourceFile, search *refSear
19761973
state.getReferencesInSourceFile(sourceFile, search, true /*addReferencesHere*/)
19771974
}
19781975
}
1976+
1977+
func (state *refState) explicitlyInheritsFrom(symbol *ast.Symbol, parent *ast.Symbol) bool {
1978+
if symbol == parent {
1979+
return true
1980+
}
1981+
1982+
// Check cache first
1983+
key := inheritKey{symbol: symbol, parent: parent}
1984+
if cached, ok := state.inheritsFromCache[key]; ok {
1985+
return cached
1986+
}
1987+
1988+
// Set to false initially to prevent infinite recursion
1989+
state.inheritsFromCache[key] = false
1990+
1991+
if symbol.Declarations == nil {
1992+
return false
1993+
}
1994+
1995+
inherits := core.Some(symbol.Declarations, func(declaration *ast.Node) bool {
1996+
superTypeNodes := getAllSuperTypeNodes(declaration)
1997+
return core.Some(superTypeNodes, func(typeReference *ast.TypeNode) bool {
1998+
typ := state.checker.GetTypeAtLocation(typeReference.AsNode())
1999+
return typ != nil && typ.Symbol() != nil && state.explicitlyInheritsFrom(typ.Symbol(), parent)
2000+
})
2001+
})
2002+
2003+
// Update cache with the actual result
2004+
state.inheritsFromCache[key] = inherits
2005+
return inherits
2006+
}

0 commit comments

Comments
 (0)