如何正确使 vim 缩进 java 注释? [英] How do I make vim indent java annotations correctly?

查看:32
本文介绍了如何正确使 vim 缩进 java 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java代码用注解缩进时,vim坚持这样缩进:

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

@Test
    public void ...

我希望注释与方法定义在同一列中,但我似乎找不到告诉 vim 这样做的方法,除非可能使用缩进表达式,但我不确定是否可以使用与常规 cindent 一起使用.

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.

推荐答案

我无法删除自己的答案,因为它已被接受,但是 @pydave 的答案 似乎是更好(更强大)的解决方案.<小时>您可能应该通过设置 filetype plugin indent on 来使用 java FileType 的缩进文件(而不是使用 cindent).

I cannot delete my own answer because it has already been accepted, but @pydave's answer seems to be the better (more robust) solution.


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

也就是说,来自我的 linux 发行版的 Vim 7.1 附带的缩进文件(查看当前的 vim svn,这对于 7.2 仍然适用)尚未考虑注释.因此我复制了 /usr/share/vim/vim71/indent/java.vim(见 https://vim.svn.sourceforge.net/svnroot/vim/branches/vim7.1/runtime/indent/java.vim) 到 ~/.vim/indent/java.vim 并在末尾添加以下几行:

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.

这篇关于如何正确使 vim 缩进 java 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