title: Formatting Source Code programmatically with JDT
body: <p>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 without a selection.</p>
  
  <p>Any pointers for the API in JDT to format the source code programmatically is highly appreciated. </p>
  
  <p>Addition: I tried it like this, but the code isn't changed. What am I mssing?</p>
  
  <pre><code>private void formatUnitSourceCode(ICompilationUnit targetUnit, IProgressMonitor monitor) throws JavaModelException {
      CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
      TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, targetUnit.getSource(), 0, targetUnit.getSource().length(), 0, null);
      targetUnit.applyTextEdit(formatEdit, monitor);
  }
  </code></pre>