Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a Java file in Vim like Eclipse

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;
    }
like image 992
Frank Cheng Avatar asked Nov 28 '11 03:11

Frank Cheng


1 Answers

Indentation in VIM (and in any other editor or IDE) is carried on by indentation rules coded by someone. There is no guarantee that any two system will follow same indentation practice, since there are different indentation practices around.

I also use VIM for minor editing Java files, and I'm not aware of any common alternative indentation script for Java, other than the one included in the official distribution. I you're familiar with VIM scripting you can try to edit the indentation script to your needs. It resides at $VIMRUNTIME/indent/java.vim.

By the way your example is a little uncommon. Using curly braces for cases of a switch statement is unnecessary. I guess VIM indentation scripts indent blocks by considering the block type and gets confused with this kind of uncommon blocks. Netbeans also get a little confused with this example, it aligns the case blocks in a reasonable way, but the closing curly brace of switch statement is completely out of alignment. That kind of odd behavior will not be so common with default VIM indentation. Actually if you remove the curly braces of the case statements VIM indentation aligns the statements quite well.

like image 52
infiniteRefactor Avatar answered Oct 23 '22 08:10

infiniteRefactor