Skip to content
Newer
Older
100644 418 lines (355 sloc) 13.5 KB
4984d5a @gsinclair Initial revision
gsinclair authored
1 " Vim indent file
ad3a6a9 @dkearns add Release-Coordinator header line and remove GPL license
dkearns authored
2 " Language: Ruby
48f969c @dkearns update Nikolai's email address
dkearns authored
3 " Maintainer: Nikolai Weibull <now at bitwi.se>
699670a @dkearns Update all RubyForge URLs with new GitHub equivalents.
dkearns authored
4 " URL: https://github.com/vim-ruby/vim-ruby
ad3a6a9 @dkearns add Release-Coordinator header line and remove GPL license
dkearns authored
5 " Anon CVS: See above site
6 " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
4984d5a @gsinclair Initial revision
gsinclair authored
7
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
8 " 0. Initialization {{{1
9 " =================
10
4984d5a @gsinclair Initial revision
gsinclair authored
11 " Only load this indent file when no other was loaded.
12 if exists("b:did_indent")
13 finish
14 endif
15 let b:did_indent = 1
16
3eaf396 @dkearns make sure 'smartindent' is disabled
dkearns authored
17 setlocal nosmartindent
18
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
19 " Now, set up our indentation expression and keys that trigger it.
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
20 setlocal indentexpr=GetRubyIndent(v:lnum)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
21 setlocal indentkeys=0{,0},0),0],!^F,o,O,e
22 setlocal indentkeys+==end,=elsif,=when,=ensure,=rescue,==begin,==end
4984d5a @gsinclair Initial revision
gsinclair authored
23
24 " Only define the function once.
25 if exists("*GetRubyIndent")
26 finish
27 endif
28
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
29 let s:cpo_save = &cpo
30 set cpo&vim
31
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
32 " 1. Variables {{{1
33 " ============
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
34
b1151fe @dkearns fix comment typo
dkearns authored
35 " Regex of syntax group names that are or delimit string or are comments.
4935afd Don’t match inside Regexes
Nikolai Weibull authored
36 let s:syng_strcom = '\<ruby\%(Regexp\|RegexpDelimiter' .
37 \ '\|String\|StringEscape\|ASCIICode' .
518bc52 @tpope removed string delimiter from patterns
tpope authored
38 \ '\|Interpolation\|NoInterpolation\|Comment\|Documentation\)\>'
afa7136 Small changes to fix obscure indentation issues.
Nikolai Weibull authored
39
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
40 " Regex of syntax group names that are strings.
41 let s:syng_string =
518bc52 @tpope removed string delimiter from patterns
tpope authored
42 \ '\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\)\>'
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
43
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
44 " Regex of syntax group names that are strings or documentation.
45 let s:syng_stringdoc =
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
46 \'\<ruby\%(String\|Interpolation\|NoInterpolation\|StringEscape\|Documentation\)\>'
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
47
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
48 " Expression used to check whether we should skip a match with searchpair().
49 let s:skip_expr =
782b58e @tpope String related fixes
tpope authored
50 \ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
51
52 " Regex used for words that, at the start of a line, add a level of indent.
ef29e34 Fixes bugs introduced in earlier commits. Been running without fault for
Nikolai Weibull authored
53 let s:ruby_indent_keywords = '^\s*\zs\<\%(module\|class\|def\|if\|for' .
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
54 \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure' .
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
55 \ '\|rescue\):\@!\>' .
0983d4f Indent/continue lines ending with %
Nikolai Weibull authored
56 \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
4b612b2 @tpope Fix indent of if as 1.9 hash key
tpope authored
57 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
58
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
59 " Regex used for words that, at the start of a line, remove a level of indent.
60 let s:ruby_deindent_keywords =
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
61 \ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|end\):\@!\>'
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
62
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
63 " Regex that defines the start-match for the 'end' keyword.
ad7fe8a Updated to indent correctly. There's more to be done, as a statement …
Nikolai Weibull authored
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)?
ef29e34 Fixes bugs introduced in earlier commits. Been running without fault for
Nikolai Weibull authored
66 let s:end_start_regex = '^\s*\zs\<\%(module\|class\|def\|if\|for' .
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
67 \ '\|while\|until\|case\|unless\|begin\):\@!\>' .
0983d4f Indent/continue lines ending with %
Nikolai Weibull authored
68 \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
69 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>' .
70 \ '\|\<do:\@!\>'
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
71
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
72 " Regex that defines the middle-match for the 'end' keyword.
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
73 let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>'
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
74
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
75 " Regex that defines the end-match for the 'end' keyword.
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
76 let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>'
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
77
78 " Expression used for searchpair() call for finding match for 'end' keyword.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
79 let s:end_skip_expr = s:skip_expr .
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
80 \ ' || (expand("<cword>") == "do"' .
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
81 \ ' && getline(".") =~ "^\\s*\\<\\(while\\|until\\|for\\):\\@!\\>")'
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
82
83 " Regex that defines continuation lines, not including (, {, or [.
abfe860 @tpope Fix indent after a word ending in or/and
tpope authored
84 let s:non_bracket_continuation_regex = '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
85
86 " Regex that defines continuation lines.
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
87 " TODO: this needs to deal with if ...: and so on
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
88 let s:continuation_regex =
abfe860 @tpope Fix indent after a word ending in or/and
tpope authored
89 \ '\%([({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
90
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
91 " Regex that defines bracket continuations
92 let s:bracket_continuation_regex = '\%([({[]\)\s*\%(#.*\)\=$'
93
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
94 " Regex that defines blocks.
95 let s:block_regex =
e0a1d84 @tpope Fix further "reservedword:" indenting
tpope authored
96 \ '\%(\<do:\@!\>\|{\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=\s*\%(#.*\)\=$'
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
97
98 " 2. Auxiliary Functions {{{1
99 " ======================
100
101 " Check if the character at lnum:col is inside a string, comment, or is ascii.
102 function s:IsInStringOrComment(lnum, col)
782b58e @tpope String related fixes
tpope authored
103 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
104 endfunction
105
106 " Check if the character at lnum:col is inside a string.
107 function s:IsInString(lnum, col)
782b58e @tpope String related fixes
tpope authored
108 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
109 endfunction
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
110
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
111 " Check if the character at lnum:col is inside a string or documentation.
112 function s:IsInStringOrDocumentation(lnum, col)
782b58e @tpope String related fixes
tpope authored
113 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
114 endfunction
115
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
116 " Find line above 'lnum' that isn't empty, in a comment, or in a string.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
117 function s:PrevNonBlankNonString(lnum)
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
118 let in_block = 0
119 let lnum = prevnonblank(a:lnum)
120 while lnum > 0
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
121 " Go in and out of blocks comments as necessary.
122 " If the line isn't empty (with opt. comment) or in a string, end search.
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
123 let line = getline(lnum)
8bee58b @AndrewRadev Fix rdoc comment indent
AndrewRadev authored
124 if line =~ '^=begin'
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
125 if in_block
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
126 let in_block = 0
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
127 else
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
128 break
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
129 endif
8bee58b @AndrewRadev Fix rdoc comment indent
AndrewRadev authored
130 elseif !in_block && line =~ '^=end'
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
131 let in_block = 1
132 elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
133 \ && s:IsInStringOrComment(lnum, strlen(line)))
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
134 break
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
135 endif
136 let lnum = prevnonblank(lnum - 1)
137 endwhile
138 return lnum
139 endfunction
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
140
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
141 " Find line above 'lnum' that started the continuation 'lnum' may be part of.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
142 function s:GetMSL(lnum)
143 " Start on the line we're at and use its indent.
144 let msl = a:lnum
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
145 let msl_body = getline(msl)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
146 let lnum = s:PrevNonBlankNonString(a:lnum - 1)
147 while lnum > 0
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
148 " If we have a continuation line, or we're in a string, use line as MSL.
149 " Otherwise, terminate search as we have found our MSL already.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
150 let line = getline(lnum)
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
151
152 if line =~ s:non_bracket_continuation_regex && msl_body =~ s:non_bracket_continuation_regex
153 " If the current line is a non-bracket continuation and so is the
154 " previous one, keep its indent and continue looking for an MSL.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
155 let msl = lnum
852596f @AndrewRadev Fix a bug with lambdas within hashes
AndrewRadev authored
156 elseif line =~ s:non_bracket_continuation_regex && (msl_body =~ s:bracket_continuation_regex || msl_body =~ s:block_regex)
157 " If the current line is a bracket continuation or a block-starter, but
158 " the previous is a non-bracket one, respect the previous' indentation,
159 " and stop here.
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
160 return lnum
852596f @AndrewRadev Fix a bug with lambdas within hashes
AndrewRadev authored
161 elseif line =~ s:bracket_continuation_regex && (msl_body =~ s:bracket_continuation_regex || msl_body =~ s:block_regex)
162 " If both lines are bracket continuations (the current may also be a
163 " block-starter), use the current one's and stop here
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
164 return msl
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
165 else
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
166 let col = match(line, s:continuation_regex) + 1
167 if (col > 0 && !s:IsInStringOrComment(lnum, col))
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
168 \ || s:IsInString(lnum, strlen(line))
169 let msl = lnum
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
170 else
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
171 break
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
172 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
173 endif
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
174
852596f @AndrewRadev Fix a bug with lambdas within hashes
AndrewRadev authored
175 let msl_body = getline(msl)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
176 let lnum = s:PrevNonBlankNonString(lnum - 1)
177 endwhile
178 return msl
3080b46 Speed-up fix and multi-line string handling.
Nikolai Weibull authored
179 endfunction
180
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
181 " Check if line 'lnum' has more opening brackets than closing ones.
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
182 function s:FindRightmostOpenBracket(lnum)
183 let open = {'parentheses': [], 'braces': [], 'brackets': []}
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
184 let line = getline(a:lnum)
185 let pos = match(line, '[][(){}]', 0)
186 while pos != -1
187 if !s:IsInStringOrComment(a:lnum, pos + 1)
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
188 if line[pos] == '('
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
189 call add(open.parentheses, {'type': '(', 'pos': pos})
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
190 elseif line[pos] == ')'
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
191 let open.parentheses = open.parentheses[0:-2]
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
192 elseif line[pos] == '{'
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
193 call add(open.braces, {'type': '{', 'pos': pos})
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
194 elseif line[pos] == '}'
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
195 let open.braces = open.braces[0:-2]
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
196 elseif line[pos] == '['
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
197 call add(open.brackets, {'type': '[', 'pos': pos})
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
198 elseif line[pos] == ']'
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
199 let open.brackets = open.brackets[0:-2]
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer in …
Nikolai Weibull authored
200 endif
201 endif
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
202 let pos = match(line, '[][(){}]', pos + 1)
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer in …
Nikolai Weibull authored
203 endwhile
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
204 let rightmost = {'type': '(', 'pos': -1}
205 for open in open.parentheses + open.braces + open.brackets
206 if open.pos > rightmost.pos
207 let rightmost = open
208 endif
209 endfor
210 return rightmost
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer in …
Nikolai Weibull authored
211 endfunction
212
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
213 function s:Match(lnum, regex)
22f9e5a @tpope Force case sensitivity in indentation algorithm
tpope authored
214 let col = match(getline(a:lnum), '\C'.a:regex) + 1
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
215 return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
216 endfunction
217
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
218 function s:MatchLast(lnum, regex)
219 let line = getline(a:lnum)
220 let col = match(line, '.*\zs' . a:regex)
221 while col != -1 && s:IsInStringOrComment(a:lnum, col)
222 let line = strpart(line, 0, col)
223 let col = match(line, '.*' . a:regex)
224 endwhile
225 return col + 1
226 endfunction
227
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
228 " 3. GetRubyIndent Function {{{1
229 " =========================
230
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
231 function GetRubyIndent(...)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
232 " 3.1. Setup {{{2
233 " ----------
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
234
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
235 " For the current line, use the first argument if given, else v:lnum
236 let clnum = a:0 ? a:1 : v:lnum
237
238 " Set up variables for restoring position in file. Could use clnum here.
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
239 let vcol = col('.')
240
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
241 " 3.2. Work on the current line {{{2
242 " -----------------------------
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
243
244 " Get the current line.
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
245 let line = getline(clnum)
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
246 let ind = -1
247
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
248 " If we got a closing bracket on an empty line, find its match and indent
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
249 " according to it. For parentheses we indent to its column - 1, for the
250 " others we indent to the containing line's MSL's level. Return -1 if fail.
251 let col = matchend(line, '^\s*[]})]')
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
252 if col > 0 && !s:IsInStringOrComment(clnum, col)
253 call cursor(clnum, col)
d4c1cf2 Added support for ?:.
Nikolai Weibull authored
254 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
255 if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
256 if line[col-1]==')' && col('.') != col('$') - 1
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
257 let ind = virtcol('.') - 1
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
258 else
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
259 let ind = indent(s:GetMSL(line('.')))
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
260 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
261 endif
262 return ind
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
263 endif
264
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
265 " If we have a =begin or =end set indent to first column.
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
266 if match(line, '^\s*\%(=begin\|=end\)$') != -1
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
267 return 0
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
268 endif
269
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
270 " If we have a deindenting keyword, find its match and indent to its level.
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
271 " TODO: this is messy
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
272 if s:Match(clnum, s:ruby_deindent_keywords)
273 call cursor(clnum, 1)
ad7fe8a Updated to indent correctly. There's more to be done, as a statement …
Nikolai Weibull authored
274 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
275 \ s:end_skip_expr) > 0
f8709fd Fix for #2473.
Nikolai Weibull authored
276 let line = getline('.')
277 if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
043f4ab @AndrewRadev Revert "Option to control hanging indent"
AndrewRadev authored
278 \ strpart(line, col('.') - 1, 2) !~ 'do'
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
279 let ind = virtcol('.') - 1
ad7fe8a Updated to indent correctly. There's more to be done, as a statement …
Nikolai Weibull authored
280 else
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
281 let ind = indent('.')
ad7fe8a Updated to indent correctly. There's more to be done, as a statement …
Nikolai Weibull authored
282 endif
283 endif
284 return ind
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
285 endif
286
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
287 " If we are in a multi-line string or line-comment, don't do anything to it.
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
288 if s:IsInStringOrDocumentation(clnum, matchend(line, '^\s*') + 1)
518bc52 @tpope removed string delimiter from patterns
tpope authored
289 return indent('.')
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
290 endif
291
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
292 " 3.3. Work on the previous line. {{{2
293 " -------------------------------
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
294
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
295 " Find a non-blank, non-multi-line string line above the current line.
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
296 let lnum = s:PrevNonBlankNonString(clnum - 1)
3080b46 Speed-up fix and multi-line string handling.
Nikolai Weibull authored
297
52491f4 @tpope copy previous string indent inside strings
tpope authored
298 " If the line is empty and inside a string, use the previous line.
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
299 if line =~ '^\s*$' && lnum != prevnonblank(clnum - 1)
300 return indent(prevnonblank(clnum))
52491f4 @tpope copy previous string indent inside strings
tpope authored
301 endif
302
4984d5a @gsinclair Initial revision
gsinclair authored
303 " At the start of the file use zero indent.
304 if lnum == 0
305 return 0
306 endif
307
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
308 " Set up variables for current line.
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
309 let line = getline(lnum)
310 let ind = indent(lnum)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
311
312 " If the previous line ended with a block opening, add a level of indent.
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
313 if s:Match(lnum, s:block_regex)
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
314 return indent(lnum) + &sw
4984d5a @gsinclair Initial revision
gsinclair authored
315 endif
316
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
317 " If the previous line contained an opening bracket, and we are still in it,
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
318 " add indent depending on the bracket type.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
319 if line =~ '[[({]'
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
320 let open = s:FindRightmostOpenBracket(lnum)
321 if open.pos != -1
322 if open.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
323 if col('.') + 1 == col('$')
324 return ind + &sw
325 else
326 return virtcol('.')
327 endif
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
328 else
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
329 let nonspace = matchend(line, '\S', open.pos + 1) - 1
330 return nonspace > 0 ? nonspace : ind + &sw
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
331 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
332 else
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
333 call cursor(clnum, vcol)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
334 end
4984d5a @gsinclair Initial revision
gsinclair authored
335 endif
336
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
337 " If the previous line ended with an "end", match that "end"s beginning's
338 " indent.
10cb669 @dkearns ignore instance, class, and global variables named "end" when looking to
dkearns authored
339 let col = s:Match(lnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
340 if col > 0
341 call cursor(lnum, col)
342 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
343 \ s:end_skip_expr) > 0
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
344 let n = line('.')
345 let ind = indent('.')
346 let msl = s:GetMSL(n)
347 if msl != n
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
348 let ind = indent(msl)
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
349 end
350 return ind
351 endif
352 end
353
354 let col = s:Match(lnum, s:ruby_indent_keywords)
355 if col > 0
356 call cursor(lnum, col)
043f4ab @AndrewRadev Revert "Option to control hanging indent"
AndrewRadev authored
357 let ind = virtcol('.') - 1 + &sw
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
358 " TODO: make this better (we need to count them) (or, if a searchpair
359 " fails, we know that something is lacking an end and thus we indent a
360 " level
361 if s:Match(lnum, s:end_end_regex)
362 let ind = indent('.')
363 endif
364 return ind
365 endif
366
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
367 " 3.4. Work on the MSL line. {{{2
368 " --------------------------
369
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
370 " Set up variables to use and search for MSL to the previous line.
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
371 let p_lnum = lnum
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
372 let lnum = s:GetMSL(lnum)
4984d5a @gsinclair Initial revision
gsinclair authored
373
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
374 " If the previous line wasn't a MSL and is continuation return its indent.
375 " TODO: the || s:IsInString() thing worries me a bit.
376 if p_lnum != lnum
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
377 if s:Match(p_lnum,s:non_bracket_continuation_regex)||s:IsInString(p_lnum,strlen(line))
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
378 return ind
379 endif
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
380 endif
381
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
382 " Set up more variables, now that we know we wasn't continuation bound.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
383 let line = getline(lnum)
384 let msl_ind = indent(lnum)
385
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
386 " If the MSL line had an indenting keyword in it, add a level of indent.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
387 " TODO: this does not take into account contrived things such as
388 " module Foo; class Bar; end
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
389 if s:Match(lnum, s:ruby_indent_keywords)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
390 let ind = msl_ind + &sw
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
391 if s:Match(lnum, s:end_end_regex)
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
392 let ind = ind - &sw
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
393 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
394 return ind
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
395 endif
396
852596f @AndrewRadev Fix a bug with lambdas within hashes
AndrewRadev authored
397 " If the previous line ended with [*+/.,-=], but wasn't a block ending,
398 " indent one extra level.
399 if s:Match(lnum, s:non_bracket_continuation_regex) && !s:Match(lnum, '^\s*\(}\|end\)')
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
400 if lnum == p_lnum
401 let ind = msl_ind + &sw
402 else
403 let ind = msl_ind
404 endif
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
405 endif
406
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
407 " }}}2
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
408
4984d5a @gsinclair Initial revision
gsinclair authored
409 return ind
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer in …
Nikolai Weibull authored
410 endfunction
4984d5a @gsinclair Initial revision
gsinclair authored
411
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
412 " }}}1
413
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
414 let &cpo = s:cpo_save
415 unlet s:cpo_save
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
416
8aa9cee @AndrewRadev Use spaces for indentation of indent file
AndrewRadev authored
417 " vim:set sw=2 sts=2 ts=8 et:
Something went wrong with that request. Please try again.