views:

975

answers:

2

When indenting java code with annotations, vim insists on indenting like this:

@Test
    public void ...

I want the annotation to be in the same column as the method definition but I can't seem to find a way to tell vim to do that, except maybe using an indent expression but I'm not sure if I can use that together with regular cindent.

edit: The filetype plugin was already turned on I just got a bit confused about indenting plugins. The accepted answer may be a bit hackish but works for me as well.

+5  A: 

You should probably be using the indentation file for the java FileType (instead of using cindent) by setting filetype plugin indent on.

That said, the indentation file coming with the Vim 7.1 from my linux distribution (looking at the current vim svn this is still true for 7.2) doesn't account for annotations yet. I therefore copied /usr/share/vim/vim71/indent/java.vim (see https://vim.svn.sourceforge.net/svnroot/vim/branches/vim7.1/runtime/indent/java.vim) to ~/.vim/indent/java.vim and added the following lines right before the end:

let lnum = prevnonblank(v:lnum - 1)
let line = getline(lnum)
if line =~ '^\s*@.*$'
    let theIndent = indent(lnum)
endif

I'm not sure if this breaks any of the other indentations, but it works for me.

WMR
This didn't work for me - I added it right at the end. I'm using vim 7.2What am I doing wrong ?
Naseer
:facepalm: worked now - I was adding it at the very end and not at the end of the function :DThanks !
Naseer
This worked for me as well, thanks.
Lyle
Worked fine for me too. I also changed the cinoptions line at the beginning to this: cinoptions+=(s,m1,M0,j1
jkramer
A: 

Hey, this is a great solution, thank you so much for posting it. If anyone reading this has commit status on the vim project, I think this would be an excellent feature to include in builds from now on.

Miles Thomason