robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c3defb4b45
master
1" Vim indent file 2" Language: Ruby 3" Maintainer: Andrew Radev <andrey.radev@gmail.com> 4" Previous Maintainer: Nikolai Weibull <now at bitwi.se> 5" URL: https://github.com/vim-ruby/vim-ruby 6" Last Change: 2023 Dec 22 7 8" 0. Initialization {{{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 17if ! exists ( 'g:ruby_indent_access_modifier_style' ) 18" Possible values: "normal", "indent", "outdent" 19let g: ruby_indent_access_modifier_style = 'normal' 20endif 21 22if ! exists ( 'g:ruby_indent_assignment_style' ) 23" Possible values: "variable", "hanging" 24let g: ruby_indent_assignment_style = 'hanging' 25endif 26 27if ! exists ( 'g:ruby_indent_block_style' ) 28" Possible values: "expression", "do" 29let g: ruby_indent_block_style = 'do' 30endif 31 32if ! exists ( 'g:ruby_indent_hanging_elements' ) 33" Non-zero means hanging indents are enabled, zero means disabled 34let g: ruby_indent_hanging_elements = 1 35endif 36 37setlocal nosmartindent 38 39" Now, set up our indentation expression and keys that trigger it. 40setlocal indentexpr = GetRubyIndent (v:lnum) 41setlocal indentkeys = 0{,0},0),0],!^F,o,O,e,:,. 42setlocal indentkeys += =end,=else,=elsif,=when,=in\ ,=ensure,=rescue,==begin,==end 43setlocal indentkeys += =private,=protected,=public 44 45let b: undo_indent = "setlocal indentexpr< indentkeys< smartindent<" 46 47" Only define the function once. 48if exists ( "*GetRubyIndent" ) 49finish 50endif 51 52let s: cpo_save = & cpo 53set cpo & vim 54 55" 1. Variables {{{1 56" ============ 57 58" Syntax group names that are strings. 59let s: syng_string = 60 \[ 'String' , 'Interpolation' , 'InterpolationDelimiter' , 'StringEscape' ] 61 62" Syntax group names that are strings or documentation. 63let s: syng_stringdoc = s: syng_string + [ 'Documentation' ] 64 65" Syntax group names that are or delimit strings/symbols/regexes or are comments. 66let s: syng_strcom = s: syng_stringdoc + [ 67 \'Character' , 68 \'Comment' , 69 \'HeredocDelimiter' , 70 \'PercentRegexpDelimiter' , 71 \'PercentStringDelimiter' , 72 \'PercentSymbolDelimiter' , 73 \'Regexp' , 74 \'RegexpCharClass' , 75 \'RegexpDelimiter' , 76 \'RegexpEscape' , 77 \'StringDelimiter' , 78 \'Symbol' , 79 \'SymbolDelimiter' , 80 \] 81 82" Expression used to check whether we should skip a match with searchpair(). 83let s: skip_expr = 84 \'index(map(' . string ( s: syng_strcom ) . ',"hlID(''ruby''.v:val)"), synID(line("."),col("."),1)) >= 0' 85 86" Regex used for words that, at the start of a line, add a level of indent. 87let s: ruby_indent_keywords = 88 \'^\s*\zs\<\%(module\|class\|if\|for' . 89 \'\|while\|until\|else\|elsif\|case\|when\|in\|unless\|begin\|ensure\|rescue' . 90 \'\|\%(\K\k*[!?]\?\s\+\)\=def\):\@!\>' . 91 \'\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' . 92 \'\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>' 93 94" Def without an end clause: def method_call(...) = <expression> 95let s: ruby_endless_def = 96 \'\<def\s\+\%(\k\+\.\)\=\%(\k\+[=!?]\=\|' . 97 \'[-+*/%&^<>~!]\|' . 98 \'\*\*\|>>\|<<\|' . 99 \'===\?\|\!=\|=\~\|\!\~\|' . 100 \'<=>\|<=\|>=\|' . 101 \'[-+!\~]@\|\[\]' . 102 \'\)\%((.*)\|\s\)\s*=' 103 104" Regex used for words that, at the start of a line, remove a level of indent. 105let s: ruby_deindent_keywords = 106 \'^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|in\|end\):\@!\>' 107 108" Regex that defines the start-match for the 'end' keyword. 109"let s:end_start_regex = '\%(^\|[^.]\)\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\|do\)\>' 110" TODO: the do here should be restricted somewhat (only at end of line)? 111let s: end_start_regex = 112 \'\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' . 113 \'\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' . 114 \'\|\%(\K\k*[!?]\?\s\+\)\=def\):\@!\>' . 115 \'\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>' 116 117" Regex that defines the middle-match for the 'end' keyword. 118let s: end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|\%(\%(^\|;\)\s*\)\@<=\<in\|elsif\):\@!\>' 119 120" Regex that defines the end-match for the 'end' keyword. 121let s: end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>' 122 123" Expression used for searchpair() call for finding a match for an 'end' keyword. 124function ! s: EndSkipExpr () 125if eval ( s: skip_expr ) 126return 1 127elseif expand ( '<cword>' ) == 'do' 128 \&& getline ( "." ) =~ '^\s*\<\(while\|until\|for\):\@!\>' 129return 1 130elseif getline ( '.' ) =~ s: ruby_endless_def 131return 1 132elseif getline ( '.' ) =~ '\<def\s\+\k\+[!?]\=([^)]*$' 133" Then it's a `def method(` with a possible `) =` later 134call search ( '\<def\s\+\k\+\zs(' , 'W' , line ( '.' )) 135normal ! % 136return getline ( '.' ) =~ ')\s*=' 137else 138return 0 139endif 140endfunction 141 142let s: end_skip_expr = function ( 's:EndSkipExpr' ) 143 144" Regex that defines continuation lines, not including (, {, or [. 145let s: non_bracket_continuation_regex = 146 \'\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$' 147 148" Regex that defines continuation lines. 149let s: continuation_regex = 150 \'\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$' 151 152" Regex that defines continuable keywords 153let s: continuable_regex = 154 \'\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' . 155 \'\<\%(if\|for\|while\|until\|unless\):\@!\>' 156 157" Regex that defines bracket continuations 158let s: bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$' 159 160" Regex that defines dot continuations 161let s: dot_continuation_regex = '%\@<!\.\s*\%(#.*\)\=$' 162 163" Regex that defines backslash continuations 164let s: backslash_continuation_regex = '%\@<!\\\s*$' 165 166" Regex that defines end of bracket continuation followed by another continuation 167let s: bracket_switch_continuation_regex = '^\([^(]\+\zs).\+\)\+' . s: continuation_regex 168 169" Regex that defines the first part of a splat pattern 170let s: splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$' 171 172" Regex that describes all indent access modifiers 173let s: access_modifier_regex = '\C^\s*\%(public\|protected\|private\)\s*\%(#.*\)\=$' 174 175" Regex that describes the indent access modifiers (excludes public) 176let s: indent_access_modifier_regex = '\C^\s*\%(protected\|private\)\s*\%(#.*\)\=$' 177 178" Regex that defines blocks. 179" 180" Note that there's a slight problem with this regex and s:continuation_regex. 181" Code like this will be matched by both: 182" 183" method_call do |(a, b)| 184" 185" The reason is that the pipe matches a hanging "|" operator. 186" 187let s: block_regex = 188 \'\%(\<do:\@!\>\|%\@<!{\)\s*\%(|[^|]*|\)\=\s*\%(#.*\)\=$' 189 190let s: block_continuation_regex = '^\s*[^])}\t ].*' . s: block_regex 191 192" Regex that describes a leading operator (only a method call's dot for now) 193let s: leading_operator_regex = '^\s*\%(&\=\.\)' 194 195" 2. GetRubyIndent Function {{{1 196" ========================= 197 198function ! GetRubyIndent (...) abort 199" 2.1. Setup {{{2 200" ---------- 201 202let indent_info = {} 203 204" The value of a single shift-width 205if exists ( '*shiftwidth' ) 206let indent_info . sw = shiftwidth () 207else 208let indent_info . sw = & sw 209endif 210 211" For the current line, use the first argument if given, else v:lnum 212let indent_info . clnum = a: 0 ? a: 1 : v: lnum 213let indent_info . cline = getline ( indent_info . clnum ) 214 215" Set up variables for restoring position in file. Could use clnum here. 216let indent_info . col = col ( '.' ) 217 218" 2.2. Work on the current line {{{2 219" ----------------------------- 220let indent_callback_names = [ 221 \'s:AccessModifier' , 222 \'s:ClosingBracketOnEmptyLine' , 223 \'s:BlockComment' , 224 \'s:DeindentingKeyword' , 225 \'s:MultilineStringOrLineComment' , 226 \'s:ClosingHeredocDelimiter' , 227 \'s:LeadingOperator' , 228 \] 229 230for callback_name in indent_callback_names 231" Decho "Running: ".callback_name 232let indent = call ( function ( callback_name ), [ indent_info ]) 233 234if indent >= 0 235" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) 236return indent 237endif 238endfor 239 240" 2.3. Work on the previous line. {{{2 241" ------------------------------- 242 243" Special case: we don't need the real s:PrevNonBlankNonString for an empty 244" line inside a string. And that call can be quite expensive in that 245" particular situation. 246let indent_callback_names = [ 247 \'s:EmptyInsideString' , 248 \] 249 250for callback_name in indent_callback_names 251" Decho "Running: ".callback_name 252let indent = call ( function ( callback_name ), [ indent_info ]) 253 254if indent >= 0 255" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) 256return indent 257endif 258endfor 259 260" Previous line number 261let indent_info . plnum = s: PrevNonBlankNonString ( indent_info . clnum - 1 ) 262let indent_info . pline = getline ( indent_info . plnum ) 263 264let indent_callback_names = [ 265 \'s:StartOfFile' , 266 \'s:AfterAccessModifier' , 267 \'s:ContinuedLine' , 268 \'s:AfterBlockOpening' , 269 \'s:AfterHangingSplat' , 270 \'s:AfterUnbalancedBracket' , 271 \'s:AfterLeadingOperator' , 272 \'s:AfterEndKeyword' , 273 \'s:AfterIndentKeyword' , 274 \] 275 276for callback_name in indent_callback_names 277" Decho "Running: ".callback_name 278let indent = call ( function ( callback_name ), [ indent_info ]) 279 280if indent >= 0 281" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) 282return indent 283endif 284endfor 285 286" 2.4. Work on the MSL line. {{{2 287" -------------------------- 288let indent_callback_names = [ 289 \'s:PreviousNotMSL' , 290 \'s:IndentingKeywordInMSL' , 291 \'s:ContinuedHangingOperator' , 292 \] 293 294" Most Significant line based on the previous one -- in case it's a 295" continuation of something above 296let indent_info . plnum_msl = s: GetMSL ( indent_info . plnum ) 297 298for callback_name in indent_callback_names 299" Decho "Running: ".callback_name 300let indent = call ( function ( callback_name ), [ indent_info ]) 301 302if indent >= 0 303" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) 304return indent 305endif 306endfor 307 308" }}}2 309 310" By default, just return the previous line's indent 311" Decho "Default case matched" 312return indent ( indent_info . plnum ) 313endfunction 314 315" 3. Indenting Logic Callbacks {{{1 316" ============================ 317 318function ! s: AccessModifier ( cline_info ) abort 319let info = a: cline_info 320 321" If this line is an access modifier keyword, align according to the closest 322" class declaration. 323if g: ruby_indent_access_modifier_style == 'indent' 324if s: Match ( info . clnum , s: access_modifier_regex ) 325let class_lnum = s: FindContainingClass () 326if class_lnum > 0 327return indent ( class_lnum ) + info . sw 328endif 329endif 330elseif g: ruby_indent_access_modifier_style == 'outdent' 331if s: Match ( info . clnum , s: access_modifier_regex ) 332let class_lnum = s: FindContainingClass () 333if class_lnum > 0 334return indent ( class_lnum ) 335endif 336endif 337endif 338 339return -1 340endfunction 341 342function ! s: ClosingBracketOnEmptyLine ( cline_info ) abort 343let info = a: cline_info 344 345" If we got a closing bracket on an empty line, find its match and indent 346" according to it. For parentheses we indent to its column - 1, for the 347" others we indent to the containing line's MSL's level. Return -1 if fail. 348let col = matchend ( info . cline , '^\s*[]})]' ) 349 350if col > 0 && ! s: IsInStringOrComment ( info . clnum , col ) 351call cursor ( info . clnum , col ) 352let closing_bracket = info . cline [ col - 1 ] 353let bracket_pair = strpart ( '(){}[]' , stridx ( ')}]' , closing_bracket ) * 2 , 2 ) 354 355if searchpair ( escape ( bracket_pair [ 0 ], '\[' ), '' , bracket_pair [ 1 ], 'bW' , s: skip_expr ) > 0 356if closing_bracket == ')' && col ( '.' ) != col ( '$' ) - 1 357if g: ruby_indent_hanging_elements 358let ind = virtcol ( '.' ) - 1 359else 360let ind = indent ( line ( '.' )) 361end 362elseif g: ruby_indent_block_style == 'do' 363let ind = indent ( line ( '.' )) 364else " g:ruby_indent_block_style == 'expression' 365let ind = indent ( s: GetMSL ( line ( '.' ))) 366endif 367endif 368 369return ind 370endif 371 372return -1 373endfunction 374 375function ! s: BlockComment ( cline_info ) abort 376" If we have a =begin or =end set indent to first column. 377if match ( a: cline_info . cline , '^\s*\%(=begin\|=end\)$' ) != -1 378return 0 379endif 380return -1 381endfunction 382 383function ! s: DeindentingKeyword ( cline_info ) abort 384let info = a: cline_info 385 386" If we have a deindenting keyword, find its match and indent to its level. 387" TODO: this is messy 388if s: Match ( info . clnum , s: ruby_deindent_keywords ) 389call cursor ( info . clnum , 1 ) 390 391if searchpair ( s: end_start_regex , s: end_middle_regex , s: end_end_regex , 'bW' , 392 \s: end_skip_expr ) > 0 393let msl = s: GetMSL ( line ( '.' )) 394let line = getline ( line ( '.' )) 395 396if s: IsAssignment ( line , col ( '.' )) && 397 \strpart ( line , col ( '.' ) - 1 , 2 ) !~ 'do' 398" assignment to case/begin/etc, on the same line 399if g: ruby_indent_assignment_style == 'hanging' 400" hanging indent 401let ind = virtcol ( '.' ) - 1 402else 403" align with variable 404let ind = indent ( line ( '.' )) 405endif 406elseif g: ruby_indent_block_style == 'do' 407" align to line of the "do", not to the MSL 408let ind = indent ( line ( '.' )) 409elseif getline ( msl ) =~ '=\s*\(#.*\)\=$' 410" in the case of assignment to the MSL, align to the starting line, 411" not to the MSL 412let ind = indent ( line ( '.' )) 413else 414" align to the MSL 415let ind = indent ( msl ) 416endif 417endif 418return ind 419endif 420 421return -1 422endfunction 423 424function ! s: MultilineStringOrLineComment ( cline_info ) abort 425let info = a: cline_info 426 427" If we are in a multi-line string or line-comment, don't do anything to it. 428if s: IsInStringOrDocumentation ( info . clnum , matchend ( info . cline , '^\s*' ) + 1 ) 429return indent ( info . clnum ) 430endif 431return -1 432endfunction 433 434function ! s: ClosingHeredocDelimiter ( cline_info ) abort 435let info = a: cline_info 436 437" If we are at the closing delimiter of a "<<" heredoc-style string, set the 438" indent to 0. 439if info . cline =~ '^\k\+\s*$' 440 \&& s: IsInStringDelimiter ( info . clnum , 1 ) 441 \&& search ( '\V<<' . info . cline , 'nbW' ) > 0 442return 0 443endif 444 445return -1 446endfunction 447 448function ! s: LeadingOperator ( cline_info ) abort 449" If the current line starts with a leading operator, add a level of indent. 450if s: Match ( a: cline_info . clnum , s: leading_operator_regex ) 451return indent ( s: GetMSL ( a: cline_info . clnum )) + a: cline_info . sw 452endif 453return -1 454endfunction 455 456function ! s: EmptyInsideString ( pline_info ) abort 457" If the line is empty and inside a string (the previous line is a string, 458" too), use the previous line's indent 459let info = a: pline_info 460 461let plnum = prevnonblank ( info . clnum - 1 ) 462let pline = getline ( plnum ) 463 464if info . cline =~ '^\s*$' 465 \&& s: IsInStringOrComment ( plnum , 1 ) 466 \&& s: IsInStringOrComment ( plnum , strlen ( pline )) 467return indent ( plnum ) 468endif 469return -1 470endfunction 471 472function ! s: StartOfFile ( pline_info ) abort 473" At the start of the file use zero indent. 474if a: pline_info . plnum == 0 475return 0 476endif 477return -1 478endfunction 479 480function ! s: AfterAccessModifier ( pline_info ) abort 481let info = a: pline_info 482 483if g: ruby_indent_access_modifier_style == 'indent' 484" If the previous line was a private/protected keyword, add a 485" level of indent. 486if s: Match ( info . plnum , s: indent_access_modifier_regex ) 487return indent ( info . plnum ) + info . sw 488endif 489elseif g: ruby_indent_access_modifier_style == 'outdent' 490" If the previous line was a private/protected/public keyword, add 491" a level of indent, since the keyword has been out-dented. 492if s: Match ( info . plnum , s: access_modifier_regex ) 493return indent ( info . plnum ) + info . sw 494endif 495endif 496return -1 497endfunction 498 499" Example: 500" 501" if foo || bar || 502" baz || bing 503" puts "foo" 504" end 505" 506function ! s: ContinuedLine ( pline_info ) abort 507let info = a: pline_info 508 509let col = s: Match ( info . plnum , s: ruby_indent_keywords ) 510if s: Match ( info . plnum , s: continuable_regex ) && 511 \s: Match ( info . plnum , s: continuation_regex ) 512if col > 0 && s: IsAssignment ( info . pline , col ) 513if g: ruby_indent_assignment_style == 'hanging' 514" hanging indent 515let ind = col - 1 516else 517" align with variable 518let ind = indent ( info . plnum ) 519endif 520else 521let ind = indent ( s: GetMSL ( info . plnum )) 522endif 523return ind + info . sw + info . sw 524endif 525return -1 526endfunction 527 528function ! s: AfterBlockOpening ( pline_info ) abort 529let info = a: pline_info 530 531" If the previous line ended with a block opening, add a level of indent. 532if s: Match ( info . plnum , s: block_regex ) 533if g: ruby_indent_block_style == 'do' 534" don't align to the msl, align to the "do" 535let ind = indent ( info . plnum ) + info . sw 536else 537let plnum_msl = s: GetMSL ( info . plnum ) 538 539if getline ( plnum_msl ) =~ '=\s*\(#.*\)\=$' 540" in the case of assignment to the msl, align to the starting line, 541" not to the msl 542let ind = indent ( info . plnum ) + info . sw 543else 544let ind = indent ( plnum_msl ) + info . sw 545endif 546endif 547 548return ind 549endif 550 551return -1 552endfunction 553 554function ! s: AfterLeadingOperator ( pline_info ) abort 555" If the previous line started with a leading operator, use its MSL's level 556" of indent 557if s: Match ( a: pline_info . plnum , s: leading_operator_regex ) 558return indent ( s: GetMSL ( a: pline_info . plnum )) 559endif 560return -1 561endfunction 562 563function ! s: AfterHangingSplat ( pline_info ) abort 564let info = a: pline_info 565 566" If the previous line ended with the "*" of a splat, add a level of indent 567if info . pline =~ s: splat_regex 568return indent ( info . plnum ) + info . sw 569endif 570return -1 571endfunction 572 573function ! s: AfterUnbalancedBracket ( pline_info ) abort 574let info = a: pline_info 575 576" If the previous line contained unclosed opening brackets and we are still 577" in them, find the rightmost one and add indent depending on the bracket 578" type. 579" 580" If it contained hanging closing brackets, find the rightmost one, find its 581" match and indent according to that. 582if info . pline =~ '[[({]' || info . pline =~ '[])}]\s*\%(#.*\)\=$' 583let [ opening , closing ] = s: ExtraBrackets ( info . plnum ) 584 585if opening . pos != -1 586if ! g: ruby_indent_hanging_elements 587return indent ( info . plnum ) + info . sw 588elseif opening . type == '(' && searchpair ( '(' , '' , ')' , 'bW' , s: skip_expr ) > 0 589if col ( '.' ) + 1 == col ( '$' ) 590return indent ( info . plnum ) + info . sw 591else 592return virtcol ( '.' ) 593endif 594else 595let nonspace = matchend ( info . pline , '\S' , opening . pos + 1 ) - 1 596return nonspace > 0 ? nonspace : indent ( info . plnum ) + info . sw 597endif 598elseif closing . pos != -1 599call cursor ( info . plnum , closing . pos + 1 ) 600normal ! % 601 602if strpart ( info . pline , closing . pos ) =~ '^)\s*=' 603" special case: the closing `) =` of an endless def 604return indent ( s: GetMSL ( line ( '.' ))) 605endif 606 607if s: Match ( line ( '.' ), s: ruby_indent_keywords ) 608return indent ( '.' ) + info . sw 609else 610return indent ( s: GetMSL ( line ( '.' ))) 611endif 612else 613call cursor ( info . clnum , info . col ) 614end 615endif 616 617return -1 618endfunction 619 620function ! s: AfterEndKeyword ( pline_info ) abort 621let info = a: pline_info 622" If the previous line ended with an "end", match that "end"s beginning's 623" indent. 624let col = s: Match ( info . plnum , '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$' ) 625if col > 0 626call cursor ( info . plnum , col ) 627if searchpair ( s: end_start_regex , '' , s: end_end_regex , 'bW' , 628 \s: end_skip_expr ) > 0 629let n = line ( '.' ) 630let ind = indent ( '.' ) 631let msl = s: GetMSL ( n ) 632if msl != n 633let ind = indent ( msl ) 634end 635return ind 636endif 637end 638return -1 639endfunction 640 641function ! s: AfterIndentKeyword ( pline_info ) abort 642let info = a: pline_info 643let col = s: Match ( info . plnum , s: ruby_indent_keywords ) 644 645if col > 0 && s: Match ( info . plnum , s: ruby_endless_def ) <= 0 646call cursor ( info . plnum , col ) 647let ind = virtcol ( '.' ) - 1 + info . sw 648" TODO: make this better (we need to count them) (or, if a searchpair 649" fails, we know that something is lacking an end and thus we indent a 650" level 651if s: Match ( info . plnum , s: end_end_regex ) 652let ind = indent ( '.' ) 653elseif s: IsAssignment ( info . pline , col ) 654if g: ruby_indent_assignment_style == 'hanging' 655" hanging indent 656let ind = col + info . sw - 1 657else 658" align with variable 659let ind = indent ( info . plnum ) + info . sw 660endif 661endif 662return ind 663endif 664 665return -1 666endfunction 667 668function ! s: PreviousNotMSL ( msl_info ) abort 669let info = a: msl_info 670 671" If the previous line wasn't a MSL 672if info . plnum != info . plnum_msl 673" If previous line ends bracket and begins non-bracket continuation decrease indent by 1. 674if s: Match ( info . plnum , s: bracket_switch_continuation_regex ) 675" TODO (2016-10-07) Wrong/unused? How could it be "1"? 676return indent ( info . plnum ) - 1 677" If previous line is a continuation return its indent. 678elseif s: Match ( info . plnum , s: non_bracket_continuation_regex ) 679return indent ( info . plnum ) 680endif 681endif 682 683return -1 684endfunction 685 686function ! s: IndentingKeywordInMSL ( msl_info ) abort 687let info = a: msl_info 688" If the MSL line had an indenting keyword in it, add a level of indent. 689" TODO: this does not take into account contrived things such as 690" module Foo; class Bar; end 691let col = s: Match ( info . plnum_msl , s: ruby_indent_keywords ) 692if col > 0 && s: Match ( info . plnum_msl , s: ruby_endless_def ) <= 0 693let ind = indent ( info . plnum_msl ) + info . sw 694if s: Match ( info . plnum_msl , s: end_end_regex ) 695let ind = ind - info . sw 696elseif s: IsAssignment ( getline ( info . plnum_msl ), col ) 697if g: ruby_indent_assignment_style == 'hanging' 698" hanging indent 699let ind = col + info . sw - 1 700else 701" align with variable 702let ind = indent ( info . plnum_msl ) + info . sw 703endif 704endif 705return ind 706endif 707return -1 708endfunction 709 710function ! s: ContinuedHangingOperator ( msl_info ) abort 711let info = a: msl_info 712 713" If the previous line ended with [*+/.,-=], but wasn't a block ending or a 714" closing bracket, indent one extra level. 715if s: Match ( info . plnum_msl , s: non_bracket_continuation_regex ) && ! s: Match ( info . plnum_msl , '^\s*\([\])}]\|end\)' ) 716if info . plnum_msl == info . plnum 717let ind = indent ( info . plnum_msl ) + info . sw 718else 719let ind = indent ( info . plnum_msl ) 720endif 721return ind 722endif 723 724return -1 725endfunction 726 727" 4. Auxiliary Functions {{{1 728" ====================== 729 730function ! s: IsInRubyGroup ( groups , lnum , col ) abort 731let ids = map ( copy ( a: groups ), 'hlID("ruby".v:val)' ) 732return index ( ids , synID ( a: lnum , a: col , 1 )) >= 0 733endfunction 734 735" Check if the character at lnum:col is inside a string, comment, or is ascii. 736function ! s: IsInStringOrComment ( lnum , col ) abort 737return s: IsInRubyGroup ( s: syng_strcom , a: lnum , a: col ) 738endfunction 739 740" Check if the character at lnum:col is inside a string. 741function ! s: IsInString ( lnum , col ) abort 742return s: IsInRubyGroup ( s: syng_string , a: lnum , a: col ) 743endfunction 744 745" Check if the character at lnum:col is inside a string or documentation. 746function ! s: IsInStringOrDocumentation ( lnum , col ) abort 747return s: IsInRubyGroup ( s: syng_stringdoc , a: lnum , a: col ) 748endfunction 749 750" Check if the character at lnum:col is inside a string delimiter 751function ! s: IsInStringDelimiter ( lnum , col ) abort 752return s: IsInRubyGroup ( 753 \[ 'HeredocDelimiter' , 'PercentStringDelimiter' , 'StringDelimiter' ], 754 \a: lnum , a: col 755 \) 756endfunction 757 758function ! s: IsAssignment ( str , pos ) abort 759return strpart ( a: str , 0 , a: pos - 1 ) =~ '=\s*$' 760endfunction 761 762" Find line above 'lnum' that isn't empty, in a comment, or in a string. 763function ! s: PrevNonBlankNonString ( lnum ) abort 764let in_block = 0 765let lnum = prevnonblank ( a: lnum ) 766while lnum > 0 767" Go in and out of blocks comments as necessary. 768" If the line isn't empty (with opt. comment) or in a string, end search. 769let line = getline ( lnum ) 770if line =~ '^=begin' 771if in_block 772let in_block = 0 773else 774break 775endif 776elseif ! in_block && line =~ '^=end' 777let in_block = 1 778elseif ! in_block && line !~ '^\s*#.*$' && ! ( s: IsInStringOrComment ( lnum , 1 ) 779 \&& s: IsInStringOrComment ( lnum , strlen ( line ))) 780break 781endif 782let lnum = prevnonblank ( lnum - 1 ) 783endwhile 784return lnum 785endfunction 786 787" Find line above 'lnum' that started the continuation 'lnum' may be part of. 788function ! s: GetMSL ( lnum ) abort 789" Start on the line we're at and use its indent. 790let msl = a: lnum 791let lnum = s: PrevNonBlankNonString ( a: lnum - 1 ) 792while lnum > 0 793" If we have a continuation line, or we're in a string, use line as MSL. 794" Otherwise, terminate search as we have found our MSL already. 795let line = getline ( lnum ) 796 797if ! s: Match ( msl , s: backslash_continuation_regex ) && 798 \s: Match ( lnum , s: backslash_continuation_regex ) 799" If the current line doesn't end in a backslash, but the previous one 800" does, look for that line's msl 801" 802" Example: 803" foo = "bar" \ 804" "baz" 805" 806let msl = lnum 807elseif s: Match ( msl , s: leading_operator_regex ) 808" If the current line starts with a leading operator, keep its indent 809" and keep looking for an MSL. 810let msl = lnum 811elseif s: Match ( lnum , s: splat_regex ) 812" If the above line looks like the "*" of a splat, use the current one's 813" indentation. 814" 815" Example: 816" Hash[* 817" method_call do 818" something 819" 820return msl 821elseif s: Match ( lnum , s: non_bracket_continuation_regex ) && 822 \s: Match ( msl , s: non_bracket_continuation_regex ) 823" If the current line is a non-bracket continuation and so is the 824" previous one, keep its indent and continue looking for an MSL. 825" 826" Example: 827" method_call one, 828" two, 829" three 830" 831let msl = lnum 832elseif s: Match ( lnum , s: dot_continuation_regex ) && 833 \( s: Match ( msl , s: bracket_continuation_regex ) || s: Match ( msl , s: block_continuation_regex )) 834" If the current line is a bracket continuation or a block-starter, but 835" the previous is a dot, keep going to see if the previous line is the 836" start of another continuation. 837" 838" Example: 839" parent. 840" method_call { 841" three 842" 843let msl = lnum 844elseif s: Match ( lnum , s: non_bracket_continuation_regex ) && 845 \( s: Match ( msl , s: bracket_continuation_regex ) || s: Match ( msl , s: block_continuation_regex )) 846" If the current line is a bracket continuation or a block-starter, but 847" the previous is a non-bracket one, respect the previous' indentation, 848" and stop here. 849" 850" Example: 851" method_call one, 852" two { 853" three 854" 855return lnum 856elseif s: Match ( lnum , s: bracket_continuation_regex ) && 857 \( s: Match ( msl , s: bracket_continuation_regex ) || s: Match ( msl , s: block_continuation_regex )) 858" If both lines are bracket continuations (the current may also be a 859" block-starter), use the current one's and stop here 860" 861" Example: 862" method_call( 863" other_method_call( 864" foo 865return msl 866elseif s: Match ( lnum , s: block_regex ) && 867 \! s: Match ( msl , s: continuation_regex ) && 868 \! s: Match ( msl , s: block_continuation_regex ) 869" If the previous line is a block-starter and the current one is 870" mostly ordinary, use the current one as the MSL. 871" 872" Example: 873" method_call do 874" something 875" something_else 876return msl 877else 878let col = match ( line , s: continuation_regex ) + 1 879if ( col > 0 && ! s: IsInStringOrComment ( lnum , col )) 880 \|| s: IsInString ( lnum , strlen ( line )) 881let msl = lnum 882else 883break 884endif 885endif 886 887let lnum = s: PrevNonBlankNonString ( lnum - 1 ) 888endwhile 889return msl 890endfunction 891 892" Check if line 'lnum' has more opening brackets than closing ones. 893function ! s: ExtraBrackets ( lnum ) abort 894let opening = { 'parentheses' : [], 'braces' : [], 'brackets' : []} 895let closing = { 'parentheses' : [], 'braces' : [], 'brackets' : []} 896 897let line = getline ( a: lnum ) 898let pos = match ( line , '[][(){}]' , 0 ) 899 900" Save any encountered opening brackets, and remove them once a matching 901" closing one has been found. If a closing bracket shows up that doesn't 902" close anything, save it for later. 903while pos != -1 904if ! s: IsInStringOrComment ( a: lnum , pos + 1 ) 905if line [ pos ] == '(' 906call add ( opening . parentheses , { 'type' : '(' , 'pos' : pos }) 907elseif line [ pos ] == ')' 908if empty ( opening . parentheses ) 909call add ( closing . parentheses , { 'type' : ')' , 'pos' : pos }) 910else 911let opening . parentheses = opening . parentheses [ 0 : -2 ] 912endif 913elseif line [ pos ] == '{' 914call add ( opening . braces , { 'type' : '{' , 'pos' : pos }) 915elseif line [ pos ] == '}' 916if empty ( opening . braces ) 917call add ( closing . braces , { 'type' : '}' , 'pos' : pos }) 918else 919let opening . braces = opening . braces [ 0 : -2 ] 920endif 921elseif line [ pos ] == '[' 922call add ( opening . brackets , { 'type' : '[' , 'pos' : pos }) 923elseif line [ pos ] == ']' 924if empty ( opening . brackets ) 925call add ( closing . brackets , { 'type' : ']' , 'pos' : pos }) 926else 927let opening . brackets = opening . brackets [ 0 : -2 ] 928endif 929endif 930endif 931 932let pos = match ( line , '[][(){}]' , pos + 1 ) 933endwhile 934 935" Find the rightmost brackets, since they're the ones that are important in 936" both opening and closing cases 937let rightmost_opening = { 'type' : '(' , 'pos' : -1 } 938let rightmost_closing = { 'type' : ')' , 'pos' : -1 } 939 940for opening in opening . parentheses + opening . braces + opening . brackets 941if opening . pos > rightmost_opening . pos 942let rightmost_opening = opening 943endif 944endfor 945 946for closing in closing . parentheses + closing . braces + closing . brackets 947if closing . pos > rightmost_closing . pos 948let rightmost_closing = closing 949endif 950endfor 951 952return [ rightmost_opening , rightmost_closing ] 953endfunction 954 955function ! s: Match ( lnum , regex ) abort 956let line = getline ( a: lnum ) 957let offset = match ( line , '\C' . a: regex ) 958let col = offset + 1 959 960while offset > -1 && s: IsInStringOrComment ( a: lnum , col ) 961let offset = match ( line , '\C' . a: regex , offset + 1 ) 962let col = offset + 1 963endwhile 964 965if offset > -1 966return col 967else 968return 0 969endif 970endfunction 971 972" Locates the containing class/module's definition line, ignoring nested classes 973" along the way. 974" 975function ! s: FindContainingClass () abort 976let saved_position = getpos ( '.' ) 977 978while searchpair ( s: end_start_regex , s: end_middle_regex , s: end_end_regex , 'bW' , 979 \s: end_skip_expr ) > 0 980if expand ( '<cword>' ) =~# '\<class\|module\>' 981let found_lnum = line ( '.' ) 982call setpos ( '.' , saved_position ) 983return found_lnum 984endif 985endwhile 986 987call setpos ( '.' , saved_position ) 988return 0 989endfunction 990 991" }}}1 992 993let & cpo = s: cpo_save 994unlet s: cpo_save 995 996" vim:set sw=2 sts=2 ts=8 et: