Search in sources :

Example 6 with IJSONCleanupStrategy

use of org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy in project webtools.sourceediting by eclipse.

the class JSONArrayFormatter method formatValue.

@Override
protected void formatValue(IJSONNode node, StringBuilder source, IJSONNode value) {
    IJSONCleanupStrategy stgy = getCleanupStrategy(node);
    IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
    int start = value.getStartOffset();
    int end = value.getEndOffset();
    if (value.getNextSibling() != null) {
        int s = value.getEndOffset();
        int e = value.getNextSibling().getStartOffset();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(s, e - s), stgy);
        if (regions.length > 0) {
            end++;
        }
    }
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
    for (int i = 0; i < regions.length; i++) {
        source.append(decoratedRegion(regions[i], 0, stgy));
    }
    source.append(getLineDelimiter(node));
    source.append(getIndent(node));
}
Also used : IJSONCleanupStrategy(org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 7 with IJSONCleanupStrategy

use of org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy in project webtools.sourceediting by eclipse.

the class JSONPairFormatter method formatPre.

@Override
protected void formatPre(IJSONNode node, IRegion region, StringBuilder source) {
    IJSONCleanupStrategy stgy = getCleanupStrategy(node);
    IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy);
    int length = (regions.length >= 2) ? 2 : regions.length;
    for (int i = 0; i < length; i++) {
        source.append(decoratedRegion(regions[i], 0, stgy));
        if (regions[i].getType() == JSONRegionContexts.JSON_COLON) {
            source.append(' ');
        }
    }
}
Also used : IJSONCleanupStrategy(org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 8 with IJSONCleanupStrategy

use of org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy in project webtools.sourceediting by eclipse.

the class JSONArrayFormatter method formatPost.

@Override
protected void formatPost(IJSONNode node, IRegion region, StringBuilder source) {
    IJSONCleanupStrategy stgy = getCleanupStrategy(node);
    if (region.getOffset() >= 0 && region.getLength() >= 0) {
        IStructuredDocument document = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(document, region, stgy);
        if (regions.length > 0 && regions[regions.length - 1] != null) {
            CompoundRegion r = regions[regions.length - 1];
            if (r != null && r.getType() == JSONRegionContexts.JSON_ARRAY_CLOSE) {
                source.append(decoratedRegion(r, 0, stgy));
            }
        }
    }
}
Also used : IJSONCleanupStrategy(org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 9 with IJSONCleanupStrategy

use of org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy in project webtools.sourceediting by eclipse.

the class JSONArrayFormatter method formatObject.

@Override
protected void formatObject(IJSONNode node, StringBuilder source, IJSONNode jsonObject) {
    IJSONCleanupStrategy stgy = getCleanupStrategy(node);
    IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
    IStructuredDocumentRegion[] structuredRegions = structuredDocument.getStructuredDocumentRegions(node.getStartOffset(), node.getEndOffset());
    if (structuredRegions.length >= 2) {
        int start = structuredRegions[1].getStartOffset();
        int end = node.getEndOffset();
        IJSONSourceFormatter formatter = (IJSONSourceFormatter) ((INodeNotifier) jsonObject).getAdapterFor(IJSONSourceFormatter.class);
        if (formatter == null) {
            formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(jsonObject);
        }
        StringBuilder objectSource = formatter.format(jsonObject, new FormatRegion(start, end - start));
        if (objectSource != null) {
            source.append(objectSource);
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONCleanupStrategy(org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 10 with IJSONCleanupStrategy

use of org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy in project webtools.sourceediting by eclipse.

the class JSONArrayFormatter method formatPre.

@Override
protected void formatPre(IJSONNode node, IRegion region, StringBuilder source) {
    IJSONCleanupStrategy stgy = getCleanupStrategy(node);
    if (region.getOffset() >= 0 && region.getLength() >= 0) {
        IStructuredDocument document = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(document, region, stgy);
        if (regions.length > 0 && regions[0] != null && regions[0].getType() == JSONRegionContexts.JSON_ARRAY_OPEN) {
            source.append(decoratedRegion(regions[0], 0, stgy));
        }
    }
    if (node instanceof IJSONArray && node.hasChildNodes()) {
        source.append(getLineDelimiter(node));
        source.append(getIndent(node));
        source.append(getIndentString());
    }
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) IJSONCleanupStrategy(org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Aggregations

IJSONCleanupStrategy (org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy)14 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)13 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)2 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)2 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)1 IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)1 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)1 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)1 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)1