Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 416 lines (353 sloc) 13.461 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 =
782b58e @tpope String related fixes
tpope 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…
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…
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 [.
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev 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 =
210702f Lines ending in and or or are continuation lines
Nikolai Weibull 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)
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
124 if line =~ '^=begin$'
125 if in_block
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns authored
126 let in_block = 0
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
127 else
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns authored
128 break
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
129 endif
d16bdc8 Fixed bug #114 and related issues.
Nikolai Weibull authored
130 elseif !in_block && line =~ '^=end$'
131 let in_block = 1
132 elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns 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
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
156 elseif line =~ s:non_bracket_continuation_regex && msl_body =~ s:bracket_continuation_regex
157 " If the current line is a non-bracket continuation, but the previous is
158 " a bracket one, respect the previous' indentation, and stop here.
159 return lnum
160 elseif line =~ s:bracket_continuation_regex && msl_body =~ s:bracket_continuation_regex
161 " If both lines are bracket continuations, use the current one's and
162 " stop here
163 return msl
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
164 else
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
165 let col = match(line, s:continuation_regex) + 1
166 if (col > 0 && !s:IsInStringOrComment(lnum, col))
167 \ || s:IsInString(lnum, strlen(line))
168 let msl = lnum
169 else
170 break
171 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
172 endif
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
173
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
174 let lnum = s:PrevNonBlankNonString(lnum - 1)
175 endwhile
176 return msl
3080b46 Speed-up fix and multi-line string handling.
Nikolai Weibull authored
177 endfunction
178
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
179 " Check if line 'lnum' has more opening brackets than closing ones.
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
180 function s:FindRightmostOpenBracket(lnum)
181 let open = {'parentheses': [], 'braces': [], 'brackets': []}
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
182 let line = getline(a:lnum)
183 let pos = match(line, '[][(){}]', 0)
184 while pos != -1
185 if !s:IsInStringOrComment(a:lnum, pos + 1)
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
186 if line[pos] == '('
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
187 call add(open.parentheses, {'type': '(', 'pos': pos})
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
188 elseif line[pos] == ')'
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
189 let open.parentheses = open.parentheses[0:-2]
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
190 elseif line[pos] == '{'
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
191 call add(open.braces, {'type': '{', 'pos': pos})
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
192 elseif line[pos] == '}'
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
193 let open.braces = open.braces[0:-2]
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
194 elseif line[pos] == '['
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
195 call add(open.brackets, {'type': '[', 'pos': pos})
f58c989 Use hash instead of ugly string trick
Nikolai Weibull authored
196 elseif line[pos] == ']'
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
197 let open.brackets = open.brackets[0:-2]
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer i…
Nikolai Weibull authored
198 endif
199 endif
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
200 let pos = match(line, '[][(){}]', pos + 1)
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer i…
Nikolai Weibull authored
201 endwhile
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
202 let rightmost = {'type': '(', 'pos': -1}
203 for open in open.parentheses + open.braces + open.brackets
204 if open.pos > rightmost.pos
205 let rightmost = open
206 endif
207 endfor
208 return rightmost
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer i…
Nikolai Weibull authored
209 endfunction
210
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
211 function s:Match(lnum, regex)
22f9e5a @tpope Force case sensitivity in indentation algorithm
tpope authored
212 let col = match(getline(a:lnum), '\C'.a:regex) + 1
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
213 return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
214 endfunction
215
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
216 function s:MatchLast(lnum, regex)
217 let line = getline(a:lnum)
218 let col = match(line, '.*\zs' . a:regex)
219 while col != -1 && s:IsInStringOrComment(a:lnum, col)
220 let line = strpart(line, 0, col)
221 let col = match(line, '.*' . a:regex)
222 endwhile
223 return col + 1
224 endfunction
225
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
226 " 3. GetRubyIndent Function {{{1
227 " =========================
228
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
229 function GetRubyIndent(...)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
230 " 3.1. Setup {{{2
231 " ----------
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
232
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
233 " For the current line, use the first argument if given, else v:lnum
234 let clnum = a:0 ? a:1 : v:lnum
235
236 " Set up variables for restoring position in file. Could use clnum here.
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
237 let vcol = col('.')
238
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
239 " 3.2. Work on the current line {{{2
240 " -----------------------------
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
241
242 " Get the current line.
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
243 let line = getline(clnum)
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
244 let ind = -1
245
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
246 " 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
247 " according to it. For parentheses we indent to its column - 1, for the
248 " others we indent to the containing line's MSL's level. Return -1 if fail.
249 let col = matchend(line, '^\s*[]})]')
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
250 if col > 0 && !s:IsInStringOrComment(clnum, col)
251 call cursor(clnum, col)
d4c1cf2 Added support for ?:.
Nikolai Weibull authored
252 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
253 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
254 if line[col-1]==')' && col('.') != col('$') - 1
2e03283 Fix spacing
Nikolai Weibull authored
255 let ind = virtcol('.') - 1
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
256 else
257 let ind = indent(s:GetMSL(line('.')))
258 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
259 endif
260 return ind
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
261 endif
262
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
263 " If we have a =begin or =end set indent to first column.
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
264 if match(line, '^\s*\%(=begin\|=end\)$') != -1
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
265 return 0
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
266 endif
267
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
268 " 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
269 " TODO: this is messy
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
270 if s:Match(clnum, s:ruby_deindent_keywords)
271 call cursor(clnum, 1)
ad7fe8a Updated to indent correctly. There's more to be done, as a statement…
Nikolai Weibull authored
272 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns authored
273 \ s:end_skip_expr) > 0
f8709fd Fix for #2473.
Nikolai Weibull authored
274 let line = getline('.')
275 if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
276 \ strpart(line, col('.') - 1, 2) !~ 'do'
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns authored
277 let ind = virtcol('.') - 1
ad7fe8a Updated to indent correctly. There's more to be done, as a statement…
Nikolai Weibull authored
278 else
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns authored
279 let ind = indent('.')
ad7fe8a Updated to indent correctly. There's more to be done, as a statement…
Nikolai Weibull authored
280 endif
281 endif
282 return ind
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
283 endif
284
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
285 " 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
286 if s:IsInStringOrDocumentation(clnum, matchend(line, '^\s*') + 1)
518bc52 @tpope removed string delimiter from patterns
tpope authored
287 return indent('.')
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
288 endif
289
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
290 " 3.3. Work on the previous line. {{{2
291 " -------------------------------
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
292
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
293 " 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
294 let lnum = s:PrevNonBlankNonString(clnum - 1)
3080b46 Speed-up fix and multi-line string handling.
Nikolai Weibull authored
295
52491f4 @tpope copy previous string indent inside strings
tpope authored
296 " 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
297 if line =~ '^\s*$' && lnum != prevnonblank(clnum - 1)
298 return indent(prevnonblank(clnum))
52491f4 @tpope copy previous string indent inside strings
tpope authored
299 endif
300
4984d5a @gsinclair Initial revision
gsinclair authored
301 " At the start of the file use zero indent.
302 if lnum == 0
303 return 0
304 endif
305
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
306 " Set up variables for current line.
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
307 let line = getline(lnum)
308 let ind = indent(lnum)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
309
310 " If the previous line ended with a block opening, add a level of indent.
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
311 if s:Match(lnum, s:block_regex)
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
312 return indent(lnum) + &sw
4984d5a @gsinclair Initial revision
gsinclair authored
313 endif
314
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
315 " If the previous line contained an opening bracket, and we are still in it,
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
316 " add indent depending on the bracket type.
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
317 if line =~ '[[({]'
9396969 Indent brackets to bracket’s position + space
Nikolai Weibull authored
318 let open = s:FindRightmostOpenBracket(lnum)
319 if open.pos != -1
320 if open.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
321 if col('.') + 1 == col('$')
322 return ind + &sw
323 else
324 return virtcol('.')
325 endif
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
326 else
d6f9955 Fix indentation of brackets
Nikolai Weibull authored
327 let nonspace = matchend(line, '\S', open.pos + 1) - 1
328 return nonspace > 0 ? nonspace : ind + &sw
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
329 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
330 else
bcdd3f9 @AndrewRadev Pass v:lnum as an argument to GetRubyIndent
AndrewRadev authored
331 call cursor(clnum, vcol)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
332 end
4984d5a @gsinclair Initial revision
gsinclair authored
333 endif
334
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
335 " If the previous line ended with an "end", match that "end"s beginning's
336 " indent.
10cb669 @dkearns ignore instance, class, and global variables named "end" when looking to
dkearns authored
337 let col = s:Match(lnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
338 if col > 0
339 call cursor(lnum, col)
340 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns authored
341 \ s:end_skip_expr) > 0
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
342 let n = line('.')
343 let ind = indent('.')
344 let msl = s:GetMSL(n)
345 if msl != n
ec45322 @dkearns replace spaces with tabs, where possible, to reduce file sizes as req…
dkearns authored
346 let ind = indent(msl)
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
347 end
348 return ind
349 endif
350 end
351
352 let col = s:Match(lnum, s:ruby_indent_keywords)
353 if col > 0
354 call cursor(lnum, col)
355 let ind = virtcol('.') - 1 + &sw
356 " let ind = indent(lnum) + &sw
357 " TODO: make this better (we need to count them) (or, if a searchpair
358 " fails, we know that something is lacking an end and thus we indent a
359 " level
360 if s:Match(lnum, s:end_end_regex)
361 let ind = indent('.')
362 endif
363 return ind
364 endif
365
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
366 " 3.4. Work on the MSL line. {{{2
367 " --------------------------
368
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
369 " Set up variables to use and search for MSL to the previous line.
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
370 let p_lnum = lnum
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
371 let lnum = s:GetMSL(lnum)
4984d5a @gsinclair Initial revision
gsinclair authored
372
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
373 " If the previous line wasn't a MSL and is continuation return its indent.
374 " TODO: the || s:IsInString() thing worries me a bit.
375 if p_lnum != lnum
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
376 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
377 return ind
378 endif
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
379 endif
380
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
381 " 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
382 let line = getline(lnum)
383 let msl_ind = indent(lnum)
384
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
385 " 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
386 " TODO: this does not take into account contrived things such as
387 " module Foo; class Bar; end
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
388 if s:Match(lnum, s:ruby_indent_keywords)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
389 let ind = msl_ind + &sw
62aab94 Fix bug #62. Cleanup, fixup, speedup.
Nikolai Weibull authored
390 if s:Match(lnum, s:end_end_regex)
b6e111e Initial import. This one should work great.
Nikolai Weibull authored
391 let ind = ind - &sw
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
392 endif
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
393 return ind
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
394 endif
395
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
396 " If the previous line ended with [*+/.-=], indent one extra level.
2f1e771 @AndrewRadev Fix nested hash problem
AndrewRadev authored
397 if s:Match(lnum, s:non_bracket_continuation_regex)
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
398 if lnum == p_lnum
399 let ind = msl_ind + &sw
400 else
401 let ind = msl_ind
402 endif
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
403 endif
404
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
405 " }}}2
503b954 @gsinclair Much improved version submitted by Nikolai Weibull
gsinclair authored
406
4984d5a @gsinclair Initial revision
gsinclair authored
407 return ind
938dce5 Hohoho. Santa comes early this year. Made parts very much clearer i…
Nikolai Weibull authored
408 endfunction
4984d5a @gsinclair Initial revision
gsinclair authored
409
44028fa Total rewrite of indent/ruby.vim. Closes bugs #59 and #71.
Nikolai Weibull authored
410 " }}}1
411
3260b5c 1. resetting of cpoptions (wouldn't always be done, depending on if
Nikolai Weibull authored
412 let &cpo = s:cpo_save
413 unlet s:cpo_save
91937dd @tpope Different indent behavior for a line ending in an open parenthesis.
tpope authored
414
b2d3cba @tpope indent/eruby.vim: fix quirk in optional argument handling
tpope authored
415 " vim:set sw=2 sts=2 ts=8 noet:
Something went wrong with that request. Please try again.