summaryrefslogtreecommitdiffstats
path: root/indent/eelixir.vim
blob: 0c98c1625a34b8631ef3c64f334a4fdc8ca16af9 (plain) (blame)
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
71
72
73
74
75
76
if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'elixir') != -1
  finish
endif

if exists("b:did_indent")
  finish
endif

runtime! indent/elixir.vim
unlet! b:did_indent
setlocal indentexpr=

let s:cpo_save = &cpo
set cpo&vim

if exists("b:eelixir_subtype")
  exe "runtime! indent/".b:eelixir_subtype.".vim"
else
  runtime! indent/html.vim
endif
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:eelixir_subtype_indentexpr = &l:indentexpr

let b:did_indent = 1

setlocal indentexpr=GetEelixirIndent()
setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=else,=elsif,=catch,=after,=rescue

" Only define the function once.
if exists("*GetEelixirIndent")
  finish
endif

function! GetEelixirIndent(...)
  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,1)
  let inelixir = searchpair('<%','','%>','W')
  call cursor(v:lnum,vcol)
  if inelixir && getline(v:lnum) !~ '^<%\|^\s*%>'
    let ind = GetElixirIndent()
  else
    exe "let ind = ".b:eelixir_subtype_indentexpr
  endif
  let lnum = prevnonblank(v:lnum-1)
  let line = getline(lnum)
  let cline = getline(v:lnum)
  if cline =~# '^\s*<%\s*\%(end\|else\|elsif\|catch\|after\|rescue\)\>.*%>'
    let ind -= &sw
  elseif line =~# '\S\s*<%\s*end\s*%>'
    let ind -= &sw
  endif
  if line =~# '<%[=%]\=\s*.*\(\<do\|->\)\s*%>' ||
        \ line =~# '<%\s*\%(else\|elsif\|catch\|after\|rescue\)\>.*%>'
    let ind += &sw
  endif
  if cline =~# '^\s*%>\s*$'
    let ind -= &sw
  endif
  return ind
endfunction

let &cpo = s:cpo_save
unlet s:cpo_save