blob: 505832549538a9fe7b14fd1779c816aee4cacaa7 [file] [log] [blame]
Bram Moolenaar1e015462005-09-25 22:16:38 +00001" Vim indent file
Bram Moolenaar9964e462007-05-05 17:54:07 +00002" Language: eRuby
Bram Moolenaar1d689522010-05-28 20:54:39 +02003" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
Bram Moolenaarec7944a2013-06-12 21:29:15 +02004" URL: https://github.com/vim-ruby/vim-ruby
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00005" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
Bram Moolenaar1e015462005-09-25 22:16:38 +00006
Bram Moolenaar1e015462005-09-25 22:16:38 +00007if exists("b:did_indent")
8 finish
9endif
10
Bram Moolenaar9964e462007-05-05 17:54:07 +000011runtime! indent/ruby.vim
12unlet! b:did_indent
Bram Moolenaarc236c162008-07-13 17:41:49 +000013setlocal indentexpr=
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000014
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000015if exists("b:eruby_subtype")
16 exe "runtime! indent/".b:eruby_subtype.".vim"
17else
18 runtime! indent/html.vim
19endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000020unlet! b:did_indent
21
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020022" Force HTML indent to not keep state.
23let b:html_indent_usestate = 0
24
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000025if &l:indentexpr == ''
26 if &l:cindent
27 let &l:indentexpr = 'cindent(v:lnum)'
28 else
29 let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
30 endif
31endif
32let b:eruby_subtype_indentexpr = &l:indentexpr
33
Bram Moolenaar9964e462007-05-05 17:54:07 +000034let b:did_indent = 1
35
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000036setlocal indentexpr=GetErubyIndent()
Bram Moolenaar9964e462007-05-05 17:54:07 +000037setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=else,=elsif,=rescue,=ensure,=when
38
39" Only define the function once.
40if exists("*GetErubyIndent")
41 finish
42endif
43
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020044" this file uses line continuations
45let s:cpo_sav = &cpo
46set cpo&vim
47
Bram Moolenaarc236c162008-07-13 17:41:49 +000048function! GetErubyIndent(...)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020049 " The value of a single shift-width
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020050 let sw = shiftwidth()
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020051
Bram Moolenaarc236c162008-07-13 17:41:49 +000052 if a:0 && a:1 == '.'
53 let v:lnum = line('.')
54 elseif a:0 && a:1 =~ '^\d'
55 let v:lnum = a:1
56 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000057 let vcol = col('.')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000058 call cursor(v:lnum,1)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000059 let inruby = searchpair('<%','','%>','W')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000060 call cursor(v:lnum,vcol)
Bram Moolenaarec7944a2013-06-12 21:29:15 +020061 if inruby && getline(v:lnum) !~ '^<%\|^\s*[-=]\=%>'
62 let ind = GetRubyIndent(v:lnum)
Bram Moolenaar9964e462007-05-05 17:54:07 +000063 else
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000064 exe "let ind = ".b:eruby_subtype_indentexpr
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020065
66 " Workaround for Andy Wokula's HTML indent. This should be removed after
67 " some time, since the newest version is fixed in a different way.
68 if b:eruby_subtype_indentexpr =~# '^HtmlIndent('
69 \ && exists('b:indent')
70 \ && type(b:indent) == type({})
71 \ && has_key(b:indent, 'lnum')
72 " Force HTML indent to not keep state
73 let b:indent.lnum = -1
74 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000075 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000076 let lnum = prevnonblank(v:lnum-1)
Bram Moolenaar9964e462007-05-05 17:54:07 +000077 let line = getline(lnum)
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000078 let cline = getline(v:lnum)
Bram Moolenaarec7944a2013-06-12 21:29:15 +020079 if cline =~# '^\s*<%[-=]\=\s*\%(}\|end\|else\|\%(ensure\|rescue\|elsif\|when\).\{-\}\)\s*\%([-=]\=%>\|$\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020080 let ind = ind - sw
Bram Moolenaar1d689522010-05-28 20:54:39 +020081 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020082 if line =~# '\S\s*<%[-=]\=\s*\%(}\|end\).\{-\}\s*\%([-=]\=%>\|$\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020083 let ind = ind - sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000084 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020085 if line =~# '\%({\|\<do\)\%(\s*|[^|]*|\)\=\s*[-=]\=%>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020086 let ind = ind + sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +020087 elseif line =~# '<%[-=]\=\s*\%(module\|class\|def\|if\|for\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue\)\>.*%>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020088 let ind = ind + sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000089 endif
Bram Moolenaarc236c162008-07-13 17:41:49 +000090 if line =~# '^\s*<%[=#-]\=\s*$' && cline !~# '^\s*end\>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020091 let ind = ind + sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000092 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020093 if line !~# '^\s*<%' && line =~# '%>\s*$' && line !~# '^\s*end\>'
94 let ind = ind - sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +020095 endif
96 if cline =~# '^\s*[-=]\=%>\s*$'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020097 let ind = ind - sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000098 endif
99 return ind
100endfunction
101
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200102let &cpo = s:cpo_sav
103unlet! s:cpo_sav
104
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000105" vim:set sw=2 sts=2 ts=8 noet: