blob: a97f4828d6cda70dc4a37302167856007b4bc1be [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00002" Language: Ruby
3" Maintainer: Nikolai Weibull <now at bitwi.se>
Bram Moolenaarec7944a2013-06-12 21:29:15 +02004" URL: https://github.com/vim-ruby/vim-ruby
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00005" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
Bram Moolenaar60a795a2005-09-16 21:55:43 +00006
7" 0. Initialization {{{1
8" =================
Bram Moolenaar071d4272004-06-13 20:20:40 +00009
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
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020016if !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
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000026setlocal nosmartindent
27
Bram Moolenaar60a795a2005-09-16 21:55:43 +000028" Now, set up our indentation expression and keys that trigger it.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020029setlocal indentexpr=GetRubyIndent(v:lnum)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020030setlocal indentkeys=0{,0},0),0],!^F,o,O,e,:,.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020031setlocal indentkeys+==end,=else,=elsif,=when,=ensure,=rescue,==begin,==end
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020032setlocal indentkeys+==private,=protected,=public
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
34" Only define the function once.
35if exists("*GetRubyIndent")
36 finish
37endif
38
Bram Moolenaar60a795a2005-09-16 21:55:43 +000039let s:cpo_save = &cpo
40set cpo&vim
41
42" 1. Variables {{{1
43" ============
44
Bram Moolenaarec7944a2013-06-12 21:29:15 +020045" 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' .
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020048 \ '\|Interpolation\|InterpolationDelimiter\|NoInterpolation\|Comment\|Documentation\)\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000049
Bram Moolenaar60a795a2005-09-16 21:55:43 +000050" Regex of syntax group names that are strings.
51let s:syng_string =
Bram Moolenaarc236c162008-07-13 17:41:49 +000052 \ '\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\)\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000053
54" Regex of syntax group names that are strings or documentation.
55let s:syng_stringdoc =
Bram Moolenaarec7944a2013-06-12 21:29:15 +020056 \'\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\|Documentation\)\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000057
58" Expression used to check whether we should skip a match with searchpair().
59let s:skip_expr =
Bram Moolenaarc236c162008-07-13 17:41:49 +000060 \ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000061
62" Regex used for words that, at the start of a line, add a level of indent.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020063let 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\):\@!\>' .
Bram Moolenaarec7944a2013-06-12 21:29:15 +020067 \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
68 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000069
70" Regex used for words that, at the start of a line, remove a level of indent.
71let s:ruby_deindent_keywords =
Bram Moolenaarec7944a2013-06-12 21:29:15 +020072 \ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|end\):\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000073
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)?
Bram Moolenaarec7944a2013-06-12 21:29:15 +020077let s:end_start_regex =
78 \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020079 \ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' .
80 \ '\|\%(public\|protected\|private\)\=\s*def\):\@!\>' .
Bram Moolenaarec7944a2013-06-12 21:29:15 +020081 \ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000082
83" Regex that defines the middle-match for the 'end' keyword.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020084let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000085
86" Regex that defines the end-match for the 'end' keyword.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020087let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000088
89" Expression used for searchpair() call for finding match for 'end' keyword.
90let s:end_skip_expr = s:skip_expr .
91 \ ' || (expand("<cword>") == "do"' .
Bram Moolenaarec7944a2013-06-12 21:29:15 +020092 \ ' && getline(".") =~ "^\\s*\\<\\(while\\|until\\|for\\):\\@!\\>")'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000093
94" Regex that defines continuation lines, not including (, {, or [.
Bram Moolenaar45758762016-10-12 23:08:06 +020095let s:non_bracket_continuation_regex =
96 \ '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000097
98" Regex that defines continuation lines.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020099let s:continuation_regex =
Bram Moolenaar45758762016-10-12 23:08:06 +0200100 \ '\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200101
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200102" Regex that defines continuable keywords
103let s:continuable_regex =
104 \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
105 \ '\<\%(if\|for\|while\|until\|unless\):\@!\>'
106
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200107" Regex that defines bracket continuations
108let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'
109
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200110" 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
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200119" Regex that defines the first part of a splat pattern
120let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$'
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000121
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200122" 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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000128" Regex that defines blocks.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200129"
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"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000137let s:block_regex =
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200138 \ '\%(\<do:\@!\>\|%\@<!{\)\s*\%(|[^|]*|\)\=\s*\%(#.*\)\=$'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200139
140let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000141
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200142" Regex that describes a leading operator (only a method call's dot for now)
143let s:leading_operator_regex = '^\s*[.]'
144
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000145" 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)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000150 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000151endfunction
152
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000153" Check if the character at lnum:col is inside a string.
154function s:IsInString(lnum, col)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000155 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000156endfunction
157
158" Check if the character at lnum:col is inside a string or documentation.
159function s:IsInStringOrDocumentation(lnum, col)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000160 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000161endfunction
162
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200163" 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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000168" 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)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200176 if line =~ '^=begin'
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000177 if in_block
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200178 let in_block = 0
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000179 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200180 break
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000181 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200182 elseif !in_block && line =~ '^=end'
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000183 let in_block = 1
184 elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200185 \ && s:IsInStringOrComment(lnum, strlen(line)))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000186 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
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200197 let msl_body = getline(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000198 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)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200203
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200204 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)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200219 " 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
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200228 elseif s:Match(lnum, s:non_bracket_continuation_regex) &&
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200229 \ 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 "
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000238 let msl = lnum
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200239 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
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200251 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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000284 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200285 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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000292 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200293
294 let msl_body = getline(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000295 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.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200301function s:ExtraBrackets(lnum)
302 let opening = {'parentheses': [], 'braces': [], 'brackets': []}
303 let closing = {'parentheses': [], 'braces': [], 'brackets': []}
304
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000305 let line = getline(a:lnum)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200306 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.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000311 while pos != -1
312 if !s:IsInStringOrComment(a:lnum, pos + 1)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200313 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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000337 endif
338 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200339
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000340 let pos = match(line, '[][(){}]', pos + 1)
341 endwhile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200342
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]
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000361endfunction
362
363function s:Match(lnum, regex)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200364 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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000378endfunction
379
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200380" 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
Bram Moolenaar45758762016-10-12 23:08:06 +0200393 endwhile
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200394
395 call setpos('.', saved_position)
396 return 0
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000397endfunction
398
399" 3. GetRubyIndent Function {{{1
400" =========================
401
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200402function GetRubyIndent(...)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000403 " 3.1. Setup {{{2
404 " ----------
405
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200406 " The value of a single shift-width
407 if exists('*shiftwidth')
408 let sw = shiftwidth()
409 else
410 let sw = &sw
411 endif
412
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200413 " For the current line, use the first argument if given, else v:lnum
414 let clnum = a:0 ? a:1 : v:lnum
415
416 " Set up variables for restoring position in file. Could use clnum here.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000417 let vcol = col('.')
418
419 " 3.2. Work on the current line {{{2
420 " -----------------------------
421
422 " Get the current line.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200423 let line = getline(clnum)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000424 let ind = -1
425
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200426 " If this line is an access modifier keyword, align according to the closest
427 " class declaration.
428 if g:ruby_indent_access_modifier_style == 'indent'
429 if s:Match(clnum, s:access_modifier_regex)
430 let class_line = s:FindContainingClass()
431 if class_line > 0
432 return indent(class_line) + sw
433 endif
434 endif
435 elseif g:ruby_indent_access_modifier_style == 'outdent'
436 if s:Match(clnum, s:access_modifier_regex)
437 let class_line = s:FindContainingClass()
438 if class_line > 0
439 return indent(class_line)
440 endif
441 endif
442 endif
443
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000444 " If we got a closing bracket on an empty line, find its match and indent
445 " according to it. For parentheses we indent to its column - 1, for the
446 " others we indent to the containing line's MSL's level. Return -1 if fail.
447 let col = matchend(line, '^\s*[]})]')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200448 if col > 0 && !s:IsInStringOrComment(clnum, col)
449 call cursor(clnum, col)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000450 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
451 if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000452 if line[col-1]==')' && col('.') != col('$') - 1
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200453 let ind = virtcol('.') - 1
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200454 elseif g:ruby_indent_block_style == 'do'
455 let ind = indent(line('.'))
456 else " g:ruby_indent_block_style == 'expression'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200457 let ind = indent(s:GetMSL(line('.')))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000458 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000459 endif
460 return ind
461 endif
462
463 " If we have a =begin or =end set indent to first column.
464 if match(line, '^\s*\%(=begin\|=end\)$') != -1
465 return 0
466 endif
467
468 " If we have a deindenting keyword, find its match and indent to its level.
469 " TODO: this is messy
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200470 if s:Match(clnum, s:ruby_deindent_keywords)
471 call cursor(clnum, 1)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000472 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200473 \ s:end_skip_expr) > 0
474 let msl = s:GetMSL(line('.'))
475 let line = getline(line('.'))
476
Bram Moolenaar1e015462005-09-25 22:16:38 +0000477 if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200478 \ strpart(line, col('.') - 1, 2) !~ 'do'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200479 " assignment to case/begin/etc, on the same line, hanging indent
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200480 let ind = virtcol('.') - 1
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200481 elseif g:ruby_indent_block_style == 'do'
482 " align to line of the "do", not to the MSL
483 let ind = indent(line('.'))
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200484 elseif getline(msl) =~ '=\s*\(#.*\)\=$'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200485 " in the case of assignment to the MSL, align to the starting line,
486 " not to the MSL
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200487 let ind = indent(line('.'))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000488 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200489 " align to the MSL
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200490 let ind = indent(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000491 endif
492 endif
493 return ind
494 endif
495
496 " If we are in a multi-line string or line-comment, don't do anything to it.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200497 if s:IsInStringOrDocumentation(clnum, matchend(line, '^\s*') + 1)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000498 return indent('.')
499 endif
500
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200501 " If we are at the closing delimiter of a "<<" heredoc-style string, set the
502 " indent to 0.
503 if line =~ '^\k\+\s*$'
504 \ && s:IsInStringDelimiter(clnum, 1)
505 \ && search('\V<<'.line, 'nbW') > 0
506 return 0
507 endif
508
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200509 " If the current line starts with a leading operator, add a level of indent.
510 if s:Match(clnum, s:leading_operator_regex)
511 return indent(s:GetMSL(clnum)) + sw
512 endif
513
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000514 " 3.3. Work on the previous line. {{{2
515 " -------------------------------
516
517 " Find a non-blank, non-multi-line string line above the current line.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200518 let lnum = s:PrevNonBlankNonString(clnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
Bram Moolenaarc236c162008-07-13 17:41:49 +0000520 " If the line is empty and inside a string, use the previous line.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200521 if line =~ '^\s*$' && lnum != prevnonblank(clnum - 1)
522 return indent(prevnonblank(clnum))
Bram Moolenaarc236c162008-07-13 17:41:49 +0000523 endif
524
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 " At the start of the file use zero indent.
526 if lnum == 0
527 return 0
528 endif
529
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200530 " Set up variables for the previous line.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000531 let line = getline(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 let ind = indent(lnum)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000533
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200534 if g:ruby_indent_access_modifier_style == 'indent'
535 " If the previous line was a private/protected keyword, add a
536 " level of indent.
537 if s:Match(lnum, s:indent_access_modifier_regex)
538 return indent(lnum) + sw
539 endif
540 elseif g:ruby_indent_access_modifier_style == 'outdent'
541 " If the previous line was a private/protected/public keyword, add
542 " a level of indent, since the keyword has been out-dented.
543 if s:Match(lnum, s:access_modifier_regex)
544 return indent(lnum) + sw
545 endif
546 endif
547
548 if s:Match(lnum, s:continuable_regex) && s:Match(lnum, s:continuation_regex)
549 return indent(s:GetMSL(lnum)) + sw + sw
550 endif
551
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000552 " If the previous line ended with a block opening, add a level of indent.
553 if s:Match(lnum, s:block_regex)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200554 let msl = s:GetMSL(lnum)
555
556 if g:ruby_indent_block_style == 'do'
557 " don't align to the msl, align to the "do"
558 let ind = indent(lnum) + sw
559 elseif getline(msl) =~ '=\s*\(#.*\)\=$'
560 " in the case of assignment to the msl, align to the starting line,
561 " not to the msl
562 let ind = indent(lnum) + sw
563 else
564 let ind = indent(msl) + sw
565 endif
566 return ind
567 endif
568
569 " If the previous line started with a leading operator, use its MSL's level
570 " of indent
571 if s:Match(lnum, s:leading_operator_regex)
572 return indent(s:GetMSL(lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 endif
574
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200575 " If the previous line ended with the "*" of a splat, add a level of indent
576 if line =~ s:splat_regex
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200577 return indent(lnum) + sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200578 endif
579
580 " If the previous line contained unclosed opening brackets and we are still
581 " in them, find the rightmost one and add indent depending on the bracket
582 " type.
583 "
584 " If it contained hanging closing brackets, find the rightmost one, find its
585 " match and indent according to that.
586 if line =~ '[[({]' || line =~ '[])}]\s*\%(#.*\)\=$'
587 let [opening, closing] = s:ExtraBrackets(lnum)
588
589 if opening.pos != -1
590 if opening.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
591 if col('.') + 1 == col('$')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200592 return ind + sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200593 else
594 return virtcol('.')
595 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000596 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200597 let nonspace = matchend(line, '\S', opening.pos + 1) - 1
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200598 return nonspace > 0 ? nonspace : ind + sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000599 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200600 elseif closing.pos != -1
601 call cursor(lnum, closing.pos + 1)
602 normal! %
603
604 if s:Match(line('.'), s:ruby_indent_keywords)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200605 return indent('.') + sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200606 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200607 return indent(s:GetMSL(line('.')))
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200608 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000609 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200610 call cursor(clnum, vcol)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000611 end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 endif
613
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000614 " If the previous line ended with an "end", match that "end"s beginning's
615 " indent.
Bram Moolenaar720c7102007-05-10 18:07:50 +0000616 let col = s:Match(lnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000617 if col > 0
618 call cursor(lnum, col)
619 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200620 \ s:end_skip_expr) > 0
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000621 let n = line('.')
622 let ind = indent('.')
623 let msl = s:GetMSL(n)
624 if msl != n
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200625 let ind = indent(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000626 end
627 return ind
628 endif
629 end
630
631 let col = s:Match(lnum, s:ruby_indent_keywords)
632 if col > 0
633 call cursor(lnum, col)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200634 let ind = virtcol('.') - 1 + sw
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000635 " TODO: make this better (we need to count them) (or, if a searchpair
636 " fails, we know that something is lacking an end and thus we indent a
637 " level
638 if s:Match(lnum, s:end_end_regex)
639 let ind = indent('.')
640 endif
641 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 endif
643
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000644 " 3.4. Work on the MSL line. {{{2
645 " --------------------------
646
647 " Set up variables to use and search for MSL to the previous line.
648 let p_lnum = lnum
649 let lnum = s:GetMSL(lnum)
650
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200651 " If the previous line wasn't a MSL.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000652 if p_lnum != lnum
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200653 " If previous line ends bracket and begins non-bracket continuation decrease indent by 1.
654 if s:Match(p_lnum, s:bracket_switch_continuation_regex)
655 return ind - 1
656 " If previous line is a continuation return its indent.
657 " TODO: the || s:IsInString() thing worries me a bit.
658 elseif s:Match(p_lnum, s:non_bracket_continuation_regex) || s:IsInString(p_lnum,strlen(line))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000659 return ind
660 endif
661 endif
662
663 " Set up more variables, now that we know we wasn't continuation bound.
664 let line = getline(lnum)
665 let msl_ind = indent(lnum)
666
667 " If the MSL line had an indenting keyword in it, add a level of indent.
668 " TODO: this does not take into account contrived things such as
669 " module Foo; class Bar; end
670 if s:Match(lnum, s:ruby_indent_keywords)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200671 let ind = msl_ind + sw
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000672 if s:Match(lnum, s:end_end_regex)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200673 let ind = ind - sw
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000674 endif
675 return ind
676 endif
677
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200678 " If the previous line ended with [*+/.,-=], but wasn't a block ending or a
679 " closing bracket, indent one extra level.
680 if s:Match(lnum, s:non_bracket_continuation_regex) && !s:Match(lnum, '^\s*\([\])}]\|end\)')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000681 if lnum == p_lnum
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200682 let ind = msl_ind + sw
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000683 else
684 let ind = msl_ind
685 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200686 return ind
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000687 endif
688
689 " }}}2
690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 return ind
692endfunction
693
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000694" }}}1
695
696let &cpo = s:cpo_save
697unlet s:cpo_save
Bram Moolenaar9964e462007-05-05 17:54:07 +0000698
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200699" vim:set sw=2 sts=2 ts=8 et: