utils/shfmt.sh: rename regex variables

This commit is contained in:
XuehaiPan 2021-11-12 19:07:55 +08:00
parent 3d4f367bd9
commit 2345a34095

View File

@ -229,8 +229,8 @@ align_multiline_if_condition() {
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 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() {
@ -238,7 +238,7 @@ align_multiline_if_condition() {
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]}" ]] && extra_indent=' '
@ -250,7 +250,7 @@ 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]}"
@ -287,24 +287,24 @@ 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_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$'
local singleline_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$' local singleline_for_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$'
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( *#.*)?)$'
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_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 +312,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 +351,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 +396,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 +436,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