blob: baca1d49d9c8b67f7c7af71daccb0bada1695c5e [file] [log] [blame]
Bram Moolenaarc236c162008-07-13 17:41:49 +00001" Vim indent file
Bram Moolenaar7a329912010-05-21 12:05:36 +02002" Language: Haml
3" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
Bram Moolenaarc08ee742019-12-05 22:47:25 +01004" Last Change: 2019 Dec 05
Bram Moolenaarc236c162008-07-13 17:41:49 +00005
6if exists("b:did_indent")
7 finish
8endif
9runtime! indent/ruby.vim
10unlet! b:did_indent
11let b:did_indent = 1
12
Bram Moolenaarc08ee742019-12-05 22:47:25 +010013setlocal autoindent
Bram Moolenaarc236c162008-07-13 17:41:49 +000014setlocal indentexpr=GetHamlIndent()
15setlocal indentkeys=o,O,*<Return>,},],0),!^F,=end,=else,=elsif,=rescue,=ensure,=when
16
17" Only define the function once.
18if exists("*GetHamlIndent")
19 finish
20endif
21
22let s:attributes = '\%({.\{-\}}\|\[.\{-\}\]\)'
23let s:tag = '\%([%.#][[:alnum:]_-]\+\|'.s:attributes.'\)*[<>]*'
24
25if !exists('g:haml_self_closing_tags')
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020026 let g:haml_self_closing_tags = 'base|link|meta|br|hr|img|input'
Bram Moolenaarc236c162008-07-13 17:41:49 +000027endif
28
29function! GetHamlIndent()
30 let lnum = prevnonblank(v:lnum-1)
31 if lnum == 0
32 return 0
33 endif
34 let line = substitute(getline(lnum),'\s\+$','','')
35 let cline = substitute(substitute(getline(v:lnum),'\s\+$','',''),'^\s\+','','')
36 let lastcol = strlen(line)
37 let line = substitute(line,'^\s\+','','')
38 let indent = indent(lnum)
39 let cindent = indent(v:lnum)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020040 let sw = shiftwidth()
Bram Moolenaarc236c162008-07-13 17:41:49 +000041 if cline =~# '\v^-\s*%(elsif|else|when)>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020042 let indent = cindent < indent ? cindent : indent - sw
Bram Moolenaarc236c162008-07-13 17:41:49 +000043 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020044 let increase = indent + sw
Bram Moolenaarc236c162008-07-13 17:41:49 +000045 if indent == indent(lnum)
46 let indent = cindent <= indent ? -1 : increase
47 endif
Bram Moolenaarc236c162008-07-13 17:41:49 +000048
49 let group = synIDattr(synID(lnum,lastcol,1),'name')
50
51 if line =~ '^!!!'
52 return indent
53 elseif line =~ '^/\%(\[[^]]*\]\)\=$'
54 return increase
Bram Moolenaar7a329912010-05-21 12:05:36 +020055 elseif group == 'hamlFilter'
Bram Moolenaarc236c162008-07-13 17:41:49 +000056 return increase
Bram Moolenaar7a329912010-05-21 12:05:36 +020057 elseif line =~ '^'.s:tag.'[&!]\=[=~-]\s*\%(\%(if\|else\|elsif\|unless\|case\|when\|while\|until\|for\|begin\|module\|class\|def\)\>\%(.*\<end\>\)\@!\|.*do\%(\s*|[^|]*|\)\=\s*$\)'
58 return increase
59 elseif line =~ '^'.s:tag.'[&!]\=[=~-].*,\s*$'
Bram Moolenaarc236c162008-07-13 17:41:49 +000060 return increase
61 elseif line == '-#'
62 return increase
63 elseif group =~? '\v^(hamlSelfCloser)$' || line =~? '^%\v%('.g:haml_self_closing_tags.')>'
64 return indent
65 elseif group =~? '\v^%(hamlTag|hamlAttributesDelimiter|hamlObjectDelimiter|hamlClass|hamlId|htmlTagName|htmlSpecialTagName)$'
66 return increase
67 elseif synIDattr(synID(v:lnum,1,1),'name') ==? 'hamlRubyFilter'
68 return GetRubyIndent()
69 else
70 return indent
71 endif
72endfunction
73
74" vim:set sw=2: