I am generating some classes with JDT. Afterwards I would like to format the whole ICompilationUnit, just as if I pressed Ctrl+Shift+F (Source > Format) in an open Editor wi
When generating some classes by using JDT, you can put "\t"s in your source code. Or like what you did, using code formatter. I have tested the following code:
public static void main(String[] args)
{
String code = "public class TestFormatter{public static void main(String[] args){System.out.println(\"Hello World\");}}";
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null);
IDocument doc = new Document(code);
try {
textEdit.apply(doc);
System.out.println(doc.get());
} catch (MalformedTreeException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
The apply()
method does the trick here.