Sublime Text – enabling Eclipse code formatting

My editor of choice for the longest time was emacs, but recently I’ve made the switch to Sublime Text. It’s such a snappy, beautiful, powerful editor. I’ll still use emacs (or vi even, yecch) over a text-only connection, but otherwise, Sublime Text is the bomb.

Sublime Text isn’t an IDE, however; it’s only an editor, and so is missing some bits and pieces that folks used to Eclipse or Netbeans like to have. It makes up for it by being extremely extensible and customizable, using only Python and JSON.

Sublime Text does perform code formatting for you, but most of the world has standardized on using Eclipse for such work. Such is the case for the Apache Accumulo project, which requires code to adhere to formatting rules saved off in an Eclipse-specific XML file. If you are working under Eclipse, this is just fine, and there is even an IntelliJ plugin; otherwise, well, you may have to bounce into Eclipse just to format code. Fortunately, there is an EclipseCodeFormatter plugin that can call out to Eclipse from Sublime Text to format for you.

The easiest way to install the plugin is to first grab Package Control, which is like yum or apt for Sublime Text. Then, use Package Control to install EclipseCodeFormatter.

You do need to manually configure the plugin, as described on its home page (linked above). In order to do it, you will have to up and install Eclipse. Once you do, you can immediately enter the path to the eclipse executable in the plugin configuration. On a Mac (what I use), the configuration file for the plugin is under ~/Library/Application Support/Sublime Text 2/Packages/EclipseCodeFormatter; the patch to Eclipse is something like /Applications/eclipse/eclipse (actually a symlink to inside the .app).

"eclipse_command": "/Applications/eclipse/eclipse",

To create the configuration file the plugin needs, start up Eclipse and create a new Java project. Maybe make a new Java file in there for kicks. Now, go under Preferences → Java Code Style. Under Formatter, follow the link “Configure Project Specific Settings” and enable specific settings for your project. Select the profile you want to use in Sublime Text. (You can also import an XML file if you have it, like Accumulo provides.)

Now do the same thing for Code Templates if you have specific settings for those too. Now, in your project workspace, under the .settings directory, there should be a file “org.eclipse.jdt.core.prefs” file chock full of formatting instructions. Copy that somewhere where you’d like it to live (I put it right with the plugin files). Now you can configure the plugin’s path to the config file to point to that file.

"config_file": "/Users/bhavanki/Library/Application Support/Sublime Text 2/Packages/EclipseJavaFormatter/org.eclipse.jdt.core.prefs",

You’re all set to use the plugin. You can use the Command Palette to invoke the command and format the current buffer. You can also add a keymap entry to turn on a shortcut for it. Under Mac OS X, I found that Control+Shift+F was available:

{ "keys": ["ctrl+shift+f"], "command": "eclipse_format_java" }
Advertisements

One thought on “Sublime Text – enabling Eclipse code formatting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s