Search in sources :

Example 6 with DocumentRewriteSessionType

use of org.eclipse.jface.text.DocumentRewriteSessionType in project webtools.sourceediting by eclipse.

the class FormatProcessorJSON method formatModel.

@Override
public void formatModel(IStructuredModel structuredModel, int start, int length) {
    JSONFormatUtil formatUtil = JSONFormatUtil.getInstance();
    if (structuredModel instanceof IJSONModel) {
        // BUG102822 take advantage of IDocumentExtension4
        IDocumentExtension4 docExt4 = null;
        if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
            docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
        }
        DocumentRewriteSession rewriteSession = null;
        try {
            DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
            rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
            IJSONDocument doc = ((IJSONModel) structuredModel).getDocument();
            IndexedRegion startRegion = ((IJSONModel) structuredModel).getIndexedRegion(start);
            IndexedRegion endRegion = ((IJSONModel) structuredModel).getIndexedRegion(start + length);
            if (startRegion != null && endRegion != null) {
                start = startRegion.getStartOffset();
                int offset;
                if (endRegion instanceof IJSONPair) {
                    offset = endRegion.getEndOffset();
                    IStructuredDocumentRegion nextRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(offset + 1);
                    if (nextRegion.getType() == JSONRegionContexts.JSON_COMMA) {
                        offset = nextRegion.getEndOffset();
                    }
                } else {
                    offset = endRegion.getEndOffset();
                }
                int end = offset - start;
                IJSONSourceFormatter formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(doc);
                StringBuilder buf = formatter.format(doc, new Region(start, end));
                if (buf != null) {
                    formatUtil.replaceSource(doc.getModel(), start, end, buf.toString());
                }
            }
        } finally {
            // BUG102822 take advantage of IDocumentExtension4
            if (docExt4 != null && rewriteSession != null)
                docExt4.stopRewriteSession(rewriteSession);
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) DocumentRewriteSessionType(org.eclipse.jface.text.DocumentRewriteSessionType) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IJSONSourceFormatter(org.eclipse.wst.json.core.internal.format.IJSONSourceFormatter) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JSONFormatUtil(org.eclipse.wst.json.core.internal.format.JSONFormatUtil) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument)

Aggregations

DocumentRewriteSessionType (org.eclipse.jface.text.DocumentRewriteSessionType)6 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)5 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)4 Region (org.eclipse.jface.text.Region)3 List (java.util.List)2 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)2 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)2 ArrayList (java.util.ArrayList)1 DocumentRewriteSessionEvent (org.eclipse.jface.text.DocumentRewriteSessionEvent)1 IDocument (org.eclipse.jface.text.IDocument)1 ILineTracker (org.eclipse.jface.text.ILineTracker)1 ILineTrackerExtension (org.eclipse.jface.text.ILineTrackerExtension)1 IRegion (org.eclipse.jface.text.IRegion)1 FormattingContext (org.eclipse.jface.text.formatter.FormattingContext)1 IContentFormatterExtension (org.eclipse.jface.text.formatter.IContentFormatterExtension)1 IFormattingContext (org.eclipse.jface.text.formatter.IFormattingContext)1 Point (org.eclipse.swt.graphics.Point)1 CSSFormatUtil (org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil)1 CSSSourceFormatter (org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter)1 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)1