falcon.vim 14 KB

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