2 " Language : Scala (http://scala-lang.org/)
3 " Maintainer : Stefan Matthias Aust
4 " Last Change: 2006 Apr 13
6 if exists("b:did_indent")
11 setlocal indentexpr=GetScalaIndent()
13 setlocal indentkeys=0{,0},0),!^F,<>>,<CR>
15 setlocal autoindent sw=2 et
17 if exists("*GetScalaIndent")
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)
28 function! GetScalaIndent()
29 " Find a non-blank line above the current line.
30 let lnum = prevnonblank(v:lnum - 1)
32 " Hit the start of the file, use zero indent.
37 let ind = indent(lnum)
38 let prevline = getline(lnum)
41 if prevline !~ '/>\s*$' && prevline =~ '^\s*<[a-zA-Z][^>]*>\s*$'
42 return ind + &shiftwidth
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
54 " If parenthesis are unbalanced, indent or dedent
55 let c = CountParens(prevline)
58 let ind = ind + &shiftwidth
60 let ind = ind - &shiftwidth
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
70 " Align 'for' clauses nicely
71 if prevline =~ '^\s*\<for\> (.*;\s*$'
72 let ind = ind - &shiftwidth + 5
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