diff options
| author | ruki <[email protected]> | 2023-03-03 00:59:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-03-03 00:59:55 +0800 |
| commit | 2025f59c975fa90d2bb75f7864ab93bd3831d825 (patch) | |
| tree | af1b2cb99b1828af39a09e1ae947ed6283999416 /xmake/scripts | |
| parent | 9662ca20a44e7a7639c9d8d42049f77be72253d7 (diff) | |
add fish complete
Diffstat (limited to 'xmake/scripts')
| -rwxr-xr-x | xmake/scripts/completions/register-completions.fish | 49 | ||||
| -rwxr-xr-x | xmake/scripts/completions/register-completions.zsh | 4 | ||||
| -rwxr-xr-x | xmake/scripts/register-completions.sh | 2 |
3 files changed, 53 insertions, 2 deletions
diff --git a/xmake/scripts/completions/register-completions.fish b/xmake/scripts/completions/register-completions.fish new file mode 100755 index 000000000..a06a64443 --- /dev/null +++ b/xmake/scripts/completions/register-completions.fish @@ -0,0 +1,49 @@ +# fish parameter completion for xmake +function _xmake_fish_complete + # Read the current command line + set -l raw_cmd (commandline) + set -l words (commandline -o) + # Get the current token + set -l token (commandline -ot) + + # Call XMake built-in completion to get available results + set -l result (MAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete 0 nospace "$raw_cmd") + if test (count $result) -lt 1 + # When there are no available completions, the function ends immediately + # Otherwise, the subsequent `contains` command will trigger an error + return 1 + end + + # Are there multiple selectable results? + # When there are multiple options, the user's current token is definitely incomplete + test (count $result) -gt 1 + set -l is_multiple_choice $status + # Is the only selectable option identical to the current token under the cursor (which may be empty)? + # Identical means the user has already completed the process + # Repeated option outputs are not completions at the current cursor position + contains -- $token $result + set -l is_token_incomplete $status + # Is the only selectable option identical to the last visible token of the command line array? + # When completing without a prefix in every token position, the current token is an empty string + # The last item of the command line tokenized array is the token before the cursor + # This is mainly used to handle completions triggered after a space without a prefix + contains -- $words[-1] $result + set -l not_token_completed $status + + test \( $is_token_incomplete -eq 1 \) -a \( $not_token_completed -eq 1 \) + test \( $is_multiple_choice -eq 0 \) -o \( $status -eq 0 \) + if test $status -eq 0 + # When the completion list has more than one item, or the current token is different from the unique token in the completion list + # The user must not have completed the token and needs to be provided with completion options + for item in $result + # Each result takes up a separate line, and for the "-a" parameter, it needs to be echoed separately + # Although the result itself has a newline, "-a" still treats it as a whole, + # Without tokenizing it as multiple parameters + if not string match -- '*error*' "$item" > /dev/null + echo $item + end + end + end +end + +complete -c xmake -f -a "(_xmake_fish_complete)" diff --git a/xmake/scripts/completions/register-completions.zsh b/xmake/scripts/completions/register-completions.zsh index 4a0bb9985..86f42498f 100755 --- a/xmake/scripts/completions/register-completions.zsh +++ b/xmake/scripts/completions/register-completions.zsh @@ -1,5 +1,5 @@ # zsh parameter completion for xmake -_xmake_zsh_complete() +_xmake_zsh_complete() { local completions=("$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete 0 nospace "$words")") reply=( "${(ps:\n:)completions}" ) @@ -7,7 +7,7 @@ _xmake_zsh_complete() compctl -f -S "" -K _xmake_zsh_complete xmake # zsh parameter completion for xrepo -_xrepo_zsh_complete() +_xrepo_zsh_complete() { local completions=("$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.xrepo.complete 0 nospace "$words")") reply=( "${(ps:\n:)completions}" ) diff --git a/xmake/scripts/register-completions.sh b/xmake/scripts/register-completions.sh index 48335bd89..0f60869be 100755 --- a/xmake/scripts/register-completions.sh +++ b/xmake/scripts/register-completions.sh @@ -2,5 +2,7 @@ if [[ "$SHELL" = */zsh ]]; then . "$XMAKE_PROGRAM_DIR/scripts/completions/register-completions.zsh" elif [[ "$SHELL" = */bash ]]; then . "$XMAKE_PROGRAM_DIR/scripts/completions/register-completions.bash" +elif [[ "$SHELL" = */fish ]]; then + . "$XMAKE_PROGRAM_DIR/scripts/completions/register-completions.fish" fi |
