Skip to content

Commit

Permalink
added custom macros
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed May 6, 2022
1 parent c15cc7b commit 7806533
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 177 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.18

- name: Generate
run: go generate ./cmd/...
Expand All @@ -39,7 +39,7 @@ jobs:
run: '[ "$(gofmt -d -s . | tee -a /dev/stderr)" = "" ]'

- name: "staticcheck"
run: go install honnef.co/go/tools/cmd/staticcheck@2021.1.1 && staticcheck ./...
run: go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck ./...

- name: "caralint"
run: go run ./cmd/caralint completers/*/cmd/*.go
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmd/carapace/carapace
cmd/carapace/cmd/completers.go
cmd/carapace/cmd/completers_release.go
cmd/carapace/cmd/macros.go
cmd/caraparse/caraparse
completers/*/*_completer
completers/gh_completer/cmd/action/api.git.luolix.top.json
Expand Down
163 changes: 158 additions & 5 deletions cmd/carapace/cmd/lazyInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,47 @@ package cmd

import (
"fmt"
"os"
"regexp"
"strings"
)

func getSpecs() (specs []string, dir string) {
r := regexp.MustCompile(`^[0-9a-zA-Z_\-.]+\.yaml$`) // sanity check
specs = make([]string, 0)
if configDir, err := os.UserConfigDir(); err == nil {
dir = configDir + "/carapace/specs"
if entries, err := os.ReadDir(dir); err == nil {
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".yaml") && r.MatchString(entry.Name()) {
specs = append(specs, strings.TrimSuffix(entry.Name(), ".yaml"))
}
}
} else if os.IsNotExist(err) {
os.MkdirAll(dir, os.ModePerm)
}
}
return
}

func bash_lazy(completers []string) string {
snippet := `_carapace_lazy() {
source <(carapace $1 bash)
$"_$1_completion"
}
complete -F _carapace_lazy %v
`

if specs, dir := getSpecs(); len(specs) > 0 {
snippet += fmt.Sprintf(`
_carapace_lazy_spec() {
source <(carapace --spec %v/$1.yaml bash)
$"_$1_completion"
}
complete -F _carapace_lazy_spec %v
`, dir, strings.Join(specs, " "))
}

return fmt.Sprintf(snippet, strings.Join(completers, " "))
}

Expand All @@ -24,6 +55,17 @@ func bash_ble_lazy(completers []string) string {
}
complete -F _carapace_lazy %v
`

if specs, dir := getSpecs(); len(specs) > 0 {
snippet += fmt.Sprintf(`
_carapace_lazy_spec() {
source <(carapace --spec %v/$1.yaml bash-ble)
$"_$1_completion"
}
complete -F _carapace_lazy_spec %v
`, dir, strings.Join(specs, " "))
}

return fmt.Sprintf(snippet, strings.Join(completers, " "))
}

Expand All @@ -36,6 +78,20 @@ func elvish_lazy(completers []string) string {
}
}
`
if specs, dir := getSpecs(); len(specs) > 0 {
snippet += fmt.Sprintf(`
put %v | each {|c|
set edit:completion:arg-completer[$c] = {|@arg|
set edit:completion:arg-completer[$c] = {|@arg| }
eval (carapace --spec %v/$c.yaml elvish | slurp)
$edit:completion:arg-completer[$c] $@arg
}
}
`, strings.Join(specs, " "), dir)

}

return fmt.Sprintf(snippet, strings.Join(completers, " "))
}

