forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete_index.go
98 lines (88 loc) · 2.46 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
86
87
88
89
90
91
92
93
94
95
96
97
98
package edit
import (
"errors"
"github.com/elves/elvish/parse"
"github.com/xiaq/persistent/hashmap"
)
var errCannotIterateKey = errors.New("indexee does not support iterating keys")
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 {
m, ok := ctx.indexee.(hashmap.Map)
if !ok {
return errCannotIterateKey
}
complIndexInner(m, ch)
return nil
}
func complIndexInner(m hashmap.Map, ch chan<- rawCandidate) {
for it := m.Iterator(); it.HasElem(); it.Next() {
k, _ := it.Elem()
if kstring, ok := k.(string); ok {
ch <- plainCandidate(kstring)
}
}
}