robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d3068d34cc
master
1" Vim indent file 2" Language: Falcon 3" Maintainer: Steven Oliver <oliver.steven@gmail.com> 4" Website: https://steveno@github.com/steveno/falconpl-vim.git 5" Credits: This is, to a great extent, a copy n' paste of ruby.vim. 6" 2022 April: b:undo_indent added by Doug Kearns 7 8" 1. Setup {{{1 9" ============ 10 11" Only load this indent file when no other was loaded. 12if exists ( "b:did_indent" ) 13finish 14endif 15let b: did_indent = 1 16 17setlocal nosmartindent 18 19" Setup indent function and when to use it 20setlocal indentexpr = FalconGetIndent (v:lnum) 21setlocal indentkeys = 0{,0},0),0],!^F,o,O,e 22setlocal indentkeys += =~case,=~catch,=~default,=~elif,=~else,=~end,=~\" 23 24let b: undo_indent = "setl inde< indk< si<" 25 26" Define the appropriate indent function but only once 27if exists ( "*FalconGetIndent" ) 28finish 29endif 30 31let s: cpo_save = & cpo 32set cpo & vim 33 34" 2. Variables {{{1 35" ============ 36 37" Regex of syntax group names that are strings AND comments 38let s: syng_strcom = '\<falcon\%(String\|StringEscape\|Comment\)\>' 39 40" Regex of syntax group names that are strings 41let s: syng_string = '\<falcon\%(String\|StringEscape\)\>' 42 43" Regex that defines blocks. 44" 45" Note that there's a slight problem with this regex and s:continuation_regex. 46" Code like this will be matched by both: 47" 48" method_call do |(a, b)| 49" 50" The reason is that the pipe matches a hanging "|" operator. 51" 52let s: block_regex = 53 \'\%(\<do:\@!\>\|%\@<!{\)\s*\%(|\s*(*\s*\%([*@&]\=\h\w*,\=\s*\)\%(,\s*(*\s*[*@&]\=\h\w*\s*)*\s*\)*|\)\=\s*\%(#.*\)\=$' 54 55let s: block_continuation_regex = '^\s*[^])}\t ].*' . s: block_regex 56 57" Regex that defines continuation lines. 58" TODO: this needs to deal with if ...: and so on 59let s: continuation_regex = 60 \'\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$' 61 62" Regex that defines bracket continuations 63let s: bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$' 64 65" Regex that defines continuation lines, not including (, {, or [. 66let s: non_bracket_continuation_regex = '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$' 67 68" Keywords to indent on 69let s: falcon_indent_keywords = '^\s*\(case\|catch\|class\|enum\|default\|elif\|else' . 70 \'\|for\|function\|if.*"[^"]*:.*"\|if \(\(:\)\@!.\)*$\|loop\|object\|select' . 71 \'\|switch\|try\|while\|\w*\s*=\s*\w*([$\)' 72 73" Keywords to deindent on 74let s: falcon_deindent_keywords = '^\s*\(case\|catch\|default\|elif\|else\|end\)' 75 76" 3. Functions {{{1 77" ============ 78 79" Check if the character at lnum:col is inside a string, comment, or is ascii. 80function s: IsInStringOrComment ( lnum , col ) 81return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) =~ s: syng_strcom 82endfunction 83 84" Check if the character at lnum:col is inside a string. 85function s: IsInString ( lnum , col ) 86return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) =~ s: syng_string 87endfunction 88 89" Check if the character at lnum:col is inside a string delimiter 90function s: IsInStringDelimiter ( lnum , col ) 91return synIDattr ( synID ( a: lnum , a: col , 1 ), 'name' ) == 'falconStringDelimiter' 92endfunction 93 94" Find line above 'lnum' that isn't empty, in a comment, or in a string. 95function s: PrevNonBlankNonString ( lnum ) 96let in_block = 0 97let lnum = prevnonblank ( a: lnum ) 98while lnum > 0 99" Go in and out of blocks comments as necessary. 100" If the line isn't empty (with opt. comment) or in a string, end search. 101let line = getline ( lnum ) 102if line =~ '^=begin' 103if in_block 104let in_block = 0 105else 106break 107endif 108elseif ! in_block && line =~ '^=end' 109let in_block = 1 110elseif ! in_block && line !~ '^\s*#.*$' && ! ( s: IsInStringOrComment ( lnum , 1 ) 111 \&& s: IsInStringOrComment ( lnum , strlen ( line ))) 112break 113endif 114let lnum = prevnonblank ( lnum - 1 ) 115endwhile 116return lnum 117endfunction 118 119" Find line above 'lnum' that started the continuation 'lnum' may be part of. 120function s: GetMSL ( lnum ) 121" Start on the line we're at and use its indent. 122let msl = a: lnum 123let msl_body = getline ( msl ) 124let lnum = s: PrevNonBlankNonString ( a: lnum - 1 ) 125while lnum > 0 126" If we have a continuation line, or we're in a string, use line as MSL. 127" Otherwise, terminate search as we have found our MSL already. 128let line = getline ( lnum ) 129 130if s: Match ( line , s: non_bracket_continuation_regex ) && 131 \s: Match ( msl , s: non_bracket_continuation_regex ) 132" If the current line is a non-bracket continuation and so is the 133" previous one, keep its indent and continue looking for an MSL. 134" 135" Example: 136" method_call one, 137" two, 138" three 139" 140let msl = lnum 141elseif s: Match ( lnum , s: non_bracket_continuation_regex ) && 142 \( s: Match ( msl , s: bracket_continuation_regex ) || s: Match ( msl , s: block_continuation_regex )) 143" If the current line is a bracket continuation or a block-starter, but 144" the previous is a non-bracket one, respect the previous' indentation, 145" and stop here. 146" 147" Example: 148" method_call one, 149" two { 150" three 151" 152return lnum 153elseif s: Match ( lnum , s: bracket_continuation_regex ) && 154 \( s: Match ( msl , s: bracket_continuation_regex ) || s: Match ( msl , s: block_continuation_regex )) 155" If both lines are bracket continuations (the current may also be a 156" block-starter), use the current one's and stop here 157" 158" Example: 159" method_call( 160" other_method_call( 161" foo 162return msl 163elseif s: Match ( lnum , s: block_regex ) && 164 \! s: Match ( msl , s: continuation_regex ) && 165 \! s: Match ( msl , s: block_continuation_regex ) 166" If the previous line is a block-starter and the current one is 167" mostly ordinary, use the current one as the MSL. 168" 169" Example: 170" method_call do 171" something 172" something_else 173return msl 174else 175let col = match ( line , s: continuation_regex ) + 1 176if ( col > 0 && ! s: IsInStringOrComment ( lnum , col )) 177 \|| s: IsInString ( lnum , strlen ( line )) 178let msl = lnum 179else 180break 181endif 182endif 183 184let msl_body = getline ( msl ) 185let lnum = s: PrevNonBlankNonString ( lnum - 1 ) 186endwhile 187return msl 188endfunction 189 190" Check if line 'lnum' has more opening brackets than closing ones. 191function s: ExtraBrackets ( lnum ) 192let opening = { 'parentheses' : [], 'braces' : [], 'brackets' : []} 193let closing = { 'parentheses' : [], 'braces' : [], 'brackets' : []} 194 195let line = getline ( a: lnum ) 196let pos = match ( line , '[][(){}]' , 0 ) 197 198" Save any encountered opening brackets, and remove them once a matching 199" closing one has been found. If a closing bracket shows up that doesn't 200" close anything, save it for later. 201while pos != -1 202if ! s: IsInStringOrComment ( a: lnum , pos + 1 ) 203if line [ pos ] == '(' 204call add ( opening . parentheses , { 'type' : '(' , 'pos' : pos }) 205elseif line [ pos ] == ')' 206if empty ( opening . parentheses ) 207call add ( closing . parentheses , { 'type' : ')' , 'pos' : pos }) 208else 209let opening . parentheses = opening . parentheses [ 0 : -2 ] 210endif 211elseif line [ pos ] == '{' 212call add ( opening . braces , { 'type' : '{' , 'pos' : pos }) 213elseif line [ pos ] == '}' 214if empty ( opening . braces ) 215call add ( closing . braces , { 'type' : '}' , 'pos' : pos }) 216else 217let opening . braces = opening . braces [ 0 : -2 ] 218endif 219elseif line [ pos ] == '[' 220call add ( opening . brackets , { 'type' : '[' , 'pos' : pos }) 221elseif line [ pos ] == ']' 222if empty ( opening . brackets ) 223call add ( closing . brackets , { 'type' : ']' , 'pos' : pos }) 224else 225let opening . brackets = opening . brackets [ 0 : -2 ] 226endif 227endif 228endif 229 230let pos = match ( line , '[][(){}]' , pos + 1 ) 231endwhile 232 233" Find the rightmost brackets, since they're the ones that are important in 234" both opening and closing cases 235let rightmost_opening = { 'type' : '(' , 'pos' : -1 } 236let rightmost_closing = { 'type' : ')' , 'pos' : -1 } 237 238for opening in opening . parentheses + opening . braces + opening . brackets 239if opening . pos > rightmost_opening . pos 240let rightmost_opening = opening 241endif 242endfor 243 244for closing in closing . parentheses + closing . braces + closing . brackets 245if closing . pos > rightmost_closing . pos 246let rightmost_closing = closing 247endif 248endfor 249 250return [ rightmost_opening , rightmost_closing ] 251endfunction 252 253function s: Match ( lnum , regex ) 254let col = match ( getline ( a: lnum ), '\C' . a: regex ) + 1 255return col > 0 && ! s: IsInStringOrComment ( a: lnum , col ) ? col : 0 256endfunction 257 258function s: MatchLast ( lnum , regex ) 259let line = getline ( a: lnum ) 260let col = match ( line , '.*\zs' . a: regex ) 261while col != -1 && s: IsInStringOrComment ( a: lnum , col ) 262let line = strpart ( line , 0 , col ) 263let col = match ( line , '.*' . a: regex ) 264endwhile 265return col + 1 266endfunction 267 268" 4. FalconGetIndent Routine {{{1 269" ============ 270 271function FalconGetIndent (...) 272" For the current line, use the first argument if given, else v:lnum 273let clnum = a: 0 ? a: 1 : v: lnum 274 275" Use zero indent at the top of the file 276if clnum == 0 277return 0 278endif 279 280let line = getline ( clnum ) 281let ind = -1 282 283" If we got a closing bracket on an empty line, find its match and indent 284" according to it. For parentheses we indent to its column - 1, for the 285" others we indent to the containing line's MSL's level. Return -1 if fail. 286let col = matchend ( line , '^\s*[]})]' ) 287if col > 0 && ! s: IsInStringOrComment ( clnum , col ) 288call cursor ( clnum , col ) 289let bs = strpart ( '(){}[]' , stridx ( ')}]' , line [ col - 1 ]) * 2 , 2 ) 290if searchpair ( escape ( bs [ 0 ], '\[' ), '' , bs [ 1 ], 'bW' , s: skip_expr ) > 0 291if line [ col - 1 ] == ')' && col ( '.' ) != col ( '$' ) - 1 292let ind = virtcol ( '.' ) - 1 293else 294let ind = indent ( s: GetMSL ( line ( '.' ))) 295endif 296endif 297return ind 298endif 299 300" If we have a deindenting keyword, find its match and indent to its level. 301" TODO: this is messy 302if s: Match ( clnum , s: falcon_deindent_keywords ) 303call cursor ( clnum , 1 ) 304if searchpair ( s: end_start_regex , s: end_middle_regex , s: end_end_regex , 'bW' , 305 \s: end_skip_expr ) > 0 306let msl = s: GetMSL ( line ( '.' )) 307let line = getline ( line ( '.' )) 308 309if strpart ( line , 0 , col ( '.' ) - 1 ) =~ '=\s*$' && 310 \strpart ( line , col ( '.' ) - 1 , 2 ) !~ 'do' 311let ind = virtcol ( '.' ) - 1 312elseif getline ( msl ) =~ '=\s*\(#.*\)\=$' 313let ind = indent ( line ( '.' )) 314else 315let ind = indent ( msl ) 316endif 317endif 318return ind 319endif 320 321" If we are in a multi-line string or line-comment, don't do anything to it. 322if s: IsInString ( clnum , matchend ( line , '^\s*' ) + 1 ) 323return indent ( '.' ) 324endif 325 326" Find a non-blank, non-multi-line string line above the current line. 327let lnum = s: PrevNonBlankNonString ( clnum - 1 ) 328 329" If the line is empty and inside a string, use the previous line. 330if line =~ '^\s*$' && lnum != prevnonblank ( clnum - 1 ) 331return indent ( prevnonblank ( clnum )) 332endif 333 334" At the start of the file use zero indent. 335if lnum == 0 336return 0 337endif 338 339" Set up variables for the previous line. 340let line = getline ( lnum ) 341let ind = indent ( lnum ) 342 343" If the previous line ended with a block opening, add a level of indent. 344if s: Match ( lnum , s: block_regex ) 345return indent ( s: GetMSL ( lnum )) + shiftwidth () 346endif 347 348" If it contained hanging closing brackets, find the rightmost one, find its 349" match and indent according to that. 350if line =~ '[[({]' || line =~ '[])}]\s*\%(#.*\)\=$' 351let [ opening , closing ] = s: ExtraBrackets ( lnum ) 352 353if opening . pos != -1 354if opening . type == '(' && searchpair ( '(' , '' , ')' , 'bW' , s: skip_expr ) > 0 355if col ( '.' ) + 1 == col ( '$' ) 356return ind + shiftwidth () 357else 358return virtcol ( '.' ) 359endif 360else 361let nonspace = matchend ( line , '\S' , opening . pos + 1 ) - 1 362return nonspace > 0 ? nonspace : ind + shiftwidth () 363endif 364elseif closing . pos != -1 365call cursor ( lnum , closing . pos + 1 ) 366normal ! % 367 368if s: Match ( line ( '.' ), s: falcon_indent_keywords ) 369return indent ( '.' ) + shiftwidth () 370else 371return indent ( '.' ) 372endif 373else 374call cursor ( clnum , 0 ) " FIXME: column was vcol 375end 376endif 377 378" If the previous line ended with an "end", match that "end"s beginning's 379" indent. 380let col = s: Match ( lnum , '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$' ) 381if col > 0 382call cursor ( lnum , col ) 383if searchpair ( s: end_start_regex , '' , s: end_end_regex , 'bW' , 384 \s: end_skip_expr ) > 0 385let n = line ( '.' ) 386let ind = indent ( '.' ) 387let msl = s: GetMSL ( n ) 388if msl != n 389let ind = indent ( msl ) 390end 391return ind 392endif 393end 394 395let col = s: Match ( lnum , s: falcon_indent_keywords ) 396if col > 0 397call cursor ( lnum , col ) 398let ind = virtcol ( '.' ) - 1 + shiftwidth () 399" TODO: make this better (we need to count them) (or, if a searchpair 400" fails, we know that something is lacking an end and thus we indent a 401" level 402if s: Match ( lnum , s: end_end_regex ) 403let ind = indent ( '.' ) 404endif 405return ind 406endif 407 408" Set up variables to use and search for MSL to the previous line. 409let p_lnum = lnum 410let lnum = s: GetMSL ( lnum ) 411 412" If the previous line wasn't a MSL and is continuation return its indent. 413" TODO: the || s:IsInString() thing worries me a bit. 414if p_lnum != lnum 415if s: Match ( p_lnum , s: non_bracket_continuation_regex ) || s: IsInString ( p_lnum , strlen ( line )) 416return ind 417endif 418endif 419 420" Set up more variables, now that we know we wasn't continuation bound. 421let line = getline ( lnum ) 422let msl_ind = indent ( lnum ) 423 424" If the MSL line had an indenting keyword in it, add a level of indent. 425" TODO: this does not take into account contrived things such as 426" module Foo; class Bar; end 427if s: Match ( lnum , s: falcon_indent_keywords ) 428let ind = msl_ind + shiftwidth () 429if s: Match ( lnum , s: end_end_regex ) 430let ind = ind - shiftwidth () 431endif 432return ind 433endif 434 435" If the previous line ended with [*+/.,-=], but wasn't a block ending or a 436" closing bracket, indent one extra level. 437if s: Match ( lnum , s: non_bracket_continuation_regex ) && ! s: Match ( lnum , '^\s*\([\])}]\|end\)' ) 438if lnum == p_lnum 439let ind = msl_ind + shiftwidth () 440else 441let ind = msl_ind 442endif 443return ind 444endif 445 446return ind 447endfunction 448 449" }}}1 450 451let & cpo = s: cpo_save 452unlet s: cpo_save 453 454" vim: set sw=4 sts=4 et tw=80 :