-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
macro.go
60 lines (50 loc) · 1.07 KB
/
macro.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
package spec
import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-spec/pkg/macro"
)
type Macro struct {
macro macro.Macro[carapace.Action]
}
func (m Macro) Parse(s string) carapace.Action {
a, err := m.macro.Parse(s)
if err != nil {
return carapace.ActionMessage(err.Error())
}
return *a
}
func (m Macro) Signature() string {
return m.macro.Signature()
}
var macros = make(macro.MacroMap[Macro])
func addCoreMacro(s string, m Macro) {
macros[s] = m
}
// AddMacro adds a custom macro
func AddMacro(s string, m Macro) {
macros["_."+s] = m
}
func MacroN(f func() carapace.Action) Macro {
return Macro{
macro: macro.MacroN(func() (*carapace.Action, error) {
a := f()
return &a, nil
}),
}
}
func MacroI[T any](f func(t T) carapace.Action) Macro {
return Macro{
macro: macro.MacroI(func(t T) (*carapace.Action, error) {
a := f(t)
return &a, nil
}),
}
}
func MacroV[T any](f func(t ...T) carapace.Action) Macro {
return Macro{
macro: macro.MacroV(func(t ...T) (*carapace.Action, error) {
a := f(t...)
return &a, nil
}),
}
}