@@ -4,47 +4,16 @@ package cmd
44
55import (
66 "fmt"
7- "os"
8- "regexp"
97 "strings"
108)
119
12- func getSpecs () (specs []string , dir string ) {
13- r := regexp .MustCompile (`^[0-9a-zA-Z_\-.]+\.yaml$` ) // sanity check
14- specs = make ([]string , 0 )
15- if configDir , err := os .UserConfigDir (); err == nil {
16- dir = configDir + "/carapace/specs"
17- if entries , err := os .ReadDir (dir ); err == nil {
18- for _ , entry := range entries {
19- if ! entry .IsDir () && strings .HasSuffix (entry .Name (), ".yaml" ) && r .MatchString (entry .Name ()) {
20- specs = append (specs , strings .TrimSuffix (entry .Name (), ".yaml" ))
21- }
22- }
23- } else if os .IsNotExist (err ) {
24- os .MkdirAll (dir , os .ModePerm )
25- }
26- }
27- return
28- }
29-
3010func bash_lazy (completers []string ) string {
3111 snippet := `_carapace_lazy() {
3212 source <(carapace $1 bash)
3313 $"_$1_completion"
3414}
3515complete -F _carapace_lazy %v
3616`
37-
38- if specs , dir := getSpecs (); len (specs ) > 0 {
39- snippet += fmt .Sprintf (`
40- _carapace_lazy_spec() {
41- source <(carapace --spec "%v/$1.yaml" bash)
42- $"_$1_completion"
43- }
44- complete -F _carapace_lazy_spec %v
45- ` , dir , strings .Join (specs , " " ))
46- }
47-
4817 return fmt .Sprintf (snippet , strings .Join (completers , " " ))
4918}
5019
@@ -55,17 +24,6 @@ func bash_ble_lazy(completers []string) string {
5524}
5625complete -F _carapace_lazy %v
5726`
58-
59- if specs , dir := getSpecs (); len (specs ) > 0 {
60- snippet += fmt .Sprintf (`
61- _carapace_lazy_spec() {
62- source <(carapace --spec %v/$1.yaml bash-ble)
63- $"_$1_completion_ble"
64- }
65- complete -F _carapace_lazy_spec %v
66- ` , dir , strings .Join (specs , " " ))
67- }
68-
6927 return fmt .Sprintf (snippet , strings .Join (completers , " " ))
7028}
7129
@@ -78,20 +36,6 @@ func elvish_lazy(completers []string) string {
7836 }
7937}
8038`
81- if specs , dir := getSpecs (); len (specs ) > 0 {
82- snippet += fmt .Sprintf (`
83- put %v | each {|c|
84- set edit:completion:arg-completer[$c] = {|@arg|
85- set edit:completion:arg-completer[$c] = {|@arg| }
86- eval (carapace --spec "%v/"$c".yaml" elvish | slurp)
87- $edit:completion:arg-completer[$c] $@arg
88- }
89-
90- }
91- ` , strings .Join (specs , " " ), dir )
92-
93- }
94-
9539 return fmt .Sprintf (snippet , strings .Join (completers , " " ))
9640}
9741
10751 for index , completer := range completers {
10852 complete [index ] = fmt .Sprintf (`complete -c '%v' -f -a '(_carapace_lazy %v)'` , completer , completer )
10953 }
110-
111- if specs , dir := getSpecs (); len (specs ) > 0 {
112- snippet += fmt .Sprintf (`
113- function _carapace_lazy_spec
114- complete -c $argv[1] -e
115- carapace --spec "%v/$argv[1].yaml" fish | source
116- complete --do-complete=(commandline -cp)
117- end
118-
119- ` , dir )
120- for _ , spec := range specs {
121- snippet += fmt .Sprintf (`complete -c '%v' -f -a '(_carapace_lazy_spec %v)'
122- ` , spec , spec )
123- }
124- }
125-
12654 return fmt .Sprintf (snippet , strings .Join (complete , "\n " ))
12755}
12856
12957func nushell_lazy (completers []string ) string {
130- specs , dir := getSpecs ()
131-
132- specSnippets := make ([]string , 0 )
133- for _ , spec := range specs {
134- specSnippets = append (specSnippets , fmt .Sprintf (` %v: { carapace --spec '%v/%v.yaml' nushell $spans | from json }
135- ` , spec , dir , spec ))
136- }
137- return fmt .Sprintf (`let external_completer = {|spans|
138- {
139- $spans.0: {carapace $spans.0 nushell $spans | from json } # default
140- %v } | get $spans.0 | each {|it| do $it}
58+ return `let carapace_completer = {|spans|
59+ carapace $spans.0 nushell $spans | from json
14160}
14261
14362let-env config = {
144- external_completer: $external_completer
145- }` , strings . Join ( specSnippets , "" ))
63+ external_completer: $carapace_completer
64+ }`
14665}
14766
14867func oil_lazy (completers []string ) string {
@@ -152,17 +71,6 @@ func oil_lazy(completers []string) string {
15271}
15372complete -F _carapace_lazy %v
15473`
155-
156- if specs , dir := getSpecs (); len (specs ) > 0 {
157- snippet += fmt .Sprintf (`
158- _carapace_lazy_spec() {
159- source <(carapace --spec "%v/$1.yaml" oil)
160- $"_$1_completion"
161- }
162- complete -F _carapace_lazy_spec %v
163- ` , dir , strings .Join (specs , " " ))
164- }
165-
16674 return fmt .Sprintf (snippet , strings .Join (completers , " " ))
16775}
16876
@@ -179,22 +87,6 @@ func powershell_lazy(completers []string) string {
17987 for index , completer := range completers {
18088 complete [index ] = fmt .Sprintf (`Register-ArgumentCompleter -Native -CommandName '%v' -ScriptBlock $_carapace_lazy` , completer )
18189 }
182-
183- if specs , dir := getSpecs (); len (specs ) > 0 {
184- snippet += fmt .Sprintf (`$_carapace_lazy_spec = {
185- param($wordToComplete, $commandAst, $cursorPosition)
186- $completer = $commandAst.CommandElements[0].Value
187- carapace --spec "%v/$completer.yaml" powershell | Out-String | Invoke-Expression
188- & (Get-Item "Function:_${completer}_completer") $wordToComplete $commandAst $cursorPosition
189- }
190- ` , dir )
191- for _ , spec := range specs {
192- snippet += fmt .Sprintf (`Register-ArgumentCompleter -Native -CommandName '%v' -ScriptBlock $_carapace_lazy_spec
193- ` , spec )
194- }
195-
196- }
197-
19890 return fmt .Sprintf (snippet , strings .Join (complete , "\n " ))
19991}
20092
@@ -204,12 +96,6 @@ func tcsh_lazy(completers []string) string {
20496 for index , c := range completers {
20597 snippet [index ] = fmt .Sprintf ("complete \" %v\" 'p@*@`echo \" $COMMAND_LINE'\" ''\" '\" | xargs carapace %v tcsh `@@' ;" , c , c )
20698 }
207-
208- if specs , dir := getSpecs (); len (specs ) > 0 {
209- for _ , spec := range specs {
210- snippet = append (snippet , fmt .Sprintf ("complete \" %v\" 'p@*@`echo \" $COMMAND_LINE'\" ''\" '\" | xargs carapace --spec \" %v/%v.yaml\" tcsh `@@' ;" , spec , dir , spec ))
211- }
212- }
21399 return strings .Join (snippet , "\n " )
214100}
215101
@@ -231,22 +117,6 @@ def _carapace_lazy(context):
231117 for index , completer := range completers {
232118 complete [index ] = fmt .Sprintf (`'%v'` , completer )
233119 }
234-
235- if specs , dir := getSpecs (); len (specs ) > 0 {
236- quotedSpecs := make ([]string , 0 )
237- for _ , spec := range specs {
238- quotedSpecs = append (quotedSpecs , fmt .Sprintf (`'%v'` , spec ))
239- }
240- snippet += fmt .Sprintf (`
241- if (context.command and
242- context.command.arg_index > 0 and
243- context.command.args[0].value in [%v]):
244- XSH.completers = XSH.completers.copy()
245- exec(compile(subprocess.run(['carapace', '--spec', '%v/'+context.command.args[0].value+'.yaml', 'xonsh'], stdout=subprocess.PIPE).stdout.decode('utf-8'), "", "exec"))
246- return XSH.completers[context.command.args[0].value](context)
247- ` , strings .Join (quotedSpecs , ", " ), dir )
248-
249- }
250120 snippet += `_add_one_completer('carapace_lazy', _carapace_lazy, 'start')`
251121 return fmt .Sprintf (snippet , strings .Join (complete , ", " ))
252122}
@@ -257,15 +127,5 @@ func zsh_lazy(completers []string) string {
257127}
258128compdef _carapace_lazy %v
259129`
260-
261- if specs , dir := getSpecs (); len (specs ) > 0 {
262- snippet += fmt .Sprintf (`function _carapace_lazy_spec {
263- source <(carapace --spec "%v/$words[1].yaml" zsh)
264- }
265- compdef _carapace_lazy_spec %v
266- ` , dir , strings .Join (specs , " " ))
267-
268- }
269-
270130 return fmt .Sprintf (snippet , strings .Join (completers , " " ))
271131}
0 commit comments