开发者

Skip indentation of first line after JUnit @Test annotation

In general I prefer to have annotation tags for methods, including @Test ones, on the line before the method declaration like this

@Test
public void testMyMethod() {
// Code
}

rather than

@Test public void testMyMethod() {
// Code
}

I have ja开发者_开发知识库va specific settings in ~/.vim/ftplugins/java.vim. What can I add to java.vim such that indentation is skipped at the first line after the @Test tag? At the moment vim will, as it is supposed to according to java.vim, indent 4 characters giving

@Test
____* <-- cursor placed here

while I would prefer

@Test
* <-- Cursor placed here


I do something similar in C++, where my company's coding style doesn't want indents in namespace blocks. There's an answer for that at this post, adapting it to your question would just swap the '^\s*namespace.*' bit to your desired pattern. If it's for all annotations, perhaps simply...

function! IndentAnnotation()
  let l:cline_num = line('.')
  let l:pline_num = prevnonblank(l:cline_num - 1)
  let l:pline = getline(l:pline_num)
  let l:retv = cindent('.')
  while l:pline =~# '\(^\s*{\s*\|^\s*//\|^\s*/\*\|\*/\s*$\)'
    let l:pline_num = prevnonblank(l:pline_num - 1)
    let l:pline = getline(l:pline_num)
  endwhile
  if l:pline =~# '^\s*@.*'
    let l:retv = 0
  endif
  return l:retv
endfunction

setlocal indentexpr=IndentAnnotation()

Save this in your vimfiles (inside ~/vimfiles or on windows in your homedir or inside ProgramFiles\vim\vimfiles) as indent/java.vim. You may be able to simplify by removing the while/endwhile hunt for a non-blank line if you require an annotation to occur on a line exactly above the line you're indenting (with no intervening blank lines).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