robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
1" Vim indent file 2" Language: Java 3" 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=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" ) 15finish 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=extendsindentkeys += 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" ) 32finish 33endif 34 35let s: keepcpo = & cpo 36set cpo & vim 37 38function ! SkipJavaBlanksAndComments ( startline ) 39let lnum = a: startline 40while lnum > 1 41let lnum = prevnonblank ( lnum ) 42if getline ( lnum ) =~ '\*/\s*$' 43while getline ( lnum ) !~ '/\*' && lnum > 1 44let lnum = lnum - 1 45endwhile 46if getline ( lnum ) =~ '^\s*/\*' 47let lnum = lnum - 1 48else 49break 50endif 51elseif getline ( lnum ) =~ '^\s*//' 52let lnum = lnum - 1 53else 54break 55endif 56endwhile 57return 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. 64let theIndent = cindent ( v: lnum ) 65 66" If we're in the middle of a comment then just trust cindent 67if getline ( v: lnum ) =~ '^\s*\*' 68return theIndent 69endif 70 71" find start of previous line, in case it was a continuation line 72let lnum = SkipJavaBlanksAndComments ( v: lnum - 1 ) 73 74" If the previous line starts with '@', we should have the same indent as 75" the previous one 76if getline ( lnum ) =~ '^\s*@.*$' 77return indent ( lnum ) 78endif 79 80let prev = lnum 81while prev > 1 82let next_prev = SkipJavaBlanksAndComments ( prev - 1 ) 83if getline ( next_prev ) !~ ',\s*$' 84break 85endif 86let prev = next_prev 87endwhile 88 89" Try to align "throws" lines for methods and "extends" and "implements" for 90" classes. 91if getline ( v: lnum ) =~ '^\s*\(throws\|extends\|implements\)\>' 92 \&& getline ( lnum ) !~ '^\s*\(throws\|extends\|implements\)\>' 93let theIndent = theIndent + shiftwidth () 94endif 95 96" correct for continuation lines of "throws", "implements" and "extends" 97let cont_kw = matchstr ( getline ( prev ), 98 \'^\s*\zs\(throws\|implements\|extends\)\>\ze.*,\s*$' ) 99if strlen ( cont_kw ) > 0 100let amount = strlen ( cont_kw ) + 1 101if getline ( lnum ) !~ ',\s*$' 102let theIndent = theIndent - ( amount + shiftwidth ()) 103if theIndent < 0 104let theIndent = 0 105endif 106elseif prev == lnum 107let theIndent = theIndent + amount 108if cont_kw ==# 'throws' 109let theIndent = theIndent + shiftwidth () 110endif 111endif 112elseif getline ( prev ) =~ '^\s*\(throws\|implements\|extends\)\>' 113 \&& ( getline ( prev ) =~ '{\s*$' 114 \|| getline ( v: lnum ) =~ '^\s*{\s*$' ) 115let theIndent = theIndent - shiftwidth () 116endif 117 118" When the line starts with a }, try aligning it with the matching {, 119" skipping over "throws", "extends" and "implements" clauses. 120if getline ( v: lnum ) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$' 121call cursor ( v: lnum , 1 ) 122silent normal ! % 123let lnum = line ( '.' ) 124if lnum < v: lnum 125while lnum > 1 126let next_lnum = SkipJavaBlanksAndComments ( lnum - 1 ) 127if getline ( lnum ) !~ '^\s*\(throws\|extends\|implements\)\>' 128 \&& getline ( next_lnum ) !~ ',\s*$' 129break 130endif 131let lnum = prevnonblank ( next_lnum ) 132endwhile 133return indent ( lnum ) 134endif 135endif 136 137" Below a line starting with "}" never indent more. Needed for a method 138" below a method with an indented "throws" clause. 139let lnum = SkipJavaBlanksAndComments ( v: lnum - 1 ) 140if getline ( lnum ) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$' && indent ( lnum ) < theIndent 141let theIndent = indent ( lnum ) 142endif 143 144return theIndent 145endfunction 146 147let & cpo = s: keepcpo 148unlet s: keepcpo 149 150" vi: sw=2 et