forked from hugelgupf/u-root
-
Notifications
You must be signed in to change notification settings - Fork 7
/
complete_index.go
85 lines (78 loc) · 2.24 KB
/
complete_index.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
package completion
import (
"github.com/u-root/u-root/cmds/elvish/eval/vals"
"github.com/u-root/u-root/cmds/elvish/parse"
)
type indexComplContext struct {
complContextCommon
indexee interface{}
}
func (*indexComplContext) name() string { return "index" }
// Find context information for complIndex.
//
// Right now we only support cases where there is only one level of indexing,
// e.g. $a[<Tab> is supported but $a[x][<Tab> is not.
func findIndexComplContext(n parse.Node, ev pureEvaler) complContext {
if parse.IsSep(n) {
if parse.IsIndexing(n.Parent()) {
// We are just after an opening bracket.
indexing := parse.GetIndexing(n.Parent())
if len(indexing.Indicies) == 1 {
if indexee := ev.PurelyEvalPrimary(indexing.Head); indexee != nil {
return &indexComplContext{
complContextCommon{
"", quotingForEmptySeed, n.End(), n.End()},
indexee,
}
}
}
}
if parse.IsArray(n.Parent()) {
array := n.Parent()
if parse.IsIndexing(array.Parent()) {
// We are after an existing index and spaces.
indexing := parse.GetIndexing(array.Parent())
if len(indexing.Indicies) == 1 {
if indexee := ev.PurelyEvalPrimary(indexing.Head); indexee != nil {
return &indexComplContext{
complContextCommon{
"", quotingForEmptySeed, n.End(), n.End()},
indexee,
}
}
}
}
}
}
if parse.IsPrimary(n) {
primary := parse.GetPrimary(n)
compound, seed := primaryInSimpleCompound(primary, ev)
if compound != nil {
if parse.IsArray(compound.Parent()) {
array := compound.Parent()
if parse.IsIndexing(array.Parent()) {
// We are just after an incomplete index.
indexing := parse.GetIndexing(array.Parent())
if len(indexing.Indicies) == 1 {
if indexee := ev.PurelyEvalPrimary(indexing.Head); indexee != nil {
return &indexComplContext{
complContextCommon{
seed, primary.Type, compound.Begin(), compound.End()},
indexee,
}
}
}
}
}
}
}
return nil
}
func (ctx *indexComplContext) generate(env *complEnv, ch chan<- rawCandidate) error {
return vals.IterateKeys(ctx.indexee, func(k interface{}) bool {
if kstring, ok := k.(string); ok {
ch <- plainCandidate(kstring)
}
return true
})
}