-
-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add builder tab-completion script #12296
Open
jahorton
wants to merge
5
commits into
master
Choose a base branch
from
feat/builder-autocomplete
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f07eba1
feat: add builder autocompletion script
jahorton 1b166f5
chore: builder_completion echo cleanup
jahorton b589aed
chore: builder.inc.sh cleanup in new method
jahorton e6fb213
fix: revert unintended change outside of new func
jahorton 02945c1
Merge branch 'master' into feat/builder-autocomplete
jahorton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Note: must 'source' this file again in the terminal after any edits! | ||
_comp_builder() { | ||
# Does the file actually exist? If not, abort. | ||
local CMD_PATH=`readlink -f "$1"` | ||
if [ -z "${CMD_PATH}" ]; then | ||
exit 0 | ||
fi | ||
|
||
# Is it actually a builder-script? If not, abort. If so, it defines | ||
# a special option used to provide us with completion-target data. | ||
local builder_params=`${CMD_PATH} --builder_completion_describe` || exit 0 | ||
|
||
local BASE_NAME=$(basename "$CMD_PATH") | ||
local BUILDER_ARG_STR="${COMP_LINE##*${BASE_NAME} }" | ||
|
||
local builder_args | ||
# Does not actually preserve an empty token at the end. | ||
read -r -a builder_args <<< "${BUILDER_ARG_STR}" | ||
|
||
# Determine the current token (given the caret position) and | ||
# all existing, already-completed actions, targets, and options | ||
# for the script before the current token. | ||
local current_token= | ||
# If the caret is adjacent to non-whitespace - thus is editing | ||
# a builder argument... | ||
if [ ! -z "${COMP_WORDS[COMP_CWORD]}" ]; then | ||
# Then note the last token as the token being edited... | ||
current_token="${builder_args[-1]}" | ||
# ... and thus not pre-completed. | ||
unset builder_args[-1] | ||
fi | ||
|
||
# Parse the builder-description for completion target data. | ||
local action_str target_str option_str | ||
IFS=";" read -r action_str target_str option_str <<< "$builder_params" | ||
local actions targets options | ||
IFS=" " read -r -a actions <<< "$action_str" | ||
IFS=" " read -r -a targets <<< "$target_str" | ||
IFS=" " read -r -a options <<< "$option_str" | ||
|
||
local action target | ||
if [[ $current_token =~ : ]]; then | ||
IFS=: read -r action target <<< "$current_token" | ||
target=:$target | ||
else | ||
action="$current_token" | ||
target= | ||
fi | ||
|
||
local all=() | ||
# If there is no $action component, it's a standalone target. | ||
|
||
if [[ -z "$action" ]] && [[ -z "$target" ]]; then | ||
all+="${actions[@]}" | ||
all+=" ${targets[@]}" | ||
all+=" ${options[@]}" | ||
|
||
COMPREPLY=( $(compgen -W "${all[@]}" -- "${current_token}") ) | ||
elif [[ -z "$action" ]]; then | ||
# It's an unpaired target. | ||
all="${targets[@]}" | ||
COMPREPLY=( $(compgen -W "${all[@]}" -- "${current_token}") ) | ||
|
||
# bash doesn't handle completion with colons well; we should remove the | ||
# colon prefix from each entry. | ||
local i | ||
for (( i=0; i<${#COMPREPLY[@]}; i++ )); do | ||
COMPREPLY[$i]="${COMPREPLY[$i]##*:}" | ||
done | ||
elif [[ -z "$target" ]]; then | ||
# It's an untargeted action or an option. | ||
all+="${actions[@]}" | ||
all+=" ${options[@]}" | ||
|
||
COMPREPLY=( $(compgen -W "${all[@]}" -- "${current_token}") ) | ||
else | ||
# Ah, an action-target pair. We have an existing action, so we only need to | ||
# complete the target part. | ||
|
||
# First, build up the list of legal paired tokens. | ||
local actiontargets=() | ||
for e in "${targets[@]}"; do | ||
actiontargets+=("${action}${e}") | ||
done | ||
|
||
# Now, complete from that. | ||
all+="${actiontargets[@]}" | ||
COMPREPLY=( $(compgen -W "${all[@]}" -- "${current_token}") ) | ||
|
||
# bash doesn't handle completion with colons well; we should remove the | ||
# colon prefix from each entry. | ||
local i | ||
for (( i=0; i<${#COMPREPLY[@]}; i++ )); do | ||
COMPREPLY[$i]="${COMPREPLY[$i]##*:}" | ||
done | ||
fi | ||
} | ||
|
||
# When this script is sourced by .bashrc or similar, this provides autocompletion for | ||
# builder scripts named build.sh and test.sh. | ||
complete -o bashdefault -F _comp_builder build.sh | ||
complete -o bashdefault -F _comp_builder test.sh |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removes all
+
suffixes from options.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a code comment