ruby.vim 29 KB

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