sh.vim 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. " Vim indent file
  2. " Language: Shell Script
  3. " Maintainer: Christian Brabandt <cb@256bit.org>
  4. " Original Author: Nikolai Weibull <now@bitwi.se>
  5. " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org>
  6. " Latest Revision: 2019-10-24
  7. " License: Vim (see :h license)
  8. " Repository: https://github.com/chrisbra/vim-sh-indent
  9. " Changelog:
  10. " 20190726 - Correctly skip if keywords in syntax comments
  11. " (issue #17)
  12. " 20190603 - Do not indent in zsh filetypes with an `if` in comments
  13. " 20190428 - De-indent fi correctly when typing with
  14. " https://github.com/chrisbra/vim-sh-indent/issues/15
  15. " 20190325 - Indent fi; correctly
  16. " https://github.com/chrisbra/vim-sh-indent/issues/14
  17. " 20190319 - Indent arrays (only zsh and bash)
  18. " https://github.com/chrisbra/vim-sh-indent/issues/13
  19. " 20190316 - Make use of searchpairpos for nested if sections
  20. " fixes https://github.com/chrisbra/vim-sh-indent/issues/11
  21. " 20190201 - Better check for closing if sections
  22. " 20180724 - make check for zsh syntax more rigid (needs word-boundaries)
  23. " 20180326 - better support for line continuation
  24. " 20180325 - better detection of function definitions
  25. " 20180127 - better support for zsh complex commands
  26. " 20170808: - better indent of line continuation
  27. " 20170502: - get rid of buffer-shiftwidth function
  28. " 20160912: - preserve indentation of here-doc blocks
  29. " 20160627: - detect heredocs correctly
  30. " 20160213: - detect function definition correctly
  31. " 20160202: - use shiftwidth() function
  32. " 20151215: - set b:undo_indent variable
  33. " 20150728: - add foreach detection for zsh
  34. if exists("b:did_indent")
  35. finish
  36. endif
  37. let b:did_indent = 1
  38. setlocal indentexpr=GetShIndent()
  39. setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;&
  40. setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix
  41. setlocal indentkeys-=:,0#
  42. setlocal nosmartindent
  43. let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<'
  44. if exists("*GetShIndent")
  45. finish
  46. endif
  47. let s:cpo_save = &cpo
  48. set cpo&vim
  49. let s:sh_indent_defaults = {
  50. \ 'default': function('shiftwidth'),
  51. \ 'continuation-line': function('shiftwidth'),
  52. \ 'case-labels': function('shiftwidth'),
  53. \ 'case-statements': function('shiftwidth'),
  54. \ 'case-breaks': 0 }
  55. function! s:indent_value(option)
  56. let Value = exists('b:sh_indent_options')
  57. \ && has_key(b:sh_indent_options, a:option) ?
  58. \ b:sh_indent_options[a:option] :
  59. \ s:sh_indent_defaults[a:option]
  60. if type(Value) == type(function('type'))
  61. return Value()
  62. endif
  63. return Value
  64. endfunction
  65. function! GetShIndent()
  66. let curline = getline(v:lnum)
  67. let lnum = prevnonblank(v:lnum - 1)
  68. if lnum == 0
  69. return 0
  70. endif
  71. let line = getline(lnum)
  72. let pnum = prevnonblank(lnum - 1)
  73. let pline = getline(pnum)
  74. let ind = indent(lnum)
  75. " Check contents of previous lines
  76. " should not apply to e.g. commented lines
  77. if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' ||
  78. \ (&ft is# 'zsh' && line =~ '^\s*\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>')
  79. if !s:is_end_expression(line)
  80. let ind += s:indent_value('default')
  81. endif
  82. elseif s:is_case_label(line, pnum)
  83. if !s:is_case_ended(line)
  84. let ind += s:indent_value('case-statements')
  85. endif
  86. " function definition
  87. elseif s:is_function_definition(line)
  88. if line !~ '}\s*\%(#.*\)\=$'
  89. let ind += s:indent_value('default')
  90. endif
  91. " array (only works for zsh or bash)
  92. elseif s:is_array(line) && line !~ ')\s*$' && (&ft is# 'zsh' || s:is_bash())
  93. let ind += s:indent_value('continuation-line')
  94. " end of array
  95. elseif curline =~ '^\s*)$'
  96. let ind -= s:indent_value('continuation-line')
  97. elseif s:is_continuation_line(line)
  98. if pnum == 0 || !s:is_continuation_line(pline)
  99. let ind += s:indent_value('continuation-line')
  100. endif
  101. elseif s:end_block(line) && !s:start_block(line)
  102. let ind = indent(lnum)
  103. elseif pnum != 0 &&
  104. \ s:is_continuation_line(pline) &&
  105. \ !s:end_block(curline) &&
  106. \ !s:is_end_expression(curline)
  107. " only add indent, if line and pline is in the same block
  108. let i = v:lnum
  109. let ind2 = indent(s:find_continued_lnum(pnum))
  110. while !s:is_empty(getline(i)) && i > pnum
  111. let i -= 1
  112. endw
  113. if i == pnum
  114. let ind += ind2
  115. else
  116. let ind = ind2
  117. endif
  118. endif
  119. let pine = line
  120. " Check content of current line
  121. let line = curline
  122. " Current line is a endif line, so get indent from start of "if condition" line
  123. " TODO: should we do the same for other "end" lines?
  124. if curline =~ '^\s*\%(fi\);\?\s*\%(#.*\)\=$'
  125. let ind = indent(v:lnum)
  126. let previous_line = searchpair('\<if\>', '', '\<fi\>\zs', 'bnW', 'synIDattr(synID(line("."),col("."), 1),"name") =~? "comment\\|quote"')
  127. if previous_line > 0
  128. let ind = indent(previous_line)
  129. endif
  130. elseif line =~ '^\s*\%(then\|do\|else\|elif\|done\|end\)\>' || s:end_block(line)
  131. let ind -= s:indent_value('default')
  132. elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1))
  133. let ind -= s:indent_value('default')
  134. elseif line =~ '^\s*esac\>'
  135. let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
  136. \ 0 : s:indent_value('case-statements')) +
  137. \ s:indent_value('case-labels')
  138. if s:is_case_break(pine)
  139. let ind += s:indent_value('case-breaks')
  140. endif
  141. elseif s:is_case_label(line, lnum)
  142. if s:is_case(pine)
  143. let ind = indent(lnum) + s:indent_value('case-labels')
  144. else
  145. let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
  146. \ 0 : s:indent_value('case-statements')) -
  147. \ s:indent_value('case-breaks')
  148. endif
  149. elseif s:is_case_break(line)
  150. let ind -= s:indent_value('case-breaks')
  151. elseif s:is_here_doc(line)
  152. let ind = 0
  153. " statements, executed within a here document. Keep the current indent
  154. elseif match(map(synstack(v:lnum, 1), 'synIDattr(v:val, "name")'), '\c\mheredoc') > -1
  155. return indent(v:lnum)
  156. elseif s:is_comment(line) && s:is_empty(getline(v:lnum-1))
  157. return indent(v:lnum)
  158. endif
  159. return ind > 0 ? ind : 0
  160. endfunction
  161. function! s:is_continuation_line(line)
  162. " Comment, cannot be a line continuation
  163. if a:line =~ '^\s*#'
  164. return 0
  165. else
  166. " start-of-line
  167. " \\ or && or || or |
  168. " followed optionally by { or #
  169. return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\||\)' .
  170. \ '\s*\({\s*\)\=\(#.*\)\=$'
  171. endif
  172. endfunction
  173. function! s:find_continued_lnum(lnum)
  174. let i = a:lnum
  175. while i > 1 && s:is_continuation_line(getline(i - 1))
  176. let i -= 1
  177. endwhile
  178. return i
  179. endfunction
  180. function! s:is_function_definition(line)
  181. return a:line =~ '^\s*\<\k\+\>\s*()\s*{' ||
  182. \ a:line =~ '^\s*{' ||
  183. \ a:line =~ '^\s*function\s*\k\+\s*\%(()\)\?\s*{'
  184. endfunction
  185. function! s:is_array(line)
  186. return a:line =~ '^\s*\<\k\+\>=('
  187. endfunction
  188. function! s:is_case_label(line, pnum)
  189. if a:line !~ '^\s*(\=.*)'
  190. return 0
  191. endif
  192. if a:pnum > 0
  193. let pine = getline(a:pnum)
  194. if !(s:is_case(pine) || s:is_case_ended(pine))
  195. return 0
  196. endif
  197. endif
  198. let suffix = substitute(a:line, '^\s*(\=', "", "")
  199. let nesting = 0
  200. let i = 0
  201. let n = strlen(suffix)
  202. while i < n
  203. let c = suffix[i]
  204. let i += 1
  205. if c == '\\'
  206. let i += 1
  207. elseif c == '('
  208. let nesting += 1
  209. elseif c == ')'
  210. if nesting == 0
  211. return 1
  212. endif
  213. let nesting -= 1
  214. endif
  215. endwhile
  216. return 0
  217. endfunction
  218. function! s:is_case(line)
  219. return a:line =~ '^\s*case\>'
  220. endfunction
  221. function! s:is_case_break(line)
  222. return a:line =~ '^\s*;[;&]'
  223. endfunction
  224. function! s:is_here_doc(line)
  225. if a:line =~ '^\w\+$'
  226. let here_pat = '<<-\?'. s:escape(a:line). '\$'
  227. return search(here_pat, 'bnW') > 0
  228. endif
  229. return 0
  230. endfunction
  231. function! s:is_case_ended(line)
  232. return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$'
  233. endfunction
  234. function! s:is_case_empty(line)
  235. if a:line =~ '^\s*$' || a:line =~ '^\s*#'
  236. return s:is_case_empty(getline(v:lnum - 1))
  237. else
  238. return a:line =~ '^\s*case\>'
  239. endif
  240. endfunction
  241. function! s:escape(pattern)
  242. return '\V'. escape(a:pattern, '\\')
  243. endfunction
  244. function! s:is_empty(line)
  245. return a:line =~ '^\s*$'
  246. endfunction
  247. function! s:end_block(line)
  248. return a:line =~ '^\s*}'
  249. endfunction
  250. function! s:start_block(line)
  251. return a:line =~ '{\s*\(#.*\)\?$'
  252. endfunction
  253. function! s:find_start_block(lnum)
  254. let i = a:lnum
  255. while i > 1 && !s:start_block(getline(i))
  256. let i -= 1
  257. endwhile
  258. return i
  259. endfunction
  260. function! s:is_comment(line)
  261. return a:line =~ '^\s*#'
  262. endfunction
  263. function! s:is_end_expression(line)
  264. return a:line =~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$'
  265. endfunction
  266. function! s:is_bash()
  267. return get(g:, 'is_bash', 0) || get(b:, 'is_bash', 0)
  268. endfunction
  269. let &cpo = s:cpo_save
  270. unlet s:cpo_save