blob: acd99d9c7dabade906139e21cc4ef4ae8519ebad [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 Moolenaar47c532e2022-03-19 15:18:53 +00004" Last Change: 2022 Mar 15
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
Bram Moolenaar47c532e2022-03-19 15:18:53 +000017let b:undo_indent = "setl ai< inde< indk<"
18
Bram Moolenaarc236c162008-07-13 17:41:49 +000019" Only define the function once.
20if exists("*GetHamlIndent")
21 finish
22endif
23
24let s:attributes = '\%({.\{-\}}\|\[.\{-\}\]\)'
25let s:tag = '\%([%.#][[:alnum:]_-]\+\|'.s:attributes.'\)*[<>]*'
26
27if !exists('g:haml_self_closing_tags')
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020028 let g:haml_self_closing_tags = 'base|link|meta|br|hr|img|input'
Bram Moolenaarc236c162008-07-13 17:41:49 +000029endif
30
31function! GetHamlIndent()
32 let lnum = prevnonblank(v:lnum-1)
33 if lnum == 0
34 return 0
35 endif
36 let line = substitute(getline(lnum),'\s\+$','','')
37 let cline = substitute(substitute(getline(v:lnum),'\s\+$','',''),'^\s\+','','')
38 let lastcol = strlen(line)
39 let line = substitute(line,'^\s\+','','')
40 let indent = indent(lnum)
41 let cindent = indent(v:lnum)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020042 let sw = shiftwidth()
Bram Moolenaarc236c162008-07-13 17:41:49 +000043 if cline =~# '\v^-\s*%(elsif|else|when)>'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020044 let indent = cindent < indent ? cindent : indent - sw
Bram Moolenaarc236c162008-07-13 17:41:49 +000045 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020046 let increase = indent + sw
Bram Moolenaarc236c162008-07-13 17:41:49 +000047 if indent == indent(lnum)
48 let indent = cindent <= indent ? -1 : increase
49 endif
Bram Moolenaarc236c162008-07-13 17:41:49 +000050
51 let group = synIDattr(synID(lnum,lastcol,1),'name')
52
53 if line =~ '^!!!'
54 return indent
55 elseif line =~ '^/\%(\[[^]]*\]\)\=$'
56 return increase
Bram Moolenaar7a329912010-05-21 12:05:36 +020057 elseif group == 'hamlFilter'
Bram Moolenaarc236c162008-07-13 17:41:49 +000058 return increase
Bram Moolenaar7a329912010-05-21 12:05:36 +020059 elseif line =~ '^'.s:tag.'[&!]\=[=~-]\s*\%(\%(if\|else\|elsif\|unless\|case\|when\|while\|until\|for\|begin\|module\|class\|def\)\>\%(.*\<end\>\)\@!\|.*do\%(\s*|[^|]*|\)\=\s*$\)'
60 return increase
61 elseif line =~ '^'.s:tag.'[&!]\=[=~-].*,\s*$'
Bram Moolenaarc236c162008-07-13 17:41:49 +000062 return increase
63 elseif line == '-#'
64 return increase
65 elseif group =~? '\v^(hamlSelfCloser)$' || line =~? '^%\v%('.g:haml_self_closing_tags.')>'
66 return indent
67 elseif group =~? '\v^%(hamlTag|hamlAttributesDelimiter|hamlObjectDelimiter|hamlClass|hamlId|htmlTagName|htmlSpecialTagName)$'
68 return increase
69 elseif synIDattr(synID(v:lnum,1,1),'name') ==? 'hamlRubyFilter'
70 return GetRubyIndent()
71 else
72 return indent
73 endif
74endfunction
75
76" vim:set sw=2: