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() {
|
align_multiline_if_condition() {
|
||||||
local line
|
local line
|
||||||
local lastline=''
|
local lastline=''
|
||||||
local base_indent='' # indents before `if`
|
local base_indent='' # indents before `if`
|
||||||
local extra_indent='' # 2 extra spaces for `elif`
|
local elif_extra_indent='' # 2 extra spaces for `elif`
|
||||||
local multiline_if_begin_regex='^( *)(el)?if '
|
local multiline_if_then_begin_regex='^( *)(el)?if '
|
||||||
local multiline_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
local multiline_if_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
||||||
local within_test_regex='^( *)(((! )?-[fdLrwxeszn] )|([^\[]+ == ))'
|
local within_test_regex='^( *)(((! )?-[fdLrwxeszn] )|([^\[]+ (==|!=|=~) ))'
|
||||||
|
|
||||||
trim() {
|
trim() {
|
||||||
[[ "$1" =~ [^[:space:]](.*[^[:space:]])? ]]
|
[[ "$1" =~ [^[:space:]](.*[^[:space:]])? ]]
|
||||||
printf "%s" "${BASH_REMATCH[0]}"
|
printf "%s" "${BASH_REMATCH[0]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ "$1" =~ ${multiline_if_begin_regex} ]]
|
if [[ "$1" =~ ${multiline_if_then_begin_regex} ]]
|
||||||
then
|
then
|
||||||
base_indent="${BASH_REMATCH[1]}"
|
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"
|
echo "$1"
|
||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
@ -250,16 +250,21 @@ align_multiline_if_condition() {
|
|||||||
do
|
do
|
||||||
line="$1"
|
line="$1"
|
||||||
shift
|
shift
|
||||||
if [[ "${line}" =~ ${multiline_then_end_regex} ]]
|
if [[ "${line}" =~ ${multiline_if_then_end_regex} ]]
|
||||||
then
|
then
|
||||||
line="${BASH_REMATCH[1]}"
|
line="${BASH_REMATCH[1]}"
|
||||||
lastline="${base_indent}${BASH_REMATCH[2]}"
|
lastline="${base_indent}${BASH_REMATCH[2]}"
|
||||||
fi
|
fi
|
||||||
if [[ "${line}" =~ ${within_test_regex} ]]
|
if [[ "${line}" =~ ${within_test_regex} ]]
|
||||||
then
|
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
|
else
|
||||||
echo "${base_indent}${extra_indent} $(trim "${line}")"
|
echo "${base_indent}${elif_extra_indent} $(trim "${line}")"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -287,24 +292,28 @@ wrap_then_do() {
|
|||||||
local -a processed=()
|
local -a processed=()
|
||||||
local -a buffer=()
|
local -a buffer=()
|
||||||
local line
|
local line
|
||||||
local singleline_then_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$'
|
local singleline_if_then_fi_regex='^( *)if (.+)\; then (.+)\; fi( *#.*)?$'
|
||||||
local singleline_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$'
|
local singleline_if_then_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$'
|
||||||
local multiline_if_begin_regex='^( *)(el)?if '
|
local singleline_for_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$'
|
||||||
local multiline_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
local multiline_if_then_begin_regex='^( *)(el)?if '
|
||||||
|
local multiline_if_then_end_regex='^(.*)\; (then( *#.*)?)$'
|
||||||
|
|
||||||
while IFS='' read -r line
|
while IFS='' read -r line
|
||||||
do
|
do
|
||||||
if [[ "${#buffer[@]}" == 0 ]]
|
if [[ "${#buffer[@]}" == 0 ]]
|
||||||
then
|
then
|
||||||
if [[ "${line}" =~ ${singleline_then_regex} ]]
|
if [[ "${line}" =~ ${singleline_if_then_fi_regex} ]]
|
||||||
|
then
|
||||||
|
processed+=("${line}")
|
||||||
|
elif [[ "${line}" =~ ${singleline_if_then_regex} ]]
|
||||||
then
|
then
|
||||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}if ${BASH_REMATCH[3]}")
|
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}if ${BASH_REMATCH[3]}")
|
||||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}")
|
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}")
|
||||||
elif [[ "${line}" =~ ${singleline_do_regex} ]]
|
elif [[ "${line}" =~ ${singleline_for_do_regex} ]]
|
||||||
then
|
then
|
||||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]} ${BASH_REMATCH[3]}")
|
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]} ${BASH_REMATCH[3]}")
|
||||||
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}")
|
processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}")
|
||||||
elif [[ "${line}" =~ ${multiline_if_begin_regex} ]]
|
elif [[ "${line}" =~ ${multiline_if_then_begin_regex} ]]
|
||||||
then
|
then
|
||||||
buffer=("${line}")
|
buffer=("${line}")
|
||||||
else
|
else
|
||||||
@ -312,7 +321,7 @@ wrap_then_do() {
|
|||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
buffer+=("${line}")
|
buffer+=("${line}")
|
||||||
if [[ "${line}" =~ ${multiline_then_end_regex} ]]
|
if [[ "${line}" =~ ${multiline_if_then_end_regex} ]]
|
||||||
then
|
then
|
||||||
while IFS='' read -r line
|
while IFS='' read -r line
|
||||||
do
|
do
|
||||||
@ -351,7 +360,7 @@ format() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
tempfile="$(dirname "${file}")/.${file##*/}.temp"
|
tempfile="$(dirname "${file}")/.${file##*/}.formatted~"
|
||||||
cp -af "${file}" "${tempfile}"
|
cp -af "${file}" "${tempfile}"
|
||||||
fi
|
fi
|
||||||
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
|
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
|
||||||
@ -396,6 +405,8 @@ format() {
|
|||||||
then
|
then
|
||||||
cp -af "${tempfile}" "${file}"
|
cp -af "${tempfile}" "${file}"
|
||||||
else
|
else
|
||||||
|
# Show a linebreak between outputs
|
||||||
|
[[ "${RETCODE}" != 0 ]] && onoe
|
||||||
# Show differences
|
# Show differences
|
||||||
"${DIFF}" "${DIFF_ARGS[@]}" "${file}" "${tempfile}" 1>&2
|
"${DIFF}" "${DIFF_ARGS[@]}" "${file}" "${tempfile}" 1>&2
|
||||||
fi
|
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." ;;
|
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}." ;;
|
*) onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code ${retcode}." ;;
|
||||||
esac
|
esac
|
||||||
onoe
|
|
||||||
RETCODE=1
|
RETCODE=1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user