-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
81 lines (68 loc) · 1.47 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package errgoengine
import "io/fs"
type Store struct {
DepGraph DepGraph
Documents map[string]*Document
Symbols map[string]*SymbolTree
FS fs.ReadFileFS
}
func NewEmptyStore() *Store {
return &Store{
DepGraph: DepGraph{},
Documents: map[string]*Document{},
Symbols: map[string]*SymbolTree{},
}
}
func (store *Store) FindSymbol(docPath string, name string, pos int) Symbol {
// Find local symbols first
tree := store.Symbols[docPath]
if pos != -1 {
// go innerwards first
for len(tree.Scopes) != 0 {
found := false
for _, s := range tree.Scopes {
if pos >= s.StartPos.Index && pos <= s.EndPos.Index {
found = true
tree = s
break
}
}
if !found {
break
}
}
}
if tree != nil {
parent := tree
// search innerwards first then outside
for parent != nil {
if sym := parent.Find(name); sym != nil {
return sym
} else {
if parent == tree.Parent {
break
}
parent = tree.Parent
}
}
}
return nil
}
func (store *ContextData) AddDocument(doc *Document) *Document {
if store.Documents == nil {
store.Documents = make(map[string]*Document)
}
store.Documents[doc.Path] = doc
return doc
}
func (store *Store) InitOrGetSymbolTree(docPath string) *SymbolTree {
if store.Symbols == nil {
store.Symbols = make(map[string]*SymbolTree)
}
if store.Symbols[docPath] == nil {
store.Symbols[docPath] = &SymbolTree{
Symbols: make(map[string]Symbol),
}
}
return store.Symbols[docPath]
}