如何在Vim中像Eclipse一样格式化Java文件

7

I am using Vim to edit a Java file, but I find the way Vim formats Java files is very different from Eclipse.

If I select the following code and press =, Vim does not format the code the way I would like. Can anyone help me?

Before Format:

  case RINGTONE_PICKED: {
                            Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                            handleRingtonePicked(pickedUri);
                            break;
                        }
    case PHOTO_PICKED_WITH_DATA: {

        if (mPhotoEditorView != null) {
            final Bitmap photo = data.getParcelableExtra("data");
            mPhotoEditorView.setPhotoBitmap(photo);
        } else {
            // The contact that requested the photo is no longer present.
            // TODO: Show error message
        }

        break;
    }

After Format:

  case RINGTONE_PICKED: {
                            Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                            handleRingtonePicked(pickedUri);
                            break;
                        }
  case PHOTO_PICKED_WITH_DATA: {

                                   if (mPhotoEditorView != null) {
                                       final Bitmap photo = data.getParcelableExtra("data");
                                       mPhotoEditorView.setPhotoBitmap(photo);
                                   } else {
                                       // The contact that requested the photo is no longer present.
                                       // TODO: Show error message
                                   }

                                   break;
                               }

This is what I want:

    case RINGTONE_PICKED: {
        Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        handleRingtonePicked(pickedUri);
        break;
        }
    case PHOTO_PICKED_WITH_DATA: {

        if (mPhotoEditorView != null) {
            final Bitmap photo = data.getParcelableExtra("data");
            mPhotoEditorView.setPhotoBitmap(photo);
        } else {
            // The contact that requested the photo is no longer present.
            // TODO: Show error message
        }

        break;
    }


2
请确认您已设置:syntax on:set syntax?返回syntax=java,并且在您的$VIMRUNTIME/indent/目录中有一个java.vim文件?(Java缩进文件自2005年以来没有更新过,因此任何最近的版本都应该可以。) - Conspicuous Compiler
我尝试了,但不幸失败了。我认为可能有一个插件可以像Eclipse一样改变格式方式。 - Frank Cheng
请问您说失败时,能否澄清一下您的意思? - Conspicuous Compiler
格式样式仍然不像Eclipse。 - Frank Cheng
实际上,我只想在Vim中实现Eclipse中的Ctrl+Shift+F功能。 - Frank Cheng
显示剩余2条评论
1个回答

5

VIM(以及任何其他编辑器或IDE)中的缩进是由某个人编写的缩进规则来实现的。不能保证任何两个系统都遵循相同的缩进惯例,因为有不同的缩进惯例。

我也使用VIM轻微编辑Java文件,除了官方发行版中包含的缩进脚本之外,我不知道是否有常见的替代缩进脚本。如果您熟悉VIM脚本编写,可以尝试根据自己的需求编辑缩进脚本。它位于$VIMRUNTIME/indent/java.vim

顺便说一下,您的示例有点不常见。在switch语句的情况下使用花括号是不必要的。我想VIM缩进脚本通过考虑块类型来缩进块,并且对这种不常见的块感到困惑。Netbeans也会对这个示例感到有点困惑,它以合理的方式对齐case块,但是switch语句的右括号完全不对齐。这种奇怪的行为在默认的VIM缩进中不会那么常见。实际上,如果您删除case语句的花括号,VIM缩进将非常好地对齐语句。


谢谢。我也认为花括号引起了这个问题。在我将语法方法设置为syntax之后,vim的自动缩进总是让我满意。 - Frank Cheng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接