Skip to content

Commit 2df7690

Browse files
committed
refactor(shell): Use CLI_BINARY_NAME_UNDERSCORE for function names
- Add fig_util as build-dependency to access CLI_BINARY_NAME constant - Introduce CLI_BINARY_NAME_UNDERSCORE placeholder for function names (handles hyphens) - Replace _{{CLI_BINARY_NAME}}_ with _{{CLI_BINARY_NAME_UNDERSCORE}}_ in all function names - Keep {{CLI_BINARY_NAME}} for actual binary command usage - Update build.rs to convert hyphens to underscores for function names - Update test to use underscore version for function name checks
1 parent b40f486 commit 2df7690

File tree

14 files changed

+89
-82
lines changed

14 files changed

+89
-82
lines changed

crates/fig_integrations/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@ core-foundation = "0.10.0"
4747
plist = "1.7.1"
4848
objc.workspace = true
4949

50+
[build-dependencies]
51+
fig_util.workspace = true
52+
5053
[dev-dependencies]
5154
tempfile.workspace = true

crates/fig_integrations/build.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
const CODEX_FOLDER: &str = "src/shell/inline_shell_completion";
1+
use fig_util::CLI_BINARY_NAME;
22

3-
// Binary name constants for parameterization
4-
const CLI_BINARY_NAME: &str = "q";
3+
const CODEX_FOLDER: &str = "src/shell/inline_shell_completion";
54

