-
Notifications
You must be signed in to change notification settings - Fork 15
/
htmljinja.vim
70 lines (54 loc) · 1.6 KB
/
htmljinja.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
if exists("b:did_indent")
finish
endif
runtime! indent/html.vim
unlet! b:did_indent
if &l:indentexpr == ''
if &l:cindent
let &l:indentexpr = 'cindent(v:lnum)'
else
let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
endif
endif
let b:html_indentexpr = &l:indentexpr
let b:did_indent = 1
setlocal indentexpr=GetJinjaIndent()
setlocal indentkeys=o,O,*<Return>,{,},o,O,!^F,<>>
" Only define the function once.
if exists("*GetJinjaIndent")
finish
endif
function! GetJinjaIndent(...)
if a:0 && a:1 == '.'
let v:lnum = line('.')
elseif a:0 && a:1 =~ '^\d'
let v:lnum = a:1
endif
let vcol = col('.')
call cursor(v:lnum,vcol)
exe "let ind = ".b:html_indentexpr
let lnum = prevnonblank(v:lnum-1)
let pnb = getline(lnum)
let cur = getline(v:lnum)
let tagstart = '.*' . '{%\s*'
let tagend = '.*%}' . '.*'
let blocktags = '\(block\|for\|if\|with\|autoescape\|filter\|trans\|macro\|set\)'
let midtags = '\(else\|elif\|pluralize\)'
let pnb_blockstart = pnb =~# tagstart . blocktags . tagend
let pnb_blockend = pnb =~# tagstart . 'end' . blocktags . tagend
let pnb_blockmid = pnb =~# tagstart . midtags . tagend
let cur_blockstart = cur =~# tagstart . blocktags . tagend
let cur_blockend = cur =~# tagstart . 'end' . blocktags . tagend
let cur_blockmid = cur =~# tagstart . midtags . tagend
if pnb_blockstart && !pnb_blockend
let ind = ind + &sw
elseif pnb_blockmid && !pnb_blockend
let ind = ind + &sw
endif
if cur_blockend && !cur_blockstart
let ind = ind - &sw
elseif cur_blockmid
let ind = ind - &sw
endif
return ind
endfunction