From e01941b41c8cc98448f8fad9a08ca131d6ad9f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Wed, 10 May 2023 23:01:56 +0200 Subject: [PATCH] Meta/ShellCompletions: Complete binaries for `run lagom` This completion only works if you have lagom already built in some capacity, since it scans the build directory tree for binaries, removing known false positives. However, that is both more accurate than asking ninja for the targets and filtering those, and it also makes it independent of the build system used. --- Meta/ShellCompletions/zsh/_serenity | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/Meta/ShellCompletions/zsh/_serenity b/Meta/ShellCompletions/zsh/_serenity index ea34d84238..eaf39866a0 100644 --- a/Meta/ShellCompletions/zsh/_serenity +++ b/Meta/ShellCompletions/zsh/_serenity @@ -1,11 +1,25 @@ #compdef serenity serenity.sh +get_top_dir() { + git rev-parse --show-toplevel +} + +get_lagom_executables() { + # Make completion work with user-defined source directory. + SERENITY_SOURCE_DIR="${SERENITY_SOURCE_DIR:-$(get_top_dir)}" + # If the Lagom binary directory is missing, this creates an empty list instead of erroring. + # Known false positives need to be filtered manually; please add new ones. + find "${SERENITY_SOURCE_DIR}/Build/lagom" -mindepth 1 -type f -executable -not -name '*.so*' \ + -not \( -name 'SQLServer' -o -name 'a.out' -o -name 'CMake*.bin' \) \ + -printf '%f\n' 2>/dev/null || true +} + _serenity() { local args args=( '1:command:->commands' '2:target:->targets' - '3:toolchain:->toolchains' + '3:toolchain_or_executable:->toolchains_or_executables' '*:: :->args' ) @@ -41,6 +55,12 @@ _serenity() { 'Clang:Toolchain clang' ) + local lagom_executables + # Prevent splitting array on spaces with IFS; bash would have `mapfile` for this. + IFS=$'\n'; set -f + lagom_executables=($(get_lagom_executables)) + unset IFS; set +f + _arguments -C -S "$args[@]" local command @@ -69,10 +89,13 @@ _serenity() { esac _describe 'target' targets ;; - toolchains) + toolchains_or_executables) if [[ "$command" != help && "$target" != lagom ]]; then # Toolchain-dependent invocations. - _describe 'toolchain' toolchains + _describe 'toolchain_or_executable' toolchains + elif [[ "$command" = run && "$target" = lagom ]]; then + # With `run lagom` this already is the executable. + _describe 'toolchain_or_executable' lagom_executables fi ;; args)