annotate runtime/indent/java.vim @ 14651:5d076ad59cf4

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