Skip to content

Commit

Permalink
Better indenting for nested if sections
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbra committed Mar 16, 2019
1 parent aa27b3a commit ef2e050
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 1 deletion.
4 changes: 3 additions & 1 deletion indent/sh.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
" License: Vim (see :h license)
" Repository: https://github.com/chrisbra/vim-sh-indent
" Changelog:
" 20190316 - Make use of searchpairpos for nested if sections
" fixes #11
" 20190201 - Better check for closing if sections
" 20180724 - make check for zsh syntax more rigid (needs word-boundaries)
" 20180326 - better support for line continuation
Expand Down Expand Up @@ -115,7 +117,7 @@ function! GetShIndent()
" Current line is a endif line, so get indent from start of "if condition" line
" TODO: should we do the same for other "end" lines?
if curline =~ '^\s*\%(fi\)\s*\%(#.*\)\=$'
let previous_line = search('if.\{-\};\s*then\s*\%(#.*\)\=$', 'bnW')
let previous_line = searchpair('\<if\>', '', '\<fi\>', 'bnW')
if previous_line > 0
let ind = indent(previous_line)
endif
Expand Down
10 changes: 10 additions & 0 deletions test/02/cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

vim --clean \
-c ':unlet! b:did_indent' \
-c ':delfunc! GetShIndent' \
-c ':so ../../indent/sh.vim' \
-c ':set sw=0 sts=-1 ts=2 et' \
-c 'norm! gg=G' \
-c ':saveas! output.sh' \
-c ':q!' indent.sh
24 changes: 24 additions & 0 deletions test/02/indent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

if [[ /bin/true ]]; then
echo "true"
else
echo "false"
if [[ /bin/false ]]; then
echo "false"
else
echo "true"
fi
fi
if [[ /bin/true ]]
then
echo "true"
else
echo "false"
if [[ /bin/false ]]
then
echo "false"
else
echo "true"
fi
fi
24 changes: 24 additions & 0 deletions test/02/reference.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

if [[ /bin/true ]]; then
echo "true"
else
echo "false"
if [[ /bin/false ]]; then
echo "false"
else
echo "true"
fi
fi
if [[ /bin/true ]]
then
echo "true"
else
echo "false"
if [[ /bin/false ]]
then
echo "false"
else
echo "true"
fi
fi
10 changes: 10 additions & 0 deletions test/03/cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

vim --clean \
-c ':unlet! b:did_indent' \
-c ':delfunc! GetShIndent' \
-c ':so ../../indent/sh.vim' \
-c ':set sw=0 sts=-1 ts=2 et' \
-c 'norm! gg=G' \
-c ':saveas! output.sh' \
-c ':q!' indent.sh
13 changes: 13 additions & 0 deletions test/03/indent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#/bin/sh

if true; then
echo 'pipe beginning'
sed 's/beginn/end/'
fi

# TODO:
# The line after the pipe will be indented. Is this correct?
if true; then
echo 'pipe beginning' |
sed 's/beginn/end/'
fi
13 changes: 13 additions & 0 deletions test/03/reference.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#/bin/sh

if true; then
echo 'pipe beginning'
sed 's/beginn/end/'
fi

# TODO:
# The line after the pipe will be indented. Is this correct?
if true; then
echo 'pipe beginning' |
sed 's/beginn/end/'
fi

0 comments on commit ef2e050

Please sign in to comment.