Expand All @@ -49,9 +105,24 @@ end
`
complete := make([]string, len(completers))
for index, completer := range completers {
complete[index] = fmt.Sprintf(` complete --erase -c '%v'
complete -c '%v' -f -a '(_carapace_lazy %v)'`, completer, completer, completer)
complete[index] = fmt.Sprintf(`complete -c '%v' -f -a '(_carapace_lazy %v)'`, completer, completer)
}

if specs, dir := getSpecs(); len(specs) > 0 {
snippet += fmt.Sprintf(`
function _carapace_lazy_spec
complete -c $argv[1] -e
carapace --spec %v/$argv[1].yaml fish | source
complete --do-complete=(commandline -cp)
end
`, dir)
for _, spec := range specs {
snippet += fmt.Sprintf(`complete -c '%v' -f -a '(_carapace_lazy_spec %v)'
`, spec, spec)
}
}

return fmt.Sprintf(snippet, strings.Join(complete, "\n"))
}

Expand All @@ -62,7 +133,7 @@ func nushell_lazy(completers []string) string {
...args: string@"nu-complete carapace"
]`, completer)
}
return fmt.Sprintf(`module carapace {
snippet := fmt.Sprintf(`module carapace {
def "nu-complete carapace" [line: string, pos: int] {
let words = ($line | str substring [0 $pos] | split row " ")
if ($line | str substring [0 $pos] | str ends-with " ") {
Expand All @@ -73,9 +144,32 @@ func nushell_lazy(completers []string) string {
}
%v
`, strings.Join(exports, "\n"))

// if specs, dir := getSpecs(); len(specs) > 0 {
// snippet += fmt.Sprintf(`
// def "nu-complete carapace-spec" [line: string, pos: int] {
// let words = ($line | str substring [0 $pos] | split row " ")
// if ($line | str substring [0 $pos] | str ends-with " ") {
// carapace --spec '%v'/$words.0'.yaml' nushell ($words | append "") | from json
// } else {
// carapace --spec '%v'/$words.0'.yaml' nushell $words | from json
// }
// }
//`, dir, dir)
//
// for _, spec := range specs {
// snippet += fmt.Sprintf(` export extern "%v" [
// ...args: string@"nu-complete carapace-spec"
// ]`, spec)
// }
// }

snippet += `
}
use carapace *
`, strings.Join(exports, "\n"))
`
return snippet
}

func oil_lazy(completers []string) string {
Expand All @@ -85,6 +179,17 @@ func oil_lazy(completers []string) string {
}
complete -F _carapace_lazy %v
`

if specs, dir := getSpecs(); len(specs) > 0 {
snippet += fmt.Sprintf(`
_carapace_lazy_spec() {
source <(carapace --spec %v/$1.yaml oil)
$"_$1_completion"
}
complete -F _carapace_lazy_spec %v
`, dir, strings.Join(specs, " "))
}

return fmt.Sprintf(snippet, strings.Join(completers, " "))
}

Expand All @@ -101,6 +206,22 @@ func powershell_lazy(completers []string) string {
for index, completer := range completers {
complete[index] = fmt.Sprintf(`Register-ArgumentCompleter -Native -CommandName '%v' -ScriptBlock $_carapace_lazy`, completer)
}

if specs, dir := getSpecs(); len(specs) > 0 {
snippet += fmt.Sprintf(`$_carapace_lazy_spec = {
param($wordToComplete, $commandAst, $cursorPosition)
$completer = $commandAst.CommandElements[0].Value
carapace --spec %v/$completer.yaml powershell | Out-String | Invoke-Expression
& (Get-Item "Function:_${completer}_completer") $wordToComplete $commandAst $cursorPosition
}
`, dir)
for _, spec := range specs {
snippet += fmt.Sprintf(`Register-ArgumentCompleter -Native -CommandName '%v' -ScriptBlock $_carapace_lazy_spec
`, spec)
}

}

return fmt.Sprintf(snippet, strings.Join(complete, "\n"))
}

Expand All @@ -110,6 +231,12 @@ func tcsh_lazy(completers []string) string {
for index, c := range completers {
snippet[index] = fmt.Sprintf("complete \"%v\" 'p@*@`echo \"$COMMAND_LINE'\"''\"'\" | xargs carapace %v tcsh `@@' ;", c, c)
}

if specs, dir := getSpecs(); len(specs) > 0 {
for _, spec := range specs {
snippet = append(snippet, fmt.Sprintf("complete \"%v\" 'p@*@`echo \"$COMMAND_LINE'\"''\"'\" | xargs carapace --spec %v/%v.yaml tcsh `@@' ;", spec, dir, spec))
}
}
return strings.Join(snippet, "\n")
}

Expand All @@ -126,12 +253,28 @@ def _carapace_lazy(context):
builtins.__xonsh__.completers = builtins.__xonsh__.completers.copy()
exec(compile(subprocess.run(['carapace', context.command.args[0].value, 'xonsh'], stdout=subprocess.PIPE).stdout.decode('utf-8'), "", "exec"))
return builtins.__xonsh__.completers[context.command.args[0].value](context)
_add_one_completer('carapace_lazy', _carapace_lazy, 'start')
`
complete := make([]string, len(completers))
for index, completer := range completers {
complete[index] = fmt.Sprintf(`'%v'`, completer)
}

if specs, dir := getSpecs(); len(specs) > 0 {
quotedSpecs := make([]string, 0)
for _, spec := range specs {
quotedSpecs = append(quotedSpecs, fmt.Sprintf(`'%v'`, spec))
}
snippet += fmt.Sprintf(`
if (context.command and
context.command.arg_index > 0 and
context.command.args[0].value in [%v]):
builtins.__xonsh__.completers = builtins.__xonsh__.completers.copy()
exec(compile(subprocess.run(['carapace', '--spec', '%v/'+context.command.args[0].value+'.yaml', 'xonsh'], stdout=subprocess.PIPE).stdout.decode('utf-8'), "", "exec"))
return builtins.__xonsh__.completers[context.command.args[0].value](context)
`, strings.Join(quotedSpecs, ", "), dir)

}
snippet += `_add_one_completer('carapace_lazy', _carapace_lazy, 'start')`
return fmt.Sprintf(snippet, strings.Join(complete, ", "))
}

Expand All @@ -141,5 +284,15 @@ func zsh_lazy(completers []string) string {
}
compdef _carapace_lazy %v
`

if specs, dir := getSpecs(); len(specs) > 0 {
snippet += fmt.Sprintf(`function _carapace_lazy_spec {
source <(carapace --spec %v/$words[1].yaml zsh)
}
compdef _carapace_lazy_spec %v
`, dir, strings.Join(specs, " "))

}

return fmt.Sprintf(snippet, strings.Join(completers, " "))
}
Loading

0 comments on commit 7806533

Please sign in to comment.