// EObject context = getContext(state.getContents().get(0));
IDomNode root = getSerializer().serializeToDom(state.getContents().get(0), false);
org.eclipse.xtext.util.ReplaceRegion r = getFormatter().format(
root, new TextRegion(region.getOffset(), region.getLength()), //
getFormattingContextFactory().create(state, FormattingOption.Format), errors);
ReplaceRegion replaceRegion = new ReplaceRegion(r.getOffset(), r.getLength(), r.getText());
try {
if(replaceRegion != null) {
String current = null;
try {
current = doc.get(replaceRegion.getOffset(), replaceRegion.getLength());
}
catch(BadLocationException e) {
// ignore, current is null
}
// Optimize - if replacement is equal to current
if(current == null || !current.equals(replaceRegion.getText()))
doc.replace(replaceRegion.getOffset(), replaceRegion.getLength(), r.getText());
}
}
catch(BadLocationException e) {
throw new RuntimeException(e);
}