-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpackage_scope.go
75 lines (65 loc) · 1.88 KB
/
package_scope.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
package symbol
// PackageScope represents a single package, which can contain one or more files
type PackageScope struct {
// Maps the file's name to its definitions
Files map[string]*FileScope
}
func NewPackageScope() *PackageScope {
return &PackageScope{
Files: make(map[string]*FileScope),
}
}
func (ps *PackageScope) ExcludeFile(excludedFileName string) *PackageScope {
newScope := &PackageScope{Files: make(map[string]*FileScope)}
for fileName, fileScope := range ps.Files {
if fileName != excludedFileName {
newScope.Files[fileName] = fileScope
}
}
return newScope
}
func (ps *PackageScope) FindStaticField() Finder {
pf := PackageFieldFinder(*ps)
return &pf
}
type PackageFieldFinder PackageScope
func (pf *PackageFieldFinder) By(criteria func(d *Definition) bool) []*Definition {
results := []*Definition{}
for _, file := range pf.Files {
for _, field := range file.BaseClass.Fields {
if criteria(field) {
results = append(results, field)
}
}
}
return results
}
func (ps *PackageFieldFinder) ByName(name string) []*Definition {
return ps.By(func(d *Definition) bool {
return d.Name == name
})
}
func (ps *PackageFieldFinder) ByOriginalName(originalName string) []*Definition {
return ps.By(func(d *Definition) bool {
return d.Name == originalName
})
}
func (ps *PackageScope) AddSymbolsFromFile(symbols *FileScope) {
ps.Files[symbols.BaseClass.Class.Name] = symbols
}
// FindClass searches for a class in the given package and returns a scope for it
// the class may be the subclass of another class
func (ps *PackageScope) FindClass(name string) *ClassScope {
for _, fileScope := range ps.Files {
if fileScope.BaseClass.Class.OriginalName == name {
return fileScope.BaseClass
}
for _, subclass := range fileScope.BaseClass.Subclasses {
class := subclass.FindClass(name)
if class != nil {
return fileScope.BaseClass
}
}
}
return nil
}