Monday, June 4, 2012

Eclipse plugin that aligns Java assignments and declarations

**UPDATE**
I added a small new feature to the plugin - a command that will move the cursor to the opening brace of the scope that the cursor is currently in. Both round and curly braces are considered. The command is bound to Ctrl+5 by default.

 Introduction

Like most developers, I'm pretty particular about how I format my code. For example when I've got a few assignment statements together, I like to align the equals signs in a column. Eclipse does not do this for you inside methods. I also like to have my variable declarations to be columnized, Eclipse can do this, but not in the nicest way. I found myself spending a lot of time formatting code. So I created a plugin that will format my code in the way I like it and I decided to share this plugin.

The plugin is called OCDFormat, make of that what you will.... :)

Installation

  • Download the binary here, by right clickint / 'Save as' the OCDFormat_1.0.0.1.jar file
  • add it to your Eclipse plugins or dropins directory

Usage

Highlight a piece of code that you'd like to have formatted, and use either the keyboard shortcut (Ctrl+4) or the toolbar button ( ) to apply the formatting. The idea is that you'd highlight small coherent blocks of code to format - this is not a Ctrl+A / Format type of plugin.

Examples 

Here are some example of the difference in formatting :

OCDFormat


Eclipse



As you can see, Eclipse does not format declarations and assignment statements inside methods at all. At field level Eclipse will always align all your variable declarations and assignments together, even if you select just a few lines and tell Eclipse to format them, it will format them as if you'd selected all the statements, leaving the assignments hanging way out to the right, and not allowing separate formatting for the 'groups' of assignments. With OCDFormat, you can format smaller regions independently.

Conclusion

I would have liked to incorporate the plugin into the Eclipse formatter, but I don't have the time to figure out the integration.


2 comments:

  1. Is this got integrated to Standard Eclipse Formatter? Will it work for Eclispe Juno?

    ReplyDelete
  2. Unfortunately, I doesn't work with Indigo as well :(

    ReplyDelete