utils/shfmt.sh: add != and =~ to within test regex
This commit is contained in:
parent
2345a34095
commit
391e7e22f8
@ -227,11 +227,11 @@ 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_then_begin_regex='^( *)(el)?if '
|
local multiline_if_then_begin_regex='^( *)(el)?if '
|
||||||
local multiline_if_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:]])? ]]
|
||||||
@ -241,7 +241,7 @@ align_multiline_if_condition() {
|
|||||||
if [[ "$1" =~ ${multiline_if_then_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
|
||||||
@ -257,9 +257,14 @@ align_multiline_if_condition() {
|
|||||||
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
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user