diff --git a/CHANGELOG.md b/CHANGELOG.md index f756fad..c03a91a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ For breaking changes, check [here](#breaking-changes). [Babashka CLI](https://github.com/babashka/cli): turn Clojure functions into CLIs! +## Unreleased + +- zsh completion: disable sorting for registered program names to preserve candidate order + ## 0.12.90 - `dispatch`: add `:supplied` to the opts map's `:org.babashka/cli` metadata with the set of option keys given on the command line diff --git a/src/babashka/cli.cljc b/src/babashka/cli.cljc index 4d7b9dc..0808394 100644 --- a/src/babashka/cli.cljc +++ b/src/babashka/cli.cljc @@ -1754,6 +1754,9 @@ names-zstyle (str/join "\n" (map #(str "zstyle ':completion:*:*:" % ":*:options' prefix-needed false") names)) + names-zsort (str/join "\n" + (map #(str "zstyle ':completion:*:*:" % ":*' sort false") + names)) names-nu (str "[" (str/join " " (map #(str "\"" % "\"") names)) "]")] (case shell :bash (str fn "() @@ -1841,6 +1844,8 @@ complete -F " fn " " names-sp " # zsh hides options until a dash is typed. After a command there is usually # nothing else to complete, so opt out for these programs only " names-zstyle " +# show candidates in the order the program emits them +" names-zsort " # register the bare name(s); zsh's _normal completes ./name and /abs/name via the basename compdef " fn " " names-sp " ") diff --git a/test/babashka/cli/completion_test.cljc b/test/babashka/cli/completion_test.cljc index 2427916..64b5bd9 100644 --- a/test/babashka/cli/completion_test.cljc +++ b/test/babashka/cli/completion_test.cljc @@ -177,6 +177,12 @@ (is (str/includes? zsh "zstyle ':completion:*:*:sq:*:options' prefix-needed false")) (is (str/includes? zsh "zstyle ':completion:*:*:squint:*:options' prefix-needed false"))))) +(deftest zsh-snippet-keeps-order-test + (testing "every registered name turns off zsh sorting" + (let [zsh (snippet-via-cmd cmd-table {:prog "x"} "zsh" "--prog" "sq" "--prog" "squint")] + (is (str/includes? zsh "zstyle ':completion:*:*:sq:*' sort false")) + (is (str/includes? zsh "zstyle ':completion:*:*:squint:*' sort false"))))) + (defn- complete-out "Run the completion handler for `cmdline` and return its emitted `value\\tdescription` lines as a set of strings." diff --git a/test/resources/completion/completion.zsh b/test/resources/completion/completion.zsh index e8c8bb2..2b881f8 100644 --- a/test/resources/completion/completion.zsh +++ b/test/resources/completion/completion.zsh @@ -35,5 +35,7 @@ _babashka_cli_complete_myprogram() { # zsh hides options until a dash is typed. After a command there is usually # nothing else to complete, so opt out for these programs only zstyle ':completion:*:*:myprogram:*:options' prefix-needed false +# show candidates in the order the program emits them +zstyle ':completion:*:*:myprogram:*' sort false # register the bare name(s); zsh's _normal completes ./name and /abs/name via the basename compdef _babashka_cli_complete_myprogram myprogram