comparison runtime/indent/ruby.vim @ 15512:f0f06837a699

Update runtime files. commit https://github.com/vim/vim/commit/d09091d4955c5f41de69928f2db85611ed54ed23 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jan 17 16:07:22 2019 +0100 Update runtime files.
author Bram Moolenaar <Bram@vim.org>
date Thu, 17 Jan 2019 16:15:08 +0100
parents 63b0b7b79b25
children 70ce979e76bc
comparison
equal deleted inserted replaced
15511:f41122780189 15512:f0f06837a699
1 " Vim indent file 1 " Vim indent file
2 " Language: Ruby 2 " Language: Ruby
3 " Maintainer: Nikolai Weibull <now at bitwi.se> 3 " Maintainer: Andrew Radev <andrey.radev@gmail.com>
4 " Previous Maintainer: Nikolai Weibull <now at bitwi.se>
4 " URL: https://github.com/vim-ruby/vim-ruby 5 " URL: https://github.com/vim-ruby/vim-ruby
5 " Release Coordinator: Doug Kearns <dougkearns@gmail.com> 6 " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
7 " Last Change: 2019 Jan 06
6 8
7 " 0. Initialization {{{1 9 " 0. Initialization {{{1
8 " ================= 10 " =================
9 11
10 " Only load this indent file when no other was loaded. 12 " Only load this indent file when no other was loaded.
16 if !exists('g:ruby_indent_access_modifier_style') 18 if !exists('g:ruby_indent_access_modifier_style')
17 " Possible values: "normal", "indent", "outdent" 19 " Possible values: "normal", "indent", "outdent"
18 let g:ruby_indent_access_modifier_style = 'normal' 20 let g:ruby_indent_access_modifier_style = 'normal'
19 endif 21 endif
20 22
23 if !exists('g:ruby_indent_assignment_style')
24 " Possible values: "variable", "hanging"
25 let g:ruby_indent_assignment_style = 'hanging'
26 endif
27
21 if !exists('g:ruby_indent_block_style') 28 if !exists('g:ruby_indent_block_style')
22 " Possible values: "expression", "do" 29 " Possible values: "expression", "do"
23 let g:ruby_indent_block_style = 'expression' 30 let g:ruby_indent_block_style = 'expression'
24 endif 31 endif
25 32
40 set cpo&vim 47 set cpo&vim
41 48
42 " 1. Variables {{{1 49 " 1. Variables {{{1
43 " ============ 50 " ============
44 51
45 " Regex of syntax group names that are or delimit strings/symbols or are comments. 52 " Syntax group names that are strings.
46 let s:syng_strcom = '\<ruby\%(Regexp\|RegexpDelimiter\|RegexpEscape' .
47 \ '\|Symbol\|String\|StringDelimiter\|StringEscape\|ASCIICode' .
48 \ '\|Interpolation\|InterpolationDelimiter\|NoInterpolation\|Comment\|Documentation\)\>'
49
50 " Regex of syntax group names that are strings.
51 let s:syng_string = 53 let s:syng_string =
52 \ '\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\)\>' 54 \ ['String', 'Interpolation', 'InterpolationDelimiter', 'NoInterpolation', 'StringEscape']
53 55
54 " Regex of syntax group names that are strings or documentation. 56 " Syntax group names that are strings or documentation.
55 let s:syng_stringdoc = 57 let s:syng_stringdoc = s:syng_string + ['Documentation']
56 \'\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\|Documentation\)\>' 58
59 " Syntax group names that are or delimit strings/symbols/regexes or are comments.
60 let s:syng_strcom = s:syng_stringdoc +
61 \ ['Regexp', 'RegexpDelimiter', 'RegexpEscape',
62 \ 'Symbol', 'StringDelimiter', 'ASCIICode', 'Comment']
57 63
58 " Expression used to check whether we should skip a match with searchpair(). 64 " Expression used to check whether we should skip a match with searchpair().
59 let s:skip_expr = 65 let s:skip_expr =
60 \ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'" 66 \ 'index(map('.string(s:syng_strcom).',"hlID(''ruby''.v:val)"), synID(line("."),col("."),1)) >= 0'
61 67
62 " Regex used for words that, at the start of a line, add a level of indent. 68 " Regex used for words that, at the start of a line, add a level of indent.
63 let s:ruby_indent_keywords = 69 let s:ruby_indent_keywords =
64 \ '^\s*\zs\<\%(module\|class\|if\|for' . 70 \ '^\s*\zs\<\%(module\|class\|if\|for' .
65 \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue' . 71 \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue' .
66 \ '\|\%(public\|protected\|private\)\=\s*def\):\@!\>' . 72 \ '\|\%(\K\k*[!?]\?\)\=\s*def\):\@!\>' .
67 \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' . 73 \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
68 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>' 74 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'
69 75
70 " Regex used for words that, at the start of a line, remove a level of indent. 76 " Regex used for words that, at the start of a line, remove a level of indent.
71 let s:ruby_deindent_keywords = 77 let s:ruby_deindent_keywords =
75 "let s:end_start_regex = '\%(^\|[^.]\)\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\|do\)\>' 81 "let s:end_start_regex = '\%(^\|[^.]\)\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\|do\)\>'
76 " TODO: the do here should be restricted somewhat (only at end of line)? 82 " TODO: the do here should be restricted somewhat (only at end of line)?
77 let s:end_start_regex = 83 let s:end_start_regex =
78 \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' . 84 \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
79 \ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' . 85 \ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' .
80 \ '\|\%(public\|protected\|private\)\=\s*def\):\@!\>' . 86 \ '\|\%(\K\k*[!?]\?\)\=\s*def\):\@!\>' .
81 \ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>' 87 \ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>'
82 88
83 " Regex that defines the middle-match for the 'end' keyword. 89 " Regex that defines the middle-match for the 'end' keyword.
84 let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>' 90 let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>'
85 91
140 let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex 146 let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex
141 147
142 " Regex that describes a leading operator (only a method call's dot for now) 148 " Regex that describes a leading operator (only a method call's dot for now)
143 let s:leading_operator_regex = '^\s*[.]' 149 let s:leading_operator_regex = '^\s*[.]'
144 150
145 " 2. Auxiliary Functions {{{1 151 " 2. GetRubyIndent Function {{{1
152 " =========================
153
154 function! GetRubyIndent(...) abort
155 " 2.1. Setup {{{2
156 " ----------
157
158 let indent_info = {}
159
160 " The value of a single shift-width
161 if exists('*shiftwidth')
162 let indent_info.sw = shiftwidth()
163 else
164 let indent_info.sw = &sw
165 endif
166
167 " For the current line, use the first argument if given, else v:lnum
168 let indent_info.clnum = a:0 ? a:1 : v:lnum
169 let indent_info.cline = getline(indent_info.clnum)
170
171 " Set up variables for restoring position in file. Could use clnum here.
172 let indent_info.col = col('.')
173
174 " 2.2. Work on the current line {{{2
175 " -----------------------------
176 let indent_callback_names = [
177 \ 's:AccessModifier',
178 \ 's:ClosingBracketOnEmptyLine',
179 \ 's:BlockComment',
180 \ 's:DeindentingKeyword',
181 \ 's:MultilineStringOrLineComment',
182 \ 's:ClosingHeredocDelimiter',
183 \ 's:LeadingOperator',
184 \ ]
185
186 for callback_name in indent_callback_names
187 " Decho "Running: ".callback_name
188 let indent = call(function(callback_name), [indent_info])
189
190 if indent >= 0
191 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
192 return indent
193 endif
194 endfor
195
196 " 2.3. Work on the previous line. {{{2
197 " -------------------------------
198
199 " Special case: we don't need the real s:PrevNonBlankNonString for an empty
200 " line inside a string. And that call can be quite expensive in that
201 " particular situation.
202 let indent_callback_names = [
203 \ 's:EmptyInsideString',
204 \ ]
205
206 for callback_name in indent_callback_names
207 " Decho "Running: ".callback_name
208 let indent = call(function(callback_name), [indent_info])
209
210 if indent >= 0
211 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
212 return indent
213 endif
214 endfor
215
216 " Previous line number
217 let indent_info.plnum = s:PrevNonBlankNonString(indent_info.clnum - 1)
218 let indent_info.pline = getline(indent_info.plnum)
219
220 let indent_callback_names = [
221 \ 's:StartOfFile',
222 \ 's:AfterAccessModifier',
223 \ 's:ContinuedLine',
224 \ 's:AfterBlockOpening',
225 \ 's:AfterHangingSplat',
226 \ 's:AfterUnbalancedBracket',
227 \ 's:AfterLeadingOperator',
228 \ 's:AfterEndKeyword',
229 \ 's:AfterIndentKeyword',
230 \ ]
231
232 for callback_name in indent_callback_names
233 " Decho "Running: ".callback_name
234 let indent = call(function(callback_name), [indent_info])
235
236 if indent >= 0
237 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
238 return indent
239 endif
240 endfor
241
242 " 2.4. Work on the MSL line. {{{2
243 " --------------------------
244 let indent_callback_names = [
245 \ 's:PreviousNotMSL',
246 \ 's:IndentingKeywordInMSL',
247 \ 's:ContinuedHangingOperator',
248 \ ]
249
250 " Most Significant line based on the previous one -- in case it's a
251 " contination of something above
252 let indent_info.plnum_msl = s:GetMSL(indent_info.plnum)
253
254 for callback_name in indent_callback_names
255 " Decho "Running: ".callback_name
256 let indent = call(function(callback_name), [indent_info])
257
258 if indent >= 0
259 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
260 return indent
261 endif
262 endfor
263
264 " }}}2
265
266 " By default, just return the previous line's indent
267 " Decho "Default case matched"
268 return indent(indent_info.plnum)
269 endfunction
270
271 " 3. Indenting Logic Callbacks {{{1
272 " ============================
273
274 function! s:AccessModifier(cline_info) abort
275 let info = a:cline_info
276
277 " If this line is an access modifier keyword, align according to the closest
278 " class declaration.
279 if g:ruby_indent_access_modifier_style == 'indent'
280 if s:Match(info.clnum, s:access_modifier_regex)
281 let class_lnum = s:FindContainingClass()
282 if class_lnum > 0
283 return indent(class_lnum) + info.sw
284 endif
285 endif
286 elseif g:ruby_indent_access_modifier_style == 'outdent'
287 if s:Match(info.clnum, s:access_modifier_regex)
288 let class_lnum = s:FindContainingClass()
289 if class_lnum > 0
290 return indent(class_lnum)
291 endif
292 endif
293 endif
294
295 return -1
296 endfunction
297
298 function! s:ClosingBracketOnEmptyLine(cline_info) abort
299 let info = a:cline_info
300
301 " If we got a closing bracket on an empty line, find its match and indent
302 " according to it. For parentheses we indent to its column - 1, for the
303 " others we indent to the containing line's MSL's level. Return -1 if fail.
304 let col = matchend(info.cline, '^\s*[]})]')
305
306 if col > 0 && !s:IsInStringOrComment(info.clnum, col)
307 call cursor(info.clnum, col)
308 let closing_bracket = info.cline[col - 1]
309 let bracket_pair = strpart('(){}[]', stridx(')}]', closing_bracket) * 2, 2)
310
311 if searchpair(escape(bracket_pair[0], '\['), '', bracket_pair[1], 'bW', s:skip_expr) > 0
312 if closing_bracket == ')' && col('.') != col('$') - 1
313 let ind = virtcol('.') - 1
314 elseif g:ruby_indent_block_style == 'do'
315 let ind = indent(line('.'))
316 else " g:ruby_indent_block_style == 'expression'
317 let ind = indent(s:GetMSL(line('.')))
318 endif
319 endif
320
321 return ind
322 endif
323
324 return -1
325 endfunction
326
327 function! s:BlockComment(cline_info) abort
328 " If we have a =begin or =end set indent to first column.
329 if match(a:cline_info.cline, '^\s*\%(=begin\|=end\)$') != -1
330 return 0
331 endif
332 return -1
333 endfunction
334
335 function! s:DeindentingKeyword(cline_info) abort
336 let info = a:cline_info
337
338 " If we have a deindenting keyword, find its match and indent to its level.
339 " TODO: this is messy
340 if s:Match(info.clnum, s:ruby_deindent_keywords)
341 call cursor(info.clnum, 1)
342
343 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
344 \ s:end_skip_expr) > 0
345 let msl = s:GetMSL(line('.'))
346 let line = getline(line('.'))
347
348 if s:IsAssignment(line, col('.')) &&
349 \ strpart(line, col('.') - 1, 2) !~ 'do'
350 " assignment to case/begin/etc, on the same line
351 if g:ruby_indent_assignment_style == 'hanging'
352 " hanging indent
353 let ind = virtcol('.') - 1
354 else
355 " align with variable
356 let ind = indent(line('.'))
357 endif
358 elseif g:ruby_indent_block_style == 'do'
359 " align to line of the "do", not to the MSL
360 let ind = indent(line('.'))
361 elseif getline(msl) =~ '=\s*\(#.*\)\=$'
362 " in the case of assignment to the MSL, align to the starting line,
363 " not to the MSL
364 let ind = indent(line('.'))
365 else
366 " align to the MSL
367 let ind = indent(msl)
368 endif
369 endif
370 return ind
371 endif
372
373 return -1
374 endfunction
375
376 function! s:MultilineStringOrLineComment(cline_info) abort
377 let info = a:cline_info
378
379 " If we are in a multi-line string or line-comment, don't do anything to it.
380 if s:IsInStringOrDocumentation(info.clnum, matchend(info.cline, '^\s*') + 1)
381 return indent(info.clnum)
382 endif
383 return -1
384 endfunction
385
386 function! s:ClosingHeredocDelimiter(cline_info) abort
387 let info = a:cline_info
388
389 " If we are at the closing delimiter of a "<<" heredoc-style string, set the
390 " indent to 0.
391 if info.cline =~ '^\k\+\s*$'
392 \ && s:IsInStringDelimiter(info.clnum, 1)
393 \ && search('\V<<'.info.cline, 'nbW') > 0
394 return 0
395 endif
396
397 return -1
398 endfunction
399
400 function! s:LeadingOperator(cline_info) abort
401 " If the current line starts with a leading operator, add a level of indent.
402 if s:Match(a:cline_info.clnum, s:leading_operator_regex)
403 return indent(s:GetMSL(a:cline_info.clnum)) + a:cline_info.sw
404 endif
405 return -1
406 endfunction
407
408 function! s:EmptyInsideString(pline_info) abort
409 " If the line is empty and inside a string (the previous line is a string,
410 " too), use the previous line's indent
411 let info = a:pline_info
412
413 let plnum = prevnonblank(info.clnum - 1)
414 let pline = getline(plnum)
415
416 if info.cline =~ '^\s*$'
417 \ && s:IsInStringOrComment(plnum, 1)
418 \ && s:IsInStringOrComment(plnum, strlen(pline))
419 return indent(plnum)
420 endif
421 return -1
422 endfunction
423
424 function! s:StartOfFile(pline_info) abort
425 " At the start of the file use zero indent.
426 if a:pline_info.plnum == 0
427 return 0
428 endif
429 return -1
430 endfunction
431
432 function! s:AfterAccessModifier(pline_info) abort
433 let info = a:pline_info
434
435 if g:ruby_indent_access_modifier_style == 'indent'
436 " If the previous line was a private/protected keyword, add a
437 " level of indent.
438 if s:Match(info.plnum, s:indent_access_modifier_regex)
439 return indent(info.plnum) + info.sw
440 endif
441 elseif g:ruby_indent_access_modifier_style == 'outdent'
442 " If the previous line was a private/protected/public keyword, add
443 " a level of indent, since the keyword has been out-dented.
444 if s:Match(info.plnum, s:access_modifier_regex)
445 return indent(info.plnum) + info.sw
446 endif
447 endif
448 return -1
449 endfunction
450
451 " Example:
452 "
453 " if foo || bar ||
454 " baz || bing
455 " puts "foo"
456 " end
457 "
458 function! s:ContinuedLine(pline_info) abort
459 let info = a:pline_info
460
461 let col = s:Match(info.plnum, s:ruby_indent_keywords)
462 if s:Match(info.plnum, s:continuable_regex) &&
463 \ s:Match(info.plnum, s:continuation_regex)
464 if col > 0 && s:IsAssignment(info.pline, col)
465 if g:ruby_indent_assignment_style == 'hanging'
466 " hanging indent
467 let ind = col - 1
468 else
469 " align with variable
470 let ind = indent(info.plnum)
471 endif
472 else
473 let ind = indent(s:GetMSL(info.plnum))
474 endif
475 return ind + info.sw + info.sw
476 endif
477 return -1
478 endfunction
479
480 function! s:AfterBlockOpening(pline_info) abort
481 let info = a:pline_info
482
483 " If the previous line ended with a block opening, add a level of indent.
484 if s:Match(info.plnum, s:block_regex)
485 if g:ruby_indent_block_style == 'do'
486 " don't align to the msl, align to the "do"
487 let ind = indent(info.plnum) + info.sw
488 else
489 let plnum_msl = s:GetMSL(info.plnum)
490
491 if getline(plnum_msl) =~ '=\s*\(#.*\)\=$'
492 " in the case of assignment to the msl, align to the starting line,
493 " not to the msl
494 let ind = indent(info.plnum) + info.sw
495 else
496 let ind = indent(plnum_msl) + info.sw
497 endif
498 endif
499
500 return ind
501 endif
502
503 return -1
504 endfunction
505
506 function! s:AfterLeadingOperator(pline_info) abort
507 " If the previous line started with a leading operator, use its MSL's level
508 " of indent
509 if s:Match(a:pline_info.plnum, s:leading_operator_regex)
510 return indent(s:GetMSL(a:pline_info.plnum))
511 endif
512 return -1
513 endfunction
514
515 function! s:AfterHangingSplat(pline_info) abort
516 let info = a:pline_info
517
518 " If the previous line ended with the "*" of a splat, add a level of indent
519 if info.pline =~ s:splat_regex
520 return indent(info.plnum) + info.sw
521 endif
522 return -1
523 endfunction
524
525 function! s:AfterUnbalancedBracket(pline_info) abort
526 let info = a:pline_info
527
528 " If the previous line contained unclosed opening brackets and we are still
529 " in them, find the rightmost one and add indent depending on the bracket
530 " type.
531 "
532 " If it contained hanging closing brackets, find the rightmost one, find its
533 " match and indent according to that.
534 if info.pline =~ '[[({]' || info.pline =~ '[])}]\s*\%(#.*\)\=$'
535 let [opening, closing] = s:ExtraBrackets(info.plnum)
536
537 if opening.pos != -1
538 if opening.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
539 if col('.') + 1 == col('$')
540 return indent(info.plnum) + info.sw
541 else
542 return virtcol('.')
543 endif
544 else
545 let nonspace = matchend(info.pline, '\S', opening.pos + 1) - 1
546 return nonspace > 0 ? nonspace : indent(info.plnum) + info.sw
547 endif
548 elseif closing.pos != -1
549 call cursor(info.plnum, closing.pos + 1)
550 normal! %
551
552 if s:Match(line('.'), s:ruby_indent_keywords)
553 return indent('.') + info.sw
554 else
555 return indent(s:GetMSL(line('.')))
556 endif
557 else
558 call cursor(info.clnum, info.col)
559 end
560 endif
561
562 return -1
563 endfunction
564
565 function! s:AfterEndKeyword(pline_info) abort
566 let info = a:pline_info
567 " If the previous line ended with an "end", match that "end"s beginning's
568 " indent.
569 let col = s:Match(info.plnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
570 if col > 0
571 call cursor(info.plnum, col)
572 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
573 \ s:end_skip_expr) > 0
574 let n = line('.')
575 let ind = indent('.')
576 let msl = s:GetMSL(n)
577 if msl != n
578 let ind = indent(msl)
579 end
580 return ind
581 endif
582 end
583 return -1
584 endfunction
585
586 function! s:AfterIndentKeyword(pline_info) abort
587 let info = a:pline_info
588 let col = s:Match(info.plnum, s:ruby_indent_keywords)
589
590 if col > 0
591 call cursor(info.plnum, col)
592 let ind = virtcol('.') - 1 + info.sw
593 " TODO: make this better (we need to count them) (or, if a searchpair
594 " fails, we know that something is lacking an end and thus we indent a
595 " level
596 if s:Match(info.plnum, s:end_end_regex)
597 let ind = indent('.')
598 elseif s:IsAssignment(info.pline, col)
599 if g:ruby_indent_assignment_style == 'hanging'
600 " hanging indent
601 let ind = col + info.sw - 1
602 else
603 " align with variable
604 let ind = indent(info.plnum) + info.sw
605 endif
606 endif
607 return ind
608 endif
609
610 return -1
611 endfunction
612
613 function! s:PreviousNotMSL(msl_info) abort
614 let info = a:msl_info
615
616 " If the previous line wasn't a MSL
617 if info.plnum != info.plnum_msl
618 " If previous line ends bracket and begins non-bracket continuation decrease indent by 1.
619 if s:Match(info.plnum, s:bracket_switch_continuation_regex)
620 " TODO (2016-10-07) Wrong/unused? How could it be "1"?
621 return indent(info.plnum) - 1
622 " If previous line is a continuation return its indent.
623 " TODO: the || s:IsInString() thing worries me a bit.
624 elseif s:Match(info.plnum, s:non_bracket_continuation_regex) || s:IsInString(info.plnum, strlen(line))
625 return indent(info.plnum)
626 endif
627 endif
628
629 return -1
630 endfunction
631
632 function! s:IndentingKeywordInMSL(msl_info) abort
633 let info = a:msl_info
634 " If the MSL line had an indenting keyword in it, add a level of indent.
635 " TODO: this does not take into account contrived things such as
636 " module Foo; class Bar; end
637 let col = s:Match(info.plnum_msl, s:ruby_indent_keywords)
638 if col > 0
639 let ind = indent(info.plnum_msl) + info.sw
640 if s:Match(info.plnum_msl, s:end_end_regex)
641 let ind = ind - info.sw
642 elseif s:IsAssignment(getline(info.plnum_msl), col)
643 if g:ruby_indent_assignment_style == 'hanging'
644 " hanging indent
645 let ind = col + info.sw - 1
646 else
647 " align with variable
648 let ind = indent(info.plnum_msl) + info.sw
649 endif
650 endif
651 return ind
652 endif
653 return -1
654 endfunction
655
656 function! s:ContinuedHangingOperator(msl_info) abort
657 let info = a:msl_info
658
659 " If the previous line ended with [*+/.,-=], but wasn't a block ending or a
660 " closing bracket, indent one extra level.
661 if s:Match(info.plnum_msl, s:non_bracket_continuation_regex) && !s:Match(info.plnum_msl, '^\s*\([\])}]\|end\)')
662 if info.plnum_msl == info.plnum
663 let ind = indent(info.plnum_msl) + info.sw
664 else
665 let ind = indent(info.plnum_msl)
666 endif
667 return ind
668 endif
669
670 return -1
671 endfunction
672
673 " 4. Auxiliary Functions {{{1
146 " ====================== 674 " ======================
147 675
676 function! s:IsInRubyGroup(groups, lnum, col) abort
677 let ids = map(copy(a:groups), 'hlID("ruby".v:val)')
678 return index(ids, synID(a:lnum, a:col, 1)) >= 0
679 endfunction
680
148 " Check if the character at lnum:col is inside a string, comment, or is ascii. 681 " Check if the character at lnum:col is inside a string, comment, or is ascii.
149 function s:IsInStringOrComment(lnum, col) 682 function! s:IsInStringOrComment(lnum, col) abort
150 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom 683 return s:IsInRubyGroup(s:syng_strcom, a:lnum, a:col)
151 endfunction 684 endfunction
152 685
153 " Check if the character at lnum:col is inside a string. 686 " Check if the character at lnum:col is inside a string.
154 function s:IsInString(lnum, col) 687 function! s:IsInString(lnum, col) abort
155 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string 688 return s:IsInRubyGroup(s:syng_string, a:lnum, a:col)
156 endfunction 689 endfunction
157 690
158 " Check if the character at lnum:col is inside a string or documentation. 691 " Check if the character at lnum:col is inside a string or documentation.
159 function s:IsInStringOrDocumentation(lnum, col) 692 function! s:IsInStringOrDocumentation(lnum, col) abort
160 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc 693 return s:IsInRubyGroup(s:syng_stringdoc, a:lnum, a:col)
161 endfunction 694 endfunction
162 695
163 " Check if the character at lnum:col is inside a string delimiter 696 " Check if the character at lnum:col is inside a string delimiter
164 function s:IsInStringDelimiter(lnum, col) 697 function! s:IsInStringDelimiter(lnum, col) abort
165 return synIDattr(synID(a:lnum, a:col, 1), 'name') == 'rubyStringDelimiter' 698 return s:IsInRubyGroup(['StringDelimiter'], a:lnum, a:col)
699 endfunction
700
701 function! s:IsAssignment(str, pos) abort
702 return strpart(a:str, 0, a:pos - 1) =~ '=\s*$'
166 endfunction 703 endfunction
167 704
168 " Find line above 'lnum' that isn't empty, in a comment, or in a string. 705 " Find line above 'lnum' that isn't empty, in a comment, or in a string.
169 function s:PrevNonBlankNonString(lnum) 706 function! s:PrevNonBlankNonString(lnum) abort
170 let in_block = 0 707 let in_block = 0
171 let lnum = prevnonblank(a:lnum) 708 let lnum = prevnonblank(a:lnum)
172 while lnum > 0 709 while lnum > 0
173 " Go in and out of blocks comments as necessary. 710 " Go in and out of blocks comments as necessary.
174 " If the line isn't empty (with opt. comment) or in a string, end search. 711 " If the line isn't empty (with opt. comment) or in a string, end search.
189 endwhile 726 endwhile
190 return lnum 727 return lnum
191 endfunction 728 endfunction
192 729
193 " Find line above 'lnum' that started the continuation 'lnum' may be part of. 730 " Find line above 'lnum' that started the continuation 'lnum' may be part of.
194 function s:GetMSL(lnum) 731 function! s:GetMSL(lnum) abort
195 " Start on the line we're at and use its indent. 732 " Start on the line we're at and use its indent.
196 let msl = a:lnum 733 let msl = a:lnum
197 let msl_body = getline(msl)
198 let lnum = s:PrevNonBlankNonString(a:lnum - 1) 734 let lnum = s:PrevNonBlankNonString(a:lnum - 1)
199 while lnum > 0 735 while lnum > 0
200 " If we have a continuation line, or we're in a string, use line as MSL. 736 " If we have a continuation line, or we're in a string, use line as MSL.
201 " Otherwise, terminate search as we have found our MSL already. 737 " Otherwise, terminate search as we have found our MSL already.
202 let line = getline(lnum) 738 let line = getline(lnum)
289 else 825 else
290 break 826 break
291 endif 827 endif
292 endif 828 endif
293 829
294 let msl_body = getline(msl)
295 let lnum = s:PrevNonBlankNonString(lnum - 1) 830 let lnum = s:PrevNonBlankNonString(lnum - 1)
296 endwhile 831 endwhile
297 return msl 832 return msl
298 endfunction 833 endfunction
299 834
300 " Check if line 'lnum' has more opening brackets than closing ones. 835 " Check if line 'lnum' has more opening brackets than closing ones.
301 function s:ExtraBrackets(lnum) 836 function! s:ExtraBrackets(lnum) abort
302 let opening = {'parentheses': [], 'braces': [], 'brackets': []} 837 let opening = {'parentheses': [], 'braces': [], 'brackets': []}
303 let closing = {'parentheses': [], 'braces': [], 'brackets': []} 838 let closing = {'parentheses': [], 'braces': [], 'brackets': []}
304 839
305 let line = getline(a:lnum) 840 let line = getline(a:lnum)
306 let pos = match(line, '[][(){}]', 0) 841 let pos = match(line, '[][(){}]', 0)
358 endfor 893 endfor
359 894
360 return [rightmost_opening, rightmost_closing] 895 return [rightmost_opening, rightmost_closing]
361 endfunction 896 endfunction
362 897
363 function s:Match(lnum, regex) 898 function! s:Match(lnum, regex) abort
364 let line = getline(a:lnum) 899 let line = getline(a:lnum)
365 let offset = match(line, '\C'.a:regex) 900 let offset = match(line, '\C'.a:regex)
366 let col = offset + 1 901 let col = offset + 1
367 902
368 while offset > -1 && s:IsInStringOrComment(a:lnum, col) 903 while offset > -1 && s:IsInStringOrComment(a:lnum, col)
378 endfunction 913 endfunction
379 914
380 " Locates the containing class/module's definition line, ignoring nested classes 915 " Locates the containing class/module's definition line, ignoring nested classes
381 " along the way. 916 " along the way.
382 " 917 "
383 function! s:FindContainingClass() 918 function! s:FindContainingClass() abort
384 let saved_position = getpos('.') 919 let saved_position = getpos('.')
385 920
386 while searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW', 921 while searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
387 \ s:end_skip_expr) > 0 922 \ s:end_skip_expr) > 0
388 if expand('<cword>') =~# '\<class\|module\>' 923 if expand('<cword>') =~# '\<class\|module\>'
394 929
395 call setpos('.', saved_position) 930 call setpos('.', saved_position)
396 return 0 931 return 0
397 endfunction 932 endfunction
398 933
399 " 3. GetRubyIndent Function {{{1
400 " =========================
401
402 function GetRubyIndent(...)
403 " 3.1. Setup {{{2
404 " ----------
405
406 " The value of a single shift-width
407 let sw = shiftwidth()
408
409 " For the current line, use the first argument if given, else v:lnum
410 let clnum = a:0 ? a:1 : v:lnum
411
412 " Set up variables for restoring position in file. Could use clnum here.
413 let vcol = col('.')
414
415 " 3.2. Work on the current line {{{2
416 " -----------------------------
417
418 " Get the current line.
419 let line = getline(clnum)
420 let ind = -1
421
422 " If this line is an access modifier keyword, align according to the closest
423 " class declaration.
424 if g:ruby_indent_access_modifier_style == 'indent'
425 if s:Match(clnum, s:access_modifier_regex)
426 let class_line = s:FindContainingClass()
427 if class_line > 0
428 return indent(class_line) + sw
429 endif
430 endif
431 elseif g:ruby_indent_access_modifier_style == 'outdent'
432 if s:Match(clnum, s:access_modifier_regex)
433 let class_line = s:FindContainingClass()
434 if class_line > 0
435 return indent(class_line)
436 endif
437 endif
438 endif
439
440 " If we got a closing bracket on an empty line, find its match and indent
441 " according to it. For parentheses we indent to its column - 1, for the
442 " others we indent to the containing line's MSL's level. Return -1 if fail.
443 let col = matchend(line, '^\s*[]})]')
444 if col > 0 && !s:IsInStringOrComment(clnum, col)
445 call cursor(clnum, col)
446 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
447 if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
448 if line[col-1]==')' && col('.') != col('$') - 1
449 let ind = virtcol('.') - 1
450 elseif g:ruby_indent_block_style == 'do'
451 let ind = indent(line('.'))
452 else " g:ruby_indent_block_style == 'expression'
453 let ind = indent(s:GetMSL(line('.')))
454 endif
455 endif
456 return ind
457 endif
458
459 " If we have a =begin or =end set indent to first column.
460 if match(line, '^\s*\%(=begin\|=end\)$') != -1
461 return 0
462 endif
463
464 " If we have a deindenting keyword, find its match and indent to its level.
465 " TODO: this is messy
466 if s:Match(clnum, s:ruby_deindent_keywords)
467 call cursor(clnum, 1)
468 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
469 \ s:end_skip_expr) > 0
470 let msl = s:GetMSL(line('.'))
471 let line = getline(line('.'))
472
473 if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
474 \ strpart(line, col('.') - 1, 2) !~ 'do'
475 " assignment to case/begin/etc, on the same line, hanging indent
476 let ind = virtcol('.') - 1
477 elseif g:ruby_indent_block_style == 'do'
478 " align to line of the "do", not to the MSL
479 let ind = indent(line('.'))
480 elseif getline(msl) =~ '=\s*\(#.*\)\=$'
481 " in the case of assignment to the MSL, align to the starting line,
482 " not to the MSL
483 let ind = indent(line('.'))
484 else
485 " align to the MSL
486 let ind = indent(msl)
487 endif
488 endif
489 return ind
490 endif
491
492 " If we are in a multi-line string or line-comment, don't do anything to it.
493 if s:IsInStringOrDocumentation(clnum, matchend(line, '^\s*') + 1)
494 return indent('.')
495 endif
496
497 " If we are at the closing delimiter of a "<<" heredoc-style string, set the
498 " indent to 0.
499 if line =~ '^\k\+\s*$'
500 \ && s:IsInStringDelimiter(clnum, 1)
501 \ && search('\V<<'.line, 'nbW') > 0
502 return 0
503 endif
504
505 " If the current line starts with a leading operator, add a level of indent.
506 if s:Match(clnum, s:leading_operator_regex)
507 return indent(s:GetMSL(clnum)) + sw
508 endif
509
510 " 3.3. Work on the previous line. {{{2
511 " -------------------------------
512
513 " Find a non-blank, non-multi-line string line above the current line.
514 let lnum = s:PrevNonBlankNonString(clnum - 1)
515
516 " If the line is empty and inside a string, use the previous line.
517 if line =~ '^\s*$' && lnum != prevnonblank(clnum - 1)
518 return indent(prevnonblank(clnum))
519 endif
520
521 " At the start of the file use zero indent.
522 if lnum == 0
523 return 0
524 endif
525
526 " Set up variables for the previous line.
527 let line = getline(lnum)
528 let ind = indent(lnum)
529
530 if g:ruby_indent_access_modifier_style == 'indent'
531 " If the previous line was a private/protected keyword, add a
532 " level of indent.
533 if s:Match(lnum, s:indent_access_modifier_regex)
534 return indent(lnum) + sw
535 endif
536 elseif g:ruby_indent_access_modifier_style == 'outdent'
537 " If the previous line was a private/protected/public keyword, add
538 " a level of indent, since the keyword has been out-dented.
539 if s:Match(lnum, s:access_modifier_regex)
540 return indent(lnum) + sw
541 endif
542 endif
543
544 if s:Match(lnum, s:continuable_regex) && s:Match(lnum, s:continuation_regex)
545 return indent(s:GetMSL(lnum)) + sw + sw
546 endif
547
548 " If the previous line ended with a block opening, add a level of indent.
549 if s:Match(lnum, s:block_regex)
550 let msl = s:GetMSL(lnum)
551
552 if g:ruby_indent_block_style == 'do'
553 " don't align to the msl, align to the "do"
554 let ind = indent(lnum) + sw
555 elseif getline(msl) =~ '=\s*\(#.*\)\=$'
556 " in the case of assignment to the msl, align to the starting line,
557 " not to the msl
558 let ind = indent(lnum) + sw
559 else
560 let ind = indent(msl) + sw
561 endif
562 return ind
563 endif
564
565 " If the previous line started with a leading operator, use its MSL's level
566 " of indent
567 if s:Match(lnum, s:leading_operator_regex)
568 return indent(s:GetMSL(lnum))
569 endif
570
571 " If the previous line ended with the "*" of a splat, add a level of indent
572 if line =~ s:splat_regex
573 return indent(lnum) + sw
574 endif
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.
582 if line =~ '[[({]' || line =~ '[])}]\s*\%(#.*\)\=$'
583 let [opening, closing] = s:ExtraBrackets(lnum)
584
585 if opening.pos != -1
586 if opening.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
587 if col('.') + 1 == col('$')
588 return ind + sw
589 else
590 return virtcol('.')
591 endif
592 else
593 let nonspace = matchend(line, '\S', opening.pos + 1) - 1
594 return nonspace > 0 ? nonspace : ind + sw
595 endif
596 elseif closing.pos != -1
597 call cursor(lnum, closing.pos + 1)
598 normal! %
599
600 if s:Match(line('.'), s:ruby_indent_keywords)
601 return indent('.') + sw
602 else
603 return indent(s:GetMSL(line('.')))
604 endif
605 else
606 call cursor(clnum, vcol)
607 end
608 endif
609
610 " If the previous line ended with an "end", match that "end"s beginning's
611 " indent.
612 let col = s:Match(lnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
613 if col > 0
614 call cursor(lnum, col)
615 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
616 \ s:end_skip_expr) > 0
617 let n = line('.')
618 let ind = indent('.')
619 let msl = s:GetMSL(n)
620 if msl != n
621 let ind = indent(msl)
622 end
623 return ind
624 endif
625 end
626
627 let col = s:Match(lnum, s:ruby_indent_keywords)
628 if col > 0
629 call cursor(lnum, col)
630 let ind = virtcol('.') - 1 + sw
631 " TODO: make this better (we need to count them) (or, if a searchpair
632 " fails, we know that something is lacking an end and thus we indent a
633 " level
634 if s:Match(lnum, s:end_end_regex)
635 let ind = indent('.')
636 endif
637 return ind
638 endif
639
640 " 3.4. Work on the MSL line. {{{2
641 " --------------------------
642
643 " Set up variables to use and search for MSL to the previous line.
644 let p_lnum = lnum
645 let lnum = s:GetMSL(lnum)
646
647 " If the previous line wasn't a MSL.
648 if p_lnum != lnum
649 " If previous line ends bracket and begins non-bracket continuation decrease indent by 1.
650 if s:Match(p_lnum, s:bracket_switch_continuation_regex)
651 return ind - 1
652 " If previous line is a continuation return its indent.
653 " TODO: the || s:IsInString() thing worries me a bit.
654 elseif s:Match(p_lnum, s:non_bracket_continuation_regex) || s:IsInString(p_lnum,strlen(line))
655 return ind
656 endif
657 endif
658
659 " Set up more variables, now that we know we wasn't continuation bound.
660 let line = getline(lnum)
661 let msl_ind = indent(lnum)
662
663 " If the MSL line had an indenting keyword in it, add a level of indent.
664 " TODO: this does not take into account contrived things such as
665 " module Foo; class Bar; end
666 if s:Match(lnum, s:ruby_indent_keywords)
667 let ind = msl_ind + sw
668 if s:Match(lnum, s:end_end_regex)
669 let ind = ind - sw
670 endif
671 return ind
672 endif
673
674 " If the previous line ended with [*+/.,-=], but wasn't a block ending or a
675 " closing bracket, indent one extra level.
676 if s:Match(lnum, s:non_bracket_continuation_regex) && !s:Match(lnum, '^\s*\([\])}]\|end\)')
677 if lnum == p_lnum
678 let ind = msl_ind + sw
679 else
680 let ind = msl_ind
681 endif
682 return ind
683 endif
684
685 " }}}2
686
687 return ind
688 endfunction
689
690 " }}}1 934 " }}}1
691 935
692 let &cpo = s:cpo_save 936 let &cpo = s:cpo_save
693 unlet s:cpo_save 937 unlet s:cpo_save
694 938