sh.vim 9.3 KB

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