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> |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 6be7f87 | 2012-01-20 21:08:56 +0100 | [diff] [blame] | 9 | " License: Same as Vim. |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 10 | " Copyright (c) 2012-2016 Hong Xu |
| 11 | " Before 2012, this file was maintained by Toby Allsopp. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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. |
Bram Moolenaar | b982ca5 | 2005-03-28 21:02:15 +0000 | [diff] [blame] | 20 | setlocal cindent cinoptions& cinoptions+=j1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | b982ca5 | 2005-03-28 21:02:15 +0000 | [diff] [blame] | 28 | let b:undo_indent = "set cin< cino< indentkeys< indentexpr<" |
| 29 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | " Only define the function once. |
| 31 | if exists("*GetJavaIndent") |
| 32 | finish |
| 33 | endif |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 34 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 35 | let s:keepcpo= &cpo |
| 36 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | 6be7f87 | 2012-01-20 21:08:56 +0100 | [diff] [blame] | 73 | |
| 74 | " If the previous line starts with '@', we should have the same indent as |
| 75 | " the previous one |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 76 | if getline(lnum) =~ '^\s*@.*$' |
Bram Moolenaar | 6be7f87 | 2012-01-20 21:08:56 +0100 | [diff] [blame] | 77 | return indent(lnum) |
| 78 | endif |
| 79 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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. |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 91 | if getline(v:lnum) =~ '^\s*\(throws\|extends\|implements\)\>' |
| 92 | \ && getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>' |
| 93 | let theIndent = theIndent + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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*$' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 102 | let theIndent = theIndent - (amount + shiftwidth()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 103 | if theIndent < 0 |
| 104 | let theIndent = 0 |
| 105 | endif |
| 106 | elseif prev == lnum |
| 107 | let theIndent = theIndent + amount |
| 108 | if cont_kw ==# 'throws' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 109 | let theIndent = theIndent + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 110 | endif |
| 111 | endif |
| 112 | elseif getline(prev) =~ '^\s*\(throws\|implements\|extends\)\>' |
| 113 | \ && (getline(prev) =~ '{\s*$' |
| 114 | \ || getline(v:lnum) =~ '^\s*{\s*$') |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 115 | let theIndent = theIndent - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 122 | silent normal! % |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 147 | let &cpo = s:keepcpo |
| 148 | unlet s:keepcpo |
| 149 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | " vi: sw=2 et |