You can try running the formatter as a standalone application (also detailed here).
eclipse -vm <path to virtual machine> -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>
Try first to define formatting settings with eclipse IDE in order to achieve the right result, then export those settings, and use that configuration file in the eclipse.exe parameters.
Or see also "Generating a Config File for the Formatter Application"
eclipse [...] -config <myExportedSettings>
In a java program, you can try to directly format by:
- Creating an instance of
CodeFormatter
- Using the method void
format(aString)
on this instance to format aString. It will return the formatted string.
Thanks to Geo himself and his report in his blog entry, I now know you need to use DefaultCodeFormatter
String code = "public class geo{public static void main(String[] args){System.out.println(\"geo\");}}";
CodeFormatter cf = new DefaultCodeFormatter();
TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null);
IDocument dc = new Document(code);
try {
te.apply(dc);
System.out.println(dc.get());
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Again, full details in the blog entry. Thank you Geo for that feedback!