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