vim.vim 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. " Vim indent file
  2. " Language: Vim script
  3. " Maintainer: Bram Moolenaar <Bram@vim.org>
  4. " Last Change: 2022 Jun 24
  5. " Only load this indent file when no other was loaded.
  6. if exists("b:did_indent")
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal indentexpr=GetVimIndent()
  11. setlocal indentkeys+==endif,=enddef,=endfu,=endfor,=endwh,=endtry,=},=else,=cat,=finall,=END,0\\,0=\"\\\
  12. setlocal indentkeys-=0#
  13. setlocal indentkeys-=:
  14. let b:undo_indent = "setl indentkeys< indentexpr<"
  15. " Only define the function once.
  16. if exists("*GetVimIndent")
  17. finish
  18. endif
  19. let s:keepcpo= &cpo
  20. set cpo&vim
  21. function GetVimIndent()
  22. let ignorecase_save = &ignorecase
  23. try
  24. let &ignorecase = 0
  25. return GetVimIndentIntern()
  26. finally
  27. let &ignorecase = ignorecase_save
  28. endtry
  29. endfunc
  30. " Legacy script line continuation and Vim9 script operators that must mean an
  31. " expression that continues from the previous line.
  32. let s:lineContPat = '^\s*\(\\\|"\\ \|->\)'
  33. function GetVimIndentIntern()
  34. " If the current line has line continuation and the previous one too, use
  35. " the same indent. This does not skip empty lines.
  36. let cur_text = getline(v:lnum)
  37. let cur_has_linecont = cur_text =~ s:lineContPat
  38. if cur_has_linecont && v:lnum > 1 && getline(v:lnum - 1) =~ s:lineContPat
  39. return indent(v:lnum - 1)
  40. endif
  41. " Find a non-blank line above the current line.
  42. let lnum = prevnonblank(v:lnum - 1)
  43. " The previous line, ignoring line continuation
  44. let prev_text_end = lnum > 0 ? getline(lnum) : ''
  45. " If the current line doesn't start with '\' or '"\ ' and below a line that
  46. " starts with '\' or '"\ ', use the indent of the line above it.
  47. if !cur_has_linecont
  48. while lnum > 0 && getline(lnum) =~ s:lineContPat
  49. let lnum = lnum - 1
  50. endwhile
  51. endif
  52. " At the start of the file use zero indent.
  53. if lnum == 0
  54. return 0
  55. endif
  56. " the start of the previous line, skipping over line continuation
  57. let prev_text = getline(lnum)
  58. let found_cont = 0
  59. " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
  60. " and :else. Add it three times for a line that starts with '\' or '"\ '
  61. " after a line that doesn't (or g:vim_indent_cont if it exists).
  62. let ind = indent(lnum)
  63. " In heredoc indenting works completely differently.
  64. if has('syntax_items')
  65. let syn_here = synIDattr(synID(v:lnum, 1, 1), "name")
  66. if syn_here =~ 'vimLetHereDocStop'
  67. " End of heredoc: use indent of matching start line
  68. let lnum = v:lnum - 1
  69. while lnum > 0
  70. let attr = synIDattr(synID(lnum, 1, 1), "name")
  71. if attr != '' && attr !~ 'vimLetHereDoc'
  72. return indent(lnum)
  73. endif
  74. let lnum -= 1
  75. endwhile
  76. return 0
  77. endif
  78. if syn_here =~ 'vimLetHereDoc'
  79. if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc'
  80. " First line in heredoc: increase indent
  81. return ind + shiftwidth()
  82. endif
  83. " Heredoc continues: no change in indent
  84. return ind
  85. endif
  86. endif
  87. if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
  88. let found_cont = 1
  89. if exists("g:vim_indent_cont")
  90. let ind = ind + g:vim_indent_cont
  91. else
  92. let ind = ind + shiftwidth() * 3
  93. endif
  94. elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>'
  95. let ind = ind + shiftwidth()
  96. else
  97. " A line starting with :au does not increment/decrement indent.
  98. " A { may start a block or a dict. Assume that when a } follows it's a
  99. " terminated dict.
  100. " ":function" starts a block but "function(" doesn't.
  101. if prev_text !~ '^\s*au\%[tocmd]' && prev_text !~ '^\s*{.*}'
  102. let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|def\|el\%[seif]\)\>\|fu\%[nction][! ]\)')
  103. if i >= 0
  104. let ind += shiftwidth()
  105. if strpart(prev_text, i, 1) == '|' && has('syntax_items')
  106. \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\|PatSep\)$'
  107. let ind -= shiftwidth()
  108. endif
  109. endif
  110. endif
  111. endif
  112. " If the previous line contains an "end" after a pipe, but not in an ":au"
  113. " command. And not when there is a backslash before the pipe.
  114. " And when syntax HL is enabled avoid a match inside a string.
  115. let i = match(prev_text, '[^\\]|\s*\(ene\@!\)')
  116. if i > 0 && prev_text !~ '^\s*au\%[tocmd]'
  117. if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
  118. let ind = ind - shiftwidth()
  119. endif
  120. endif
  121. " For a line starting with "}" find the matching "{". Align with that line,
  122. " it is either the matching block start or dictionary start.
  123. " Use the mapped "%" from matchit to find the match, otherwise we may match
  124. " a { inside a comment or string.
  125. if cur_text =~ '^\s*}'
  126. if maparg('%') != ''
  127. exe v:lnum
  128. silent! normal %
  129. if line('.') < v:lnum
  130. let ind = indent('.')
  131. endif
  132. else
  133. " todo: use searchpair() to find a match
  134. endif
  135. endif
  136. " Look back for a line to align with
  137. while lnum > 1
  138. " Below a line starting with "}" find the matching "{".
  139. if prev_text =~ '^\s*}'
  140. if maparg('%') != ''
  141. exe lnum
  142. silent! normal %
  143. if line('.') < lnum
  144. let lnum = line('.')
  145. let ind = indent(lnum)
  146. let prev_text = getline(lnum)
  147. else
  148. break
  149. endif
  150. else
  151. " todo: use searchpair() to find a match
  152. break
  153. endif
  154. elseif prev_text =~ s:lineContPat
  155. " looks like a continuation like, go back one line
  156. let lnum = lnum - 1
  157. let ind = indent(lnum)
  158. let prev_text = getline(lnum)
  159. else
  160. break
  161. endif
  162. endwhile
  163. " Below a line starting with "]" we must be below the end of a list.
  164. " Include a "}" and "},} in case a dictionary ends too.
  165. if prev_text_end =~ '^\s*\(},\=\s*\)\=]'
  166. let ind = ind - shiftwidth()
  167. endif
  168. let ends_in_comment = has('syntax_items')
  169. \ && synIDattr(synID(lnum, len(getline(lnum)), 1), "name") =~ '\(Comment\|String\)$'
  170. " A line ending in "{" or "[" is most likely the start of a dict/list literal,
  171. " indent the next line more. Not for a continuation line or {{{.
  172. if !ends_in_comment && prev_text_end =~ '\s[{[]\s*$' && !found_cont
  173. let ind = ind + shiftwidth()
  174. endif
  175. " Subtract a 'shiftwidth' on a :endif, :endwhile, :endfor, :catch, :finally,
  176. " :endtry, :endfun, :enddef, :else and :augroup END.
  177. " Although ":en" would be enough only match short command names as in
  178. " 'indentkeys'.
  179. if cur_text =~ '^\s*\(endif\|endwh\|endfor\|endtry\|endfu\|enddef\|cat\|finall\|else\|aug\%[roup]\s\+[eE][nN][dD]\)'
  180. let ind = ind - shiftwidth()
  181. if ind < 0
  182. let ind = 0
  183. endif
  184. endif
  185. return ind
  186. endfunction
  187. let &cpo = s:keepcpo
  188. unlet s:keepcpo
  189. " vim:sw=2