X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java index 80e6949..2dae127 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java @@ -12,13 +12,20 @@ package net.sourceforge.phpeclipse.phpeditor; -import org.eclipse.swt.widgets.Composite; - +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.IRewriteTarget; +import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.formatter.FormattingContextProperties; +import org.eclipse.jface.text.formatter.IContentFormatterExtension2; +import org.eclipse.jface.text.formatter.IFormattingContext; import org.eclipse.jface.text.information.IInformationPresenter; import org.eclipse.jface.text.source.IOverviewRuler; import org.eclipse.jface.text.source.IVerticalRuler; import org.eclipse.jface.text.source.SourceViewer; import org.eclipse.jface.text.source.SourceViewerConfiguration; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; //import net.sourceforge.phpdt.ui.text.JavaSourceViewerConfiguration; @@ -58,6 +65,60 @@ public class JavaSourceViewer extends SourceViewer { case OPEN_STRUCTURE: fStructurePresenter.showInformation(); return; + case FORMAT : + { + final Point selection= rememberSelection(); + + final IDocument document= getDocument(); + IRegion region= new Region(selection.x, selection.y); + if (region.getLength()==0) { + region = new Region(0, document.getLength()); + } + final IRewriteTarget target= getRewriteTarget(); + final IFormattingContext 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, region); + } + try { + setRedraw(false); + startSequentialRewriteMode(false); + target.beginCompoundChange(); + + final String rememberedContents= document.get(); + + try { + + if (fContentFormatter instanceof IContentFormatterExtension2) { + + final IContentFormatterExtension2 extension= (IContentFormatterExtension2)fContentFormatter; + extension.format(document, context); + + } else + fContentFormatter.format(document, region); + + } catch (RuntimeException x) { + // firewall 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 + document.set(rememberedContents); + throw x; + } + + } finally { + + target.endCompoundChange(); + stopSequentialRewriteMode(); + setRedraw(true); + + restoreSelection(); + context.dispose(); + } + return; + } } super.doOperation(operation);