/
candidate.go
176 lines (148 loc) · 5.36 KB
/
candidate.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package completion
import (
"fmt"
"os"
"sort"
"github.com/elves/elvish/edit/ui"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/eval/vals"
"github.com/elves/elvish/parse"
"github.com/xiaq/persistent/hash"
)
type candidate struct {
code string // This is what will be substituted on the command line.
menu ui.Styled // This is what is displayed in the completion menu.
}
// rawCandidate is what can be converted to a candidate.
type rawCandidate interface {
text() string
cook(q parse.PrimaryType) *candidate
}
type rawCandidates []rawCandidate
func (cs rawCandidates) Len() int { return len(cs) }
func (cs rawCandidates) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
func (cs rawCandidates) Less(i, j int) bool { return cs[i].text() < cs[j].text() }
// plainCandidate is a minimal implementation of rawCandidate.
type plainCandidate string
func (plainCandidate) Kind() string { return "string" }
func (p plainCandidate) Equal(a interface{}) bool { return p == a }
func (p plainCandidate) Hash() uint32 { return hash.String(string(p)) }
func (p plainCandidate) Repr(l int) string { return vals.Repr(string(p), l) }
func (p plainCandidate) text() string { return string(p) }
func (p plainCandidate) cook(q parse.PrimaryType) *candidate {
s := string(p)
quoted, _ := parse.QuoteAs(s, q)
return &candidate{code: quoted, menu: ui.Unstyled(s)}
}
// noQuoteCandidate is a rawCandidate that does not quote when cooked.
type noQuoteCandidate string
func (noQuoteCandidate) Kind() string { return "string" }
func (nq noQuoteCandidate) Equal(a interface{}) bool { return nq == a }
func (nq noQuoteCandidate) Hash() uint32 { return hash.String(string(nq)) }
func (nq noQuoteCandidate) Repr(l int) string { return vals.Repr(string(nq), l) }
func (nq noQuoteCandidate) text() string { return string(nq) }
func (nq noQuoteCandidate) cook(parse.PrimaryType) *candidate {
s := string(nq)
return &candidate{code: s, menu: ui.Unstyled(s)}
}
// complexCandidate is an implementation of rawCandidate that offers
// customization options.
type complexCandidate struct {
stem string // Used in the code and the menu.
codeSuffix string // Appended to the code.
displaySuffix string // Appended to the display.
style ui.Styles // Used in the menu.
}
func (c *complexCandidate) Index(k interface{}) (interface{}, bool) {
switch k {
case "stem":
return c.stem, true
case "code-suffix":
return c.codeSuffix, true
case "display-suffix":
return c.displaySuffix, true
case "style":
return c.style.String(), true
}
return nil, false
}
func (c *complexCandidate) IterateKeys(f func(interface{}) bool) {
vals.Feed(f, "stem", "code-suffix", "display-suffix", "style")
}
func (c *complexCandidate) Kind() string { return "map" }
func (c *complexCandidate) Equal(a interface{}) bool {
rhs, ok := a.(*complexCandidate)
return ok && c.stem == rhs.stem && c.codeSuffix == rhs.codeSuffix && c.displaySuffix == rhs.displaySuffix && c.style.Eq(rhs.style)
}
func (c *complexCandidate) Hash() uint32 {
h := hash.DJBInit
h = hash.DJBCombine(h, hash.String(c.stem))
h = hash.DJBCombine(h, hash.String(c.codeSuffix))
h = hash.DJBCombine(h, hash.String(c.displaySuffix))
h = hash.DJBCombine(h, c.style.Hash())
return h
}
func (c *complexCandidate) Repr(indent int) string {
// TODO(xiaq): Pretty-print when indent >= 0
return fmt.Sprintf("(edit:complex-candidate %s &code-suffix=%s &display-suffix=%s &style=%s)",
parse.Quote(c.stem), parse.Quote(c.codeSuffix),
parse.Quote(c.displaySuffix), parse.Quote(c.style.String()))
}
func (c *complexCandidate) text() string { return c.stem }
func (c *complexCandidate) cook(q parse.PrimaryType) *candidate {
quoted, _ := parse.QuoteAs(c.stem, q)
return &candidate{
code: quoted + c.codeSuffix,
menu: ui.Styled{c.stem + c.displaySuffix, c.style},
}
}
// makeComplexCandidate composes a complexCandidate.
func makeComplexCandidate(rawOpts eval.RawOptions, stem string) *complexCandidate {
opts := struct {
CodeSuffix string
DisplaySuffix string
Style string
}{}
rawOpts.Scan(&opts)
return &complexCandidate{
stem: stem,
codeSuffix: opts.CodeSuffix,
displaySuffix: opts.DisplaySuffix,
style: ui.StylesFromString(opts.Style),
}
}
func filterRawCandidates(ev *eval.Evaler, matcher eval.Callable, seed string, chanRawCandidate <-chan rawCandidate) ([]rawCandidate, error) {
matcherInput := make(chan interface{})
stopCollector := make(chan struct{})
var collected []rawCandidate
go func() {
defer close(matcherInput)
for rc := range chanRawCandidate {
collected = append(collected, rc)
select {
case matcherInput <- rc.text():
case <-stopCollector:
return
}
}
}()
defer close(stopCollector)
ports := []*eval.Port{
{Chan: matcherInput, File: eval.DevNull}, {File: os.Stdout}, {File: os.Stderr}}
ec := eval.NewTopFrame(ev, eval.NewInternalSource("[editor matcher]"), ports)
args := []interface{}{seed}
values, err := ec.CaptureOutput(matcher, args, eval.NoOpts)
if err != nil {
return nil, err
} else if len(values) != len(collected) {
return nil, errIncorrectNumOfResults
}
var filtered []rawCandidate
for i, value := range values {
if vals.Bool(value) {
filtered = append(filtered, collected[i])
}
}
sort.Sort(rawCandidates(filtered))
return filtered, nil
}