home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / runtime / indent / java.vim < prev    next >
Encoding:
Text File  |  2002-10-04  |  3.0 KB  |  112 lines

  1. " Vim indent file
  2. " Language:    Java
  3. " Maintainer:    Bram Moolenaar <Bram@vim.org>
  4. " Last Change:    2002 Oct 04
  5.  
  6. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8.    finish
  9. endif
  10. let b:did_indent = 1
  11.  
  12. " Indent Java anonymous classes correctly.
  13. setlocal cinoptions& cinoptions+=j1
  14.  
  15. " The "extends" and "implements" lines start off with the wrong indent.
  16. setlocal indentkeys& indentkeys+=0=extends indentkeys+=0=implements
  17.  
  18. " Set the function to do the work.
  19. setlocal indentexpr=GetJavaIndent()
  20.  
  21. " Only define the function once.
  22. if exists("*GetJavaIndent")
  23.     finish
  24. endif
  25.  
  26. function GetJavaIndent()
  27.  
  28.   " Java is just like C; use the built-in C indenting and then correct a few
  29.   " specific cases.
  30.   let theIndent = cindent(v:lnum)
  31.  
  32.   " find start of previous line, in case it was a continuation line
  33.   let prev = prevnonblank(v:lnum - 1)
  34.   while prev > 1
  35.     if getline(prev - 1) !~ ',\s*$'
  36.       break
  37.     endif
  38.     let prev = prev - 1
  39.   endwhile
  40.  
  41.   " Try to align "throws" lines for methods and "extends" and "implements" for
  42.   " classes.
  43.   if getline(v:lnum) =~ '^\s*\(extends\|implements\)\>'
  44.     \ && getline(prev) !~ '^\s*\(extends\|implements\)\>'
  45.     let theIndent = theIndent + &sw
  46.   endif
  47.  
  48.   " correct for continuation lines of "throws" and "implements"
  49.   if getline(prev) =~ '^\s*\(throws\|implements\)\>.*,\s*$'
  50.     if getline(prev) =~ '^\s*throws'
  51.       let amount = &sw + 7    " add length of 'throws '
  52.     else
  53.       let amount = 11        " length of 'implements'.
  54.     endif
  55.     if getline(v:lnum - 1) !~ ',\s*$'
  56.       let theIndent = theIndent - amount
  57.       if theIndent < 0
  58.     let theIndent = 0
  59.       endif
  60.     elseif prev == v:lnum - 1
  61.       let theIndent = theIndent + amount
  62.     endif
  63.   elseif getline(prev) =~ '^\s*throws\>'
  64.     let theIndent = theIndent - &sw
  65.   endif
  66.  
  67.   " When the line starts with a }, try aligning it with the matching {,
  68.   " skipping over "throws", "extends" and "implements" clauses.
  69.   if getline(v:lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$'
  70.     call cursor(v:lnum, 1)
  71.     silent normal %
  72.     let lnum = line('.')
  73.     if lnum < v:lnum
  74.       while lnum > 1
  75.     if getline(lnum) !~ '^\s*\(throws\|extends\|implements\)\>'
  76.           \ && getline(prevnonblank(lnum - 1)) !~ ',\s*$'
  77.       break
  78.     endif
  79.     let lnum = prevnonblank(lnum - 1)
  80.       endwhile
  81.       return indent(lnum)
  82.     endif
  83.   endif
  84.  
  85.   " Below a line starting with "}" never indent more.  Needed for a method
  86.   " below a method with an indented "throws" clause.
  87.   " First ignore comment lines.
  88.   let lnum = v:lnum - 1
  89.   while lnum > 1
  90.     let lnum = prevnonblank(lnum)
  91.     if getline(lnum) =~ '\*/\s*$'
  92.       while getline(lnum) !~ '/\*' && lnum > 1
  93.     let lnum = lnum - 1
  94.       endwhile
  95.       if getline(lnum) =~ '^\s*/\*'
  96.     let lnum = lnum - 1
  97.       else
  98.     break
  99.       endif
  100.     elseif getline(lnum) =~ '^\s*//'
  101.       let lnum = lnum - 1
  102.     else
  103.       break
  104.     endif
  105.   endwhile
  106.   if getline(lnum) =~ '^\s*}\s*\(//.*\|/\*.*\)\=$' && indent(lnum) < theIndent
  107.     let theIndent = indent(lnum)
  108.   endif
  109.  
  110.   return theIndent
  111. endfunction
  112.