How to format a Java file in Vim like Eclipse

I use Vim to edit a Java file, but I find that the Vim formats of Java file formats are very different from Eclipse.

If I select the following code and press = , Vim does not format the code as I would like. Can anybody 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; } 
+6
source share
1 answer

Indentation in VIM (and in any other editor or in the IDE) is followed by indentation rules encoded by someone. There is no guarantee that either of the two systems will follow the same indentation practice, since there are different indentation methods.

I also use VIM for minor editing of Java files, and I don’t know about the general alternative script indentation for Java other than that included in the official distribution. I am familiar with VIM scripts, you can try to edit the script indentation according to your needs. It is located in $VIMRUNTIME/indent/java.vim .

By the way, your example is a bit unusual. The use of curly brackets is not necessary for switch statements. I assume that the indentation for VIM indentation is indented by looking at the type of block and confused with such unusual blocks. Netbeans is also a bit confused with this example; it aligns case blocks in a reasonable way, but the closing curly brace of the switch statement is not completely consistent. Such strange behavior will not be so common with default VIM. In fact, if you remove the curly braces of the case statements, the VIM indent aligns the statements pretty well.

+4
source

Source: https://habr.com/ru/post/902513/


All Articles