blob: 664ad61aa5f23ea193072f5a80a47ed36d7da618 [file] [log] [blame]
Bram Moolenaar4a748032010-09-30 21:47:56 +02001" Vim indent file
2" Language: Falcon
3" Maintainer: Steven Oliver <oliver.steven@gmail.com>
4" Website: https://steveno@github.com/steveno/falconpl-vim.git
Bram Moolenaarec7944a2013-06-12 21:29:15 +02005" Credits: This is, to a great extent, a copy n' paste of ruby.vim.
Bram Moolenaar4a748032010-09-30 21:47:56 +02006
Bram Moolenaarec7944a2013-06-12 21:29:15 +02007" 1. Setup {{{1
8" ============
Bram Moolenaar4a748032010-09-30 21:47:56 +02009
10" Only load this indent file when no other was loaded.
11if exists("b:did_indent")
12 finish
13endif
14let b:did_indent = 1
15
16setlocal nosmartindent
17
18" Setup indent function and when to use it
Bram Moolenaarec7944a2013-06-12 21:29:15 +020019setlocal indentexpr=FalconGetIndent(v:lnum)
Bram Moolenaar4a748032010-09-30 21:47:56 +020020setlocal indentkeys=0{,0},0),0],!^F,o,O,e
21setlocal indentkeys+==~case,=~catch,=~default,=~elif,=~else,=~end,=~\"
22
23" Define the appropriate indent function but only once
24if exists("*FalconGetIndent")
25 finish
26endif
27
28let s:cpo_save = &cpo
29set cpo&vim
30
Bram Moolenaarec7944a2013-06-12 21:29:15 +020031" 2. Variables {{{1
32" ============
Bram Moolenaar4a748032010-09-30 21:47:56 +020033
34" Regex of syntax group names that are strings AND comments
35let s:syng_strcom = '\<falcon\%(String\|StringEscape\|Comment\)\>'
36
37" Regex of syntax group names that are strings
38let s:syng_string = '\<falcon\%(String\|StringEscape\)\>'
39
Bram Moolenaarec7944a2013-06-12 21:29:15 +020040" Regex that defines blocks.
41"
42" Note that there's a slight problem with this regex and s:continuation_regex.
43" Code like this will be matched by both:
44"
45" method_call do |(a, b)|
46"
47" The reason is that the pipe matches a hanging "|" operator.
48"
49let s:block_regex =
50 \ '\%(\<do:\@!\>\|%\@<!{\)\s*\%(|\s*(*\s*\%([*@&]\=\h\w*,\=\s*\)\%(,\s*(*\s*[*@&]\=\h\w*\s*)*\s*\)*|\)\=\s*\%(#.*\)\=$'
51
52let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex
53
54" Regex that defines continuation lines.
55" TODO: this needs to deal with if ...: and so on
56let s:continuation_regex =
57 \ '\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
58
59" Regex that defines bracket continuations
60let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'
61
62" Regex that defines continuation lines, not including (, {, or [.
63let s:non_bracket_continuation_regex = '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)\s*\%(#.*\)\=$'
64
Bram Moolenaar4a748032010-09-30 21:47:56 +020065" Keywords to indent on
66let s:falcon_indent_keywords = '^\s*\(case\|catch\|class\|enum\|default\|elif\|else' .
67 \ '\|for\|function\|if.*"[^"]*:.*"\|if \(\(:\)\@!.\)*$\|loop\|object\|select' .
68 \ '\|switch\|try\|while\|\w*\s*=\s*\w*([$\)'
69
70" Keywords to deindent on
71let s:falcon_deindent_keywords = '^\s*\(case\|catch\|default\|elif\|else\|end\)'
72
Bram Moolenaarec7944a2013-06-12 21:29:15 +020073" 3. Functions {{{1
74" ============
Bram Moolenaar4a748032010-09-30 21:47:56 +020075
Bram Moolenaarec7944a2013-06-12 21:29:15 +020076" Check if the character at lnum:col is inside a string, comment, or is ascii.
Bram Moolenaar4a748032010-09-30 21:47:56 +020077function s:IsInStringOrComment(lnum, col)
78 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
79endfunction
80
Bram Moolenaarec7944a2013-06-12 21:29:15 +020081" Check if the character at lnum:col is inside a string.
82function s:IsInString(lnum, col)
83 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
84endfunction
Bram Moolenaar4a748032010-09-30 21:47:56 +020085
Bram Moolenaarec7944a2013-06-12 21:29:15 +020086" Check if the character at lnum:col is inside a string delimiter
87function s:IsInStringDelimiter(lnum, col)
88 return synIDattr(synID(a:lnum, a:col, 1), 'name') == 'falconStringDelimiter'
89endfunction
Bram Moolenaar4a748032010-09-30 21:47:56 +020090
Bram Moolenaarec7944a2013-06-12 21:29:15 +020091" Find line above 'lnum' that isn't empty, in a comment, or in a string.
92function s:PrevNonBlankNonString(lnum)
93 let in_block = 0
94 let lnum = prevnonblank(a:lnum)
95 while lnum > 0
96 " Go in and out of blocks comments as necessary.
97 " If the line isn't empty (with opt. comment) or in a string, end search.
98 let line = getline(lnum)
99 if line =~ '^=begin'
100 if in_block
101 let in_block = 0
102 else
103 break
104 endif
105 elseif !in_block && line =~ '^=end'
106 let in_block = 1
107 elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
108 \ && s:IsInStringOrComment(lnum, strlen(line)))
109 break
110 endif
111 let lnum = prevnonblank(lnum - 1)
112 endwhile
113 return lnum
114endfunction
Bram Moolenaar4a748032010-09-30 21:47:56 +0200115
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200116" Find line above 'lnum' that started the continuation 'lnum' may be part of.
117function s:GetMSL(lnum)
118 " Start on the line we're at and use its indent.
119 let msl = a:lnum
120 let msl_body = getline(msl)
121 let lnum = s:PrevNonBlankNonString(a:lnum - 1)
122 while lnum > 0
123 " If we have a continuation line, or we're in a string, use line as MSL.
124 " Otherwise, terminate search as we have found our MSL already.
125 let line = getline(lnum)
126
127 if s:Match(line, s:non_bracket_continuation_regex) &&
128 \ s:Match(msl, s:non_bracket_continuation_regex)
129 " If the current line is a non-bracket continuation and so is the
130 " previous one, keep its indent and continue looking for an MSL.
131 "
132 " Example:
133 " method_call one,
134 " two,
135 " three
136 "
137 let msl = lnum
138 elseif s:Match(lnum, s:non_bracket_continuation_regex) &&
139 \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
140 " If the current line is a bracket continuation or a block-starter, but
141 " the previous is a non-bracket one, respect the previous' indentation,
142 " and stop here.
143 "
144 " Example:
145 " method_call one,
146 " two {
147 " three
148 "
149 return lnum
150 elseif s:Match(lnum, s:bracket_continuation_regex) &&
151 \ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
152 " If both lines are bracket continuations (the current may also be a
153 " block-starter), use the current one's and stop here
154 "
155 " Example:
156 " method_call(
157 " other_method_call(
158 " foo
159 return msl
160 elseif s:Match(lnum, s:block_regex) &&
161 \ !s:Match(msl, s:continuation_regex) &&
162 \ !s:Match(msl, s:block_continuation_regex)
163 " If the previous line is a block-starter and the current one is
164 " mostly ordinary, use the current one as the MSL.
165 "
166 " Example:
167 " method_call do
168 " something
169 " something_else
170 return msl
171 else
172 let col = match(line, s:continuation_regex) + 1
173 if (col > 0 && !s:IsInStringOrComment(lnum, col))
174 \ || s:IsInString(lnum, strlen(line))
175 let msl = lnum
176 else
177 break
178 endif
179 endif
180
181 let msl_body = getline(msl)
182 let lnum = s:PrevNonBlankNonString(lnum - 1)
183 endwhile
184 return msl
185endfunction
186
187" Check if line 'lnum' has more opening brackets than closing ones.
188function s:ExtraBrackets(lnum)
189 let opening = {'parentheses': [], 'braces': [], 'brackets': []}
190 let closing = {'parentheses': [], 'braces': [], 'brackets': []}
191
192 let line = getline(a:lnum)
193 let pos = match(line, '[][(){}]', 0)
194
195 " Save any encountered opening brackets, and remove them once a matching
196 " closing one has been found. If a closing bracket shows up that doesn't
197 " close anything, save it for later.
198 while pos != -1
199 if !s:IsInStringOrComment(a:lnum, pos + 1)
200 if line[pos] == '('
201 call add(opening.parentheses, {'type': '(', 'pos': pos})
202 elseif line[pos] == ')'
203 if empty(opening.parentheses)
204 call add(closing.parentheses, {'type': ')', 'pos': pos})
205 else
206 let opening.parentheses = opening.parentheses[0:-2]
207 endif
208 elseif line[pos] == '{'
209 call add(opening.braces, {'type': '{', 'pos': pos})
210 elseif line[pos] == '}'
211 if empty(opening.braces)
212 call add(closing.braces, {'type': '}', 'pos': pos})
213 else
214 let opening.braces = opening.braces[0:-2]
215 endif
216 elseif line[pos] == '['
217 call add(opening.brackets, {'type': '[', 'pos': pos})
218 elseif line[pos] == ']'
219 if empty(opening.brackets)
220 call add(closing.brackets, {'type': ']', 'pos': pos})
221 else
222 let opening.brackets = opening.brackets[0:-2]
223 endif
224 endif
225 endif
226
227 let pos = match(line, '[][(){}]', pos + 1)
228 endwhile
229
230 " Find the rightmost brackets, since they're the ones that are important in
231 " both opening and closing cases
232 let rightmost_opening = {'type': '(', 'pos': -1}
233 let rightmost_closing = {'type': ')', 'pos': -1}
234
235 for opening in opening.parentheses + opening.braces + opening.brackets
236 if opening.pos > rightmost_opening.pos
237 let rightmost_opening = opening
238 endif
239 endfor
240
241 for closing in closing.parentheses + closing.braces + closing.brackets
242 if closing.pos > rightmost_closing.pos
243 let rightmost_closing = closing
244 endif
245 endfor
246
247 return [rightmost_opening, rightmost_closing]
248endfunction
249
250function s:Match(lnum, regex)
251 let col = match(getline(a:lnum), '\C'.a:regex) + 1
252 return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
253endfunction
254
255function s:MatchLast(lnum, regex)
256 let line = getline(a:lnum)
257 let col = match(line, '.*\zs' . a:regex)
258 while col != -1 && s:IsInStringOrComment(a:lnum, col)
259 let line = strpart(line, 0, col)
260 let col = match(line, '.*' . a:regex)
261 endwhile
262 return col + 1
263endfunction
264
265" 4. FalconGetIndent Routine {{{1
266" ============
267
268function FalconGetIndent(...)
269 " For the current line, use the first argument if given, else v:lnum
270 let clnum = a:0 ? a:1 : v:lnum
Bram Moolenaar4a748032010-09-30 21:47:56 +0200271
272 " Use zero indent at the top of the file
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200273 if clnum == 0
Bram Moolenaar4a748032010-09-30 21:47:56 +0200274 return 0
275 endif
276
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200277 let line = getline(clnum)
278 let ind = -1
279
280 " If we got a closing bracket on an empty line, find its match and indent
281 " according to it. For parentheses we indent to its column - 1, for the
282 " others we indent to the containing line's MSL's level. Return -1 if fail.
283 let col = matchend(line, '^\s*[]})]')
284 if col > 0 && !s:IsInStringOrComment(clnum, col)
285 call cursor(clnum, col)
286 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
287 if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
288 if line[col-1]==')' && col('.') != col('$') - 1
289 let ind = virtcol('.') - 1
290 else
291 let ind = indent(s:GetMSL(line('.')))
292 endif
293 endif
294 return ind
295 endif
296
297 " If we have a deindenting keyword, find its match and indent to its level.
298 " TODO: this is messy
299 if s:Match(clnum, s:falcon_deindent_keywords)
300 call cursor(clnum, 1)
301 if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
302 \ s:end_skip_expr) > 0
303 let msl = s:GetMSL(line('.'))
304 let line = getline(line('.'))
305
306 if strpart(line, 0, col('.') - 1) =~ '=\s*$' &&
307 \ strpart(line, col('.') - 1, 2) !~ 'do'
308 let ind = virtcol('.') - 1
309 elseif getline(msl) =~ '=\s*\(#.*\)\=$'
310 let ind = indent(line('.'))
311 else
312 let ind = indent(msl)
313 endif
314 endif
315 return ind
316 endif
317
318 " If we are in a multi-line string or line-comment, don't do anything to it.
319 if s:IsInString(clnum, matchend(line, '^\s*') + 1)
320 return indent('.')
321 endif
322
323 " Find a non-blank, non-multi-line string line above the current line.
324 let lnum = s:PrevNonBlankNonString(clnum - 1)
325
326 " If the line is empty and inside a string, use the previous line.
327 if line =~ '^\s*$' && lnum != prevnonblank(clnum - 1)
328 return indent(prevnonblank(clnum))
329 endif
330
331 " At the start of the file use zero indent.
332 if lnum == 0
333 return 0
334 endif
335
336 " Set up variables for the previous line.
337 let line = getline(lnum)
Bram Moolenaar4a748032010-09-30 21:47:56 +0200338 let ind = indent(lnum)
Bram Moolenaar4a748032010-09-30 21:47:56 +0200339
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200340 " If the previous line ended with a block opening, add a level of indent.
341 if s:Match(lnum, s:block_regex)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200342 return indent(s:GetMSL(lnum)) + shiftwidth()
Bram Moolenaar4a748032010-09-30 21:47:56 +0200343 endif
344
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200345 " If it contained hanging closing brackets, find the rightmost one, find its
346 " match and indent according to that.
347 if line =~ '[[({]' || line =~ '[])}]\s*\%(#.*\)\=$'
348 let [opening, closing] = s:ExtraBrackets(lnum)
349
350 if opening.pos != -1
351 if opening.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
352 if col('.') + 1 == col('$')
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200353 return ind + shiftwidth()
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200354 else
355 return virtcol('.')
356 endif
357 else
358 let nonspace = matchend(line, '\S', opening.pos + 1) - 1
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200359 return nonspace > 0 ? nonspace : ind + shiftwidth()
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200360 endif
361 elseif closing.pos != -1
362 call cursor(lnum, closing.pos + 1)
363 normal! %
364
365 if s:Match(line('.'), s:falcon_indent_keywords)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200366 return indent('.') + shiftwidth()
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200367 else
368 return indent('.')
369 endif
370 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100371 call cursor(clnum, 0) " FIXME: column was vcol
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200372 end
Bram Moolenaar4a748032010-09-30 21:47:56 +0200373 endif
374
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200375 " If the previous line ended with an "end", match that "end"s beginning's
376 " indent.
377 let col = s:Match(lnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
378 if col > 0
379 call cursor(lnum, col)
380 if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
381 \ s:end_skip_expr) > 0
382 let n = line('.')
383 let ind = indent('.')
384 let msl = s:GetMSL(n)
385 if msl != n
386 let ind = indent(msl)
387 end
388 return ind
389 endif
390 end
391
392 let col = s:Match(lnum, s:falcon_indent_keywords)
393 if col > 0
394 call cursor(lnum, col)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200395 let ind = virtcol('.') - 1 + shiftwidth()
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200396 " TODO: make this better (we need to count them) (or, if a searchpair
397 " fails, we know that something is lacking an end and thus we indent a
398 " level
399 if s:Match(lnum, s:end_end_regex)
400 let ind = indent('.')
401 endif
402 return ind
Bram Moolenaar4a748032010-09-30 21:47:56 +0200403 endif
404
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200405 " Set up variables to use and search for MSL to the previous line.
406 let p_lnum = lnum
407 let lnum = s:GetMSL(lnum)
408
409 " If the previous line wasn't a MSL and is continuation return its indent.
410 " TODO: the || s:IsInString() thing worries me a bit.
411 if p_lnum != lnum
412 if s:Match(p_lnum, s:non_bracket_continuation_regex) || s:IsInString(p_lnum,strlen(line))
413 return ind
414 endif
Bram Moolenaar4a748032010-09-30 21:47:56 +0200415 endif
416
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200417 " Set up more variables, now that we know we wasn't continuation bound.
418 let line = getline(lnum)
419 let msl_ind = indent(lnum)
420
421 " If the MSL line had an indenting keyword in it, add a level of indent.
422 " TODO: this does not take into account contrived things such as
423 " module Foo; class Bar; end
424 if s:Match(lnum, s:falcon_indent_keywords)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200425 let ind = msl_ind + shiftwidth()
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200426 if s:Match(lnum, s:end_end_regex)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200427 let ind = ind - shiftwidth()
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200428 endif
429 return ind
Bram Moolenaar4a748032010-09-30 21:47:56 +0200430 endif
431
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200432 " If the previous line ended with [*+/.,-=], but wasn't a block ending or a
433 " closing bracket, indent one extra level.
434 if s:Match(lnum, s:non_bracket_continuation_regex) && !s:Match(lnum, '^\s*\([\])}]\|end\)')
435 if lnum == p_lnum
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200436 let ind = msl_ind + shiftwidth()
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200437 else
438 let ind = msl_ind
439 endif
440 return ind
Bram Moolenaar4a748032010-09-30 21:47:56 +0200441 endif
442
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200443 return ind
Bram Moolenaar4a748032010-09-30 21:47:56 +0200444endfunction
445
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200446" }}}1
447
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100448let &cpo = s:cpo_save
449unlet s:cpo_save
450
Bram Moolenaar4a748032010-09-30 21:47:56 +0200451" vim: set sw=4 sts=4 et tw=80 :