Skip to content

Commit

Permalink
IH-676: Do not suggest already entered parameters
Browse files Browse the repository at this point in the history
Check if the parameter has already been provided and if so, do not include it
in the list of autocompletions anymore.

This should be safe - xe has other ways of entering parameters multiple times:
with commas, or as parts of records. To verify, cli_operations.ml receives the
command line arguments as an associative list and only contains List.assoc and
the like, no List.find_all (apart from the special case in
5d1601c which detects if a parameter was
entered multiple times and errors out, warning the user - which only underlines
that entering a parameter multiple times is not the desired behaviour, can
confuse things and lead to errors)

Signed-off-by: Andrii Sultanov <[email protected]>
  • Loading branch information
last-genius authored and lindig committed Aug 23, 2024
1 parent 37e77d3 commit 6334cbd
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ocaml/xe-cli/bash-completion
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,16 @@ _xe()

IFS=$'\n,'
get_params_for_command "${OLDSTYLE_WORDS[1]}"
set_completions "$SUBCOMMAND_PARAMS" "$param"

# Don't suggest already provided parameters
local params_len=$(( $COMP_CWORD - 2 ))
params_len=$([[ "$params_len" -lt 0 ]] && echo 0 || echo "$params_len")
local previous_params="${OLDSTYLE_WORDS[@]:2:$params_len}"
previous_params=$( echo "$previous_params" | cut -d= -f1 | \
sed -r '/^\s*$/d' | cut -d: -f1 | \
sed -re 's/^/-e "\\s*/g' -e 's/$/[=:]"/g' | paste -sd " ")

set_completions "$SUBCOMMAND_PARAMS" "$param" "" "$previous_params"

return 0
fi
Expand Down Expand Up @@ -706,6 +715,7 @@ set_completions()
{
local prefix="$2"
local description_cmd="$3"
local excludes="$4"
# Replace each sequence of non-escaped commas with a newline, then de-escape commas and backslashes.
# Only suggest words that start with the currently typed out prefix
# TODO: Do not generate space suffixes, which have to be removed here.
Expand All @@ -714,6 +724,11 @@ set_completions()
sed -e 's/ *$//' | \
grep "^${prefix}.*" )

if [[ "$excludes" ]]; then
__xe_debug "Excluding previously entered parameters: '$excludes'"
words=$(echo "$words" | eval "grep -v $excludes")
fi

__xe_debug "set_completions()"
if [[ $SHOW_DESCRIPTION == 1 ]]; then
local max_cmd_length=$( echo "$words" | wc -L )
Expand Down

0 comments on commit 6334cbd

Please sign in to comment.