]> git.r.bdr.sh - rbdr/dotfiles/blob - vim/indent/scala.vim
Ignore vim/.netrwhist
[rbdr/dotfiles] / vim / indent / scala.vim
1 " Vim indent file
2 " Language : Scala (http://scala-lang.org/)
3 " Maintainer : Stefan Matthias Aust
4 " Last Change: 2006 Apr 13
5
6 if exists("b:did_indent")
7 finish
8 endif
9 let b:did_indent = 1
10
11 setlocal indentexpr=GetScalaIndent()
12
13 setlocal indentkeys=0{,0},0),!^F,<>>,<CR>
14
15 setlocal autoindent sw=2 et
16
17 if exists("*GetScalaIndent")
18 finish
19 endif
20
21 function! CountParens(line)
22 let line = substitute(a:line, '"\(.\|\\"\)*"', '', 'g')
23 let open = substitute(line, '[^(]', '', 'g')
24 let close = substitute(line, '[^)]', '', 'g')
25 return strlen(open) - strlen(close)
26 endfunction
27
28 function! GetScalaIndent()
29 " Find a non-blank line above the current line.
30 let lnum = prevnonblank(v:lnum - 1)
31
32 " Hit the start of the file, use zero indent.
33 if lnum == 0
34 return 0
35 endif
36
37 let ind = indent(lnum)
38 let prevline = getline(lnum)
39
40 "Indent html literals
41 if prevline !~ '/>\s*$' && prevline =~ '^\s*<[a-zA-Z][^>]*>\s*$'
42 return ind + &shiftwidth
43 endif
44
45 " Add a 'shiftwidth' after lines that start a block
46 " If if, for or while end with ), this is a one-line block
47 " If val, var, def end with =, this is a one-line block
48 if prevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\|va[lr]\|def\)\>.*[)=]\s*$'
49 \ || prevline =~ '^\s*\<else\>\s*$'
50 \ || prevline =~ '{\s*$'
51 let ind = ind + &shiftwidth
52 endif
53
54 " If parenthesis are unbalanced, indent or dedent
55 let c = CountParens(prevline)
56 echo c
57 if c > 0
58 let ind = ind + &shiftwidth
59 elseif c < 0
60 let ind = ind - &shiftwidth
61 endif
62
63 " Dedent after if, for, while and val, var, def without block
64 let pprevline = getline(prevnonblank(lnum - 1))
65 if pprevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\|va[lr]\|def\)\>.*[)=]\s*$'
66 \ || pprevline =~ '^\s*\<else\>\s*$'
67 let ind = ind - &shiftwidth
68 endif
69
70 " Align 'for' clauses nicely
71 if prevline =~ '^\s*\<for\> (.*;\s*$'
72 let ind = ind - &shiftwidth + 5
73 endif
74
75 " Subtract a 'shiftwidth' on '}' or html
76 let thisline = getline(v:lnum)
77 if thisline =~ '^\s*[})]'
78 \ || thisline =~ '^\s*</[a-zA-Z][^>]*>'
79 let ind = ind - &shiftwidth
80 endif
81
82 return ind
83 endfunction