blob: afabd4fe5b16335330b96057bcad43067edda097 [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
50 if exists('*shiftwidth')
51 let sw = shiftwidth()
52 else
53 let sw = &sw
54 endif
55
Bram Moolenaarc236c162008-07-13 17:41:49 +000056 if a:0 && a:1 == '.'
57 let v:lnum = line('.')
58 elseif a:0 && a:1 =~ '^\d'
59 let v:lnum = a:1
60 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000061 let vcol = col('.')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000062 call cursor(v:lnum,1)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000063 let inruby = searchpair('<%','','%>','W')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000064 call cursor(v:lnum,vcol)
Bram Moolenaarec7944a2013-06-12 21:29:15 +020065 if inruby && getline(v:lnum) !~ '^<%\|^\s*[-=]\=%>'
66 let ind = GetRubyIndent(v:lnum)
Bram Moolenaar9964e462007-05-05 17:54:07 +000067 else
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000068 exe "let ind = ".b:eruby_subtype_indentexpr
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020069
70 " Workaround for Andy Wokula's HTML indent. This should be removed after
71 " some time, since the newest version is fixed in a different way.
72 if b:eruby_subtype_indentexpr =~# '^HtmlIndent('
73 \ && exists('b:indent')
74 \ && type(b:indent) == type({})
75 \ && has_key(b:indent, 'lnum')
76 " Force HTML indent to not keep state
77 let b:indent.lnum = -1
78 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000079 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000080 let lnum = prevnonblank(v:lnum-1)
Bram Moolenaar9964e462007-05-05 17:54:07 +000081 let line = getline(lnum)
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000082 let cline = getline(v:lnum)
Bram Moolenaarec7944a2013-06-12 21:29:15 +020083 if cline =~# '^\s*<%[-=]\=\s*\%(}\|end\|else\|\%(ensure\|rescue\|elsif\|when\).\{-\}\)\s*\%([-=]\=%>\|$\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020084 let ind = ind - sw
Bram Moolenaar1d689522010-05-28 20:54:39 +020085 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020086 if line =~# '\S\s*<%[-=]\=\s*\%(}\|end\).\{-\}\s*\%([-=]\=%>\|$\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020087 let ind = ind - sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000088 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020089 if line =~# '\%({\|\<do\)\%(\s*|[^|]*|\)\=\s*[-=]\=%>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020090 let ind = ind + sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +020091 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 +020092 let ind = ind + sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000093 endif
Bram Moolenaarc236c162008-07-13 17:41:49 +000094 if line =~# '^\s*<%[=#-]\=\s*$' && cline !~# '^\s*end\>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020095 let ind = ind + sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000096 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020097 if line !~# '^\s*<%' && line =~# '%>\s*$' && line !~# '^\s*end\>'
98 let ind = ind - sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +020099 endif
100 if cline =~# '^\s*[-=]\=%>\s*$'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200101 let ind = ind - sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000102 endif
103 return ind
104endfunction
105
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200106let &cpo = s:cpo_sav
107unlet! s:cpo_sav
108
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000109" vim:set sw=2 sts=2 ts=8 noet: