blob: 7beb0388d1085a905e8b08991de1905dfb564cbe [file] [log] [blame]
Bram Moolenaar7a329912010-05-21 12:05:36 +02001" Vim indent file
2" Language: Liquid
3" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
Bram Moolenaar3ec574f2017-06-13 18:12:01 +02004" Last Change: 2017 Jun 13
Bram Moolenaar7a329912010-05-21 12:05:36 +02005
6if exists('b:did_indent')
7 finish
8endif
9
10set indentexpr=
11if exists('b:liquid_subtype')
12 exe 'runtime! indent/'.b:liquid_subtype.'.vim'
13else
14 runtime! indent/html.vim
15endif
16unlet! b:did_indent
17
18if &l:indentexpr == ''
19 if &l:cindent
20 let &l:indentexpr = 'cindent(v:lnum)'
21 else
22 let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
23 endif
24endif
25let b:liquid_subtype_indentexpr = &l:indentexpr
26
27let b:did_indent = 1
28
29setlocal indentexpr=GetLiquidIndent()
30setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=endif,=endunless,=endifchanged,=endcase,=endfor,=endtablerow,=endcapture,=else,=elsif,=when,=empty
31
32" Only define the function once.
33if exists('*GetLiquidIndent')
34 finish
35endif
36
37function! s:count(string,pattern)
38 let string = substitute(a:string,'\C'.a:pattern,"\n",'g')
39 return strlen(substitute(string,"[^\n]",'','g'))
40endfunction
41
42function! GetLiquidIndent(...)
43 if a:0 && a:1 == '.'
44 let v:lnum = line('.')
45 elseif a:0 && a:1 =~ '^\d'
46 let v:lnum = a:1
47 endif
48 let vcol = col('.')
49 call cursor(v:lnum,1)
50 exe "let ind = ".b:liquid_subtype_indentexpr
51 let lnum = prevnonblank(v:lnum-1)
52 let line = getline(lnum)
53 let cline = getline(v:lnum)
54 let line = substitute(line,'\C^\%(\s*{%\s*end\w*\s*%}\)\+','','')
55 let line .= matchstr(cline,'\C^\%(\s*{%\s*end\w*\s*%}\)\+')
56 let cline = substitute(cline,'\C^\%(\s*{%\s*end\w*\s*%}\)\+','','')
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020057 let sw = shiftwidth()
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020058 let ind += sw * s:count(line,'{%\s*\%(if\|elsif\|else\|unless\|ifchanged\|case\|when\|for\|empty\|tablerow\|capture\)\>')
59 let ind -= sw * s:count(line,'{%\s*end\%(if\|unless\|ifchanged\|case\|for\|tablerow\|capture\)\>')
60 let ind -= sw * s:count(cline,'{%\s*\%(elsif\|else\|when\|empty\)\>')
61 let ind -= sw * s:count(cline,'{%\s*end\w*$')
Bram Moolenaar7a329912010-05-21 12:05:36 +020062 return ind
63endfunction