diff --git a/cmd/carapace/cmd/lazyinit/cmd.go b/cmd/carapace/cmd/lazyinit/cmd.go new file mode 100644 index 0000000000..a7226ae57f --- /dev/null +++ b/cmd/carapace/cmd/lazyinit/cmd.go @@ -0,0 +1,33 @@ +package lazyinit + +import ( + "fmt" + "strings" +) + +func CmdClink(completers []string) string { + // TODO pathSnippet + snippet := `local function carapace_completion(command) + return function(word, word_index, line_state, match_builder) + local compline = string.sub(line_state:getline(), 1, line_state:getcursor()) + local prog = string.format("env CARAPACE_COMPLINE=%%s carapace %%s cmd-clink '' ''", string.format("%%q", compline), command) + + local output = io.popen(prog):read("*a") + for line in string.gmatch(output, '[^\r\n]+') do + match_builder:addmatch(string.gsub(line, '\t.*', "")) + end + return true + end +end + +%v +` + argmatchers := make([]string, 0, len(completers)) + for _, completer := range completers { + argmatchers = append(argmatchers, + fmt.Sprintf(`clink.argmatcher("%v"):addarg({carapace_completion("%v")}):loop(1)`, completer, completer), + fmt.Sprintf(`clink.argmatcher("%v.exe"):addarg({carapace_completion("%v")}):loop(1)`, completer, completer), + ) + } + return fmt.Sprintf(snippet, strings.Join(argmatchers, "\n")) +} diff --git a/cmd/carapace/cmd/lazyinit/snippet.go b/cmd/carapace/cmd/lazyinit/snippet.go index 0b4b0686c7..3a440c2f7e 100644 --- a/cmd/carapace/cmd/lazyinit/snippet.go +++ b/cmd/carapace/cmd/lazyinit/snippet.go @@ -71,6 +71,8 @@ func Snippet(shell string) string { return Bash(completerNames) case "bash-ble": return BashBle(completerNames) + case "cmd-clink": + return CmdClink(completerNames) case "elvish": return Elvish(completerNames) case "fish": diff --git a/cmd/carapace/cmd/root.go b/cmd/carapace/cmd/root.go index 22c32b409f..2fa08e02a6 100644 --- a/cmd/carapace/cmd/root.go +++ b/cmd/carapace/cmd/root.go @@ -161,6 +161,7 @@ func Execute(version string) error { switch shell { case "bash", "bash-ble", + "cmd-clink", "elvish", "fish", "nushell", diff --git a/go.mod b/go.mod index dcc34898ea..bf7ff97678 100644 --- a/go.mod +++ b/go.mod @@ -22,3 +22,5 @@ require ( ) replace github.com/spf13/pflag => github.com/carapace-sh/carapace-pflag v1.0.0 + +replace github.com/carapace-sh/carapace => github.com/carapace-sh/carapace v1.5.3-0.20241229192213-225be410a233 diff --git a/go.sum b/go.sum index 0edeaec514..31b339c372 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/carapace-sh/carapace v1.5.2 h1:ddFLVzPLmEUnKTtglMDDnfbFSYyCMEd0xn1ysguSWLw= -github.com/carapace-sh/carapace v1.5.2/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs= +github.com/carapace-sh/carapace v1.5.3-0.20241229192213-225be410a233 h1:cceMGeKF831Wo8Jnr4fPFjU1PCmFaOBquMZDtF6vCqc= +github.com/carapace-sh/carapace v1.5.3-0.20241229192213-225be410a233/go.mod h1:0EJBqcIPrDiZ7+QMKPd0/LhJz3inAvdTz8JfLnH7SsU= github.com/carapace-sh/carapace-bridge v1.2.2 h1:Zlzw7BajF4iHAFBClCYcptEG+7+F324dFyiGzGiU/kw= github.com/carapace-sh/carapace-bridge v1.2.2/go.mod h1:HpN3iEdCmQtAD1pd/qHEnWmAF719CeNyJrY8FJ5/E4o= github.com/carapace-sh/carapace-pflag v1.0.0 h1:uJMhl+vwEM/Eb0UdxZUuv4jo4rUAyPijkRGP5gfCuCE=