Skip to content

Commit 4b38f2c

Browse files
committed
fix: avoid adding the same symbol tree inside itself to avoid a loop
1 parent 9a7f637 commit 4b38f2c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

symbol_tree.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func (tree *SymbolTree) Find(name string) Symbol {
5555
func (tree *SymbolTree) GetNearestScopedTree(index int) *SymbolTree {
5656
if tree.Scopes != nil {
5757
for _, scopedTree := range tree.Scopes {
58+
if scopedTree == tree || scopedTree == nil {
59+
continue
60+
}
61+
5862
if index >= scopedTree.StartPos.Index && index <= scopedTree.EndPos.Index {
5963
return scopedTree.GetNearestScopedTree(index)
6064
}
@@ -84,11 +88,18 @@ func (tree *SymbolTree) Add(sym Symbol) {
8488
tree.EndPos = loc.EndPos
8589
}
8690

87-
if cSym := CastChildrenSymbol(sym); cSym != nil {
91+
if cSym := CastChildrenSymbol(sym); cSym != nil && cSym.Children() != tree {
8892
if tree.Scopes == nil {
8993
tree.Scopes = []*SymbolTree{}
9094
}
9195

96+
// check if already exists
97+
for _, scopedTree := range tree.Scopes {
98+
if scopedTree == cSym.Children() {
99+
return
100+
}
101+
}
102+
92103
tree.Scopes = append(tree.Scopes, cSym.Children())
93104
cSym.Children().Parent = tree
94105

0 commit comments

Comments
 (0)