codekingpro/portable-devtools
114k
1" Vim indent file2" Language: Java3" Previous Maintainer: Toby Allsopp <toby.allsopp@peace.com>4" Current Maintainer: Hong Xu <hong@topbug.net>5" Homepage: http://www.vim.org/scripts/script.php?script_id=38996" https://github.com/xuhdev/indent-java.vim7" Last Change: 2016 Mar 78" Version: 1.19" License: Same as Vim.10" Copyright (c) 2012-2016 Hong Xu11" 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 finish16endif17let b:did_indent = 118 19" Indent Java anonymous classes correctly.20setlocal cindent cinoptions& cinoptions+=j121 22" The "extends" and "implements" lines start off with the wrong indent.23setlocal indentkeys& indentkeys+=0=extends indentkeys+=0=implements24 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 finish33endif34 35let s:keepcpo= &cpo36set cpo&vim37 38function! SkipJavaBlanksAndComments(startline)39 let lnum = a:startline40 while lnum > 141 let lnum = prevnonblank(lnum)42 if getline(lnum) =~ '\*/\s*$'43 while getline(lnum) !~ '/\*' && lnum > 144 let lnum = lnum - 145 endwhile46 if getline(lnum) =~ '^\s*/\*'47 let lnum = lnum - 148 else49 break50 endif51 elseif getline(lnum) =~ '^\s*//'52 let lnum = lnum - 153 else54 break55 endif56 endwhile57 return lnum58endfunction59 60function GetJavaIndent()61 62 " Java is just like C; use the built-in C indenting and then correct a few63 " specific cases.64 let theIndent = cindent(v:lnum)65 66 " If we're in the middle of a comment then just trust cindent67 if getline(v:lnum) =~ '^\s*\*'68 return theIndent69 endif70 71 " find start of previous line, in case it was a continuation line72 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)73 74 " If the previous line starts with '@', we should have the same indent as75 " the previous one76 if getline(lnum) =~ '^\s*@.*$'77 return indent(lnum)78 endif79 80 let prev = lnum81 while prev > 182 let next_prev = SkipJavaBlanksAndComments(prev - 1)83 if getline(next_prev) !~ ',\s*$'84 break85 endif86 let prev = next_prev87 endwhile88 89 " Try to align "throws" lines for methods and "extends" and "implements" for90 " classes.91 if getline(v:lnum) =~ '^\s*\(throws\|extends\|implements\)\>'92 \ && getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>'93 let theIndent = theIndent + shiftwidth()94 endif95 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) > 0100 let amount = strlen(cont_kw) + 1101 if getline(lnum) !~ ',\s*$'102 let theIndent = theIndent - (amount + shiftwidth())103 if theIndent < 0104 let theIndent = 0105 endif106 elseif prev == lnum107 let theIndent = theIndent + amount108 if cont_kw ==# 'throws'109 let theIndent = theIndent + shiftwidth()110 endif111 endif112 elseif getline(prev) =~ '^\s*\(throws\|implements\|extends\)\>'113 \ && (getline(prev) =~ '{\s*$'114 \ || getline(v:lnum) =~ '^\s*{\s*$')115 let theIndent = theIndent - shiftwidth()116 endif117 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:lnum125 while lnum > 1126 let next_lnum = SkipJavaBlanksAndComments(lnum - 1)127 if getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>'128 \ && getline(next_lnum) !~ ',\s*$'129 break130 endif131 let lnum = prevnonblank(next_lnum)132 endwhile133 return indent(lnum)134 endif135 endif136 137 " Below a line starting with "}" never indent more. Needed for a method138 " below a method with an indented "throws" clause.139 let lnum = SkipJavaBlanksAndComments(v:lnum - 1)140 if getline(lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$' && indent(lnum) < theIndent141 let theIndent = indent(lnum)142 endif143 144 return theIndent145endfunction146 147let &cpo = s:keepcpo148unlet s:keepcpo149 150" vi: sw=2 et151 