blob: 6ff15ab958317dcb0aa5527a27efbecccfb66096 [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 Moolenaard09091d2019-01-17 16:07:22 +01006" Last Change: 2019 Jan 06
Bram Moolenaar1e015462005-09-25 22:16:38 +00007
Bram Moolenaar1e015462005-09-25 22:16:38 +00008if exists("b:did_indent")
9 finish
10endif
11
Bram Moolenaar9964e462007-05-05 17:54:07 +000012runtime! indent/ruby.vim
13unlet! b:did_indent
Bram Moolenaarc236c162008-07-13 17:41:49 +000014setlocal indentexpr=
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000015
Bram Moolenaard09091d2019-01-17 16:07:22 +010016if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=# 'eruby'
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000017 exe "runtime! indent/".b:eruby_subtype.".vim"
18else
19 runtime! indent/html.vim
20endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000021unlet! b:did_indent
22
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020023" Force HTML indent to not keep state.
24let b:html_indent_usestate = 0
25
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000026if &l:indentexpr == ''
27 if &l:cindent
28 let &l:indentexpr = 'cindent(v:lnum)'
29 else
30 let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
31 endif
32endif
33let b:eruby_subtype_indentexpr = &l:indentexpr
34
Bram Moolenaar9964e462007-05-05 17:54:07 +000035let b:did_indent = 1
36
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000037setlocal indentexpr=GetErubyIndent()
Bram Moolenaar9964e462007-05-05 17:54:07 +000038setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=else,=elsif,=rescue,=ensure,=when
39
40" Only define the function once.
41if exists("*GetErubyIndent")
42 finish
43endif
44
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020045" this file uses line continuations
46let s:cpo_sav = &cpo
47set cpo&vim
48
Bram Moolenaarc236c162008-07-13 17:41:49 +000049function! GetErubyIndent(...)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020050 " The value of a single shift-width
Bram Moolenaard09091d2019-01-17 16:07:22 +010051 if exists('*shiftwidth')
52 let sw = shiftwidth()
53 else
54 let sw = &sw
55 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020056
Bram Moolenaarc236c162008-07-13 17:41:49 +000057 if a:0 && a:1 == '.'
58 let v:lnum = line('.')
59 elseif a:0 && a:1 =~ '^\d'
60 let v:lnum = a:1
61 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000062 let vcol = col('.')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000063 call cursor(v:lnum,1)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000064 let inruby = searchpair('<%','','%>','W')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000065 call cursor(v:lnum,vcol)
Bram Moolenaarec7944a2013-06-12 21:29:15 +020066 if inruby && getline(v:lnum) !~ '^<%\|^\s*[-=]\=%>'
67 let ind = GetRubyIndent(v:lnum)
Bram Moolenaar9964e462007-05-05 17:54:07 +000068 else
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000069 exe "let ind = ".b:eruby_subtype_indentexpr
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020070
71 " Workaround for Andy Wokula's HTML indent. This should be removed after
72 " some time, since the newest version is fixed in a different way.
73 if b:eruby_subtype_indentexpr =~# '^HtmlIndent('
74 \ && exists('b:indent')
75 \ && type(b:indent) == type({})
76 \ && has_key(b:indent, 'lnum')
77 " Force HTML indent to not keep state
78 let b:indent.lnum = -1
79 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000080 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000081 let lnum = prevnonblank(v:lnum-1)
Bram Moolenaar9964e462007-05-05 17:54:07 +000082 let line = getline(lnum)
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000083 let cline = getline(v:lnum)
Bram Moolenaarec7944a2013-06-12 21:29:15 +020084 if cline =~# '^\s*<%[-=]\=\s*\%(}\|end\|else\|\%(ensure\|rescue\|elsif\|when\).\{-\}\)\s*\%([-=]\=%>\|$\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020085 let ind = ind - sw
Bram Moolenaar1d689522010-05-28 20:54:39 +020086 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020087 if line =~# '\S\s*<%[-=]\=\s*\%(}\|end\).\{-\}\s*\%([-=]\=%>\|$\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020088 let ind = ind - sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000089 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020090 if line =~# '\%({\|\<do\)\%(\s*|[^|]*|\)\=\s*[-=]\=%>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020091 let ind = ind + sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +020092 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 +020093 let ind = ind + sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000094 endif
Bram Moolenaarc236c162008-07-13 17:41:49 +000095 if line =~# '^\s*<%[=#-]\=\s*$' && cline !~# '^\s*end\>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020096 let ind = ind + sw
Bram Moolenaar9964e462007-05-05 17:54:07 +000097 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020098 if line !~# '^\s*<%' && line =~# '%>\s*$' && line !~# '^\s*end\>'
Bram Moolenaard09091d2019-01-17 16:07:22 +010099 \ && synID(v:lnum, match(cline, '\S') + 1, 1) != hlID('htmlEndTag')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200100 let ind = ind - sw
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200101 endif
102 if cline =~# '^\s*[-=]\=%>\s*$'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200103 let ind = ind - sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000104 endif
105 return ind
106endfunction
107
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200108let &cpo = s:cpo_sav
109unlet! s:cpo_sav
110
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000111" vim:set sw=2 sts=2 ts=8 noet: