forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
87 lines (78 loc) · 2.13 KB
/
matcher.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
package edit
import (
"errors"
"strings"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/eval/types"
"github.com/elves/elvish/util"
"github.com/xiaq/persistent/hashmap"
)
var (
errIncorrectNumOfResults = errors.New("matcher must return a bool for each candidate")
errMatcherMustBeFn = errors.New("matcher must be a function")
errMatcherInputMustBeString = errors.New("matcher input must be string")
)
var (
matchPrefix = eval.NewBuiltinFn(
"edit:match-prefix", wrapMatcher(strings.HasPrefix))
matchSubstr = eval.NewBuiltinFn(
"edit:match-substr", wrapMatcher(strings.Contains))
matchSubseq = eval.NewBuiltinFn(
"edit:match-subseq", wrapMatcher(util.HasSubseq))
)
func init() {
atEditorInit(func(ed *editor, ns eval.Ns) {
ed.matcher = types.MakeMapFromKV("", matchPrefix)
ns["-matcher"] = eval.NewVariableFromPtr(&ed.matcher)
})
}
func lookupMatcher(m hashmap.Map, name string) (eval.Callable, bool) {
key := name
if !hashmap.HasKey(m, key) {
// Use fallback matcher
if !hashmap.HasKey(m, "") {
return nil, false
}
key = ""
}
value, _ := m.Get(key)
matcher, ok := value.(eval.Callable)
return matcher, ok
}
func wrapMatcher(matcher func(s, p string) bool) interface{} {
return func(fm *eval.Frame,
opts eval.Options, pattern string, inputs eval.Inputs) {
var options struct {
IgnoreCase bool
SmartCase bool
}
opts.ScanToStruct(&options)
switch {
case options.IgnoreCase && options.SmartCase:
throwf("-ignore-case and -smart-case cannot be used together")
case options.IgnoreCase:
innerMatcher := matcher
matcher = func(s, p string) bool {
return innerMatcher(strings.ToLower(s), strings.ToLower(p))
}
case options.SmartCase:
innerMatcher := matcher
matcher = func(s, p string) bool {
if p == strings.ToLower(p) {
// Ignore case is pattern is all lower case.
return innerMatcher(strings.ToLower(s), p)
} else {
return innerMatcher(s, p)
}
}
}
out := fm.OutputChan()
inputs(func(v interface{}) {
s, ok := v.(string)
if !ok {
throw(errMatcherInputMustBeString)
}
out <- types.Bool(matcher(s, pattern))
})
}
}