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