xref: /vim-8.2.3635/runtime/indent/ruby.vim (revision b4d39282)
1" Vim indent file
2" Language:	Ruby
3" Maintainer:	Gavin Sinclair <gsinclair at soyabean.com.au>
4" Developer:	Nikolai Weibull <source at pcppopper.org>
5" Info:		$Id$
6" URL:		http://vim-ruby.rubyforge.org
7" Anon CVS:	See above site
8" ----------------------------------------------------------------------------
9
10" 0. Initialization {{{1
11" =================
12
13" Only load this indent file when no other was loaded.
14if exists("b:did_indent")
15  finish
16endif
17let b:did_indent = 1
18
19" Now, set up our indentation expression and keys that trigger it.
20setlocal indentexpr=GetRubyIndent()
21setlocal indentkeys=0{,0},0),0],!^F,o,O,e
22setlocal indentkeys+==end,=elsif,=when,=ensure,=rescue,==begin,==end
23
24" Only define the function once.
25if exists("*GetRubyIndent")
26  finish
27endif
28
29let s:cpo_save = &cpo
30set cpo&vim
31
32" 1. Variables {{{1
33" ============
34
35" Regex of syntax group names that are or delimit string or are comments.
36let s:syng_strcom = '\<ruby\%(String\|StringDelimiter\|ASCIICode' .
37      \ '\|Interpolation\|NoInterpolation\|Escape\|Comment\|Documentation\)\>'
38
39" Regex of syntax group names that are strings.
40let s:syng_string =
41      \ '\<ruby\%(String\|StringDelimiter\|Interpolation\|NoInterpolation\|Escape\)\>'
42
43" Regex of syntax group names that are strings or documentation.
44let s:syng_stringdoc =
45  \'\<ruby\%(String\|StringDelimiter\|Interpolation\|NoInterpolation\|Escape\|Documentation\)\>'
46
47" Expression used to check whether we should skip a match with searchpair().
48let s:skip_expr =
49      \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '".s:syng_strcom."'"
50
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' .
54      \ '\|rescue\)\>' .
55      \ '\|\%([*+/,=:-]\|<<\|>>\)\s*\zs' .
56      \    '\<\%(if\|for\|while\|until\|case\|unless\|begin\)\>'
57
58" Regex used for words that, at the start of a line, remove a level of indent.
59let s:ruby_deindent_keywords =
60      \ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|end\)\>'
61
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)?
65let s:end_start_regex = '^\s*\zs\<\%(module\|class\|def\|if\|for' .
66      \ '\|while\|until\|case\|unless\|begin\)\>' .
67      \ '\|\%([*+/,=:-]\|<<\|>>\)\s*\zs' .
68      \    '\<\%(if\|for\|while\|until\|case\|unless\|begin\)\>' .
69      \ '\|\<do\>'
70
71" Regex that defines the middle-match for the 'end' keyword.
72let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue\>\|when\|elsif\)\>'
73
74" Regex that defines the end-match for the 'end' keyword.
75let s:end_end_regex = '\%(^\|[^.:]\)\@<=\<end\>'
76
77" Expression used for searchpair() call for finding match for 'end' keyword.
78let s:end_skip_expr = s:skip_expr .
79      \ ' || (expand("<cword>") == "do"' .
80      \ ' && getline(".") =~ "^\\s*\\<while\\|until\\|for\\>")'
81
82" Regex that defines continuation lines, not including (, {, or [.
83let s:continuation_regex = '\%([\\*+/.,=:-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
84
85" Regex that defines continuation lines.
86" TODO: this needs to deal with if ...: and so on
87let s:continuation_regex2 =
88      \ '\%([\\*+/.,=:({[-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
89
90" Regex that defines blocks.
91let s:block_regex =
92      \ '\%(\<do\>\|{\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=\s*\%(#.*\)\=$'
93
94" 2. Auxiliary Functions {{{1
95" ======================
96
97" Check if the character at lnum:col is inside a string, comment, or is ascii.
98function s:IsInStringOrComment(lnum, col)
99  return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_strcom
100endfunction
101
102" Check if the character at lnum:col is inside a string.
103function s:IsInString(lnum, col)
104  return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_string
105endfunction
106
107" Check if the character at lnum:col is inside a string or documentation.
108function s:IsInStringOrDocumentation(lnum, col)
109  return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_stringdoc
110endfunction
111
112" Find line above 'lnum' that isn't empty, in a comment, or in a string.
113function s:PrevNonBlankNonString(lnum)
114  let in_block = 0
115  let lnum = prevnonblank(a:lnum)
116  while lnum > 0
117    " Go in and out of blocks comments as necessary.
118    " If the line isn't empty (with opt. comment) or in a string, end search.
119    let line = getline(lnum)
120    if line =~ '^=begin$'
121      if in_block
122	let in_block = 0
123      else
124	break
125      endif
126    elseif !in_block && line =~ '^=end$'
127      let in_block = 1
128    elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
129	  \ && s:IsInStringOrComment(lnum, strlen(line)))
130      break
131    endif
132    let lnum = prevnonblank(lnum - 1)
133  endwhile
134  return lnum
135endfunction
136
137" Find line above 'lnum' that started the continuation 'lnum' may be part of.
138function s:GetMSL(lnum)
139  " Start on the line we're at and use its indent.
140  let msl = a:lnum
141  let lnum = s:PrevNonBlankNonString(a:lnum - 1)
142  while lnum > 0
143    " If we have a continuation line, or we're in a string, use line as MSL.
144    " Otherwise, terminate search as we have found our MSL already.
145    let line = getline(lnum)
146    let col = match(line, s:continuation_regex2) + 1
147    if (col > 0 && !s:IsInStringOrComment(lnum, col))
148	  \ || s:IsInString(lnum, strlen(line))
149      let msl = lnum
150    else
151      break
152    endif
153    let lnum = s:PrevNonBlankNonString(lnum - 1)
154  endwhile
155  return msl
156endfunction
157
158" Check if line 'lnum' has more opening brackets than closing ones.
159function s:LineHasOpeningBrackets(lnum)
160  let open_0 = 0
161  let open_2 = 0
162  let open_4 = 0
163  let line = getline(a:lnum)
164  let pos = match(line, '[][(){}]', 0)
165  while pos != -1
166    if !s:IsInStringOrComment(a:lnum, pos + 1)
167      let idx = stridx('(){}[]', line[pos])
168      if idx % 2 == 0
169	let open_{idx} = open_{idx} + 1
170      else
171	let open_{idx - 1} = open_{idx - 1} - 1
172      endif
173    endif
174    let pos = match(line, '[][(){}]', pos + 1)
175  endwhile
176  return (open_0 > 0) . (open_2 > 0) . (open_4 > 0)
177endfunction
178
179function s:Match(lnum, regex)
180  let col = match(getline(a:lnum), a:regex) + 1
181  return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
182endfunction
183
184function s:MatchLast(lnum, regex)
185  let line = getline(a:lnum)
186  let col = match(line, '.*\zs' . a:regex)
187  while col != -1 && s:IsInStringOrComment(a:lnum, col)
188    let line = strpart(line, 0, col)
189    let col = match(line, '.*' . a:regex)
190  endwhile
191  return col + 1
192endfunction
193
194" 3. GetRubyIndent Function {{{1
195" =========================
196
197function GetRubyIndent()
198  " 3.1. Setup {{{2
199  " ----------
200
201  " Set up variables for restoring position in file.  Could use v:lnum here.
202  let vcol = col('.')
203
204  " 3.2. Work on the current line {{{2
205  " -----------------------------
206
207  " Get the current line.
208  let line = getline(v:lnum)
209  let ind = -1
210
211  " If we got a closing bracket on an empty line, find its match and indent
212  " according to it.  For parentheses we indent to its column - 1, for the
213  " others we indent to the containing line's MSL's level.  Return -1 if fail.
214  let col = matchend(line, '^\s*[]})]')
215  if col > 0 && !s:IsInStringOrComment(v:lnum, col)
216    call cursor(v:lnum, col)
217    let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
218    if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
219      let ind = line[col-1]==')' ? virtcol('.')-1 : indent(s:GetMSL(line('.')))
220    endif
221    return ind
222  endif
223
224  " If we have a =begin or =end set indent to first column.
225  if match(line, '^\s*\%(=begin\|=end\)$') != -1
226    return 0
227  endif
228
229  " If we have a deindenting keyword, find its match and indent to its level.
230  " TODO: this is messy
231  if s:Match(v:lnum, s:ruby_deindent_keywords)
232    call cursor(v:lnum, 1)
233    if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
234	    \ s:end_skip_expr) > 0
235      let line = getline('.')
236      if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
237       \ strpart(line, col('.') - 1, 2) !~ 'do'
238	let ind = virtcol('.') - 1
239      else
240	let ind = indent('.')
241      endif
242    endif
243    return ind
244  endif
245
246  " If we are in a multi-line string or line-comment, don't do anything to it.
247  if s:IsInStringOrDocumentation(v:lnum, matchend(line, '^\s*') + 1)
248    return indent('.')
249  endif
250
251  " 3.3. Work on the previous line. {{{2
252  " -------------------------------
253
254  " Find a non-blank, non-multi-line string line above the current line.
255  let lnum = s:PrevNonBlankNonString(v:lnum - 1)
256
257  " At the start of the file use zero indent.
258  if lnum == 0
259    return 0
260  endif
261
262  " Set up variables for current line.
263  let line = getline(lnum)
264  let ind = indent(lnum)
265
266  " If the previous line ended with a block opening, add a level of indent.
267  if s:Match(lnum, s:block_regex)
268    return indent(s:GetMSL(lnum)) + &sw
269  endif
270
271  " If the previous line contained an opening bracket, and we are still in it,
272  " add indent depending on the bracket type.
273  if line =~ '[[({]'
274    let counts = s:LineHasOpeningBrackets(lnum)
275    if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
276      return virtcol('.')
277    elseif counts[1] == '1' || counts[2] == '1'
278      return ind + &sw
279    else
280      call cursor(v:lnum, vcol)
281    end
282  endif
283
284  " If the previous line ended with an "end", match that "end"s beginning's
285  " indent.
286  let col = s:Match(lnum, '\%(^\|[^.]\)\<end\>\s*\%(#.*\)\=$')
287  if col > 0
288    call cursor(lnum, col)
289    if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
290		\ s:end_skip_expr) > 0
291      let n = line('.')
292      let ind = indent('.')
293      let msl = s:GetMSL(n)
294      if msl != n
295	let ind = indent(msl)
296      end
297      return ind
298    endif
299  end
300
301  let col = s:Match(lnum, s:ruby_indent_keywords)
302  if col > 0
303    call cursor(lnum, col)
304    let ind = virtcol('.') - 1 + &sw
305"    let ind = indent(lnum) + &sw
306    " TODO: make this better (we need to count them) (or, if a searchpair
307    " fails, we know that something is lacking an end and thus we indent a
308    " level
309    if s:Match(lnum, s:end_end_regex)
310      let ind = indent('.')
311    endif
312    return ind
313  endif
314
315  " 3.4. Work on the MSL line. {{{2
316  " --------------------------
317
318  " Set up variables to use and search for MSL to the previous line.
319  let p_lnum = lnum
320  let lnum = s:GetMSL(lnum)
321
322  " If the previous line wasn't a MSL and is continuation return its indent.
323  " TODO: the || s:IsInString() thing worries me a bit.
324  if p_lnum != lnum
325    if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line))
326      return ind
327    endif
328  endif
329
330  " Set up more variables, now that we know we wasn't continuation bound.
331  let line = getline(lnum)
332  let msl_ind = indent(lnum)
333
334  " If the MSL line had an indenting keyword in it, add a level of indent.
335  " TODO: this does not take into account contrived things such as
336  " module Foo; class Bar; end
337  if s:Match(lnum, s:ruby_indent_keywords)
338    let ind = msl_ind + &sw
339    if s:Match(lnum, s:end_end_regex)
340      let ind = ind - &sw
341    endif
342    return ind
343  endif
344
345  " If the previous line ended with [*+/.-=], indent one extra level.
346  if s:Match(lnum, s:continuation_regex)
347    if lnum == p_lnum
348      let ind = msl_ind + &sw
349    else
350      let ind = msl_ind
351    endif
352  endif
353
354  " }}}2
355
356  return ind
357endfunction
358
359" }}}1
360
361let &cpo = s:cpo_save
362unlet s:cpo_save
363