Merge pull request #12421 from XuehaiPan/shfmt-if-align
utils/shfmt.sh: improve shell formatter usage
This commit is contained in:
commit
d677926d7a
@ -227,21 +227,21 @@ no_forbidden_styles() {
|
||||
align_multiline_if_condition() {
|
||||
local line
|
||||
local lastline=''
|
||||
local base_indent='' # indents before `if`
|
||||
local extra_indent='' # 2 extra spaces for `elif`
|
||||
local multiline_if_begin_regex='^( *)(el)?if '
|
||||
local multiline_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
||||
local within_test_regex='^( *)(((! )?-[fdLrwxeszn] )|([^\[]+ == ))'
|
||||
local base_indent='' # indents before `if`
|
||||
local elif_extra_indent='' # 2 extra spaces for `elif`
|
||||
local multiline_if_then_begin_regex='^( *)(el)?if '
|
||||
local multiline_if_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
||||
local within_test_regex='^( *)(((! )?-[fdLrwxeszn] )|([^\[]+ (==|!=|=~) ))'
|
||||
|
||||
trim() {
|
||||
[[ "$1" =~ [^[:space:]](.*[^[:space:]])? ]]
|
||||
printf "%s" "${BASH_REMATCH[0]}"
|
||||
}
|
||||
|
||||
if [[ "$1" =~ ${multiline_if_begin_regex} ]]
|
||||
if [[ "$1" =~ ${multiline_if_then_begin_regex} ]]
|
||||
then
|
||||
base_indent="${BASH_REMATCH[1]}"
|
||||
[[ -n "${BASH_REMATCH[2]}" ]] && extra_indent=' '
|
||||
[[ -n "${BASH_REMATCH[2]}" ]] && elif_extra_indent=' ' # 2 extra spaces for `elif`
|
||||
echo "$1"
|
||||
shift
|
||||
fi
|
||||
@ -250,16 +250,21 @@ align_multiline_if_condition() {
|
||||
do
|
||||
line="$1"
|
||||
shift
|
||||
if [[ "${line}" =~ ${multiline_then_end_regex} ]]
|
||||
if [[ "${line}" =~ ${multiline_if_then_end_regex} ]]
|
||||
then
|
||||
line="${BASH_REMATCH[1]}"
|
||||
lastline="${base_indent}${BASH_REMATCH[2]}"
|
||||
fi
|
||||
if [[ "${line}" =~ ${within_test_regex} ]]
|
||||
then
|
||||
echo "${base_indent}${extra_indent} $(trim "${line}")"
|
||||
# Add 3 extra spaces (6 spaces in total) to multiline test conditions
|
||||
# before: after:
|
||||
# if [[ -n ... || if [[ -n ... ||
|
||||
# -n ... ]] -n ... ]]
|
||||
# then then
|
||||
echo "${base_indent}${elif_extra_indent} $(trim "${line}")"
|
||||
else
|
||||
echo "${base_indent}${extra_indent} $(trim "${line}")"
|
||||
echo "${base_indent}${elif_extra_indent} $(trim "${line}")"
|
||||
fi
|
||||
done
|
||||
|
||||
@ -287,24 +292,28 @@ wrap_then_do() {
|
||||
local -a processed=()
|
||||
local -a buffer=()
|
||||
local line
|
||||
local singleline_then_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$'
|
||||
local singleline_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$'
|
||||
local multiline_if_begin_regex='^( *)(el)?if '
|
||||
local multiline_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
||||
local singleline_if_then_fi_regex='^( *)if (.+)\; then (.+)\; fi( *#.*)?$'
|
||||
local singleline_if_then_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$'
|
||||
local singleline_for_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$'
|
||||
local multiline_if_then_begin_regex='^( *)(el)?if '
|
||||
local multiline_if_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
||||
|
||||
while IFS='' read -r line
|
||||
do
|
||||
if [[ "${#buffer[@]}" == 0 ]]
|
||||
then
|
||||
if [[ "${line}" =~ ${singleline_then_regex} ]]
|
||||
if [[ "${line}" =~ ${singleline_if_then_fi_regex} ]]
|
||||
then
|
||||
processed+=("${line}")
|
||||
elif [[ "${line}" =~ ${singleline_if_then_regex} ]]
|
||||
then
|
||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}if ${BASH_REMATCH[3]}")
|
||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}")
|
||||
elif [[ "${line}" =~ ${singleline_do_regex} ]]
|
||||
elif [[ "${line}" =~ ${singleline_for_do_regex} ]]
|
||||
then
|
||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]} ${BASH_REMATCH[3]}")
|
||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}")
|
||||
elif [[ "${line}" =~ ${multiline_if_begin_regex} ]]
|
||||
elif [[ "${line}" =~ ${multiline_if_then_begin_regex} ]]
|
||||
then
|
||||
buffer=("${line}")
|
||||
else
|
||||
@ -312,7 +321,7 @@ wrap_then_do() {
|
||||
fi
|
||||
else
|
||||
buffer+=("${line}")
|
||||
if [[ "${line}" =~ ${multiline_then_end_regex} ]]
|
||||
if [[ "${line}" =~ ${multiline_if_then_end_regex} ]]
|
||||
then
|
||||
while IFS='' read -r line
|
||||
do
|
||||
@ -351,7 +360,7 @@ format() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
tempfile="$(dirname "${file}")/.${file##*/}.temp"
|
||||
tempfile="$(dirname "${file}")/.${file##*/}.formatted~"
|
||||
cp -af "${file}" "${tempfile}"
|
||||
fi
|
||||
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
|
||||
@ -396,6 +405,8 @@ format() {
|
||||
then
|
||||
cp -af "${tempfile}" "${file}"
|
||||
else
|
||||
# Show a linebreak between outputs
|
||||
[[ "${RETCODE}" != 0 ]] && onoe
|
||||
# Show differences
|
||||
"${DIFF}" "${DIFF_ARGS[@]}" "${file}" "${tempfile}" 1>&2
|
||||
fi
|
||||
@ -434,7 +445,6 @@ do
|
||||
4) onoe "${0##*/}: Fixable bad styles detected in file \"${file}\", run \`brew style --fix\` to apply. Formatter exited with code 4." ;;
|
||||
*) onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code ${retcode}." ;;
|
||||
esac
|
||||
onoe
|
||||
RETCODE=1
|
||||
fi
|
||||
done
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user