Tips for coding in Java with Textmate
Friday, February 3, 2012 at 7:50PM
Tyler Coville in java, programming, programming, textmate
Recently I switched to a mac and with it started using textmate for most of my coding. Recently I have also started working on more java projects in it. I prefer the simplicity of it to eclipse but there was one thing which I really missed from eclipse which was the formatter bound to ⇧⌘F to quickly format the document. I looked around to see if there was any method in textmate to do the same thing and found you could call Indent Line which would format the document but it still doesn't format the imports or align variables like eclipse. There are command line tools which are able to do a similar thing such as Jalopy or Jindent but one hasn't been updated in awhile and the other is commercial. I found a post about using the eclipse code formatter from the command line and from there it was easy enough to bind a command in textmate to call it on the current file. [1]
/Applications/eclipse/ -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -verbose -config ~/.settings/org.eclipse.jdt.core.prefs "$TM_FILEPATH"
This calls the eclipse class on the current file then reloads the project. You need to reload the project because this directly manipulates the file and doesn't return the formatted document through stdout like tidy. There may be a way to optimize call if someone with more experience with textmate commands wants to chime in.

Also notice that it uses a config file at ~/.settings/org.eclipse.jdt.core.prefs you can generate this file by using the generate project specific format options from eclipse and saving it in your home profile or modifying it and having a project specific one. My other fix I found was in the syntax highlighting of annotations within method parameters. As you can see from this sample code from the play tutorial it breaks when dealing with annotations within method parameters.

The fault here seems to be that it doesn't take into account that annotations can be inside parameters this can be quickly remedied by including the annotations regex into the parameters section of the language definition.
parameters = {
    patterns = (
        { name = '';
          match = 'final';
        { include = '#annotations'; },
        { include = '#primitive-arrays'; },
        { include = '#primitive-types'; },
        { include = '#object-types'; },
        { name = '';
          match = '\w+';

Once the change has been made the annotation is fixed.
Article originally appeared on Tyler Coville (
See website for complete article licensing information.