/
ns_helper.go
66 lines (60 loc) · 1.32 KB
/
ns_helper.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
package edit
import (
"os"
"strings"
"github.com/elves/elvish/pkg/eval"
"github.com/elves/elvish/pkg/fsutil"
)
// Calls the passed function for each variable name in namespace ns that can be
// found from the top context.
func eachVariableInTop(builtin, global *eval.Ns, ns string, f func(s string)) {
switch ns {
case "builtin:":
builtin.IterateNames(f)
case "", ":":
global.IterateNames(f)
builtin.IterateNames(f)
case "e:":
fsutil.EachExternal(func(cmd string) {
f(cmd + eval.FnSuffix)
})
case "E:":
for _, s := range os.Environ() {
if i := strings.IndexByte(s, '='); i > 0 {
f(s[:i])
}
}
default:
segs := eval.SplitQNameSegs(ns)
mod := global.IndexName(segs[0])
if mod == nil {
mod = builtin.IndexName(segs[0])
}
for _, seg := range segs[1:] {
if mod == nil {
return
}
mod = mod.Get().(*eval.Ns).IndexName(seg)
}
if mod != nil {
mod.Get().(*eval.Ns).IterateNames(f)
}
}
}
// Calls the passed function for each namespace that can be used from the top
// context.
func eachNsInTop(builtin, global *eval.Ns, f func(s string)) {
f("builtin:")
f("e:")
f("E:")
global.IterateNames(func(name string) {
if strings.HasSuffix(name, eval.NsSuffix) {
f(name)
}
})
builtin.IterateNames(func(name string) {
if strings.HasSuffix(name, eval.NsSuffix) {
f(name)
}
})
}