-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfile.go
100 lines (77 loc) · 2.12 KB
/
file.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package parser
import (
"sort"
"strings"
"github.com/tliron/kutil/terminal"
"github.com/tliron/kutil/util"
"github.com/tliron/puccini/tosca/parsing"
)
//
// File
//
type File struct {
EntityPtr parsing.EntityPtr
Container *File
Imports Files
NameTransformer parsing.NameTransformer
importsLock util.RWLocker
}
func NewEmptyFile(parsingContext *parsing.Context, container *File, nameTransformer parsing.NameTransformer) *File {
return NewFile(parsing.NewContextContainer(parsingContext), container, nameTransformer)
}
func NewFile(entityPtr parsing.EntityPtr, container *File, nameTransformer parsing.NameTransformer) *File {
self := File{
EntityPtr: entityPtr,
Container: container,
NameTransformer: nameTransformer,
importsLock: util.NewDefaultRWLocker(),
}
if container != nil {
container.AddImport(&self)
}
return &self
}
func (self *File) AddImport(import_ *File) {
self.importsLock.Lock()
defer self.importsLock.Unlock()
self.Imports = append(self.Imports, import_)
}
func (self *File) GetContext() *parsing.Context {
return parsing.GetContext(self.EntityPtr)
}
// Print
func (self *File) PrintImports(indent int, treePrefix terminal.TreePrefix) {
self.importsLock.RLock()
imports := append(self.Imports[:0:0], self.Imports...)
self.importsLock.RUnlock()
last := len(imports) - 1
// Sort
sort.Sort(imports)
for i, file := range imports {
isLast := i == last
file.PrintNode(indent, treePrefix, isLast)
file.PrintImports(indent, append(treePrefix, isLast))
}
}
func (self *File) PrintNode(indent int, treePrefix terminal.TreePrefix, last bool) {
treePrefix.Print(indent, last)
terminal.Printf("%s\n", terminal.StdoutStylist.Value(self.GetContext().URL.String()))
}
//
// Files
//
type Files []*File
// ([sort.Interface])
func (self Files) Len() int {
return len(self)
}
// ([sort.Interface])
func (self Files) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
// ([sort.Interface])
func (self Files) Less(i, j int) bool {
iName := self[i].GetContext().URL.String()
jName := self[j].GetContext().URL.String()
return strings.Compare(iName, jName) < 0
}