/
rooter.go
43 lines (35 loc) · 999 Bytes
/
rooter.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
package wrkspc
import (
"log"
"github.com/laytan/php-parser/pkg/ast"
)
// Rooter is a common way of passing around the root and path of a file/symbol.
//
// If root is not available upon creation, you can leave it nil and it will be
// retrieved and cached when asked for.
//
// A valid path is mandatory though.
type Rooter struct {
path string
root *ast.Root
}
// NewRooter creates a new rooter struct, you can pass 0 or 1 root.
// If 0 roots, the root will be retrieved and cached when asked for.
// If 1 root, the given root is used when asked for.
// If > 2 roots, panic.
func NewRooter(path string, root ...*ast.Root) *Rooter {
if len(root) > 1 {
log.Panic("[wrkspc.NewRooter]: can only create a rooter with one root")
}
if len(root) == 1 {
return &Rooter{path: path, root: root[0]}
}
return &Rooter{path: path}
}
func (r *Rooter) Path() string { return r.path }
func (r *Rooter) Root() *ast.Root {
if r.root == nil {
r.root = Current.FIROf(r.path)
}
return r.root
}