xref: /vim-8.2.3635/runtime/indent/java.vim (revision 036986f1)
1" Vim indent file
2" Language:	Java
3" Previous Maintainer: Toby Allsopp <[email protected]>
4" Current Maintainer: Hong Xu <[email protected]>
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
9" License: Same as Vim.
10" Copyright (c) 2012-2016 Hong Xu
11" Before 2012, this file was maintained by Toby Allsopp.
12
13" Only load this indent file when no other was loaded.
14if exists("b:did_indent")
15  finish
16endif
17let b:did_indent = 1
18
19" Indent Java anonymous classes correctly.
20setlocal cindent cinoptions& cinoptions+=j1
21
22" The "extends" and "implements" lines start off with the wrong indent.
23setlocal indentkeys& indentkeys+=0=extends indentkeys+=0=implements
24
25" Set the function to do the work.
26setlocal indentexpr=GetJavaIndent()
27
28let b:undo_indent = "set cin< cino< indentkeys< indentexpr<"
29
30" Only define the function once.
31if exists("*GetJavaIndent")
32  finish
33endif
34
35let s:keepcpo= &cpo
36set cpo&vim
37
38function! 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
58endfunction
59
60function 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)
73
74  " If the previous line starts with '@', we should have the same indent as
75  " the previous one
76  if getline(lnum) =~ '^\s*@.*$'
77    return indent(lnum)
78  endif
79
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.
91  if getline(v:lnum) =~ '^\s*\(throws\|extends\|implements\)\>'
92        \ && getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>'
93    let theIndent = theIndent + shiftwidth()
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*$'
102      let theIndent = theIndent - (amount + shiftwidth())
103      if theIndent < 0
104        let theIndent = 0
105      endif
106    elseif prev == lnum
107      let theIndent = theIndent + amount
108      if cont_kw ==# 'throws'
109        let theIndent = theIndent + shiftwidth()
110      endif
111    endif
112  elseif getline(prev) =~ '^\s*\(throws\|implements\|extends\)\>'
113        \ && (getline(prev) =~ '{\s*$'
114        \  || getline(v:lnum) =~ '^\s*{\s*$')
115    let theIndent = theIndent - shiftwidth()
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)
122    silent normal! %
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
145endfunction
146
147let &cpo = s:keepcpo
148unlet s:keepcpo
149
150" vi: sw=2 et
151