vim.vim 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. " Vim indent file
  2. " Language: Vim script
  3. " Maintainer: Bram Moolenaar <Bram@vim.org>
  4. " Last Change: 2022 Mar 01
  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. let s:lineContPat = '^\s*\(\\\|"\\ \)'
  31. function GetVimIndentIntern()
  32. " Find a non-blank line above the current line.
  33. let lnum = prevnonblank(v:lnum - 1)
  34. " The previous line, ignoring line continuation
  35. let prev_text_end = lnum > 0 ? getline(lnum) : ''
  36. " If the current line doesn't start with '\' or '"\ ' and below a line that
  37. " starts with '\' or '"\ ', use the indent of the line above it.
  38. let cur_text = getline(v:lnum)
  39. if cur_text !~ s:lineContPat
  40. while lnum > 0 && getline(lnum) =~ s:lineContPat
  41. let lnum = lnum - 1
  42. endwhile
  43. endif
  44. " At the start of the file use zero indent.
  45. if lnum == 0
  46. return 0
  47. endif
  48. " the start of the previous line, skipping over line continuation
  49. let prev_text = getline(lnum)
  50. let found_cont = 0
  51. " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
  52. " and :else. Add it three times for a line that starts with '\' or '"\ '
  53. " after a line that doesn't (or g:vim_indent_cont if it exists).
  54. let ind = indent(lnum)
  55. " In heredoc indenting works completely differently.
  56. if has('syntax_items')
  57. let syn_here = synIDattr(synID(v:lnum, 1, 1), "name")
  58. if syn_here =~ 'vimLetHereDocStop'
  59. " End of heredoc: use indent of matching start line
  60. let lnum = v:lnum - 1
  61. while lnum > 0
  62. let attr = synIDattr(synID(lnum, 1, 1), "name")
  63. if attr != '' && attr !~ 'vimLetHereDoc'
  64. return indent(lnum)
  65. endif
  66. let lnum -= 1
  67. endwhile
  68. return 0
  69. endif
  70. if syn_here =~ 'vimLetHereDoc'
  71. if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc'
  72. " First line in heredoc: increase indent
  73. return ind + shiftwidth()
  74. endif
  75. " Heredoc continues: no change in indent
  76. return ind
  77. endif
  78. endif
  79. if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
  80. let found_cont = 1
  81. if exists("g:vim_indent_cont")
  82. let ind = ind + g:vim_indent_cont
  83. else
  84. let ind = ind + shiftwidth() * 3
  85. endif
  86. elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>'
  87. let ind = ind + shiftwidth()
  88. else
  89. " A line starting with :au does not increment/decrement indent.
  90. " A { may start a block or a dict. Assume that when a } follows it's a
  91. " terminated dict.
  92. " ":function" starts a block but "function(" doesn't.
  93. if prev_text !~ '^\s*au\%[tocmd]' && prev_text !~ '^\s*{.*}'
  94. let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|def\|el\%[seif]\)\>\|fu\%[nction][! ]\)')
  95. if i >= 0
  96. let ind += shiftwidth()
  97. if strpart(prev_text, i, 1) == '|' && has('syntax_items')
  98. \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\|PatSep\)$'
  99. let ind -= shiftwidth()
  100. endif
  101. endif
  102. endif
  103. endif
  104. " If the previous line contains an "end" after a pipe, but not in an ":au"
  105. " command. And not when there is a backslash before the pipe.
  106. " And when syntax HL is enabled avoid a match inside a string.
  107. let i = match(prev_text, '[^\\]|\s*\(ene\@!\)')
  108. if i > 0 && prev_text !~ '^\s*au\%[tocmd]'
  109. if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
  110. let ind = ind - shiftwidth()
  111. endif
  112. endif
  113. " For a line starting with "}" find the matching "{". If it is at the start
  114. " of the line align with it, probably end of a block.
  115. " Use the mapped "%" from matchit to find the match, otherwise we may match
  116. " a { inside a comment or string.
  117. if cur_text =~ '^\s*}'
  118. if maparg('%') != ''
  119. exe v:lnum
  120. silent! normal %
  121. if line('.') < v:lnum && getline('.') =~ '^\s*{'
  122. let ind = indent('.')
  123. endif
  124. else
  125. " todo: use searchpair() to find a match
  126. endif
  127. endif
  128. " Below a line starting with "}" find the matching "{". If it is at the
  129. " end of the line we must be below the end of a dictionary.
  130. if prev_text =~ '^\s*}'
  131. if maparg('%') != ''
  132. exe lnum
  133. silent! normal %
  134. if line('.') == lnum || getline('.') !~ '^\s*{'
  135. let ind = ind - shiftwidth()
  136. endif
  137. else
  138. " todo: use searchpair() to find a match
  139. endif
  140. endif
  141. " Below a line starting with "]" we must be below the end of a list.
  142. " Include a "}" and "},} in case a dictionary ends too.
  143. if prev_text_end =~ '^\s*\(},\=\s*\)\=]'
  144. let ind = ind - shiftwidth()
  145. endif
  146. let ends_in_comment = has('syntax_items')
  147. \ && synIDattr(synID(lnum, len(getline(lnum)), 1), "name") =~ '\(Comment\|String\)$'
  148. " A line ending in "{" or "[" is most likely the start of a dict/list literal,
  149. " indent the next line more. Not for a continuation line or {{{.
  150. if !ends_in_comment && prev_text_end =~ '\s[{[]\s*$' && !found_cont
  151. let ind = ind + shiftwidth()
  152. endif
  153. " Subtract a 'shiftwidth' on a :endif, :endwhile, :endfor, :catch, :finally,
  154. " :endtry, :endfun, :enddef, :else and :augroup END.
  155. " Although ":en" would be enough only match short command names as in
  156. " 'indentkeys'.
  157. if cur_text =~ '^\s*\(endif\|endwh\|endfor\|endtry\|endfu\|enddef\|cat\|finall\|else\|aug\%[roup]\s\+[eE][nN][dD]\)'
  158. let ind = ind - shiftwidth()
  159. if ind < 0
  160. let ind = 0
  161. endif
  162. endif
  163. return ind
  164. endfunction
  165. let &cpo = s:keepcpo
  166. unlet s:keepcpo
  167. " vim:sw=2