6

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;
    }
Community
  • 1
  • 1
Frank Cheng
  • 5,429
  • 8
  • 45
  • 71

1 Answers1

4

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.

infiniteRefactor
  • 1,511
  • 14
  • 21
  • thanks. i also think braces causes this problem. and after i set syntax method as syntax, the autoindention of vim always satisfys me. – Frank Cheng Jan 24 '12 at 02:27