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