eruby.vim 2.7 KB

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