如何在Java中注释后删除缩进 [英] How to remove indent after annotation in Java

查看:242
本文介绍了如何在Java中注释后删除缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java代码中有@Override注释,但是vim像这样自动缩进下一行:

I have @Override annotation in my java code, but vim indent the next line automatically like this:

@Override
    public String toString()
    {
        //some code
    }

我如何摆脱缩进?

据我所知,/usr/share/vim/vim73/indent中的缩进文件java.vim已解决了该问题,但问题仍然存在.这是java.vim中的代码:

As far as I know, the indent file java.vim in /usr/share/vim/vim73/indent has addressed the problem, yet the problem still remains. Here's the code in java.vim:

  " If the previous line starts with '@', we should have the same indent as
  " the previous one
  if getline(lnum) =~ '^\s*@\S\+\s*$'
     return indent(lnum)
  endif

推荐答案

我刚刚在7.3.918版本上用两个代码片段测试了这两个代码片段,并且它们都可以正常工作.这意味着您的vim设置有问题.看一下您的vimrc,看看是否有任何问题.

I just tested this on version 7.3.918 with both snippets and they both work. This means that something is wrong with your vim setup. Take a look at your vimrc and see if anything is wrong.

还要查看:set ft返回的值,它应该返回filetype = java.如果没有发生这种情况,请确保您的vimrc中有filetype plugin indent on并检查是否可以解决您的问题.

Also look at the value returned by :set ft it should return filetype=java. If this does not happen make sure you have filetype plugin indent on in your vimrc and check to see if this fixes your problem.

也不要使用以下代码段,因为vim在更高版本中会自动执行此操作.

Also the below snippet does not to be used because vim does this by itself in later versions.


我在文件~/.vim/after/indent/java.vim中有此文件,该文件取自此处

I have this in the file ~/.vim/after/indent/java.vim which was taken from here

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

    return theIndent
endfunction
setlocal indentexpr=GetJavaIndent_improved()

这似乎可以正确缩进Java注释.

This seems to work for indenting java annotations properly.

这篇关于如何在Java中注释后删除缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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