diff --git a/cmd/carapace/cmd/lazyinit/xonsh.go b/cmd/carapace/cmd/lazyinit/xonsh.go index 60e19fcdc5..f0f5cb326f 100644 --- a/cmd/carapace/cmd/lazyinit/xonsh.go +++ b/cmd/carapace/cmd/lazyinit/xonsh.go @@ -5,26 +5,49 @@ import ( "strings" ) +// Xonsh creates a generic completer (not lazy). +// Name overlaps with specific `carapace` completer but that shouldn't be an issue. func Xonsh(completers []string) string { - snippet := `from xonsh.completers._aliases import _add_one_completer -from xonsh.completers.tools import * + snippet := `from xonsh.completers.completer import add_one_completer +from xonsh.completers.tools import contextual_command_completer import os %v%v -@contextual_completer -def _carapace_lazy(context): - """carapace lazy""" - if (context.command and - context.command.arg_index > 0 and - context.command.args[0].value in [%v]): - exec(compile(subprocess.run(['carapace', context.command.args[0].value, 'xonsh'], stdout=subprocess.PIPE).stdout.decode('utf-8'), "", "exec")) - return XSH.completers[context.command.args[0].value](context) +@contextual_command_completer +def _carapace_completer(context): + """carapace completer""" + from json import loads + from xonsh.completers.tools import sub_proc_get_output, RichCompletion + + if context.command not in [%v]: + return + + def fix_prefix(s): + """quick fix for partially quoted prefix completion ('prefix',)""" + return s.translate(str.maketrans('', '', '\'"')) + + output, _ = sub_proc_get_output( + 'carapace', context.command, 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix) + ) + if not output: + return + + for c in loads(output): + yield RichCompletion( + c["Value"], + display=c["Display"], + description=c["Description"], + prefix_len=len(context.raw_prefix), + append_closing_quote=False, + style=c["Style"], + ) + +add_one_completer('carapace', _carapace_completer, 'start') ` complete := make([]string, len(completers)) for index, completer := range completers { complete[index] = fmt.Sprintf(`'%v'`, completer) } - snippet += `_add_one_completer('carapace_lazy', _carapace_lazy, 'start')` return fmt.Sprintf(snippet, pathSnippet("xonsh"), envSnippet("xonsh"), strings.Join(complete, ", ")) } diff --git a/go.mod b/go.mod index 15e9ac0477..aacc4bf4d5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/carapace-sh/carapace-bin go 1.21 require ( - github.com/carapace-sh/carapace v1.0.0 + github.com/carapace-sh/carapace v1.1.1 github.com/carapace-sh/carapace-bridge v1.0.2 github.com/carapace-sh/carapace-shlex v1.0.1 github.com/carapace-sh/carapace-spec v1.0.0 diff --git a/go.sum b/go.sum index d6d3791eb2..82553c8a61 100644 --- a/go.sum +++ b/go.sum @@ -2,18 +2,16 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= -github.com/carapace-sh/carapace v1.0.0 h1:O4XTxBjrmDG15PeSnBRvu5RdwA5Khd1k/yF6VD4sHHY= -github.com/carapace-sh/carapace v1.0.0/go.mod h1:4caYCCdFoKqiBFD8t8rxuxzgVXk9bnP/aSBOplTd1Gk= +github.com/carapace-sh/carapace v1.1.1 h1:OuWouCknq9Wqv0rM+7z8/lf3FfEUBdMd4MVXisP6j4w= +github.com/carapace-sh/carapace v1.1.1/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs= github.com/carapace-sh/carapace-bridge v1.0.2 h1:q2yVrhpxjxA0p3ZcGHpjns99KE9lCrJLc3Zgaa7kMK4= github.com/carapace-sh/carapace-bridge v1.0.2/go.mod h1:1tuz7tWpJeGMHa6Yvwlkb9QCNqxRD5VsS/4iZdK9ofQ= github.com/carapace-sh/carapace-pflag v1.0.0 h1:uJMhl+vwEM/Eb0UdxZUuv4jo4rUAyPijkRGP5gfCuCE= github.com/carapace-sh/carapace-pflag v1.0.0/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/carapace-sh/carapace-shlex v1.0.0/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M= github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c= github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M= github.com/carapace-sh/carapace-spec v1.0.0 h1:gq2aV/eLXmQLBLT8hqBGf8lOyyQdx54PNjE1o8ADlEw= github.com/carapace-sh/carapace-spec v1.0.0/go.mod h1:/HdoGk/59vcD5kCTPmtxlc2QQagyZc4hOHP7Ie0MIoI= -github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -29,7 +27,6 @@ github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=