Skip to content

Commit 6f142ba

Browse files
committed
shell: add zsh command completion support
1 parent 1364b66 commit 6f142ba

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/.direnv
1+
/.direnv*
22
/.envrc.local
33
result*
44
/.run/

flakeModules/shell.nix

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
# perSystem.cardano-parts.shell.<global|<id>>.defaultHooks
1212
# perSystem.cardano-parts.shell.<global|<id>>.defaultLintPkg
1313
# perSystem.cardano-parts.shell.<global|<id>>.defaultVars
14+
# perSystem.cardano-parts.shell.<global|<id>>.defaultZshCompFpathLoading
1415
# perSystem.cardano-parts.shell.<global|<id>>.enableFormatter
1516
# perSystem.cardano-parts.shell.<global|<id>>.enableHooks
1617
# perSystem.cardano-parts.shell.<global|<id>>.enableLint
1718
# perSystem.cardano-parts.shell.<global|<id>>.enableVars
19+
# perSystem.cardano-parts.shell.<global|<id>>.enableZshCompFpathLoading
1820
# perSystem.cardano-parts.shell.<global|<id>>.extraPkgs
1921
# perSystem.cardano-parts.shell.<global|<id>>.pkgs
2022
#
@@ -152,6 +154,41 @@ in
152154
};
153155
};
154156

157+
defaultZshCompFpathLoading = mkOption {
158+
description = mdDoc "The cardano-parts default zsh completion fpath loading hook.";
159+
default = globalDefault isGlobal (packages: ''
160+
export ZDOTDIR=$PWD/.direnv-zsh
161+
mkdir -p "$ZDOTDIR"
162+
rm -f "$ZDOTDIR/.zcompdump"*
163+
164+
cat > "$ZDOTDIR/completions.zsh" <<'EOF'
165+
for p in ${concatStringsSep " " (map (p: "${p}") packages)}; do
166+
if [ -d "$p/share/zsh/site-functions" ]; then
167+
fpath=("$p/share/zsh/site-functions" $fpath)
168+
fi
169+
done
170+
autoload -U compinit
171+
compinit
172+
EOF
173+
174+
cat > "$ZDOTDIR/.zshrc" <<'EOF'
175+
if [ -f "$HOME/.zshrc" ]; then
176+
source "$HOME/.zshrc"
177+
fi
178+
179+
source "$ZDOTDIR/completions.zsh"
180+
autoload -U compinit
181+
compinit -C
182+
183+
alias zsh-base='unset ZDOTDIR; exec zsh -l'
184+
echo
185+
echo "To return to your normal zsh without devShell fpath modifications, run \"zsh-base\" before leaving the repo directory,"
186+
echo "otherwise, close and open a new zsh shell to avoid lingering devShell command completions."
187+
echo
188+
EOF
189+
'');
190+
};
191+
155192
enableFormatter = mkOption {
156193
type = globalType isGlobal bool;
157194
description = mdDoc "Enable default cardano-parts formatter in the devShells.";
@@ -176,6 +213,12 @@ in
176213
default = globalDefault isGlobal true;
177214
};
178215

216+
enableZshCompFpathLoading = mkOption {
217+
type = globalType isGlobal bool;
218+
description = mdDoc "Enable default cardano-parts zsh completion loading into fpath in zsh devShells.";
219+
default = globalDefault isGlobal true;
220+
};
221+
179222
extraPkgs = mkOption {
180223
type = listOf package;
181224
description = mdDoc "Extra packages.";
@@ -427,12 +470,17 @@ in
427470

428471
devShells = let
429472
mkShell = id:
430-
pkgs.mkShell ({
473+
pkgs.mkShell (rec {
431474
packages = allPkgs id;
432475
shellHook =
433476
# Add optional git/shell and formatter hooks
434477
selectScope id optionalString "enableHooks" "defaultHooks"
435478
+ selectScope id optionalAttrs "enableFormatter" "defaultFormatterHook"
479+
+ (selectScope id (cond: as:
480+
if cond
481+
then as
482+
else _: "") "enableZshCompFpathLoading" "defaultZshCompFpathLoading")
483+
packages
436484
+ ''
437485
[ -z "$NOMENU" ] && menu
438486
'';

0 commit comments

Comments
 (0)