Search in sources :

Example 16 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair 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

IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)16 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)10 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)9 HashSet (java.util.HashSet)4 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)4 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)3 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)3 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)2 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1