vim.vim 6.6 KB

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