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