robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
7f8175dc48
master
1" Support for Python indenting, see runtime/indent/python.vim 2 3let s: keepcpo = & cpo 4set cpo & vim 5 6" need to inspect some old g:pyindent_* variables to be backward compatible 7let g: python_indent = extend ( get ( g:, 'python_indent' , {}), #{ 8 \closed_paren_align_last_line : v: true , 9 \open_paren : get ( g:, 'pyindent_open_paren' , 'shiftwidth() * 2' ), 10 \nested_paren : get ( g:, 'pyindent_nested_paren' , 'shiftwidth()' ), 11 \continue : get ( g:, 'pyindent_continue' , 'shiftwidth() * 2' ), 12"\ searchpair() can be slow, limit the time to 150 msec or what is put in 13"\ g:python_indent.searchpair_timeout 14 \searchpair_timeout : get ( g:, 'pyindent_searchpair_timeout' , 150 ), 15"\ Identing inside parentheses can be very slow, regardless of the searchpair() 16"\ timeout, so let the user disable this feature if he doesn't need it 17 \disable_parentheses_indenting : get ( g:, 'pyindent_disable_parentheses_indenting' , v: false ), 18 \}, 'keep' ) 19 20let s: maxoff = 50 " maximum number of lines to look backwards for () 21 22function s: SearchBracket ( fromlnum , flags ) 23" VIM_INDENT_TEST_TRACE_START 24return searchpairpos ( '[[({]' , '' , '[])}]' , a: flags , 25 \{ ->synstack ( '.' , col ( '.' )) 26 \ ->indexof ({ _ , id ->synIDattr ( id , 'name' ) =~ '\%(Comment\|Todo\|String\|Bytes\)$' }) >= 0 }, 27 \[ 0 , a: fromlnum - s: maxoff ] ->max (), g: python_indent . searchpair_timeout ) 28" VIM_INDENT_TEST_TRACE_END python#s:SearchBracket 29endfunction 30 31" See if the specified line is already user-dedented from the expected value. 32function s: Dedented ( lnum , expected ) 33return indent ( a: lnum ) <= a: expected - shiftwidth () 34endfunction 35 36" Some other filetypes which embed Python have slightly different indent 37" rules (e.g. bitbake). Those filetypes can pass an extra funcref to this 38" function which is evaluated below. 39function python#GetIndent ( lnum , ...) 40let ExtraFunc = a: 0 > 0 ? a: 1 : 0 41 42" If this line is explicitly joined: If the previous line was also joined, 43" line it up with that one, otherwise add two 'shiftwidth' 44if getline ( a: lnum - 1 ) =~ '\\$' 45if a: lnum > 1 && getline ( a: lnum - 2 ) =~ '\\$' 46return indent ( a: lnum - 1 ) 47endif 48return indent ( a: lnum - 1 ) + get ( g:, 'pyindent_continue' , g: python_indent . continue ) ->eval () 49endif 50 51" If the start of the line is in a string don't change the indent. 52if has ( 'syntax_items' ) 53 \&& synIDattr ( synID ( a: lnum , 1 , 1 ), "name" ) =~ "String$" 54return -1 55endif 56 57" Search backwards for the previous non-empty line. 58let plnum = prevnonblank ( v: lnum - 1 ) 59 60if plnum == 0 61" This is the first non-empty line, use zero indent. 62return 0 63endif 64 65if g: python_indent . disable_parentheses_indenting == 1 66let plindent = indent ( plnum ) 67let plnumstart = plnum 68else 69" Indent inside parens. 70" Align with the open paren unless it is at the end of the line. 71" E.g. 72" open_paren_not_at_EOL(100, 73" (200, 74" 300), 75" 400) 76" open_paren_at_EOL( 77" 100, 200, 300, 400) 78call cursor ( a: lnum , 1 ) 79let [ parlnum , parcol ] = s: SearchBracket ( a: lnum , 'nbW' ) 80if parlnum > 0 81if parcol != col ([ parlnum , '$' ]) - 1 82return parcol 83elseif getline ( a: lnum ) =~ '^\s*[])}]' && ! g: python_indent . closed_paren_align_last_line 84return indent ( parlnum ) 85endif 86endif 87 88call cursor ( plnum , 1 ) 89 90" If the previous line is inside parenthesis, use the indent of the starting 91" line. 92let [ parlnum , _ ] = s: SearchBracket ( plnum , 'nbW' ) 93if parlnum > 0 94if a: 0 > 0 && ExtraFunc ( parlnum ) 95" We may have found the opening brace of a bitbake Python task, e.g. 'python do_task {' 96" If so, ignore it here - it will be handled later. 97let parlnum = 0 98let plindent = indent ( plnum ) 99let plnumstart = plnum 100else 101let plindent = indent ( parlnum ) 102let plnumstart = parlnum 103endif 104else 105let plindent = indent ( plnum ) 106let plnumstart = plnum 107endif 108 109" When inside parenthesis: If at the first line below the parenthesis add 110" two 'shiftwidth', otherwise same as previous line. 111" i = (a 112" + b 113" + c) 114call cursor ( a: lnum , 1 ) 115let [ p , _ ] = s: SearchBracket ( a: lnum , 'bW' ) 116if p > 0 117if a: 0 > 0 && ExtraFunc ( p ) 118" Currently only used by bitbake 119" Handle first non-empty line inside a bitbake Python task 120if p == plnum 121return shiftwidth () 122endif 123 124" Handle the user actually trying to close a bitbake Python task 125let line = getline ( a: lnum ) 126if line =~ '^\s*}' 127return -2 128endif 129 130" Otherwise ignore the brace 131let p = 0 132else 133if p == plnum 134" When the start is inside parenthesis, only indent one 'shiftwidth'. 135let [ pp , _ ] = s: SearchBracket ( a: lnum , 'bW' ) 136if pp > 0 137return indent ( plnum ) 138 \+ get ( g:, 'pyindent_nested_paren' , g: python_indent . nested_paren ) ->eval () 139endif 140return indent ( plnum ) 141 \+ get ( g:, 'pyindent_open_paren' , g: python_indent . open_paren ) ->eval () 142endif 143if plnumstart == p 144return indent ( plnum ) 145endif 146return plindent 147endif 148endif 149endif 150 151 152" Get the line and remove a trailing comment. 153" Use syntax highlighting attributes when possible. 154let pline = getline ( plnum ) 155let pline_len = strlen ( pline ) 156if has ( 'syntax_items' ) 157" If the last character in the line is a comment, do a binary search for 158" the start of the comment. synID() is slow, a linear search would take 159" too long on a long line. 160if synstack ( plnum , pline_len ) 161 \ ->indexof ({ _ , id ->synIDattr ( id , 'name' ) =~ '\%(Comment\|Todo\)$' }) >= 0 162let min = 1 163let max = pline_len 164while min < max 165let col = ( min + max ) / 2 166if synstack ( plnum , col ) 167 \ ->indexof ({ _ , id ->synIDattr ( id , 'name' ) =~ '\%(Comment\|Todo\)$' }) >= 0 168let max = col 169else 170let min = col + 1 171endif 172endwhile 173let pline = strpart ( pline , 0 , min - 1 ) 174endif 175else 176let col = 0 177while col < pline_len 178if pline [ col ] == '#' 179let pline = strpart ( pline , 0 , col ) 180break 181endif 182let col = col + 1 183endwhile 184endif 185 186" If the previous line ended with a colon, indent this line 187if pline =~ ':\s*$' 188return plindent + shiftwidth () 189endif 190 191" If the previous line was a stop-execution statement... 192if getline ( plnum ) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' 193" See if the user has already dedented 194if s: Dedented ( a: lnum , indent ( plnum )) 195" If so, trust the user 196return -1 197endif 198" If not, recommend one dedent 199return indent ( plnum ) - shiftwidth () 200endif 201 202" If the current line begins with a keyword that lines up with "try" 203if getline ( a: lnum ) =~ '^\s*\(except\|finally\)\>' 204let lnum = a: lnum - 1 205while lnum >= 1 206if getline ( lnum ) =~ '^\s*\(try\|except\)\>' 207let ind = indent ( lnum ) 208if ind >= indent ( a: lnum ) 209return -1 " indent is already less than this 210endif 211return ind " line up with previous try or except 212endif 213let lnum = lnum - 1 214endwhile 215return -1 " no matching "try"! 216endif 217 218" If the current line begins with a header keyword, dedent 219if getline ( a: lnum ) =~ '^\s*\(elif\|else\)\>' 220 221" Unless the previous line was a one-liner 222if getline ( plnumstart ) =~ '^\s*\(for\|if\|elif\|try\)\>' 223return plindent 224endif 225 226" Or the user has already dedented 227if s: Dedented ( a: lnum , plindent ) 228return -1 229endif 230 231return plindent - shiftwidth () 232endif 233 234" When after a () construct we probably want to go back to the start line. 235" a = (b 236" + c) 237" here 238if parlnum > 0 239" ...unless the user has already dedented 240if s: Dedented ( a: lnum , plindent ) 241return -1 242else 243return plindent 244endif 245endif 246 247return -1 248endfunction 249 250let & cpo = s: keepcpo 251unlet s: keepcpo