Search in sources :

Example 6 with IJSONModel

use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.

the class TestUtil method loadModel.

public static IJSONModel loadModel(String json) {
    IJSONModel model = (IJSONModel) TestUtil.createModel();
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    // Load JSON Object
    structuredDocument.set(json);
    return model;
}
Also used : IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 7 with IJSONModel

use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.

the class JSONFormatUtil method collectJSONNodes.

public List collectJSONNodes(IStructuredModel model, int start, int length) {
    List nodes = new ArrayList();
    IndexedRegion startNode = model.getIndexedRegion(start);
    IndexedRegion endNode = model.getIndexedRegion(start + length - 1);
    if (startNode == null || endNode == null) {
        return nodes;
    }
    if (model instanceof IJSONModel && startNode instanceof IJSONNode && endNode instanceof IJSONNode) {
        // JSON model
        IJSONNode ca = getCommonAncestor((IJSONNode) startNode, (IJSONNode) endNode);
        if (ca != null) {
            for (IJSONNode node = ca.getFirstChild(); node != null && start + length < ((IndexedRegion) node).getStartOffset(); node = node.getNextSibling()) {
                if (start < ((IndexedRegion) node).getEndOffset()) {
                    nodes.add(node);
                }
            }
        }
    }
    return nodes;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 8 with IJSONModel

use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.

the class JFaceNodeContentProvider method getElements.

public Object[] getElements(Object object) {
    // The root is usually an instance of an JSONStructuredModel in
    // which case we want to extract the document.
    Object topNode = object;
    if (object instanceof IJSONModel) {
        topNode = ((IJSONModel) object).getDocument();
    }
    IJFaceNodeAdapter adapter = getAdapter(topNode);
    if (adapter != null) {
        return adapter.getElements(topNode);
    }
    return new Object[0];
}
Also used : IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IJFaceNodeAdapter(org.eclipse.wst.sse.ui.internal.contentoutline.IJFaceNodeAdapter)

Example 9 with IJSONModel

use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.

the class AbstractJSONSourceFormatter method getLineDelimiter.

String getLineDelimiter(IJSONNode node) {
    IJSONModel model = node != null ? node.getOwnerDocument().getModel() : null;
    IStructuredDocument structuredDocument = model != null ? model.getStructuredDocument() : null;
    return structuredDocument != null ? structuredDocument.getLineDelimiter() : // $NON-NLS-1$
    "\n";
}
Also used : IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 10 with IJSONModel

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

IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)10 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IJSONSourceFormatter (org.eclipse.wst.json.core.internal.format.IJSONSourceFormatter)2 JSONFormatUtil (org.eclipse.wst.json.core.internal.format.JSONFormatUtil)2 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 IFile (org.eclipse.core.resources.IFile)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 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 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1