3 " Maintainer: Toby Allsopp <toby.allsopp@peace.com> (resigned)
4 " Last Change: 2005 Mar 28
6 " Only load this indent file when no other was loaded.
7 if exists("b:did_indent")
12 " Indent Java anonymous classes correctly.
13 setlocal cindent cinoptions& cinoptions+=j1
15 " The "extends" and "implements" lines start off with the wrong indent.
16 setlocal indentkeys& indentkeys+=0=extends indentkeys+=0=implements
18 " Set the function to do the work.
19 setlocal indentexpr=GetJavaIndent()
21 let b:undo_indent = "set cin< cino< indentkeys< indentexpr<"
23 " Only define the function once.
24 if exists("*GetJavaIndent")
28 function! SkipJavaBlanksAndComments(startline)
29 let lnum = a:startline
31 let lnum = prevnonblank(lnum)
32 if getline(lnum) =~ '\*/\s*$'
33 while getline(lnum) !~ '/\*' && lnum > 1
36 if getline(lnum) =~ '^\s*/\*'
41 elseif getline(lnum) =~ '^\s*//'
50 function GetJavaIndent()
52 " Java is just like C; use the built-in C indenting and then correct a few
54 let theIndent = cindent(v:lnum)
56 " If we're in the middle of a comment then just trust cindent
57 if getline(v:lnum) =~ '^\s*\*'
61 " find start of previous line, in case it was a continuation line
62 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)
65 let next_prev = SkipJavaBlanksAndComments(prev - 1)
66 if getline(next_prev) !~ ',\s*$'
72 " Try to align "throws" lines for methods and "extends" and "implements" for
74 if getline(v:lnum) =~ '^\s*\(extends\|implements\)\>'
75 \ && getline(lnum) !~ '^\s*\(extends\|implements\)\>'
76 let theIndent = theIndent + &sw
79 " correct for continuation lines of "throws", "implements" and "extends"
80 let cont_kw = matchstr(getline(prev),
81 \ '^\s*\zs\(throws\|implements\|extends\)\>\ze.*,\s*$')
82 if strlen(cont_kw) > 0
83 let amount = strlen(cont_kw) + 1
84 if getline(lnum) !~ ',\s*$'
85 let theIndent = theIndent - (amount + &sw)
90 let theIndent = theIndent + amount
91 if cont_kw ==# 'throws'
92 let theIndent = theIndent + &sw
95 elseif getline(prev) =~ '^\s*\(throws\|implements\|extends\)\>'
96 \ && (getline(prev) =~ '{\s*$'
97 \ || getline(v:lnum) =~ '^\s*{\s*$')
98 let theIndent = theIndent - &sw
101 " When the line starts with a }, try aligning it with the matching {,
102 " skipping over "throws", "extends" and "implements" clauses.
103 if getline(v:lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$'
104 call cursor(v:lnum, 1)
109 let next_lnum = SkipJavaBlanksAndComments(lnum - 1)
110 if getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>'
111 \ && getline(next_lnum) !~ ',\s*$'
114 let lnum = prevnonblank(next_lnum)
120 " Below a line starting with "}" never indent more. Needed for a method
121 " below a method with an indented "throws" clause.
122 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)
123 if getline(lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$' && indent(lnum) < theIndent
124 let theIndent = indent(lnum)