final IDocument document= getDocument();
IFormattingContext context= null;
DocumentRewriteSession rewriteSession= null;
if (document instanceof IDocumentExtension4) {
IDocumentExtension4 extension= (IDocumentExtension4) document;
DocumentRewriteSessionType type= (selection.y == 0 && document.getLength() > 1000) || selection.y > 1000
? DocumentRewriteSessionType.SEQUENTIAL
: DocumentRewriteSessionType.UNRESTRICTED_SMALL;
rewriteSession= extension.startRewriteSession(type);
} else {
setRedraw(false);
target.beginCompoundChange();
}
try {
final String rememberedContents= document.get();
try {
if (fContentFormatter instanceof IContentFormatterExtension) {
final IContentFormatterExtension extension= (IContentFormatterExtension) fContentFormatter;
context= createFormattingContext();
if (selection.y == 0) {
context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
} else {
context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
context.setProperty(FormattingContextProperties.CONTEXT_REGION, new Region(selection.x, selection.y));
}
extension.format(document, context);
} else {
IRegion r;
if (selection.y == 0) {
IRegion coverage= getModelCoverage();
r= coverage == null ? new Region(0, 0) : coverage;
} else {
r= new Region(selection.x, selection.y);
}
fContentFormatter.format(document, r);
}
updateSlaveDocuments(document);
} catch (RuntimeException x) {
// fire wall for https://bugs.eclipse.org/bugs/show_bug.cgi?id=47472
// if something went wrong we undo the changes we just did
// TODO to be removed after 3.0 M8
document.set(rememberedContents);
throw x;
}
} finally {
if (document instanceof IDocumentExtension4) {
IDocumentExtension4 extension= (IDocumentExtension4) document;
extension.stopRewriteSession(rewriteSession);
} else {
target.endCompoundChange();
setRedraw(true);
}