eruby.vim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. " Vim indent file
  2. " Language: eRuby
  3. " Maintainer: Tim Pope <vimNOSPAM@tpope.org>
  4. " URL: https://github.com/vim-ruby/vim-ruby
  5. " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
  6. " Last Change: 2019 Jan 06
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. runtime! indent/ruby.vim
  11. unlet! b:did_indent
  12. setlocal indentexpr=
  13. if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=# 'eruby'
  14. exe "runtime! indent/".b:eruby_subtype.".vim"
  15. else
  16. runtime! indent/html.vim
  17. endif
  18. unlet! b:did_indent
  19. " Force HTML indent to not keep state.
  20. let b:html_indent_usestate = 0
  21. if &l:indentexpr == ''
  22. if &l:cindent
  23. let &l:indentexpr = 'cindent(v:lnum)'
  24. else
  25. let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
  26. endif
  27. endif
  28. let b:eruby_subtype_indentexpr = &l:indentexpr
  29. let b:did_indent = 1
  30. setlocal indentexpr=GetErubyIndent()
  31. setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=else,=elsif,=rescue,=ensure,=when
  32. " Only define the function once.
  33. if exists("*GetErubyIndent")
  34. finish
  35. endif
  36. " this file uses line continuations
  37. let s:cpo_sav = &cpo
  38. set cpo&vim
  39. function! GetErubyIndent(...)
  40. " The value of a single shift-width
  41. if exists('*shiftwidth')
  42. let sw = shiftwidth()
  43. else
  44. let sw = &sw
  45. endif
  46. if a:0 && a:1 == '.'
  47. let v:lnum = line('.')
  48. elseif a:0 && a:1 =~ '^\d'
  49. let v:lnum = a:1
  50. endif
  51. let vcol = col('.')
  52. call cursor(v:lnum,1)
  53. let inruby = searchpair('<%','','%>','W')
  54. call cursor(v:lnum,vcol)
  55. if inruby && getline(v:lnum) !~ '^<%\|^\s*[-=]\=%>'
  56. let ind = GetRubyIndent(v:lnum)
  57. else
  58. exe "let ind = ".b:eruby_subtype_indentexpr
  59. " Workaround for Andy Wokula's HTML indent. This should be removed after
  60. " some time, since the newest version is fixed in a different way.
  61. if b:eruby_subtype_indentexpr =~# '^HtmlIndent('
  62. \ && exists('b:indent')
  63. \ && type(b:indent) == type({})
  64. \ && has_key(b:indent, 'lnum')
  65. " Force HTML indent to not keep state
  66. let b:indent.lnum = -1
  67. endif
  68. endif
  69. let lnum = prevnonblank(v:lnum-1)
  70. let line = getline(lnum)
  71. let cline = getline(v:lnum)
  72. if cline =~# '^\s*<%[-=]\=\s*\%(}\|end\|else\|\%(ensure\|rescue\|elsif\|when\).\{-\}\)\s*\%([-=]\=%>\|$\)'
  73. let ind = ind - sw
  74. endif
  75. if line =~# '\S\s*<%[-=]\=\s*\%(}\|end\).\{-\}\s*\%([-=]\=%>\|$\)'
  76. let ind = ind - sw
  77. endif
  78. if line =~# '\%({\|\<do\)\%(\s*|[^|]*|\)\=\s*[-=]\=%>'
  79. let ind = ind + sw
  80. elseif line =~# '<%[-=]\=\s*\%(module\|class\|def\|if\|for\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue\)\>.*%>'
  81. let ind = ind + sw
  82. endif
  83. if line =~# '^\s*<%[=#-]\=\s*$' && cline !~# '^\s*end\>'
  84. let ind = ind + sw
  85. endif
  86. if line !~# '^\s*<%' && line =~# '%>\s*$' && line !~# '^\s*end\>'
  87. \ && synID(v:lnum, match(cline, '\S') + 1, 1) != hlID('htmlEndTag')
  88. let ind = ind - sw
  89. endif
  90. if cline =~# '^\s*[-=]\=%>\s*$'
  91. let ind = ind - sw
  92. endif
  93. return ind
  94. endfunction
  95. let &cpo = s:cpo_sav
  96. unlet! s:cpo_sav
  97. " vim:set sw=2 sts=2 ts=8 noet: