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