blob: 095b3a43c6624329af3c404a5e2dc0245d07829a [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 Moolenaar551dbcc2006-04-25 22:13:59 +000016setlocal nosmartindent
17
Bram Moolenaar60a795a2005-09-16 21:55:43 +000018" Now, set up our indentation expression and keys that trigger it.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020019setlocal indentexpr=GetRubyIndent(v:lnum)
Bram Moolenaar60a795a2005-09-16 21:55:43 +000020setlocal indentkeys=0{,0},0),0],!^F,o,O,e
Bram Moolenaarec7944a2013-06-12 21:29:15 +020021setlocal indentkeys+==end,=else,=elsif,=when,=ensure,=rescue,==begin,==end
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
23" Only define the function once.
24if exists("*GetRubyIndent")
25 finish
26endif
27
Bram Moolenaar60a795a2005-09-16 21:55:43 +000028let s:cpo_save = &cpo
29set cpo&vim
30
31" 1. Variables {{{1
32" ============
33
Bram Moolenaarec7944a2013-06-12 21:29:15 +020034" Regex of syntax group names that are or delimit strings/symbols or are comments.
35let s:syng_strcom = '\<ruby\%(Regexp\|RegexpDelimiter\|RegexpEscape' .
36 \ '\|Symbol\|String\|StringDelimiter\|StringEscape\|ASCIICode' .
Bram Moolenaarc236c162008-07-13 17:41:49 +000037 \ '\|Interpolation\|NoInterpolation\|Comment\|Documentation\)\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000038
Bram Moolenaar60a795a2005-09-16 21:55:43 +000039" Regex of syntax group names that are strings.
40let s:syng_string =
Bram Moolenaarc236c162008-07-13 17:41:49 +000041 \ '\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\)\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000042
43" Regex of syntax group names that are strings or documentation.
44let s:syng_stringdoc =
Bram Moolenaarec7944a2013-06-12 21:29:15 +020045 \'\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\|Documentation\)\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000046
47" Expression used to check whether we should skip a match with searchpair().
48let s:skip_expr =
Bram Moolenaarc236c162008-07-13 17:41:49 +000049 \ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000050
51" Regex used for words that, at the start of a line, add a level of indent.
52let s:ruby_indent_keywords = '^\s*\zs\<\%(module\|class\|def\|if\|for' .
53 \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure' .
Bram Moolenaarec7944a2013-06-12 21:29:15 +020054 \ '\|rescue\):\@!\>' .
55 \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
56 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000057
58" Regex used for words that, at the start of a line, remove a level of indent.
59let s:ruby_deindent_keywords =
Bram Moolenaarec7944a2013-06-12 21:29:15 +020060 \ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|end\):\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000061
62" Regex that defines the start-match for the 'end' keyword.
63"let s:end_start_regex = '\%(^\|[^.]\)\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\|do\)\>'
64" TODO: the do here should be restricted somewhat (only at end of line)?
Bram Moolenaarec7944a2013-06-12 21:29:15 +020065let s:end_start_regex =
66 \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
67 \ '\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\):\@!\>' .
68 \ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000069
70" Regex that defines the middle-match for the 'end' keyword.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020071let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000072
73" Regex that defines the end-match for the 'end' keyword.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020074let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000075
76" Expression used for searchpair() call for finding match for 'end' keyword.
77let s:end_skip_expr = s:skip_expr .
78 \ ' || (expand("<cword>") == "do"' .
Bram Moolenaarec7944a2013-06-12 21:29:15 +020079 \ ' && getline(".") =~ "^\\s*\\<\\(while\\|until\\|for\\):\\@!\\>")'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000080
81" Regex that defines continuation lines, not including (, {, or [.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020082let s:non_bracket_continuation_regex = '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000083
84" Regex that defines continuation lines.
85" TODO: this needs to deal with if ...: and so on
Bram Moolenaarec7944a2013-06-12 21:29:15 +020086let s:continuation_regex =
87 \ '\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
88
89" Regex that defines bracket continuations
90let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'
91
92" Regex that defines the first part of a splat pattern
93let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000094
95" Regex that defines blocks.
Bram Moolenaarec7944a2013-06-12 21:29:15 +020096"
97" Note that there's a slight problem with this regex and s:continuation_regex.
98" Code like this will be matched by both:
99"
100" method_call do |(a, b)|
101"
102" The reason is that the pipe matches a hanging "|" operator.
103"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000104let s:block_regex =
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200105 \ '\%(\<do:\@!\>\|%\@<!{\)\s*\%(|\s*(*\s*\%([*@&]\=\h\w*,\=\s*\)\%(,\s*(*\s*[*@&]\=\h\w*\s*)*\s*\)*|\)\=\s*\%(#.*\)\=$'
106
107let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000108
109" 2. Auxiliary Functions {{{1
110" ======================
111
112" Check if the character at lnum:col is inside a string, comment, or is ascii.
113function s:IsInStringOrComment(lnum, col)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000114 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000115endfunction
116
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000117" Check if the character at lnum:col is inside a string.
118function s:IsInString(lnum, col)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000119 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000120endfunction
121
122" Check if the character at lnum:col is inside a string or documentation.
123function s:IsInStringOrDocumentation(lnum, col)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000124 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000125endfunction
126
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200127" Check if the character at lnum:col is inside a string delimiter
128function s:IsInStringDelimiter(lnum, col)
129 return synIDattr(synID(a:lnum, a:col, 1), 'name') == 'rubyStringDelimiter'
130endfunction
131
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000132" Find line above 'lnum' that isn't empty, in a comment, or in a string.
133function s:PrevNonBlankNonString(lnum)
134 let in_block = 0
135 let lnum = prevnonblank(a:lnum)
136 while lnum > 0
137 " Go in and out of blocks comments as necessary.
138 " If the line isn't empty (with opt. comment) or in a string, end search.
139 let line = getline(lnum)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200140 if line =~ '^=begin'
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000141 if in_block
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200142 let in_block = 0
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000143 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200144 break
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000145 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200146 elseif !in_block && line =~ '^=end'
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000147 let in_block = 1
148 elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200149 \ && s:IsInStringOrComment(lnum, strlen(line)))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000150 break
151 endif
152 let lnum = prevnonblank(lnum - 1)
153 endwhile
154 return lnum
155endfunction
156
157" Find line above 'lnum' that started the continuation 'lnum' may be part of.
158function s:GetMSL(lnum)
159 " Start on the line we're at and use its indent.
160 let msl = a:lnum
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200161 let msl_body = getline(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000162 let lnum = s:PrevNonBlankNonString(a:lnum - 1)
163 while lnum > 0
164 " If we have a continuation line, or we're in a string, use line as MSL.
165 " Otherwise, terminate search as we have found our MSL already.
166 let line = getline(lnum)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200167
168 if s:Match(lnum, s:splat_regex)
169 " If the above line looks like the "*" of a splat, use the current one's
170 " indentation.
171 "
172 " Example:
173 " Hash[*
174 " method_call do
175 " something
176 "
177 return msl
178 elseif s:Match(line, s:non_bracket_continuation_regex) &&
179 \ s:Match(msl, s:non_bracket_continuation_regex)
180 " If the current line is a non-bracket continuation and so is the
181 " previous one, keep its indent and continue looking for an MSL.
182 "
183 " Example:
184 " method_call one,
185 " two,
186 " three
187 "
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000188 let msl = lnum
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200189 elseif s:Match(lnum, s:non_bracket_continuation_regex) &&
190 \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
191 " If the current line is a bracket continuation or a block-starter, but
192 " the previous is a non-bracket one, respect the previous' indentation,
193 " and stop here.
194 "
195 " Example:
196 " method_call one,
197 " two {
198 " three
199 "
200 return lnum
201 elseif s:Match(lnum, s:bracket_continuation_regex) &&
202 \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
203 " If both lines are bracket continuations (the current may also be a
204 " block-starter), use the current one's and stop here
205 "
206 " Example:
207 " method_call(
208 " other_method_call(
209 " foo
210 return msl
211 elseif s:Match(lnum, s:block_regex) &&
212 \ !s:Match(msl, s:continuation_regex) &&
213 \ !s:Match(msl, s:block_continuation_regex)
214 " If the previous line is a block-starter and the current one is
215 " mostly ordinary, use the current one as the MSL.
216 "
217 " Example:
218 " method_call do
219 " something
220 " something_else
221 return msl
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000222 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200223 let col = match(line, s:continuation_regex) + 1
224 if (col > 0 && !s:IsInStringOrComment(lnum, col))
225 \ || s:IsInString(lnum, strlen(line))
226 let msl = lnum
227 else
228 break
229 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000230 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200231
232 let msl_body = getline(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000233 let lnum = s:PrevNonBlankNonString(lnum - 1)
234 endwhile
235 return msl
236endfunction
237
238" Check if line 'lnum' has more opening brackets than closing ones.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200239function s:ExtraBrackets(lnum)
240 let opening = {'parentheses': [], 'braces': [], 'brackets': []}
241 let closing = {'parentheses': [], 'braces': [], 'brackets': []}
242
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000243 let line = getline(a:lnum)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200244 let pos = match(line, '[][(){}]', 0)
245
246 " Save any encountered opening brackets, and remove them once a matching
247 " closing one has been found. If a closing bracket shows up that doesn't
248 " close anything, save it for later.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000249 while pos != -1
250 if !s:IsInStringOrComment(a:lnum, pos + 1)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200251 if line[pos] == '('
252 call add(opening.parentheses, {'type': '(', 'pos': pos})
253 elseif line[pos] == ')'
254 if empty(opening.parentheses)
255 call add(closing.parentheses, {'type': ')', 'pos': pos})
256 else
257 let opening.parentheses = opening.parentheses[0:-2]
258 endif
259 elseif line[pos] == '{'
260 call add(opening.braces, {'type': '{', 'pos': pos})
261 elseif line[pos] == '}'
262 if empty(opening.braces)
263 call add(closing.braces, {'type': '}', 'pos': pos})
264 else
265 let opening.braces = opening.braces[0:-2]
266 endif
267 elseif line[pos] == '['
268 call add(opening.brackets, {'type': '[', 'pos': pos})
269 elseif line[pos] == ']'
270 if empty(opening.brackets)
271 call add(closing.brackets, {'type': ']', 'pos': pos})
272 else
273 let opening.brackets = opening.brackets[0:-2]
274 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000275 endif
276 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200277
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000278 let pos = match(line, '[][(){}]', pos + 1)
279 endwhile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200280
281 " Find the rightmost brackets, since they're the ones that are important in
282 " both opening and closing cases
283 let rightmost_opening = {'type': '(', 'pos': -1}
284 let rightmost_closing = {'type': ')', 'pos': -1}
285
286 for opening in opening.parentheses + opening.braces + opening.brackets
287 if opening.pos > rightmost_opening.pos
288 let rightmost_opening = opening
289 endif
290 endfor
291
292 for closing in closing.parentheses + closing.braces + closing.brackets
293 if closing.pos > rightmost_closing.pos
294 let rightmost_closing = closing
295 endif
296 endfor
297
298 return [rightmost_opening, rightmost_closing]
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000299endfunction
300
301function s:Match(lnum, regex)
Bram Moolenaar1d689522010-05-28 20:54:39 +0200302 let col = match(getline(a:lnum), '\C'.a:regex) + 1
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000303 return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
304endfunction
305
306function s:MatchLast(lnum, regex)
307 let line = getline(a:lnum)
308 let col = match(line, '.*\zs' . a:regex)
309 while col != -1 && s:IsInStringOrComment(a:lnum, col)
310 let line = strpart(line, 0, col)
311 let col = match(line, '.*' . a:regex)
312 endwhile
313 return col + 1
314endfunction
315
316" 3. GetRubyIndent Function {{{1
317" =========================
318
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200319function GetRubyIndent(...)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000320 " 3.1. Setup {{{2
321 " ----------
322
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200323 " For the current line, use the first argument if given, else v:lnum
324 let clnum = a:0 ? a:1 : v:lnum
325
326 " Set up variables for restoring position in file. Could use clnum here.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000327 let vcol = col('.')
328
329 " 3.2. Work on the current line {{{2
330 " -----------------------------
331
332 " Get the current line.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200333 let line = getline(clnum)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000334 let ind = -1
335
336 " If we got a closing bracket on an empty line, find its match and indent
337 " according to it. For parentheses we indent to its column - 1, for the
338 " others we indent to the containing line's MSL's level. Return -1 if fail.
339 let col = matchend(line, '^\s*[]})]')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200340 if col > 0 && !s:IsInStringOrComment(clnum, col)
341 call cursor(clnum, col)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000342 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
343 if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000344 if line[col-1]==')' && col('.') != col('$') - 1
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200345 let ind = virtcol('.') - 1
Bram Moolenaar9964e462007-05-05 17:54:07 +0000346 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200347 let ind = indent(s:GetMSL(line('.')))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000348 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000349 endif
350 return ind
351 endif
352
353 " If we have a =begin or =end set indent to first column.
354 if match(line, '^\s*\%(=begin\|=end\)$') != -1
355 return 0
356 endif
357
358 " If we have a deindenting keyword, find its match and indent to its level.
359 " TODO: this is messy
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200360 if s:Match(clnum, s:ruby_deindent_keywords)
361 call cursor(clnum, 1)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000362 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200363 \ s:end_skip_expr) > 0
364 let msl = s:GetMSL(line('.'))
365 let line = getline(line('.'))
366
Bram Moolenaar1e015462005-09-25 22:16:38 +0000367 if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200368 \ strpart(line, col('.') - 1, 2) !~ 'do'
369 let ind = virtcol('.') - 1
370 elseif getline(msl) =~ '=\s*\(#.*\)\=$'
371 let ind = indent(line('.'))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000372 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200373 let ind = indent(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000374 endif
375 endif
376 return ind
377 endif
378
379 " If we are in a multi-line string or line-comment, don't do anything to it.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200380 if s:IsInStringOrDocumentation(clnum, matchend(line, '^\s*') + 1)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000381 return indent('.')
382 endif
383
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200384 " If we are at the closing delimiter of a "<<" heredoc-style string, set the
385 " indent to 0.
386 if line =~ '^\k\+\s*$'
387 \ && s:IsInStringDelimiter(clnum, 1)
388 \ && search('\V<<'.line, 'nbW') > 0
389 return 0
390 endif
391
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000392 " 3.3. Work on the previous line. {{{2
393 " -------------------------------
394
395 " Find a non-blank, non-multi-line string line above the current line.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200396 let lnum = s:PrevNonBlankNonString(clnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
Bram Moolenaarc236c162008-07-13 17:41:49 +0000398 " If the line is empty and inside a string, use the previous line.
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200399 if line =~ '^\s*$' && lnum != prevnonblank(clnum - 1)
400 return indent(prevnonblank(clnum))
Bram Moolenaarc236c162008-07-13 17:41:49 +0000401 endif
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 " At the start of the file use zero indent.
404 if lnum == 0
405 return 0
406 endif
407
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200408 " Set up variables for the previous line.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000409 let line = getline(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 let ind = indent(lnum)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000411
412 " If the previous line ended with a block opening, add a level of indent.
413 if s:Match(lnum, s:block_regex)
414 return indent(s:GetMSL(lnum)) + &sw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 endif
416
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200417 " If the previous line ended with the "*" of a splat, add a level of indent
418 if line =~ s:splat_regex
419 return indent(lnum) + &sw
420 endif
421
422 " If the previous line contained unclosed opening brackets and we are still
423 " in them, find the rightmost one and add indent depending on the bracket
424 " type.
425 "
426 " If it contained hanging closing brackets, find the rightmost one, find its
427 " match and indent according to that.
428 if line =~ '[[({]' || line =~ '[])}]\s*\%(#.*\)\=$'
429 let [opening, closing] = s:ExtraBrackets(lnum)
430
431 if opening.pos != -1
432 if opening.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
433 if col('.') + 1 == col('$')
434 return ind + &sw
435 else
436 return virtcol('.')
437 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000438 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200439 let nonspace = matchend(line, '\S', opening.pos + 1) - 1
440 return nonspace > 0 ? nonspace : ind + &sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000441 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200442 elseif closing.pos != -1
443 call cursor(lnum, closing.pos + 1)
444 normal! %
445
446 if s:Match(line('.'), s:ruby_indent_keywords)
447 return indent('.') + &sw
448 else
449 return indent('.')
450 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000451 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200452 call cursor(clnum, vcol)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000453 end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 endif
455
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000456 " If the previous line ended with an "end", match that "end"s beginning's
457 " indent.
Bram Moolenaar720c7102007-05-10 18:07:50 +0000458 let col = s:Match(lnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000459 if col > 0
460 call cursor(lnum, col)
461 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200462 \ s:end_skip_expr) > 0
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000463 let n = line('.')
464 let ind = indent('.')
465 let msl = s:GetMSL(n)
466 if msl != n
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200467 let ind = indent(msl)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000468 end
469 return ind
470 endif
471 end
472
473 let col = s:Match(lnum, s:ruby_indent_keywords)
474 if col > 0
475 call cursor(lnum, col)
476 let ind = virtcol('.') - 1 + &sw
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000477 " TODO: make this better (we need to count them) (or, if a searchpair
478 " fails, we know that something is lacking an end and thus we indent a
479 " level
480 if s:Match(lnum, s:end_end_regex)
481 let ind = indent('.')
482 endif
483 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 endif
485
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000486 " 3.4. Work on the MSL line. {{{2
487 " --------------------------
488
489 " Set up variables to use and search for MSL to the previous line.
490 let p_lnum = lnum
491 let lnum = s:GetMSL(lnum)
492
493 " If the previous line wasn't a MSL and is continuation return its indent.
494 " TODO: the || s:IsInString() thing worries me a bit.
495 if p_lnum != lnum
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200496 if s:Match(p_lnum, s:non_bracket_continuation_regex) || s:IsInString(p_lnum,strlen(line))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000497 return ind
498 endif
499 endif
500
501 " Set up more variables, now that we know we wasn't continuation bound.
502 let line = getline(lnum)
503 let msl_ind = indent(lnum)
504
505 " If the MSL line had an indenting keyword in it, add a level of indent.
506 " TODO: this does not take into account contrived things such as
507 " module Foo; class Bar; end
508 if s:Match(lnum, s:ruby_indent_keywords)
509 let ind = msl_ind + &sw
510 if s:Match(lnum, s:end_end_regex)
511 let ind = ind - &sw
512 endif
513 return ind
514 endif
515
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200516 " If the previous line ended with [*+/.,-=], but wasn't a block ending or a
517 " closing bracket, indent one extra level.
518 if s:Match(lnum, s:non_bracket_continuation_regex) && !s:Match(lnum, '^\s*\([\])}]\|end\)')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000519 if lnum == p_lnum
520 let ind = msl_ind + &sw
521 else
522 let ind = msl_ind
523 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200524 return ind
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000525 endif
526
527 " }}}2
528
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 return ind
530endfunction
531
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000532" }}}1
533
534let &cpo = s:cpo_save
535unlet s:cpo_save
Bram Moolenaar9964e462007-05-05 17:54:07 +0000536
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200537" vim:set sw=2 sts=2 ts=8 et: