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