Search in sources :

Example 1 with JSONFormatUtil

use of org.eclipse.wst.json.core.internal.format.JSONFormatUtil in project webtools.sourceediting by eclipse.

the class CleanupProcessorJSON method cleanupModel.

public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
    JSONFormatUtil formatUtil = JSONFormatUtil.getInstance();
    if (structuredModel instanceof IJSONModel) {
        IJSONDocument doc = ((IJSONModel) structuredModel).getDocument();
        IJSONSourceFormatter formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
        StringBuilder buf = formatter.cleanup(doc);
        if (buf != null) {
            int startOffset = ((IndexedRegion) doc).getStartOffset();
            int endOffset = ((IndexedRegion) doc).getEndOffset();
            formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
        }
    }
}
Also used : JSONFormatUtil(org.eclipse.wst.json.core.internal.format.JSONFormatUtil) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IJSONSourceFormatter(org.eclipse.wst.json.core.internal.format.IJSONSourceFormatter) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 2 with JSONFormatUtil

use of org.eclipse.wst.json.core.internal.format.JSONFormatUtil 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

IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)2 IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)2 IJSONSourceFormatter (org.eclipse.wst.json.core.internal.format.IJSONSourceFormatter)2 JSONFormatUtil (org.eclipse.wst.json.core.internal.format.JSONFormatUtil)2 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)2 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1 DocumentRewriteSessionType (org.eclipse.jface.text.DocumentRewriteSessionType)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 Region (org.eclipse.jface.text.Region)1 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1