forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight.go
56 lines (51 loc) · 1.21 KB
/
highlight.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
package edit
import (
"os"
"github.com/u-root/u-root/cmds/elvish/edit/highlight"
"github.com/u-root/u-root/cmds/elvish/eval"
"github.com/u-root/u-root/cmds/elvish/parse"
"github.com/u-root/u-root/cmds/elvish/util"
)
func doHighlight(n parse.Node, ed *editor) {
s := &highlight.Emitter{
func(s string) bool { return goodFormHead(s, ed) },
ed.styling.Add,
}
s.EmitAll(n)
}
func goodFormHead(head string, ed *editor) bool {
if eval.IsBuiltinSpecial[head] {
return true
} else if util.DontSearch(head) {
// XXX don't stat twice
return util.IsExecutable(head) || isDir(head)
} else {
ev := ed.evaler
explode, ns, name := eval.ParseVariableRef(head)
if !explode {
switch ns {
case "":
if ev.Builtin[name+eval.FnSuffix] != nil || ev.Global[name+eval.FnSuffix] != nil {
return true
}
case "e":
if ed.isExternal[name] {
return true
}
default:
mod := ev.Global[ns+eval.NsSuffix]
if mod == nil {
mod = ev.Builtin[ns+eval.NsSuffix]
}
if mod != nil && mod.Get().(eval.Ns)[name+eval.FnSuffix] != nil {
return true
}
}
}
return ed.isExternal[head]
}
}
func isDir(fname string) bool {
stat, err := os.Stat(fname)
return err == nil && stat.IsDir()
}