Mercurial > vim
annotate runtime/indent/ruby.vim @ 29881:0cc9a3001717 v9.0.0279
patch 9.0.0279: the tiny version has the popup menu but not 'wildmenu'
Commit: https://github.com/vim/vim/commit/5416232707349d5f24294178f47544f2024b73ed
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Aug 26 16:58:51 2022 +0100
patch 9.0.0279: the tiny version has the popup menu but not 'wildmenu'
Problem: The tiny version has the popup menu but not 'wildmenu'.
Solution: Graduate the wildmenu feature.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 26 Aug 2022 18:00:05 +0200 |
parents | e3d6184b89fa |
children | d3d82d3f6006 |
rev | line source |
---|---|
7 | 1 " Vim indent file |
843 | 2 " Language: Ruby |
15512 | 3 " Maintainer: Andrew Radev <andrey.radev@gmail.com> |
4 " Previous Maintainer: Nikolai Weibull <now at bitwi.se> | |
4869 | 5 " URL: https://github.com/vim-ruby/vim-ruby |
843 | 6 " Release Coordinator: Doug Kearns <dougkearns@gmail.com> |
28246 | 7 " Last Change: 2022 Mar 22 |
530 | 8 |
9 " 0. Initialization {{{1 | |
10 " ================= | |
7 | 11 |
12 " Only load this indent file when no other was loaded. | |
13 if exists("b:did_indent") | |
14 finish | |
15 endif | |
16 let b:did_indent = 1 | |
17 | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
18 if !exists('g:ruby_indent_access_modifier_style') |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
19 " Possible values: "normal", "indent", "outdent" |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
20 let g:ruby_indent_access_modifier_style = 'normal' |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
21 endif |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
22 |
15512 | 23 if !exists('g:ruby_indent_assignment_style') |
24 " Possible values: "variable", "hanging" | |
25 let g:ruby_indent_assignment_style = 'hanging' | |
26 endif | |
27 | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
28 if !exists('g:ruby_indent_block_style') |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
29 " Possible values: "expression", "do" |
23931 | 30 let g:ruby_indent_block_style = 'do' |
31 endif | |
32 | |
33 if !exists('g:ruby_indent_hanging_elements') | |
34 " Non-zero means hanging indents are enabled, zero means disabled | |
35 let g:ruby_indent_hanging_elements = 1 | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
36 endif |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
37 |
843 | 38 setlocal nosmartindent |
39 | |
530 | 40 " Now, set up our indentation expression and keys that trigger it. |
4869 | 41 setlocal indentexpr=GetRubyIndent(v:lnum) |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
42 setlocal indentkeys=0{,0},0),0],!^F,o,O,e,:,. |
28246 | 43 setlocal indentkeys+==end,=else,=elsif,=when,=in\ ,=ensure,=rescue,==begin,==end |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
44 setlocal indentkeys+==private,=protected,=public |
7 | 45 |
28246 | 46 let b:undo_indent = "setlocal indentexpr< indentkeys< smartindent<" |
47 | |
7 | 48 " Only define the function once. |
49 if exists("*GetRubyIndent") | |
50 finish | |
51 endif | |
52 | |
530 | 53 let s:cpo_save = &cpo |
54 set cpo&vim | |
55 | |
56 " 1. Variables {{{1 | |
57 " ============ | |
58 | |
15512 | 59 " Syntax group names that are strings. |
60 let s:syng_string = | |
18857
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
61 \ ['String', 'Interpolation', 'InterpolationDelimiter', 'StringEscape'] |
530 | 62 |
15512 | 63 " Syntax group names that are strings or documentation. |
64 let s:syng_stringdoc = s:syng_string + ['Documentation'] | |
530 | 65 |
15512 | 66 " Syntax group names that are or delimit strings/symbols/regexes or are comments. |
18857
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
67 let s:syng_strcom = s:syng_stringdoc + [ |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
68 \ 'Character', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
69 \ 'Comment', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
70 \ 'HeredocDelimiter', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
71 \ 'PercentRegexpDelimiter', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
72 \ 'PercentStringDelimiter', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
73 \ 'PercentSymbolDelimiter', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
74 \ 'Regexp', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
75 \ 'RegexpCharClass', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
76 \ 'RegexpDelimiter', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
77 \ 'RegexpEscape', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
78 \ 'StringDelimiter', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
79 \ 'Symbol', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
80 \ 'SymbolDelimiter', |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
81 \ ] |
530 | 82 |
83 " Expression used to check whether we should skip a match with searchpair(). | |
84 let s:skip_expr = | |
15512 | 85 \ 'index(map('.string(s:syng_strcom).',"hlID(''ruby''.v:val)"), synID(line("."),col("."),1)) >= 0' |
530 | 86 |
87 " Regex used for words that, at the start of a line, add a level of indent. | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
88 let s:ruby_indent_keywords = |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
89 \ '^\s*\zs\<\%(module\|class\|if\|for' . |
28246 | 90 \ '\|while\|until\|else\|elsif\|case\|when\|in\|unless\|begin\|ensure\|rescue' . |
18857
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
91 \ '\|\%(\K\k*[!?]\?\s\+\)\=def\):\@!\>' . |
4869 | 92 \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' . |
93 \ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>' | |
530 | 94 |
28246 | 95 " Def without an end clause: def method_call(...) = <expression> |
96 let s:ruby_endless_def = '\<def\s\+\k\+[!?]\=\%((.*)\|\s\)\s*=' | |
97 | |
530 | 98 " Regex used for words that, at the start of a line, remove a level of indent. |
99 let s:ruby_deindent_keywords = | |
28246 | 100 \ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|in\|end\):\@!\>' |
530 | 101 |
102 " Regex that defines the start-match for the 'end' keyword. | |
103 "let s:end_start_regex = '\%(^\|[^.]\)\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\|do\)\>' | |
104 " TODO: the do here should be restricted somewhat (only at end of line)? | |
4869 | 105 let s:end_start_regex = |
106 \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' . | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
107 \ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' . |
18857
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
108 \ '\|\%(\K\k*[!?]\?\s\+\)\=def\):\@!\>' . |
4869 | 109 \ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>' |
530 | 110 |
111 " Regex that defines the middle-match for the 'end' keyword. | |
28246 | 112 let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|\%(\%(^\|;\)\s*\)\@<=\<in\|elsif\):\@!\>' |
530 | 113 |
114 " Regex that defines the end-match for the 'end' keyword. | |
4869 | 115 let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>' |
530 | 116 |
28246 | 117 " Expression used for searchpair() call for finding a match for an 'end' keyword. |
118 function! s:EndSkipExpr() | |
119 if eval(s:skip_expr) | |
120 return 1 | |
121 elseif expand('<cword>') == 'do' | |
122 \ && getline(".") =~ '^\s*\<\(while\|until\|for\):\@!\>' | |
123 return 1 | |
124 elseif getline('.') =~ s:ruby_endless_def | |
125 return 1 | |
126 elseif getline('.') =~ '\<def\s\+\k\+[!?]\=([^)]*$' | |
127 " Then it's a `def method(` with a possible `) =` later | |
128 call search('\<def\s\+\k\+\zs(', 'W', line('.')) | |
129 normal! % | |
130 return getline('.') =~ ')\s*=' | |
131 else | |
132 return 0 | |
133 endif | |
134 endfunction | |
135 | |
136 let s:end_skip_expr = function('s:EndSkipExpr') | |
530 | 137 |
138 " Regex that defines continuation lines, not including (, {, or [. | |
10272
57b2b8268d3a
commit https://github.com/vim/vim/commit/4575876dc865d4160f20d61bd822fbe7cafbec41
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
139 let s:non_bracket_continuation_regex = |
57b2b8268d3a
commit https://github.com/vim/vim/commit/4575876dc865d4160f20d61bd822fbe7cafbec41
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
140 \ '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$' |
530 | 141 |
142 " Regex that defines continuation lines. | |
4869 | 143 let s:continuation_regex = |
10272
57b2b8268d3a
commit https://github.com/vim/vim/commit/4575876dc865d4160f20d61bd822fbe7cafbec41
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
144 \ '\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$' |
4869 | 145 |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
146 " Regex that defines continuable keywords |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
147 let s:continuable_regex = |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
148 \ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' . |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
149 \ '\<\%(if\|for\|while\|until\|unless\):\@!\>' |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
150 |
4869 | 151 " Regex that defines bracket continuations |
152 let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$' | |
153 | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
154 " Regex that defines dot continuations |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
155 let s:dot_continuation_regex = '%\@<!\.\s*\%(#.*\)\=$' |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
156 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
157 " Regex that defines backslash continuations |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
158 let s:backslash_continuation_regex = '%\@<!\\\s*$' |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
159 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
160 " Regex that defines end of bracket continuation followed by another continuation |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
161 let s:bracket_switch_continuation_regex = '^\([^(]\+\zs).\+\)\+'.s:continuation_regex |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
162 |
4869 | 163 " Regex that defines the first part of a splat pattern |
164 let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$' | |
530 | 165 |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
166 " Regex that describes all indent access modifiers |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
167 let s:access_modifier_regex = '\C^\s*\%(public\|protected\|private\)\s*\%(#.*\)\=$' |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
168 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
169 " Regex that describes the indent access modifiers (excludes public) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
170 let s:indent_access_modifier_regex = '\C^\s*\%(protected\|private\)\s*\%(#.*\)\=$' |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
171 |
530 | 172 " Regex that defines blocks. |
4869 | 173 " |
174 " Note that there's a slight problem with this regex and s:continuation_regex. | |
175 " Code like this will be matched by both: | |
176 " | |
177 " method_call do |(a, b)| | |
178 " | |
179 " The reason is that the pipe matches a hanging "|" operator. | |
180 " | |
530 | 181 let s:block_regex = |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
182 \ '\%(\<do:\@!\>\|%\@<!{\)\s*\%(|[^|]*|\)\=\s*\%(#.*\)\=$' |
4869 | 183 |
184 let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex | |
530 | 185 |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
186 " Regex that describes a leading operator (only a method call's dot for now) |
18857
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
187 let s:leading_operator_regex = '^\s*\%(&\=\.\)' |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
188 |
15512 | 189 " 2. GetRubyIndent Function {{{1 |
190 " ========================= | |
191 | |
192 function! GetRubyIndent(...) abort | |
193 " 2.1. Setup {{{2 | |
194 " ---------- | |
195 | |
196 let indent_info = {} | |
197 | |
198 " The value of a single shift-width | |
199 if exists('*shiftwidth') | |
200 let indent_info.sw = shiftwidth() | |
201 else | |
202 let indent_info.sw = &sw | |
203 endif | |
204 | |
205 " For the current line, use the first argument if given, else v:lnum | |
206 let indent_info.clnum = a:0 ? a:1 : v:lnum | |
207 let indent_info.cline = getline(indent_info.clnum) | |
208 | |
209 " Set up variables for restoring position in file. Could use clnum here. | |
210 let indent_info.col = col('.') | |
211 | |
212 " 2.2. Work on the current line {{{2 | |
213 " ----------------------------- | |
214 let indent_callback_names = [ | |
215 \ 's:AccessModifier', | |
216 \ 's:ClosingBracketOnEmptyLine', | |
217 \ 's:BlockComment', | |
218 \ 's:DeindentingKeyword', | |
219 \ 's:MultilineStringOrLineComment', | |
220 \ 's:ClosingHeredocDelimiter', | |
221 \ 's:LeadingOperator', | |
222 \ ] | |
223 | |
224 for callback_name in indent_callback_names | |
225 " Decho "Running: ".callback_name | |
226 let indent = call(function(callback_name), [indent_info]) | |
227 | |
228 if indent >= 0 | |
229 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) | |
230 return indent | |
231 endif | |
232 endfor | |
233 | |
234 " 2.3. Work on the previous line. {{{2 | |
235 " ------------------------------- | |
236 | |
237 " Special case: we don't need the real s:PrevNonBlankNonString for an empty | |
238 " line inside a string. And that call can be quite expensive in that | |
239 " particular situation. | |
240 let indent_callback_names = [ | |
241 \ 's:EmptyInsideString', | |
242 \ ] | |
243 | |
244 for callback_name in indent_callback_names | |
245 " Decho "Running: ".callback_name | |
246 let indent = call(function(callback_name), [indent_info]) | |
247 | |
248 if indent >= 0 | |
249 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) | |
250 return indent | |
251 endif | |
252 endfor | |
253 | |
254 " Previous line number | |
255 let indent_info.plnum = s:PrevNonBlankNonString(indent_info.clnum - 1) | |
256 let indent_info.pline = getline(indent_info.plnum) | |
257 | |
258 let indent_callback_names = [ | |
259 \ 's:StartOfFile', | |
260 \ 's:AfterAccessModifier', | |
261 \ 's:ContinuedLine', | |
262 \ 's:AfterBlockOpening', | |
263 \ 's:AfterHangingSplat', | |
264 \ 's:AfterUnbalancedBracket', | |
265 \ 's:AfterLeadingOperator', | |
266 \ 's:AfterEndKeyword', | |
267 \ 's:AfterIndentKeyword', | |
268 \ ] | |
269 | |
270 for callback_name in indent_callback_names | |
271 " Decho "Running: ".callback_name | |
272 let indent = call(function(callback_name), [indent_info]) | |
273 | |
274 if indent >= 0 | |
275 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) | |
276 return indent | |
277 endif | |
278 endfor | |
279 | |
280 " 2.4. Work on the MSL line. {{{2 | |
281 " -------------------------- | |
282 let indent_callback_names = [ | |
283 \ 's:PreviousNotMSL', | |
284 \ 's:IndentingKeywordInMSL', | |
285 \ 's:ContinuedHangingOperator', | |
286 \ ] | |
287 | |
288 " Most Significant line based on the previous one -- in case it's a | |
25773 | 289 " continuation of something above |
15512 | 290 let indent_info.plnum_msl = s:GetMSL(indent_info.plnum) |
291 | |
292 for callback_name in indent_callback_names | |
293 " Decho "Running: ".callback_name | |
294 let indent = call(function(callback_name), [indent_info]) | |
295 | |
296 if indent >= 0 | |
297 " Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info) | |
298 return indent | |
299 endif | |
300 endfor | |
301 | |
302 " }}}2 | |
303 | |
304 " By default, just return the previous line's indent | |
305 " Decho "Default case matched" | |
306 return indent(indent_info.plnum) | |
307 endfunction | |
308 | |
309 " 3. Indenting Logic Callbacks {{{1 | |
310 " ============================ | |
311 | |
312 function! s:AccessModifier(cline_info) abort | |
313 let info = a:cline_info | |
314 | |
315 " If this line is an access modifier keyword, align according to the closest | |
316 " class declaration. | |
317 if g:ruby_indent_access_modifier_style == 'indent' | |
318 if s:Match(info.clnum, s:access_modifier_regex) | |
319 let class_lnum = s:FindContainingClass() | |
320 if class_lnum > 0 | |
321 return indent(class_lnum) + info.sw | |
322 endif | |
323 endif | |
324 elseif g:ruby_indent_access_modifier_style == 'outdent' | |
325 if s:Match(info.clnum, s:access_modifier_regex) | |
326 let class_lnum = s:FindContainingClass() | |
327 if class_lnum > 0 | |
328 return indent(class_lnum) | |
329 endif | |
330 endif | |
331 endif | |
332 | |
333 return -1 | |
334 endfunction | |
335 | |
336 function! s:ClosingBracketOnEmptyLine(cline_info) abort | |
337 let info = a:cline_info | |
338 | |
339 " If we got a closing bracket on an empty line, find its match and indent | |
340 " according to it. For parentheses we indent to its column - 1, for the | |
341 " others we indent to the containing line's MSL's level. Return -1 if fail. | |
342 let col = matchend(info.cline, '^\s*[]})]') | |
343 | |
344 if col > 0 && !s:IsInStringOrComment(info.clnum, col) | |
345 call cursor(info.clnum, col) | |
346 let closing_bracket = info.cline[col - 1] | |
347 let bracket_pair = strpart('(){}[]', stridx(')}]', closing_bracket) * 2, 2) | |
348 | |
349 if searchpair(escape(bracket_pair[0], '\['), '', bracket_pair[1], 'bW', s:skip_expr) > 0 | |
350 if closing_bracket == ')' && col('.') != col('$') - 1 | |
23931 | 351 if g:ruby_indent_hanging_elements |
352 let ind = virtcol('.') - 1 | |
353 else | |
354 let ind = indent(line('.')) | |
355 end | |
15512 | 356 elseif g:ruby_indent_block_style == 'do' |
357 let ind = indent(line('.')) | |
358 else " g:ruby_indent_block_style == 'expression' | |
359 let ind = indent(s:GetMSL(line('.'))) | |
360 endif | |
361 endif | |
362 | |
363 return ind | |
364 endif | |
365 | |
366 return -1 | |
367 endfunction | |
368 | |
369 function! s:BlockComment(cline_info) abort | |
370 " If we have a =begin or =end set indent to first column. | |
371 if match(a:cline_info.cline, '^\s*\%(=begin\|=end\)$') != -1 | |
372 return 0 | |
373 endif | |
374 return -1 | |
375 endfunction | |
376 | |
377 function! s:DeindentingKeyword(cline_info) abort | |
378 let info = a:cline_info | |
379 | |
380 " If we have a deindenting keyword, find its match and indent to its level. | |
381 " TODO: this is messy | |
382 if s:Match(info.clnum, s:ruby_deindent_keywords) | |
383 call cursor(info.clnum, 1) | |
384 | |
385 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW', | |
386 \ s:end_skip_expr) > 0 | |
387 let msl = s:GetMSL(line('.')) | |
388 let line = getline(line('.')) | |
389 | |
390 if s:IsAssignment(line, col('.')) && | |
391 \ strpart(line, col('.') - 1, 2) !~ 'do' | |
392 " assignment to case/begin/etc, on the same line | |
393 if g:ruby_indent_assignment_style == 'hanging' | |
394 " hanging indent | |
395 let ind = virtcol('.') - 1 | |
396 else | |
397 " align with variable | |
398 let ind = indent(line('.')) | |
399 endif | |
400 elseif g:ruby_indent_block_style == 'do' | |
401 " align to line of the "do", not to the MSL | |
402 let ind = indent(line('.')) | |
403 elseif getline(msl) =~ '=\s*\(#.*\)\=$' | |
404 " in the case of assignment to the MSL, align to the starting line, | |
405 " not to the MSL | |
406 let ind = indent(line('.')) | |
407 else | |
408 " align to the MSL | |
409 let ind = indent(msl) | |
410 endif | |
411 endif | |
412 return ind | |
413 endif | |
414 | |
415 return -1 | |
416 endfunction | |
417 | |
418 function! s:MultilineStringOrLineComment(cline_info) abort | |
419 let info = a:cline_info | |
420 | |
421 " If we are in a multi-line string or line-comment, don't do anything to it. | |
422 if s:IsInStringOrDocumentation(info.clnum, matchend(info.cline, '^\s*') + 1) | |
423 return indent(info.clnum) | |
424 endif | |
425 return -1 | |
426 endfunction | |
427 | |
428 function! s:ClosingHeredocDelimiter(cline_info) abort | |
429 let info = a:cline_info | |
430 | |
431 " If we are at the closing delimiter of a "<<" heredoc-style string, set the | |
432 " indent to 0. | |
433 if info.cline =~ '^\k\+\s*$' | |
434 \ && s:IsInStringDelimiter(info.clnum, 1) | |
435 \ && search('\V<<'.info.cline, 'nbW') > 0 | |
436 return 0 | |
437 endif | |
438 | |
439 return -1 | |
440 endfunction | |
441 | |
442 function! s:LeadingOperator(cline_info) abort | |
443 " If the current line starts with a leading operator, add a level of indent. | |
444 if s:Match(a:cline_info.clnum, s:leading_operator_regex) | |
445 return indent(s:GetMSL(a:cline_info.clnum)) + a:cline_info.sw | |
446 endif | |
447 return -1 | |
448 endfunction | |
449 | |
450 function! s:EmptyInsideString(pline_info) abort | |
451 " If the line is empty and inside a string (the previous line is a string, | |
452 " too), use the previous line's indent | |
453 let info = a:pline_info | |
454 | |
455 let plnum = prevnonblank(info.clnum - 1) | |
456 let pline = getline(plnum) | |
457 | |
458 if info.cline =~ '^\s*$' | |
459 \ && s:IsInStringOrComment(plnum, 1) | |
460 \ && s:IsInStringOrComment(plnum, strlen(pline)) | |
461 return indent(plnum) | |
462 endif | |
463 return -1 | |
464 endfunction | |
465 | |
466 function! s:StartOfFile(pline_info) abort | |
467 " At the start of the file use zero indent. | |
468 if a:pline_info.plnum == 0 | |
469 return 0 | |
470 endif | |
471 return -1 | |
472 endfunction | |
473 | |
474 function! s:AfterAccessModifier(pline_info) abort | |
475 let info = a:pline_info | |
476 | |
477 if g:ruby_indent_access_modifier_style == 'indent' | |
478 " If the previous line was a private/protected keyword, add a | |
479 " level of indent. | |
480 if s:Match(info.plnum, s:indent_access_modifier_regex) | |
481 return indent(info.plnum) + info.sw | |
482 endif | |
483 elseif g:ruby_indent_access_modifier_style == 'outdent' | |
484 " If the previous line was a private/protected/public keyword, add | |
485 " a level of indent, since the keyword has been out-dented. | |
486 if s:Match(info.plnum, s:access_modifier_regex) | |
487 return indent(info.plnum) + info.sw | |
488 endif | |
489 endif | |
490 return -1 | |
491 endfunction | |
492 | |
493 " Example: | |
494 " | |
495 " if foo || bar || | |
496 " baz || bing | |
497 " puts "foo" | |
498 " end | |
499 " | |
500 function! s:ContinuedLine(pline_info) abort | |
501 let info = a:pline_info | |
502 | |
503 let col = s:Match(info.plnum, s:ruby_indent_keywords) | |
504 if s:Match(info.plnum, s:continuable_regex) && | |
505 \ s:Match(info.plnum, s:continuation_regex) | |
506 if col > 0 && s:IsAssignment(info.pline, col) | |
507 if g:ruby_indent_assignment_style == 'hanging' | |
508 " hanging indent | |
509 let ind = col - 1 | |
510 else | |
511 " align with variable | |
512 let ind = indent(info.plnum) | |
513 endif | |
514 else | |
515 let ind = indent(s:GetMSL(info.plnum)) | |
516 endif | |
517 return ind + info.sw + info.sw | |
518 endif | |
519 return -1 | |
520 endfunction | |
521 | |
522 function! s:AfterBlockOpening(pline_info) abort | |
523 let info = a:pline_info | |
524 | |
525 " If the previous line ended with a block opening, add a level of indent. | |
526 if s:Match(info.plnum, s:block_regex) | |
527 if g:ruby_indent_block_style == 'do' | |
528 " don't align to the msl, align to the "do" | |
529 let ind = indent(info.plnum) + info.sw | |
530 else | |
531 let plnum_msl = s:GetMSL(info.plnum) | |
532 | |
533 if getline(plnum_msl) =~ '=\s*\(#.*\)\=$' | |
534 " in the case of assignment to the msl, align to the starting line, | |
535 " not to the msl | |
536 let ind = indent(info.plnum) + info.sw | |
537 else | |
538 let ind = indent(plnum_msl) + info.sw | |
539 endif | |
540 endif | |
541 | |
542 return ind | |
543 endif | |
544 | |
545 return -1 | |
546 endfunction | |
547 | |
548 function! s:AfterLeadingOperator(pline_info) abort | |
549 " If the previous line started with a leading operator, use its MSL's level | |
550 " of indent | |
551 if s:Match(a:pline_info.plnum, s:leading_operator_regex) | |
552 return indent(s:GetMSL(a:pline_info.plnum)) | |
553 endif | |
554 return -1 | |
555 endfunction | |
556 | |
557 function! s:AfterHangingSplat(pline_info) abort | |
558 let info = a:pline_info | |
559 | |
560 " If the previous line ended with the "*" of a splat, add a level of indent | |
561 if info.pline =~ s:splat_regex | |
562 return indent(info.plnum) + info.sw | |
563 endif | |
564 return -1 | |
565 endfunction | |
566 | |
567 function! s:AfterUnbalancedBracket(pline_info) abort | |
568 let info = a:pline_info | |
569 | |
570 " If the previous line contained unclosed opening brackets and we are still | |
571 " in them, find the rightmost one and add indent depending on the bracket | |
572 " type. | |
573 " | |
574 " If it contained hanging closing brackets, find the rightmost one, find its | |
575 " match and indent according to that. | |
576 if info.pline =~ '[[({]' || info.pline =~ '[])}]\s*\%(#.*\)\=$' | |
577 let [opening, closing] = s:ExtraBrackets(info.plnum) | |
578 | |
579 if opening.pos != -1 | |
23931 | 580 if !g:ruby_indent_hanging_elements |
581 return indent(info.plnum) + info.sw | |
582 elseif opening.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0 | |
15512 | 583 if col('.') + 1 == col('$') |
584 return indent(info.plnum) + info.sw | |
585 else | |
586 return virtcol('.') | |
587 endif | |
588 else | |
589 let nonspace = matchend(info.pline, '\S', opening.pos + 1) - 1 | |
590 return nonspace > 0 ? nonspace : indent(info.plnum) + info.sw | |
591 endif | |
592 elseif closing.pos != -1 | |
593 call cursor(info.plnum, closing.pos + 1) | |
594 normal! % | |
595 | |
28246 | 596 if strpart(info.pline, closing.pos) =~ '^)\s*=' |
597 " special case: the closing `) =` of an endless def | |
598 return indent(s:GetMSL(line('.'))) | |
599 endif | |
600 | |
15512 | 601 if s:Match(line('.'), s:ruby_indent_keywords) |
602 return indent('.') + info.sw | |
603 else | |
604 return indent(s:GetMSL(line('.'))) | |
605 endif | |
606 else | |
607 call cursor(info.clnum, info.col) | |
608 end | |
609 endif | |
610 | |
611 return -1 | |
612 endfunction | |
613 | |
614 function! s:AfterEndKeyword(pline_info) abort | |
615 let info = a:pline_info | |
616 " If the previous line ended with an "end", match that "end"s beginning's | |
617 " indent. | |
618 let col = s:Match(info.plnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$') | |
619 if col > 0 | |
620 call cursor(info.plnum, col) | |
621 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW', | |
622 \ s:end_skip_expr) > 0 | |
623 let n = line('.') | |
624 let ind = indent('.') | |
625 let msl = s:GetMSL(n) | |
626 if msl != n | |
627 let ind = indent(msl) | |
628 end | |
629 return ind | |
630 endif | |
631 end | |
632 return -1 | |
633 endfunction | |
634 | |
635 function! s:AfterIndentKeyword(pline_info) abort | |
636 let info = a:pline_info | |
637 let col = s:Match(info.plnum, s:ruby_indent_keywords) | |
638 | |
28246 | 639 if col > 0 && s:Match(info.plnum, s:ruby_endless_def) <= 0 |
15512 | 640 call cursor(info.plnum, col) |
641 let ind = virtcol('.') - 1 + info.sw | |
642 " TODO: make this better (we need to count them) (or, if a searchpair | |
643 " fails, we know that something is lacking an end and thus we indent a | |
644 " level | |
645 if s:Match(info.plnum, s:end_end_regex) | |
646 let ind = indent('.') | |
647 elseif s:IsAssignment(info.pline, col) | |
648 if g:ruby_indent_assignment_style == 'hanging' | |
649 " hanging indent | |
650 let ind = col + info.sw - 1 | |
651 else | |
652 " align with variable | |
653 let ind = indent(info.plnum) + info.sw | |
654 endif | |
655 endif | |
656 return ind | |
657 endif | |
658 | |
659 return -1 | |
660 endfunction | |
661 | |
662 function! s:PreviousNotMSL(msl_info) abort | |
663 let info = a:msl_info | |
664 | |
665 " If the previous line wasn't a MSL | |
666 if info.plnum != info.plnum_msl | |
667 " If previous line ends bracket and begins non-bracket continuation decrease indent by 1. | |
668 if s:Match(info.plnum, s:bracket_switch_continuation_regex) | |
669 " TODO (2016-10-07) Wrong/unused? How could it be "1"? | |
670 return indent(info.plnum) - 1 | |
671 " If previous line is a continuation return its indent. | |
23931 | 672 elseif s:Match(info.plnum, s:non_bracket_continuation_regex) |
15512 | 673 return indent(info.plnum) |
674 endif | |
675 endif | |
676 | |
677 return -1 | |
678 endfunction | |
679 | |
680 function! s:IndentingKeywordInMSL(msl_info) abort | |
681 let info = a:msl_info | |
682 " If the MSL line had an indenting keyword in it, add a level of indent. | |
683 " TODO: this does not take into account contrived things such as | |
684 " module Foo; class Bar; end | |
685 let col = s:Match(info.plnum_msl, s:ruby_indent_keywords) | |
28246 | 686 if col > 0 && s:Match(info.plnum_msl, s:ruby_endless_def) <= 0 |
15512 | 687 let ind = indent(info.plnum_msl) + info.sw |
688 if s:Match(info.plnum_msl, s:end_end_regex) | |
689 let ind = ind - info.sw | |
690 elseif s:IsAssignment(getline(info.plnum_msl), col) | |
691 if g:ruby_indent_assignment_style == 'hanging' | |
692 " hanging indent | |
693 let ind = col + info.sw - 1 | |
694 else | |
695 " align with variable | |
696 let ind = indent(info.plnum_msl) + info.sw | |
697 endif | |
698 endif | |
699 return ind | |
700 endif | |
701 return -1 | |
702 endfunction | |
703 | |
704 function! s:ContinuedHangingOperator(msl_info) abort | |
705 let info = a:msl_info | |
706 | |
707 " If the previous line ended with [*+/.,-=], but wasn't a block ending or a | |
708 " closing bracket, indent one extra level. | |
709 if s:Match(info.plnum_msl, s:non_bracket_continuation_regex) && !s:Match(info.plnum_msl, '^\s*\([\])}]\|end\)') | |
710 if info.plnum_msl == info.plnum | |
711 let ind = indent(info.plnum_msl) + info.sw | |
712 else | |
713 let ind = indent(info.plnum_msl) | |
714 endif | |
715 return ind | |
716 endif | |
717 | |
718 return -1 | |
719 endfunction | |
720 | |
721 " 4. Auxiliary Functions {{{1 | |
530 | 722 " ====================== |
723 | |
15512 | 724 function! s:IsInRubyGroup(groups, lnum, col) abort |
725 let ids = map(copy(a:groups), 'hlID("ruby".v:val)') | |
726 return index(ids, synID(a:lnum, a:col, 1)) >= 0 | |
727 endfunction | |
728 | |
530 | 729 " Check if the character at lnum:col is inside a string, comment, or is ascii. |
15512 | 730 function! s:IsInStringOrComment(lnum, col) abort |
731 return s:IsInRubyGroup(s:syng_strcom, a:lnum, a:col) | |
530 | 732 endfunction |
733 | |
734 " Check if the character at lnum:col is inside a string. | |
15512 | 735 function! s:IsInString(lnum, col) abort |
736 return s:IsInRubyGroup(s:syng_string, a:lnum, a:col) | |
530 | 737 endfunction |
738 | |
739 " Check if the character at lnum:col is inside a string or documentation. | |
15512 | 740 function! s:IsInStringOrDocumentation(lnum, col) abort |
741 return s:IsInRubyGroup(s:syng_stringdoc, a:lnum, a:col) | |
530 | 742 endfunction |
743 | |
4869 | 744 " Check if the character at lnum:col is inside a string delimiter |
15512 | 745 function! s:IsInStringDelimiter(lnum, col) abort |
18857
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
746 return s:IsInRubyGroup( |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
747 \ ['HeredocDelimiter', 'PercentStringDelimiter', 'StringDelimiter'], |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
748 \ a:lnum, a:col |
70ce979e76bc
Update a few runtime files.
Bram Moolenaar <Bram@vim.org>
parents:
15512
diff
changeset
|
749 \ ) |
15512 | 750 endfunction |
751 | |
752 function! s:IsAssignment(str, pos) abort | |
753 return strpart(a:str, 0, a:pos - 1) =~ '=\s*$' | |
4869 | 754 endfunction |
755 | |
530 | 756 " Find line above 'lnum' that isn't empty, in a comment, or in a string. |
15512 | 757 function! s:PrevNonBlankNonString(lnum) abort |
530 | 758 let in_block = 0 |
759 let lnum = prevnonblank(a:lnum) | |
760 while lnum > 0 | |
761 " Go in and out of blocks comments as necessary. | |
762 " If the line isn't empty (with opt. comment) or in a string, end search. | |
763 let line = getline(lnum) | |
4869 | 764 if line =~ '^=begin' |
530 | 765 if in_block |
4869 | 766 let in_block = 0 |
530 | 767 else |
4869 | 768 break |
530 | 769 endif |
4869 | 770 elseif !in_block && line =~ '^=end' |
530 | 771 let in_block = 1 |
772 elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1) | |
4869 | 773 \ && s:IsInStringOrComment(lnum, strlen(line))) |
530 | 774 break |
775 endif | |
776 let lnum = prevnonblank(lnum - 1) | |
777 endwhile | |
778 return lnum | |
779 endfunction | |
780 | |
781 " Find line above 'lnum' that started the continuation 'lnum' may be part of. | |
15512 | 782 function! s:GetMSL(lnum) abort |
530 | 783 " Start on the line we're at and use its indent. |
784 let msl = a:lnum | |
785 let lnum = s:PrevNonBlankNonString(a:lnum - 1) | |
786 while lnum > 0 | |
787 " If we have a continuation line, or we're in a string, use line as MSL. | |
788 " Otherwise, terminate search as we have found our MSL already. | |
789 let line = getline(lnum) | |
4869 | 790 |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
791 if !s:Match(msl, s:backslash_continuation_regex) && |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
792 \ s:Match(lnum, s:backslash_continuation_regex) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
793 " If the current line doesn't end in a backslash, but the previous one |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
794 " does, look for that line's msl |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
795 " |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
796 " Example: |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
797 " foo = "bar" \ |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
798 " "baz" |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
799 " |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
800 let msl = lnum |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
801 elseif s:Match(msl, s:leading_operator_regex) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
802 " If the current line starts with a leading operator, keep its indent |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
803 " and keep looking for an MSL. |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
804 let msl = lnum |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
805 elseif s:Match(lnum, s:splat_regex) |
4869 | 806 " If the above line looks like the "*" of a splat, use the current one's |
807 " indentation. | |
808 " | |
809 " Example: | |
810 " Hash[* | |
811 " method_call do | |
812 " something | |
813 " | |
814 return msl | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
815 elseif s:Match(lnum, s:non_bracket_continuation_regex) && |
4869 | 816 \ s:Match(msl, s:non_bracket_continuation_regex) |
817 " If the current line is a non-bracket continuation and so is the | |
818 " previous one, keep its indent and continue looking for an MSL. | |
819 " | |
820 " Example: | |
821 " method_call one, | |
822 " two, | |
823 " three | |
824 " | |
530 | 825 let msl = lnum |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
826 elseif s:Match(lnum, s:dot_continuation_regex) && |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
827 \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex)) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
828 " If the current line is a bracket continuation or a block-starter, but |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
829 " the previous is a dot, keep going to see if the previous line is the |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
830 " start of another continuation. |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
831 " |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
832 " Example: |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
833 " parent. |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
834 " method_call { |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
835 " three |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
836 " |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
837 let msl = lnum |
4869 | 838 elseif s:Match(lnum, s:non_bracket_continuation_regex) && |
839 \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex)) | |
840 " If the current line is a bracket continuation or a block-starter, but | |
841 " the previous is a non-bracket one, respect the previous' indentation, | |
842 " and stop here. | |
843 " | |
844 " Example: | |
845 " method_call one, | |
846 " two { | |
847 " three | |
848 " | |
849 return lnum | |
850 elseif s:Match(lnum, s:bracket_continuation_regex) && | |
851 \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex)) | |
852 " If both lines are bracket continuations (the current may also be a | |
853 " block-starter), use the current one's and stop here | |
854 " | |
855 " Example: | |
856 " method_call( | |
857 " other_method_call( | |
858 " foo | |
859 return msl | |
860 elseif s:Match(lnum, s:block_regex) && | |
861 \ !s:Match(msl, s:continuation_regex) && | |
862 \ !s:Match(msl, s:block_continuation_regex) | |
863 " If the previous line is a block-starter and the current one is | |
864 " mostly ordinary, use the current one as the MSL. | |
865 " | |
866 " Example: | |
867 " method_call do | |
868 " something | |
869 " something_else | |
870 return msl | |
530 | 871 else |
4869 | 872 let col = match(line, s:continuation_regex) + 1 |
873 if (col > 0 && !s:IsInStringOrComment(lnum, col)) | |
874 \ || s:IsInString(lnum, strlen(line)) | |
875 let msl = lnum | |
876 else | |
877 break | |
878 endif | |
530 | 879 endif |
4869 | 880 |
530 | 881 let lnum = s:PrevNonBlankNonString(lnum - 1) |
882 endwhile | |
883 return msl | |
884 endfunction | |
885 | |
886 " Check if line 'lnum' has more opening brackets than closing ones. | |
15512 | 887 function! s:ExtraBrackets(lnum) abort |
4869 | 888 let opening = {'parentheses': [], 'braces': [], 'brackets': []} |
889 let closing = {'parentheses': [], 'braces': [], 'brackets': []} | |
890 | |
530 | 891 let line = getline(a:lnum) |
4869 | 892 let pos = match(line, '[][(){}]', 0) |
893 | |
894 " Save any encountered opening brackets, and remove them once a matching | |
895 " closing one has been found. If a closing bracket shows up that doesn't | |
896 " close anything, save it for later. | |
530 | 897 while pos != -1 |
898 if !s:IsInStringOrComment(a:lnum, pos + 1) | |
4869 | 899 if line[pos] == '(' |
900 call add(opening.parentheses, {'type': '(', 'pos': pos}) | |
901 elseif line[pos] == ')' | |
902 if empty(opening.parentheses) | |
903 call add(closing.parentheses, {'type': ')', 'pos': pos}) | |
904 else | |
905 let opening.parentheses = opening.parentheses[0:-2] | |
906 endif | |
907 elseif line[pos] == '{' | |
908 call add(opening.braces, {'type': '{', 'pos': pos}) | |
909 elseif line[pos] == '}' | |
910 if empty(opening.braces) | |
911 call add(closing.braces, {'type': '}', 'pos': pos}) | |
912 else | |
913 let opening.braces = opening.braces[0:-2] | |
914 endif | |
915 elseif line[pos] == '[' | |
916 call add(opening.brackets, {'type': '[', 'pos': pos}) | |
917 elseif line[pos] == ']' | |
918 if empty(opening.brackets) | |
919 call add(closing.brackets, {'type': ']', 'pos': pos}) | |
920 else | |
921 let opening.brackets = opening.brackets[0:-2] | |
922 endif | |
530 | 923 endif |
924 endif | |
4869 | 925 |
530 | 926 let pos = match(line, '[][(){}]', pos + 1) |
927 endwhile | |
4869 | 928 |
929 " Find the rightmost brackets, since they're the ones that are important in | |
930 " both opening and closing cases | |
931 let rightmost_opening = {'type': '(', 'pos': -1} | |
932 let rightmost_closing = {'type': ')', 'pos': -1} | |
933 | |
934 for opening in opening.parentheses + opening.braces + opening.brackets | |
935 if opening.pos > rightmost_opening.pos | |
936 let rightmost_opening = opening | |
937 endif | |
938 endfor | |
939 | |
940 for closing in closing.parentheses + closing.braces + closing.brackets | |
941 if closing.pos > rightmost_closing.pos | |
942 let rightmost_closing = closing | |
943 endif | |
944 endfor | |
945 | |
946 return [rightmost_opening, rightmost_closing] | |
530 | 947 endfunction |
948 | |
15512 | 949 function! s:Match(lnum, regex) abort |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
950 let line = getline(a:lnum) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
951 let offset = match(line, '\C'.a:regex) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
952 let col = offset + 1 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
953 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
954 while offset > -1 && s:IsInStringOrComment(a:lnum, col) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
955 let offset = match(line, '\C'.a:regex, offset + 1) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
956 let col = offset + 1 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
957 endwhile |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
958 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
959 if offset > -1 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
960 return col |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
961 else |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
962 return 0 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
963 endif |
530 | 964 endfunction |
965 | |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
966 " Locates the containing class/module's definition line, ignoring nested classes |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
967 " along the way. |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
968 " |
15512 | 969 function! s:FindContainingClass() abort |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
970 let saved_position = getpos('.') |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
971 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
972 while searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW', |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
973 \ s:end_skip_expr) > 0 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
974 if expand('<cword>') =~# '\<class\|module\>' |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
975 let found_lnum = line('.') |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
976 call setpos('.', saved_position) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
977 return found_lnum |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
978 endif |
10272
57b2b8268d3a
commit https://github.com/vim/vim/commit/4575876dc865d4160f20d61bd822fbe7cafbec41
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
979 endwhile |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
980 |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
981 call setpos('.', saved_position) |
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
4869
diff
changeset
|
982 return 0 |
530 | 983 endfunction |
984 | |
985 " }}}1 | |
986 | |
987 let &cpo = s:cpo_save | |
988 unlet s:cpo_save | |
1121 | 989 |
4869 | 990 " vim:set sw=2 sts=2 ts=8 et: |