xref: /vim-8.2.3635/runtime/indent/ruby.vim (revision b1c9198a)
1" Vim indent file
2" Language:		Ruby
3" Maintainer:		Nikolai Weibull <now at bitwi.se>
4" URL:			https://github.com/vim-ruby/vim-ruby
5" Release Coordinator:	Doug Kearns <[email protected]>
6
7" 0. Initialization {{{1
8" =================
9
10" Only load this indent file when no other was loaded.
11if exists("b:did_indent")
12  finish
13endif
14let b:did_indent = 1
15
16if !exists('g:ruby_indent_access_modifier_style')
17  " Possible values: "normal", "indent", "outdent"
18  let g:ruby_indent_access_modifier_style = 'normal'
19endif
20
21if !exists('g:ruby_indent_block_style')
22  " Possible values: "expression", "do"
23  let g:ruby_indent_block_style = 'expression'
24endif
25
26setlocal nosmartindent
27
28" Now, set up our indentation expression and keys that trigger it.
29setlocal indentexpr=GetRubyIndent(v:lnum)
30setlocal indentkeys=0{,0},0),0],!^F,o,O,e,:,.
31setlocal indentkeys+==end,=else,=elsif,=when,=ensure,=rescue,==begin,==end
32setlocal indentkeys+==private,=protected,=public
33
34" Only define the function once.
35if exists("*GetRubyIndent")
36  finish
37endif
38
39let s:cpo_save = &cpo
40set cpo&vim
41
42" 1. Variables {{{1
43" ============
44
45" Regex of syntax group names that are or delimit strings/symbols or are comments.
46let 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.
51let s:syng_string =
52      \ '\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\)\>'
53
54" Regex of syntax group names that are strings or documentation.
55let s:syng_stringdoc =
56      \'\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\|Documentation\)\>'
57
58" Expression used to check whether we should skip a match with searchpair().
59let s:skip_expr =
60      \ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
61
62" Regex used for words that, at the start of a line, add a level of indent.
63let s:ruby_indent_keywords =
64      \ '^\s*\zs\<\%(module\|class\|if\|for' .
65      \   '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue' .
66      \   '\|\%(public\|protected\|private\)\=\s*def\):\@!\>' .
67      \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
68      \    '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'
69
70" Regex used for words that, at the start of a line, remove a level of indent.
71let s:ruby_deindent_keywords =
72      \ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|end\):\@!\>'
73
74" Regex that defines the start-match for the 'end' keyword.
75"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)?
77let s:end_start_regex =
78      \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
79      \ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' .
80      \   '\|\%(public\|protected\|private\)\=\s*def\):\@!\>' .
81      \ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>'
82
83" Regex that defines the middle-match for the 'end' keyword.
84let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>'
85
86" Regex that defines the end-match for the 'end' keyword.
87let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>'
88
89" Expression used for searchpair() call for finding match for 'end' keyword.
90let s:end_skip_expr = s:skip_expr .
91      \ ' || (expand("<cword>") == "do"' .
92      \ ' && getline(".") =~ "^\\s*\\<\\(while\\|until\\|for\\):\\@!\\>")'
93
94" Regex that defines continuation lines, not including (, {, or [.
95let s:non_bracket_continuation_regex =
96      \ '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$'
97
98" Regex that defines continuation lines.
99let s:continuation_regex =
100      \ '\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$'
101
102" Regex that defines continuable keywords
103let s:continuable_regex =
104      \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
105      \ '\<\%(if\|for\|while\|until\|unless\):\@!\>'
106
107" Regex that defines bracket continuations
108let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'
109
110" Regex that defines dot continuations
111let s:dot_continuation_regex = '%\@<!\.\s*\%(#.*\)\=$'
112
113" Regex that defines backslash continuations
114let s:backslash_continuation_regex = '%\@<!\\\s*$'
115
116" Regex that defines end of bracket continuation followed by another continuation
117let s:bracket_switch_continuation_regex = '^\([^(]\+\zs).\+\)\+'.s:continuation_regex
118
119" Regex that defines the first part of a splat pattern
120let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$'
121
122" Regex that describes all indent access modifiers
123let s:access_modifier_regex = '\C^\s*\%(public\|protected\|private\)\s*\%(#.*\)\=$'
124
125" Regex that describes the indent access modifiers (excludes public)
126let s:indent_access_modifier_regex = '\C^\s*\%(protected\|private\)\s*\%(#.*\)\=$'
127
128" Regex that defines blocks.
129"
130" Note that there's a slight problem with this regex and s:continuation_regex.
131" Code like this will be matched by both:
132"
133"   method_call do |(a, b)|
134"
135" The reason is that the pipe matches a hanging "|" operator.
136"
137let s:block_regex =
138      \ '\%(\<do:\@!\>\|%\@<!{\)\s*\%(|[^|]*|\)\=\s*\%(#.*\)\=$'
139
140let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex
141
142" Regex that describes a leading operator (only a method call's dot for now)
143let s:leading_operator_regex = '^\s*[.]'
144
145" 2. Auxiliary Functions {{{1
146" ======================
147
148" Check if the character at lnum:col is inside a string, comment, or is ascii.
149function s:IsInStringOrComment(lnum, col)
150  return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
151endfunction
152
153" Check if the character at lnum:col is inside a string.
154function s:IsInString(lnum, col)
155  return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
156endfunction
157
158" Check if the character at lnum:col is inside a string or documentation.
159function s:IsInStringOrDocumentation(lnum, col)
160  return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
161endfunction
162
163" Check if the character at lnum:col is inside a string delimiter
164function s:IsInStringDelimiter(lnum, col)
165  return synIDattr(synID(a:lnum, a:col, 1), 'name') == 'rubyStringDelimiter'
166endfunction
167
168" Find line above 'lnum' that isn't empty, in a comment, or in a string.
169function s:PrevNonBlankNonString(lnum)
170  let in_block = 0
171  let lnum = prevnonblank(a:lnum)
172  while lnum > 0
173    " 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.
175    let line = getline(lnum)
176    if line =~ '^=begin'
177      if in_block
178        let in_block = 0
179      else
180        break
181      endif
182    elseif !in_block && line =~ '^=end'
183      let in_block = 1
184    elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
185          \ && s:IsInStringOrComment(lnum, strlen(line)))
186      break
187    endif
188    let lnum = prevnonblank(lnum - 1)
189  endwhile
190  return lnum
191endfunction
192
193" Find line above 'lnum' that started the continuation 'lnum' may be part of.
194function s:GetMSL(lnum)
195  " Start on the line we're at and use its indent.
196  let msl = a:lnum
197  let msl_body = getline(msl)
198  let lnum = s:PrevNonBlankNonString(a:lnum - 1)
199  while lnum > 0
200    " 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.
202    let line = getline(lnum)
203
204    if !s:Match(msl, s:backslash_continuation_regex) &&
205          \ s:Match(lnum, s:backslash_continuation_regex)
206      " If the current line doesn't end in a backslash, but the previous one
207      " does, look for that line's msl
208      "
209      " Example:
210      "   foo = "bar" \
211      "     "baz"
212      "
213      let msl = lnum
214    elseif s:Match(msl, s:leading_operator_regex)
215      " If the current line starts with a leading operator, keep its indent
216      " and keep looking for an MSL.
217      let msl = lnum
218    elseif s:Match(lnum, s:splat_regex)
219      " If the above line looks like the "*" of a splat, use the current one's
220      " indentation.
221      "
222      " Example:
223      "   Hash[*
224      "     method_call do
225      "       something
226      "
227      return msl
228    elseif s:Match(lnum, s:non_bracket_continuation_regex) &&
229          \ s:Match(msl, s:non_bracket_continuation_regex)
230      " If the current line is a non-bracket continuation and so is the
231      " previous one, keep its indent and continue looking for an MSL.
232      "
233      " Example:
234      "   method_call one,
235      "     two,
236      "     three
237      "
238      let msl = lnum
239    elseif s:Match(lnum, s:dot_continuation_regex) &&
240          \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
241      " If the current line is a bracket continuation or a block-starter, but
242      " the previous is a dot, keep going to see if the previous line is the
243      " start of another continuation.
244      "
245      " Example:
246      "   parent.
247      "     method_call {
248      "     three
249      "
250      let msl = lnum
251    elseif s:Match(lnum, s:non_bracket_continuation_regex) &&
252          \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
253      " If the current line is a bracket continuation or a block-starter, but
254      " the previous is a non-bracket one, respect the previous' indentation,
255      " and stop here.
256      "
257      " Example:
258      "   method_call one,
259      "     two {
260      "     three
261      "
262      return lnum
263    elseif s:Match(lnum, s:bracket_continuation_regex) &&
264          \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
265      " If both lines are bracket continuations (the current may also be a
266      " block-starter), use the current one's and stop here
267      "
268      " Example:
269      "   method_call(
270      "     other_method_call(
271      "       foo
272      return msl
273    elseif s:Match(lnum, s:block_regex) &&
274          \ !s:Match(msl, s:continuation_regex) &&
275          \ !s:Match(msl, s:block_continuation_regex)
276      " If the previous line is a block-starter and the current one is
277      " mostly ordinary, use the current one as the MSL.
278      "
279      " Example:
280      "   method_call do
281      "     something
282      "     something_else
283      return msl
284    else
285      let col = match(line, s:continuation_regex) + 1
286      if (col > 0 && !s:IsInStringOrComment(lnum, col))
287            \ || s:IsInString(lnum, strlen(line))
288        let msl = lnum
289      else
290        break
291      endif
292    endif
293
294    let msl_body = getline(msl)
295    let lnum = s:PrevNonBlankNonString(lnum - 1)
296  endwhile
297  return msl
298endfunction
299
300" Check if line 'lnum' has more opening brackets than closing ones.
301function s:ExtraBrackets(lnum)
302  let opening = {'parentheses': [], 'braces': [], 'brackets': []}
303  let closing = {'parentheses': [], 'braces': [], 'brackets': []}
304
305  let line = getline(a:lnum)
306  let pos  = match(line, '[][(){}]', 0)
307
308  " Save any encountered opening brackets, and remove them once a matching
309  " closing one has been found. If a closing bracket shows up that doesn't
310  " close anything, save it for later.
311  while pos != -1
312    if !s:IsInStringOrComment(a:lnum, pos + 1)
313      if line[pos] == '('
314        call add(opening.parentheses, {'type': '(', 'pos': pos})
315      elseif line[pos] == ')'
316        if empty(opening.parentheses)
317          call add(closing.parentheses, {'type': ')', 'pos': pos})
318        else
319          let opening.parentheses = opening.parentheses[0:-2]
320        endif
321      elseif line[pos] == '{'
322        call add(opening.braces, {'type': '{', 'pos': pos})
323      elseif line[pos] == '}'
324        if empty(opening.braces)
325          call add(closing.braces, {'type': '}', 'pos': pos})
326        else
327          let opening.braces = opening.braces[0:-2]
328        endif
329      elseif line[pos] == '['
330        call add(opening.brackets, {'type': '[', 'pos': pos})
331      elseif line[pos] == ']'
332        if empty(opening.brackets)
333          call add(closing.brackets, {'type': ']', 'pos': pos})
334        else
335          let opening.brackets = opening.brackets[0:-2]
336        endif
337      endif
338    endif
339
340    let pos = match(line, '[][(){}]', pos + 1)
341  endwhile
342
343  " Find the rightmost brackets, since they're the ones that are important in
344  " both opening and closing cases
345  let rightmost_opening = {'type': '(', 'pos': -1}
346  let rightmost_closing = {'type': ')', 'pos': -1}
347
348  for opening in opening.parentheses + opening.braces + opening.brackets
349    if opening.pos > rightmost_opening.pos
350      let rightmost_opening = opening
351    endif
352  endfor
353
354  for closing in closing.parentheses + closing.braces + closing.brackets
355    if closing.pos > rightmost_closing.pos
356      let rightmost_closing = closing
357    endif
358  endfor
359
360  return [rightmost_opening, rightmost_closing]
361endfunction
362
363function s:Match(lnum, regex)
364  let line   = getline(a:lnum)
365  let offset = match(line, '\C'.a:regex)
366  let col    = offset + 1
367
368  while offset > -1 && s:IsInStringOrComment(a:lnum, col)
369    let offset = match(line, '\C'.a:regex, offset + 1)
370    let col = offset + 1
371  endwhile
372
373  if offset > -1
374    return col
375  else
376    return 0
377  endif
378endfunction
379
380" Locates the containing class/module's definition line, ignoring nested classes
381" along the way.
382"
383function! s:FindContainingClass()
384  let saved_position = getpos('.')
385
386  while searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
387        \ s:end_skip_expr) > 0
388    if expand('<cword>') =~# '\<class\|module\>'
389      let found_lnum = line('.')
390      call setpos('.', saved_position)
391      return found_lnum
392    endif
393  endwhile
394
395  call setpos('.', saved_position)
396  return 0
397endfunction
398
399" 3. GetRubyIndent Function {{{1
400" =========================
401
402function 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
688endfunction
689
690" }}}1
691
692let &cpo = s:cpo_save
693unlet s:cpo_save
694
695" vim:set sw=2 sts=2 ts=8 et:
696