Search in sources :

Example 1 with RegionIterator

use of org.eclipse.wst.json.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.

the class JSONStructuredDocumentReParser method getUpdateRangeForUnknownRegion.

private ReparseRange getUpdateRangeForUnknownRegion(int start, int end) {
    int newStart = start;
    RegionIterator iterator = new RegionIterator(fStructuredDocument, start - 1);
    if (iterator.hasPrev()) {
        // skip myself
        iterator.prev();
    }
    while (iterator.hasPrev()) {
        ITextRegion region = iterator.prev();
        if (region != null && region.getType() == JSONRegionContexts.JSON_UNKNOWN) {
            newStart = iterator.getStructuredDocumentRegion().getStartOffset(region);
        } else {
            break;
        }
    }
    if (start != newStart) {
        return new ReparseRange(newStart, end);
    }
    return null;
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.json.core.internal.util.RegionIterator)

Example 2 with RegionIterator

use of org.eclipse.wst.json.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.

the class AbstractJSONSourceFormatter method getOutsideRegions.

protected CompoundRegion[] getOutsideRegions(IStructuredDocument model, IRegion reg) {
    CompoundRegion[] ret = new CompoundRegion[2];
    RegionIterator it = new RegionIterator(model, reg.getOffset());
    it.prev();
    if (it.hasPrev()) {
        ITextRegion textRegion = it.prev();
        IStructuredDocumentRegion documentRegion = it.getStructuredDocumentRegion();
        ret[0] = new CompoundRegion(documentRegion, textRegion);
    } else {
        ret[0] = null;
    }
    it.reset(model, reg.getOffset() + reg.getLength());
    if (it.hasNext()) {
        ITextRegion textRegion = it.next();
        IStructuredDocumentRegion documentRegion = it.getStructuredDocumentRegion();
        ret[1] = new CompoundRegion(documentRegion, textRegion);
    } else {
        ret[1] = null;
    }
    return ret;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.json.core.internal.util.RegionIterator)

Example 3 with RegionIterator

use of org.eclipse.wst.json.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.

the class AbstractJSONSourceFormatter method appendDelimBefore.

protected void appendDelimBefore(IJSONNode node, CompoundRegion toAppend, StringBuilder source) {
    if (node == null || source == null)
        return;
    if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
        // for not formatting case on cleanup action
        return;
    String delim = getLineDelimiter(node);
    boolean needIndent = !(node instanceof IJSONDocument);
    if (toAppend == null) {
        source.append(delim);
        source.append(getIndent(node));
        if (needIndent)
            source.append(getIndentString());
    } else {
        String type = toAppend.getType();
        if (type == JSONRegionContexts.JSON_COMMENT) {
            RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
            it.prev();
            ITextRegion prev = it.prev();
            int[] result = null;
            if (prev == null || (prev.getType() == JSONRegionContexts.WHITE_SPACE && (result = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0))[0] >= 0)) {
                // Collapse to one empty line if there's more than one.
                if (result != null) {
                    int offset = result[0] + DefaultLineTracker.DELIMITERS[result[1]].length();
                    if (offset < it.getStructuredDocumentRegion().getText(prev).length()) {
                        if (TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), offset)[0] >= 0) {
                            source.append(delim);
                        }
                    }
                    source.append(delim);
                    source.append(getIndent(node));
                    if (needIndent)
                        source.append(getIndentString());
                }
            } else if (prev.getType() == JSONRegionContexts.JSON_COMMENT) {
                String fullText = toAppend.getDocumentRegion().getFullText(prev);
                String trimmedText = toAppend.getDocumentRegion().getText(prev);
                // $NON-NLS-1$
                String whiteSpaces = "";
                if (fullText != null && trimmedText != null)
                    whiteSpaces = fullText.substring(trimmedText.length());
                int[] delimiterFound = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, whiteSpaces, 0);
                if (delimiterFound[0] != -1) {
                    source.append(delim);
                } else {
                    appendSpaceBefore(node, toAppend.getText(), source);
                    /*
						 * If two comments can't be adjusted in one
						 * line(combined length exceeds line width), a tab is
						 * also appended along with next line delimiter , we
						 * need to remove that.
						 */
                    if (source.toString().endsWith(getIndentString())) {
                        source.delete((source.length() - getIndentString().length()), source.length());
                    }
                }
            } else {
                appendSpaceBefore(node, toAppend.getText(), source);
            }
        } else if (type == JSONRegionContexts.JSON_COMMA) {
            RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
            it.prev();
            ITextRegion prev = it.prev();
            Preferences preferences = JSONCorePlugin.getDefault().getPluginPreferences();
            if (prev.getType() == JSONRegionContexts.WHITE_SPACE && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
                source.append(delim);
                source.append(getIndent(node));
                if (needIndent)
                    source.append(getIndentString());
            } else if (preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(JSONCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != IJSONNode.PAIR_NODE)) {
                int length = getLastLineLength(node, source);
                int append = 1;
                if (length + append > preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH)) {
                    source.append(getLineDelimiter(node));
                    source.append(getIndent(node));
                    if (needIndent)
                        source.append(getIndentString());
                }
            }
        } else if (type == JSONRegionContexts.JSON_OBJECT_OPEN || type == JSONRegionContexts.JSON_OBJECT_CLOSE || type == JSONRegionContexts.JSON_ARRAY_OPEN || type == JSONRegionContexts.JSON_ARRAY_CLOSE) {
            source.append(delim);
            source.append(getIndent(node));
        } else {
            source.append(delim);
            source.append(getIndent(node));
            if (needIndent)
                source.append(getIndentString());
        }
    }
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) RegionIterator(org.eclipse.wst.json.core.internal.util.RegionIterator) Preferences(org.eclipse.core.runtime.Preferences)

Aggregations

RegionIterator (org.eclipse.wst.json.core.internal.util.RegionIterator)3 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)3 Preferences (org.eclipse.core.runtime.Preferences)1 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1