annotate runtime/indent/ruby.vim @ 1919:6e9c10c63e25 v7.2.216

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