annotate runtime/indent/java.vim @ 3281:af1e8a1714c2

Update runtime files.
author Bram Moolenaar <bram@vim.org>
date Fri, 20 Jan 2012 21:08:56 +0100
parents a686fd6c8beb
children dd6c2497c997
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
3281
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
3 " Previous Maintainer: Toby Allsopp <toby.allsopp@peace.com>
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
4 " Current Maintainer: Hong Xu <xuhdev@gmail.com>
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
5 " Last Change: 2012 Jan 20
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
6 " Version: 1.0
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
7 " License: Same as Vim.
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
8 " Copyright (c) 2012 Hong Xu
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
9 " Before 2012, this file is maintained by Toby Allsopp.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 " Only load this indent file when no other was loaded.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 if exists("b:did_indent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 let b:did_indent = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 " Indent Java anonymous classes correctly.
234
a686fd6c8beb updated for version 7.0065
vimboss
parents: 26
diff changeset
18 setlocal cindent cinoptions& cinoptions+=j1
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 " The "extends" and "implements" lines start off with the wrong indent.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 setlocal indentkeys& indentkeys+=0=extends indentkeys+=0=implements
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 " Set the function to do the work.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 setlocal indentexpr=GetJavaIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25
234
a686fd6c8beb updated for version 7.0065
vimboss
parents: 26
diff changeset
26 let b:undo_indent = "set cin< cino< indentkeys< indentexpr<"
a686fd6c8beb updated for version 7.0065
vimboss
parents: 26
diff changeset
27
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 " Only define the function once.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 if exists("*GetJavaIndent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 function! SkipJavaBlanksAndComments(startline)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 let lnum = a:startline
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 while lnum > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 let lnum = prevnonblank(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 if getline(lnum) =~ '\*/\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 while getline(lnum) !~ '/\*' && lnum > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 let lnum = lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 if 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 elseif getline(lnum) =~ '^\s*//'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 let lnum = lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 return lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 function GetJavaIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 " 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
58 " specific cases.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 let theIndent = cindent(v:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 " If we're in the middle of a comment then just trust cindent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 if getline(v:lnum) =~ '^\s*\*'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 return theIndent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 " find start of previous line, in case it was a continuation line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)
3281
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
68
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
69 " If the previous line starts with '@', we should have the same indent as
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
70 " the previous one
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
71 if getline(lnum) =~ '^\s*@\S\+\s*$'
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
72 return indent(lnum)
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
73 endif
af1e8a1714c2 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents: 234
diff changeset
74
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 let prev = lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 while prev > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 let next_prev = SkipJavaBlanksAndComments(prev - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 if getline(next_prev) !~ ',\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 let prev = next_prev
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 " Try to align "throws" lines for methods and "extends" and "implements" for
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 " classes.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 if getline(v:lnum) =~ '^\s*\(extends\|implements\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 \ && getline(lnum) !~ '^\s*\(extends\|implements\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 let theIndent = theIndent + &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 " correct for continuation lines of "throws", "implements" and "extends"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 let cont_kw = matchstr(getline(prev),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 \ '^\s*\zs\(throws\|implements\|extends\)\>\ze.*,\s*$')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 if strlen(cont_kw) > 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 let amount = strlen(cont_kw) + 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 if getline(lnum) !~ ',\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 let theIndent = theIndent - (amount + &sw)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 if theIndent < 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 let theIndent = 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 elseif prev == lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 let theIndent = theIndent + amount
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 if cont_kw ==# 'throws'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 let theIndent = theIndent + &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 elseif getline(prev) =~ '^\s*\(throws\|implements\|extends\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 \ && (getline(prev) =~ '{\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 \ || getline(v:lnum) =~ '^\s*{\s*$')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 let theIndent = theIndent - &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 " When the line starts with a }, try aligning it with the matching {,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 " skipping over "throws", "extends" and "implements" clauses.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 if getline(v:lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 call cursor(v:lnum, 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 silent normal %
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 let lnum = line('.')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 if lnum < v:lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 while lnum > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 let next_lnum = SkipJavaBlanksAndComments(lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 if getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 \ && getline(next_lnum) !~ ',\s*$'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 let lnum = prevnonblank(next_lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 return indent(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 " Below a line starting with "}" never indent more. Needed for a method
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 " below a method with an indented "throws" clause.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 if getline(lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$' && indent(lnum) < theIndent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 let theIndent = indent(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 return theIndent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 " vi: sw=2 et