65
// The order here is very specific, do no edit without understanding the implications
76
const CODEX_FILES: &[&str] = &[
@@ -36,9 +35,11 @@ fn main() {
3635
}
3736

3837
// Substitute placeholders with actual binary names
39-
let cli_binary_name_upper = CLI_BINARY_NAME.to_uppercase();
38+
let cli_binary_name_underscore = CLI_BINARY_NAME.replace('-', "_");
39+
let cli_binary_name_upper = cli_binary_name_underscore.to_uppercase();
4040
inline_shell_completion = inline_shell_completion
4141
.replace("{{CLI_BINARY_NAME}}", CLI_BINARY_NAME)
42+
.replace("{{CLI_BINARY_NAME_UNDERSCORE}}", &cli_binary_name_underscore)
4243
.replace("{{CLI_BINARY_NAME_UPPER}}", &cli_binary_name_upper);
4344

4445
std::fs::write(out_dir.join("inline_shell_completion.zsh"), inline_shell_completion).unwrap();

crates/fig_integrations/src/shell/inline_shell_completion/async.zsh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Async #
44
#--------------------------------------------------------------------#
55

6-
_{{CLI_BINARY_NAME}}_autosuggest_async_request() {
6+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_async_request() {
77
zmodload zsh/system 2>/dev/null # For `$sysparams`
88

99
typeset -g _{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_ASYNC_FD _{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_CHILD_PID
@@ -38,7 +38,7 @@ _{{CLI_BINARY_NAME}}_autosuggest_async_request() {
3838
3939
# Fetch and print the suggestion
4040
local suggestion
41-
_{{CLI_BINARY_NAME}}_autosuggest_fetch_suggestion "$1"
41+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_fetch_suggestion "$1"
4242
echo -nE "$suggestion"
4343
)
4444

@@ -51,13 +51,13 @@ _{{CLI_BINARY_NAME}}_autosuggest_async_request() {
5151
read _{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_CHILD_PID <&$_{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_ASYNC_FD
5252

5353
# When the fd is readable, call the response handler
54-
zle -F "$_{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_ASYNC_FD" _{{CLI_BINARY_NAME}}_autosuggest_async_response
54+
zle -F "$_{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_ASYNC_FD" _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_async_response
5555
}
5656

5757
# Called when new data is ready to be read from the pipe
5858
# First arg will be fd ready for reading
5959
# Second arg will be passed in case of error
60-
_{{CLI_BINARY_NAME}}_autosuggest_async_response() {
60+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_async_response() {
6161
emulate -L zsh
6262

6363
local suggestion

crates/fig_integrations/src/shell/inline_shell_completion/bind.zsh

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
# Widget Helpers #
44
#--------------------------------------------------------------------#
55

6-
_{{CLI_BINARY_NAME}}_autosuggest_incr_bind_count() {
6+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_incr_bind_count() {
77
typeset -gi bind_count=$((_{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_BIND_COUNTS[$1]+1))
88
_{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_BIND_COUNTS[$1]=$bind_count
99
}
1010

1111
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
12-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widget() {
12+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widget() {
1313
typeset -gA _{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_BIND_COUNTS
1414

1515
local widget=$1
@@ -21,26 +21,26 @@ _{{CLI_BINARY_NAME}}_autosuggest_bind_widget() {
2121
# Save a reference to the original widget
2222
case $widgets[$widget] in
2323
# Already bound
24-
user:_{{CLI_BINARY_NAME}}_autosuggest_(bound|orig)_*)
24+
user:_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_(bound|orig)_*)
2525
bind_count=$((_{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_BIND_COUNTS[$widget]))
2626
;;
2727

2828
# User-defined widget
2929
user:*)
30-
_{{CLI_BINARY_NAME}}_autosuggest_incr_bind_count $widget
30+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_incr_bind_count $widget
3131
zle -N $prefix$bind_count-$widget ${widgets[$widget]#*:}
3232
;;
3333

3434
# Built-in widget
3535
builtin)
36-
_{{CLI_BINARY_NAME}}_autosuggest_incr_bind_count $widget
37-
eval "_{{CLI_BINARY_NAME}}_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
38-
zle -N $prefix$bind_count-$widget _{{CLI_BINARY_NAME}}_autosuggest_orig_$widget
36+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_incr_bind_count $widget
37+
eval "_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
38+
zle -N $prefix$bind_count-$widget _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_orig_$widget
3939
;;
4040

4141
# Completion widget
4242
completion:*)
43-
_{{CLI_BINARY_NAME}}_autosuggest_incr_bind_count $widget
43+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_incr_bind_count $widget
4444
eval "zle -C $prefix$bind_count-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
4545
;;
4646
esac
@@ -51,16 +51,16 @@ _{{CLI_BINARY_NAME}}_autosuggest_bind_widget() {
5151
# correctly. $WIDGET cannot be trusted because other plugins call
5252
# zle without the `-w` flag (e.g. `zle self-insert` instead of
5353
# `zle self-insert -w`).
54-
eval "_{{CLI_BINARY_NAME}}_autosuggest_bound_${bind_count}_${(q)widget}() {
55-
_{{CLI_BINARY_NAME}}_autosuggest_widget_$autosuggest_action $prefix$bind_count-${(q)widget} \$@
54+
eval "_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bound_${bind_count}_${(q)widget}() {
55+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_widget_$autosuggest_action $prefix$bind_count-${(q)widget} \$@
5656
}"
5757

5858
# Create the bound widget
59-
zle -N -- $widget _{{CLI_BINARY_NAME}}_autosuggest_bound_${bind_count}_$widget
59+
zle -N -- $widget _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bound_${bind_count}_$widget
6060
}
6161

6262
# Map all configured widgets to the right autosuggest widgets
63-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widgets() {
63+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widgets() {
6464
emulate -L zsh
6565

6666
local widget
@@ -77,22 +77,22 @@ _{{CLI_BINARY_NAME}}_autosuggest_bind_widgets() {
7777
# Find every widget we might want to bind and bind it appropriately
7878
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
7979
if [[ -n ${{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]]; then
80-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widget $widget clear
80+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widget $widget clear
8181
elif [[ -n ${{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]]; then
82-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widget $widget accept
82+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widget $widget accept
8383
elif [[ -n ${{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]]; then
84-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widget $widget execute
84+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widget $widget execute
8585
elif [[ -n ${{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]]; then
86-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widget $widget partial_accept
86+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widget $widget partial_accept
8787
else
8888
# Assume any unspecified widget might modify the buffer
89-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widget $widget modify
89+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widget $widget modify
9090
fi
9191
done
9292
}
9393

9494
# Given the name of an original widget and args, invoke it, if it exists
95-
_{{CLI_BINARY_NAME}}_autosuggest_invoke_original_widget() {
95+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_invoke_original_widget() {
9696
# Do nothing unless called with at least one arg
9797
(( $# )) || return 0
9898

crates/fig_integrations/src/shell/inline_shell_completion/fetch.zsh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# from the first strategy to provide one.
77
#
88

9-
_{{CLI_BINARY_NAME}}_autosuggest_fetch_suggestion() {
9+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_fetch_suggestion() {
1010
typeset -g suggestion
1111
local -a strategies
1212
local strategy
@@ -16,7 +16,7 @@ _{{CLI_BINARY_NAME}}_autosuggest_fetch_suggestion() {
1616

1717
for strategy in $strategies; do
1818
# Try to get a suggestion from this strategy
19-
_{{CLI_BINARY_NAME}}_autosuggest_strategy_$strategy "$1"
19+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_strategy_$strategy "$1"
2020

2121
# Ensure the suggestion matches the prefix
2222
[[ "$suggestion" != "$1"* ]] && unset suggestion

crates/fig_integrations/src/shell/inline_shell_completion/highlight.zsh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#--------------------------------------------------------------------#
55

66
# If there was a highlight, remove it
7-
_{{CLI_BINARY_NAME}}_autosuggest_highlight_reset() {
7+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_highlight_reset() {
88
typeset -g _{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_LAST_HIGHLIGHT
99

1010
if [[ -n "$_{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_LAST_HIGHLIGHT" ]]; then
@@ -14,7 +14,7 @@ _{{CLI_BINARY_NAME}}_autosuggest_highlight_reset() {
1414
}
1515

1616
# If there's a suggestion, highlight it
17-
_{{CLI_BINARY_NAME}}_autosuggest_highlight_apply() {
17+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_highlight_apply() {
1818
typeset -g _{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_LAST_HIGHLIGHT
1919

2020
if (( $#POSTDISPLAY )); then

crates/fig_integrations/src/shell/inline_shell_completion/start.zsh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#--------------------------------------------------------------------#
55

66
# Start the autosuggestion widgets
7-
_{{CLI_BINARY_NAME}}_autosuggest_start() {
7+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_start() {
88
# By default we re-bind widgets on every precmd to ensure we wrap other
99
# wrappers. Specifically, highlighting breaks if our widgets are wrapped by
1010
# zsh-syntax-highlighting widgets. This also allows modifications to the
1111
# widget list variables to take effect on the next precmd. However this has
1212
# a decent performance hit, so users can set {{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_MANUAL_REBIND
1313
# to disable the automatic re-binding.
1414
if (( ${+{{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_MANUAL_REBIND} )); then
15-
add-zsh-hook -d precmd _{{CLI_BINARY_NAME}}_autosuggest_start
15+
add-zsh-hook -d precmd _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_start
1616
fi
1717

18-
_{{CLI_BINARY_NAME}}_autosuggest_bind_widgets
18+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_bind_widgets
1919
}
2020

2121
# Mark for auto-loading the functions that we use
@@ -30,4 +30,4 @@ if is-at-least 5.0.8; then
3030
fi
3131

3232
# Start the autosuggestion widgets on the next precmd
33-
add-zsh-hook precmd _{{CLI_BINARY_NAME}}_autosuggest_start
33+
add-zsh-hook precmd _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_start

crates/fig_integrations/src/shell/inline_shell_completion/strategies/completion.zsh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
# Fetches a suggestion from the completion engine
66
#
77

8-
_{{CLI_BINARY_NAME}}_autosuggest_capture_postcompletion() {
8+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_postcompletion() {
99
# Always insert the first completion into the buffer
1010
compstate[insert]=1
1111

1212
# Don't list completions
1313
unset 'compstate[list]'
1414
}
1515

16-
_{{CLI_BINARY_NAME}}_autosuggest_capture_completion_widget() {
16+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_completion_widget() {
1717
# Add a post-completion hook to be called after all completions have been
1818
# gathered. The hook can modify compstate to affect what is done with the
1919
# gathered completions.
2020
local -a +h comppostfuncs
21-
comppostfuncs=(_{{CLI_BINARY_NAME}}_autosuggest_capture_postcompletion)
21+
comppostfuncs=(_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_postcompletion)
2222

2323
# Only capture completions at the end of the buffer
2424
CURSOR=$#BUFFER
@@ -42,9 +42,9 @@ _{{CLI_BINARY_NAME}}_autosuggest_capture_completion_widget() {
4242
echo -nE - $'\0'$BUFFER$'\0'
4343
}
4444

45-
zle -N autosuggest-capture-completion _{{CLI_BINARY_NAME}}_autosuggest_capture_completion_widget
45+
zle -N autosuggest-capture-completion _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_completion_widget
4646

47-
_{{CLI_BINARY_NAME}}_autosuggest_capture_setup() {
47+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_setup() {
4848
# There is a bug in zpty module in older zsh versions by which a
4949
# zpty that exits will kill all zpty processes that were forked
5050
# before it. Here we set up a zsh exit hook to SIGKILL the zpty
@@ -69,14 +69,14 @@ _{{CLI_BINARY_NAME}}_autosuggest_capture_setup() {
6969
bindkey '^I' autosuggest-capture-completion
7070
}
7171

72-
_{{CLI_BINARY_NAME}}_autosuggest_capture_completion_sync() {
73-
_{{CLI_BINARY_NAME}}_autosuggest_capture_setup
72+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_completion_sync() {
73+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_setup
7474

7575
zle autosuggest-capture-completion
7676
}
7777

78-
_{{CLI_BINARY_NAME}}_autosuggest_capture_completion_async() {
79-
_{{CLI_BINARY_NAME}}_autosuggest_capture_setup
78+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_completion_async() {
79+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_setup
8080

8181
zmodload zsh/parameter 2>/dev/null || return # For `$functions`
8282

@@ -93,7 +93,7 @@ _{{CLI_BINARY_NAME}}_autosuggest_capture_completion_async() {
9393
vared 1
9494
}
9595

96-
_{{CLI_BINARY_NAME}}_autosuggest_strategy_completion() {
96+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_strategy_completion() {
9797
# Reset options to defaults and enable LOCAL_OPTIONS
9898
emulate -L zsh
9999

@@ -114,9 +114,9 @@ _{{CLI_BINARY_NAME}}_autosuggest_strategy_completion() {
114114

115115
# Zle will be inactive if we are in async mode
116116
if zle; then
117-
zpty ${{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_COMPLETIONS_PTY_NAME _{{CLI_BINARY_NAME}}_autosuggest_capture_completion_sync
117+
zpty ${{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_COMPLETIONS_PTY_NAME _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_completion_sync
118118
else
119-
zpty ${{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_COMPLETIONS_PTY_NAME _{{CLI_BINARY_NAME}}_autosuggest_capture_completion_async "\$1"
119+
zpty ${{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_COMPLETIONS_PTY_NAME _{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_capture_completion_async "\$1"
120120
zpty -w ${{CLI_BINARY_NAME_UPPER}}_AUTOSUGGEST_COMPLETIONS_PTY_NAME $'\t'
121121
fi
122122

crates/fig_integrations/src/shell/inline_shell_completion/strategies/history.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# prefix.
77
#
88

9-
_{{CLI_BINARY_NAME}}_autosuggest_strategy_history() {
9+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_strategy_history() {
1010
# Reset options to defaults and enable LOCAL_OPTIONS
1111
emulate -L zsh
1212

crates/fig_integrations/src/shell/inline_shell_completion/strategies/inline.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
# Suggests the inline_shell_completion command.
66
#
77

8-
_{{CLI_BINARY_NAME}}_autosuggest_strategy_inline_shell_completion() {
8+
_{{CLI_BINARY_NAME_UNDERSCORE}}_autosuggest_strategy_inline_shell_completion() {
99
typeset -g suggestion="$(command -v {{CLI_BINARY_NAME}} >/dev/null 2>&1 && {{CLI_BINARY_NAME}} _ inline-shell-completion --buffer "${BUFFER}")"
1010
}

0 commit comments

Comments
 (0)