3 " Maintainer: Nikolai Weibull <now at bitwi.se>
4 " Info: $Id: ruby.vim,v 1.47 2008/06/29 04:18:43 tpope Exp $
5 " URL: http://vim-ruby.rubyforge.org
6 " Anon CVS: See above site
7 " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
9 " 0. Initialization {{{1
12 " Only load this indent file when no other was loaded.
13 if exists("b:did_indent")
18 setlocal nosmartindent
20 " Now, set up our indentation expression and keys that trigger it.
21 setlocal indentexpr=GetRubyIndent()
22 setlocal indentkeys=0{,0},0),0],!^F,o,O,e
23 setlocal indentkeys+==end,=elsif,=when,=ensure,=rescue,==begin,==end
25 " Only define the function once.
26 if exists("*GetRubyIndent")
36 " Regex of syntax group names that are or delimit string or are comments.
37 let s:syng_strcom = '\<ruby\%(String\|StringEscape\|ASCIICode' .
38 \ '\|Interpolation\|NoInterpolation\|Comment\|Documentation\)\>'
40 " Regex of syntax group names that are strings.
42 \ '\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\)\>'
44 " Regex of syntax group names that are strings or documentation.
45 let s:syng_stringdoc =
46 \'\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\|Documentation\)\>'
48 " Expression used to check whether we should skip a match with searchpair().
50 \ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
52 " Regex used for words that, at the start of a line, add a level of indent.
53 let s:ruby_indent_keywords = '^\s*\zs\<\%(module\|class\|def\|if\|for' .
54 \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure' .
56 \ '\|\%([*+/,=-]\|<<\|>>\|:\s\)\s*\zs' .
57 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\)\>'
59 " Regex used for words that, at the start of a line, remove a level of indent.
60 let s:ruby_deindent_keywords =
61 \ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|end\)\>'
63 " Regex that defines the start-match for the 'end' keyword.
64 "let s:end_start_regex = '\%(^\|[^.]\)\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\|do\)\>'
65 " TODO: the do here should be restricted somewhat (only at end of line)?
66 let s:end_start_regex = '^\s*\zs\<\%(module\|class\|def\|if\|for' .
67 \ '\|while\|until\|case\|unless\|begin\)\>' .
68 \ '\|\%([*+/,=-]\|<<\|>>\|:\s\)\s*\zs' .
69 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\)\>' .
72 " Regex that defines the middle-match for the 'end' keyword.
73 let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue\>\|when\|elsif\)\>'
75 " Regex that defines the end-match for the 'end' keyword.
76 let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end\>'
78 " Expression used for searchpair() call for finding match for 'end' keyword.
79 let s:end_skip_expr = s:skip_expr .
80 \ ' || (expand("<cword>") == "do"' .
81 \ ' && getline(".") =~ "^\\s*\\<\\(while\\|until\\|for\\)\\>")'
83 " Regex that defines continuation lines, not including (, {, or [.
84 let s:continuation_regex = '\%([\\*+/.,:]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
86 " Regex that defines continuation lines.
87 " TODO: this needs to deal with if ...: and so on
88 let s:continuation_regex2 =
89 \ '\%([\\*+/.,:({[]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
91 " Regex that defines blocks.
93 \ '\%(\<do\>\|{\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=\s*\%(#.*\)\=$'
95 " 2. Auxiliary Functions {{{1
96 " ======================
98 " Check if the character at lnum:col is inside a string, comment, or is ascii.
99 function s:IsInStringOrComment(lnum, col)
100 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
103 " Check if the character at lnum:col is inside a string.
104 function s:IsInString(lnum, col)
105 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
108 " Check if the character at lnum:col is inside a string or documentation.
109 function s:IsInStringOrDocumentation(lnum, col)
110 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
113 " Find line above 'lnum' that isn't empty, in a comment, or in a string.
114 function s:PrevNonBlankNonString(lnum)
116 let lnum = prevnonblank(a:lnum)
118 " Go in and out of blocks comments as necessary.
119 " If the line isn't empty (with opt. comment) or in a string, end search.
120 let line = getline(lnum)
121 if line =~ '^=begin$'
127 elseif !in_block && line =~ '^=end$'
129 elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
130 \ && s:IsInStringOrComment(lnum, strlen(line)))
133 let lnum = prevnonblank(lnum - 1)
138 " Find line above 'lnum' that started the continuation 'lnum' may be part of.
139 function s:GetMSL(lnum)
140 " Start on the line we're at and use its indent.
142 let lnum = s:PrevNonBlankNonString(a:lnum - 1)
144 " If we have a continuation line, or we're in a string, use line as MSL.
145 " Otherwise, terminate search as we have found our MSL already.
146 let line = getline(lnum)
147 let col = match(line, s:continuation_regex2) + 1
148 if (col > 0 && !s:IsInStringOrComment(lnum, col))
149 \ || s:IsInString(lnum, strlen(line))
154 let lnum = s:PrevNonBlankNonString(lnum - 1)
159 " Check if line 'lnum' has more opening brackets than closing ones.
160 function s:LineHasOpeningBrackets(lnum)
164 let line = getline(a:lnum)
165 let pos = match(line, '[][(){}]', 0)
167 if !s:IsInStringOrComment(a:lnum, pos + 1)
168 let idx = stridx('(){}[]', line[pos])
170 let open_{idx} = open_{idx} + 1
172 let open_{idx - 1} = open_{idx - 1} - 1
175 let pos = match(line, '[][(){}]', pos + 1)
177 return (open_0 > 0) . (open_2 > 0) . (open_4 > 0)
180 function s:Match(lnum, regex)
181 let col = match(getline(a:lnum), a:regex) + 1
182 return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
185 function s:MatchLast(lnum, regex)
186 let line = getline(a:lnum)
187 let col = match(line, '.*\zs' . a:regex)
188 while col != -1 && s:IsInStringOrComment(a:lnum, col)
189 let line = strpart(line, 0, col)
190 let col = match(line, '.*' . a:regex)
195 " 3. GetRubyIndent Function {{{1
196 " =========================
198 function GetRubyIndent()
202 " Set up variables for restoring position in file. Could use v:lnum here.
205 " 3.2. Work on the current line {{{2
206 " -----------------------------
208 " Get the current line.
209 let line = getline(v:lnum)
212 " If we got a closing bracket on an empty line, find its match and indent
213 " according to it. For parentheses we indent to its column - 1, for the
214 " others we indent to the containing line's MSL's level. Return -1 if fail.
215 let col = matchend(line, '^\s*[]})]')
216 if col > 0 && !s:IsInStringOrComment(v:lnum, col)
217 call cursor(v:lnum, col)
218 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
219 if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
220 if line[col-1]==')' && col('.') != col('$') - 1
221 let ind = virtcol('.')-1
223 let ind = indent(s:GetMSL(line('.')))
229 " If we have a =begin or =end set indent to first column.
230 if match(line, '^\s*\%(=begin\|=end\)$') != -1
234 " If we have a deindenting keyword, find its match and indent to its level.
235 " TODO: this is messy
236 if s:Match(v:lnum, s:ruby_deindent_keywords)
237 call cursor(v:lnum, 1)
238 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
239 \ s:end_skip_expr) > 0
240 let line = getline('.')
241 if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
242 \ strpart(line, col('.') - 1, 2) !~ 'do'
243 let ind = virtcol('.') - 1
245 let ind = indent('.')
251 " If we are in a multi-line string or line-comment, don't do anything to it.
252 if s:IsInStringOrDocumentation(v:lnum, matchend(line, '^\s*') + 1)
256 " 3.3. Work on the previous line. {{{2
257 " -------------------------------
259 " Find a non-blank, non-multi-line string line above the current line.
260 let lnum = s:PrevNonBlankNonString(v:lnum - 1)
262 " If the line is empty and inside a string, use the previous line.
263 if line =~ '^\s*$' && lnum != prevnonblank(v:lnum - 1)
264 return indent(prevnonblank(v:lnum))
267 " At the start of the file use zero indent.
272 " Set up variables for current line.
273 let line = getline(lnum)
274 let ind = indent(lnum)
276 " If the previous line ended with a block opening, add a level of indent.
277 if s:Match(lnum, s:block_regex)
278 return indent(s:GetMSL(lnum)) + &sw
281 " If the previous line contained an opening bracket, and we are still in it,
282 " add indent depending on the bracket type.
284 let counts = s:LineHasOpeningBrackets(lnum)
285 if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
286 if col('.') + 1 == col('$')
291 elseif counts[1] == '1' || counts[2] == '1'
294 call cursor(v:lnum, vcol)
298 " If the previous line ended with an "end", match that "end"s beginning's
300 let col = s:Match(lnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
302 call cursor(lnum, col)
303 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
304 \ s:end_skip_expr) > 0
306 let ind = indent('.')
307 let msl = s:GetMSL(n)
309 let ind = indent(msl)
315 let col = s:Match(lnum, s:ruby_indent_keywords)
317 call cursor(lnum, col)
318 let ind = virtcol('.') - 1 + &sw
319 " let ind = indent(lnum) + &sw
320 " TODO: make this better (we need to count them) (or, if a searchpair
321 " fails, we know that something is lacking an end and thus we indent a
323 if s:Match(lnum, s:end_end_regex)
324 let ind = indent('.')
329 " 3.4. Work on the MSL line. {{{2
330 " --------------------------
332 " Set up variables to use and search for MSL to the previous line.
334 let lnum = s:GetMSL(lnum)
336 " If the previous line wasn't a MSL and is continuation return its indent.
337 " TODO: the || s:IsInString() thing worries me a bit.
339 if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line))
344 " Set up more variables, now that we know we wasn't continuation bound.
345 let line = getline(lnum)
346 let msl_ind = indent(lnum)
348 " If the MSL line had an indenting keyword in it, add a level of indent.
349 " TODO: this does not take into account contrived things such as
350 " module Foo; class Bar; end
351 if s:Match(lnum, s:ruby_indent_keywords)
352 let ind = msl_ind + &sw
353 if s:Match(lnum, s:end_end_regex)
359 " If the previous line ended with [*+/.-=], indent one extra level.
360 if s:Match(lnum, s:continuation_regex)
362 let ind = msl_ind + &sw
375 let &cpo = s:cpo_save
378 " vim:set sw=2 sts=2 ts=8 noet: