Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 8 | " ---------------------------------------------------------------------------- |
| 9 | |
| 10 | " 0. Initialization {{{1 |
| 11 | " ================= |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 12 | |
| 13 | " Only load this indent file when no other was loaded. |
| 14 | if exists("b:did_indent") |
| 15 | finish |
| 16 | endif |
| 17 | let b:did_indent = 1 |
| 18 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 19 | " Now, set up our indentation expression and keys that trigger it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | setlocal indentexpr=GetRubyIndent() |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 21 | setlocal indentkeys=0{,0},0),0],!^F,o,O,e |
| 22 | setlocal indentkeys+==end,=elsif,=when,=ensure,=rescue,==begin,==end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | |
| 24 | " Only define the function once. |
| 25 | if exists("*GetRubyIndent") |
| 26 | finish |
| 27 | endif |
| 28 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 29 | let s:cpo_save = &cpo |
| 30 | set cpo&vim |
| 31 | |
| 32 | " 1. Variables {{{1 |
| 33 | " ============ |
| 34 | |
| 35 | " Regex of syntax group names that are or delimit string or are comments. |
| 36 | let s:syng_strcom = '\<ruby\%(String\|StringDelimiter\|ASCIICode' . |
| 37 | \ '\|Interpolation\|NoInterpolation\|Escape\|Comment\|Documentation\)\>' |
| 38 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 39 | " Regex of syntax group names that are strings. |
| 40 | let s:syng_string = |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 41 | \ '\<ruby\%(String\|StringDelimiter\|Interpolation\|NoInterpolation\|Escape\)\>' |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 42 | |
| 43 | " Regex of syntax group names that are strings or documentation. |
| 44 | let s:syng_stringdoc = |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 45 | \'\<ruby\%(String\|StringDelimiter\|Interpolation\|NoInterpolation\|Escape\|Documentation\)\>' |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 46 | |
| 47 | " Expression used to check whether we should skip a match with searchpair(). |
| 48 | let 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. |
| 52 | let 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. |
| 59 | let 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)? |
| 65 | let 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. |
| 72 | let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue\>\|when\|elsif\)\>' |
| 73 | |
| 74 | " Regex that defines the end-match for the 'end' keyword. |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 75 | let s:end_end_regex = '\%(^\|[^.:]\)\@<=\<end\>' |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 76 | |
| 77 | " Expression used for searchpair() call for finding match for 'end' keyword. |
| 78 | let 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 [. |
| 83 | let s:continuation_regex = '\%([\\*+/.,=:-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$' |
| 84 | |
| 85 | " Regex that defines continuation lines. |
| 86 | " TODO: this needs to deal with if ...: and so on |
| 87 | let s:continuation_regex2 = |
| 88 | \ '\%([\\*+/.,=:({[-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$' |
| 89 | |
| 90 | " Regex that defines blocks. |
| 91 | let 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. |
| 98 | function s:IsInStringOrComment(lnum, col) |
| 99 | return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_strcom |
| 100 | endfunction |
| 101 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 102 | " Check if the character at lnum:col is inside a string. |
| 103 | function s:IsInString(lnum, col) |
| 104 | return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_string |
| 105 | endfunction |
| 106 | |
| 107 | " Check if the character at lnum:col is inside a string or documentation. |
| 108 | function s:IsInStringOrDocumentation(lnum, col) |
| 109 | return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_stringdoc |
| 110 | endfunction |
| 111 | |
| 112 | " Find line above 'lnum' that isn't empty, in a comment, or in a string. |
| 113 | function 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 |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 122 | let in_block = 0 |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 123 | else |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 124 | break |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 125 | endif |
| 126 | elseif !in_block && line =~ '^=end$' |
| 127 | let in_block = 1 |
| 128 | elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 129 | \ && s:IsInStringOrComment(lnum, strlen(line))) |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 130 | break |
| 131 | endif |
| 132 | let lnum = prevnonblank(lnum - 1) |
| 133 | endwhile |
| 134 | return lnum |
| 135 | endfunction |
| 136 | |
| 137 | " Find line above 'lnum' that started the continuation 'lnum' may be part of. |
| 138 | function 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)) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 148 | \ || s:IsInString(lnum, strlen(line)) |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 149 | let msl = lnum |
| 150 | else |
| 151 | break |
| 152 | endif |
| 153 | let lnum = s:PrevNonBlankNonString(lnum - 1) |
| 154 | endwhile |
| 155 | return msl |
| 156 | endfunction |
| 157 | |
| 158 | " Check if line 'lnum' has more opening brackets than closing ones. |
| 159 | function 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 |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 169 | let open_{idx} = open_{idx} + 1 |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 170 | else |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 171 | let open_{idx - 1} = open_{idx - 1} - 1 |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 172 | endif |
| 173 | endif |
| 174 | let pos = match(line, '[][(){}]', pos + 1) |
| 175 | endwhile |
| 176 | return (open_0 > 0) . (open_2 > 0) . (open_4 > 0) |
| 177 | endfunction |
| 178 | |
| 179 | function 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 |
| 182 | endfunction |
| 183 | |
| 184 | function 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 |
| 192 | endfunction |
| 193 | |
| 194 | " 3. GetRubyIndent Function {{{1 |
| 195 | " ========================= |
| 196 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | function GetRubyIndent() |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 232 | call cursor(v:lnum, 1) |
| 233 | if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW', |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 234 | \ s:end_skip_expr) > 0 |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 235 | let line = getline('.') |
| 236 | if strpart(line, 0, col('.') - 1) =~ '=\s*$' && |
| 237 | \ strpart(line, col('.') - 1, 2) !~ 'do' |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 238 | let ind = virtcol('.') - 1 |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 239 | else |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 240 | let ind = indent('.') |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | |
| 257 | " At the start of the file use zero indent. |
| 258 | if lnum == 0 |
| 259 | return 0 |
| 260 | endif |
| 261 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 262 | " Set up variables for current line. |
| 263 | let line = getline(lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 264 | let ind = indent(lnum) |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 269 | endif |
| 270 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 282 | endif |
| 283 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 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', |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 290 | \ s:end_skip_expr) > 0 |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 291 | let n = line('.') |
| 292 | let ind = indent('.') |
| 293 | let msl = s:GetMSL(n) |
| 294 | if msl != n |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 295 | let ind = indent(msl) |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 313 | endif |
| 314 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 356 | return ind |
| 357 | endfunction |
| 358 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 359 | " }}}1 |
| 360 | |
| 361 | let &cpo = s:cpo_save |
| 362 | unlet s:cpo_save |