annotate runtime/indent/java.vim @ 1624:18ee39301b82 v7.2a

updated for version 7.2a
author vimboss
date Tue, 24 Jun 2008 22:58:06 +0000
parents a686fd6c8beb
children af1e8a1714c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 " Vim indent file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 " Language: Java
26
404aac550f35 updated for version 7.0017
vimboss
parents: 7
diff changeset
3 " Maintainer: Toby Allsopp <toby.allsopp@peace.com> (resigned)
234
a686fd6c8beb updated for version 7.0065
vimboss
parents: 26
diff changeset
4 " Last Change: 2005 Mar 28
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 " Only load this indent file when no other was loaded.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 if exists("b:did_indent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 let b:did_indent = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 " Indent Java anonymous classes correctly.
234
a686fd6c8beb updated for version 7.0065
vimboss
parents: 26
diff changeset
13 setlocal cindent cinoptions& cinoptions+=j1
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 " The "extends" and "implements" lines start off with the wrong indent.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 setlocal indentkeys& indentkeys+=0=extends indentkeys+=0=implements
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 " Set the function to do the work.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 setlocal indentexpr=GetJavaIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20
234
a686fd6c8beb updated for version 7.0065
vimboss
parents: 26
diff changeset
21 let b:undo_indent = "set cin< cino< indentkeys< indentexpr<"
a686fd6c8beb updated for version 7.0065
vimboss
parents: 26
diff changeset
22
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 " Only define the function once.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 if exists("*GetJavaIndent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 function! SkipJavaBlanksAndComments(startline)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 let lnum = a:startline
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 while lnum > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 let lnum = prevnonblank(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 if getline(lnum) =~ '\*/\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 while getline(lnum) !~ '/\*' && lnum > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 let lnum = lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 if getline(lnum) =~ '^\s*/\*'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 let lnum = lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 elseif getline(lnum) =~ '^\s*//'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 let lnum = lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 return lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 function GetJavaIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 " Java is just like C; use the built-in C indenting and then correct a few
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 " specific cases.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 let theIndent = cindent(v:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 " If we're in the middle of a comment then just trust cindent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 if getline(v:lnum) =~ '^\s*\*'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 return theIndent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 " find start of previous line, in case it was a continuation line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 let prev = lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 while prev > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 let next_prev = SkipJavaBlanksAndComments(prev - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 if getline(next_prev) !~ ',\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 let prev = next_prev
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 " Try to align "throws" lines for methods and "extends" and "implements" for
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 " classes.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 if getline(v:lnum) =~ '^\s*\(extends\|implements\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 \ && getline(lnum) !~ '^\s*\(extends\|implements\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 let theIndent = theIndent + &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 " correct for continuation lines of "throws", "implements" and "extends"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 let cont_kw = matchstr(getline(prev),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 \ '^\s*\zs\(throws\|implements\|extends\)\>\ze.*,\s*$')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 if strlen(cont_kw) > 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 let amount = strlen(cont_kw) + 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 if getline(lnum) !~ ',\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 let theIndent = theIndent - (amount + &sw)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 if theIndent < 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 let theIndent = 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 elseif prev == lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 let theIndent = theIndent + amount
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 if cont_kw ==# 'throws'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 let theIndent = theIndent + &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 elseif getline(prev) =~ '^\s*\(throws\|implements\|extends\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 \ && (getline(prev) =~ '{\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 \ || getline(v:lnum) =~ '^\s*{\s*$')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 let theIndent = theIndent - &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 " When the line starts with a }, try aligning it with the matching {,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 " skipping over "throws", "extends" and "implements" clauses.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 if getline(v:lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 call cursor(v:lnum, 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 silent normal %
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 let lnum = line('.')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 if lnum < v:lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 while lnum > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 let next_lnum = SkipJavaBlanksAndComments(lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 if getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 \ && getline(next_lnum) !~ ',\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 let lnum = prevnonblank(next_lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 return indent(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 " Below a line starting with "}" never indent more. Needed for a method
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 " below a method with an indented "throws" clause.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 if getline(lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$' && indent(lnum) < theIndent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 let theIndent = indent(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 return theIndent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 " vi: sw=2 et