Search in sources :

Example 21 with RegionIterator

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

the class AbstractCSSSourceFormatter method getLengthToReformatBefore.

/**
 * @return int
 * @param node
 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
 * @param insertPos
 *            int
 */
public int getLengthToReformatBefore(ICSSNode node, int insertPos) {
    if (node == null)
        return 0;
    IndexedRegion nnode = (IndexedRegion) node;
    if (insertPos <= 0 || !nnode.contains(insertPos - 1))
        return 0;
    IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(insertPos - 1);
    if (flatNode == null)
        return 0;
    ITextRegion region = flatNode.getRegionAtCharacterOffset(insertPos - 1);
    if (region == null)
        return 0;
    RegionIterator it = new RegionIterator(flatNode, region);
    boolean found = false;
    while (it.hasPrev()) {
        region = it.prev();
        // CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
        if (region.getType() != CSSRegionContexts.CSS_S) {
            found = true;
            break;
        }
    }
    int pos = insertPos - (found ? it.getStructuredDocumentRegion().getTextEndOffset(region) : it.getStructuredDocumentRegion().getStartOffset(region));
    // }
    return (pos >= 0) ? pos : 0;
}
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.css.core.internal.util.RegionIterator) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 22 with RegionIterator

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

the class AbstractCSSSourceFormatter method appendDelimBefore.

/**
 */
protected void appendDelimBefore(ICSSNode node, CompoundRegion toAppend, StringBuffer 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 ICSSStyleSheet);
    if (toAppend == null) {
        source.append(delim);
        source.append(getIndent(node));
        if (needIndent)
            source.append(getIndentString());
    } else {
        String type = toAppend.getType();
        if (type == CSSRegionContexts.CSS_COMMENT) {
            RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
            it.prev();
            ITextRegion prev = it.prev();
            int[] result = null;
            if (prev == null || (prev.getType() == CSSRegionContexts.CSS_S && (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() == CSSRegionContexts.CSS_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 == CSSRegionContexts.CSS_DELIMITER || type == CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
            RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
            it.prev();
            ITextRegion prev = it.prev();
            Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
            if (prev.getType() == CSSRegionContexts.CSS_S && 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(CSSCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != ICSSNode.STYLEDECLARATION_NODE)) {
                int length = getLastLineLength(node, source);
                int append = 1;
                if (length + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) {
                    source.append(getLineDelimiter(node));
                    source.append(getIndent(node));
                    if (needIndent)
                        source.append(getIndentString());
                }
            }
        } else if (type == CSSRegionContexts.CSS_RBRACE || type == CSSRegionContexts.CSS_LBRACE) {
            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) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) Preferences(org.eclipse.core.runtime.Preferences)

Example 23 with RegionIterator

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

the class AbstractCSSSourceFormatter method getLengthToReformatAfter.

/**
 * @return int
 * @param node
 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
 * @param insertPos
 *            int
 */
public int getLengthToReformatAfter(ICSSNode node, int insertPos) {
    if (node == null)
        return 0;
    IndexedRegion nnode = (IndexedRegion) node;
    if (insertPos < 0 || !nnode.contains(insertPos))
        return 0;
    IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(insertPos);
    if (flatNode == null)
        return 0;
    ITextRegion region = flatNode.getRegionAtCharacterOffset(insertPos);
    if (region == null)
        return 0;
    RegionIterator it = new RegionIterator(flatNode, region);
    boolean found = false;
    while (it.hasNext()) {
        region = it.next();
        // CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
        if (region.getType() != CSSRegionContexts.CSS_S) {
            found = true;
            break;
        }
    }
    int pos = (found ? it.getStructuredDocumentRegion().getStartOffset(region) : it.getStructuredDocumentRegion().getTextEndOffset(region)) - insertPos;
    return (pos >= 0) ? pos : 0;
}
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.css.core.internal.util.RegionIterator) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 24 with RegionIterator

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

the class CharsetRuleFormatter method formatAttrChanged.

/**
 */
public StringBuffer formatAttrChanged(ICSSNode node, ICSSAttr attr, boolean insert, AttrChangeContext context) {
    StringBuffer buf = new StringBuffer();
    if (node == null || attr == null)
        return buf;
    if (!ICSSCharsetRule.ENCODING.equalsIgnoreCase(attr.getName()))
        return buf;
    // get region to replace
    if (context != null && ((IndexedRegion) node).getEndOffset() > 0) {
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0) {
            IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) attr).getStartOffset());
            ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) attr).getStartOffset());
            RegionIterator it = new RegionIterator(flatNode, region);
            it.prev();
            if (it.hasPrev()) {
                ITextRegion prev = it.prev();
                if (prev.getType() == CSSRegionContexts.CSS_S)
                    context.start = it.getStructuredDocumentRegion().getStartOffset(prev);
                else
                    context.start = it.getStructuredDocumentRegion().getStartOffset(region);
            } else
                context.start = it.getStructuredDocumentRegion().getStartOffset(region);
            context.end = it.getStructuredDocumentRegion().getEndOffset(region);
        } else {
            IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
            ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
            if (region.getType() == CSSRegionContexts.CSS_S) {
                context.start = flatNode.getStartOffset(region);
                context.end = flatNode.getStartOffset(region);
            } else {
                context.start = flatNode.getEndOffset() + 1;
                context.end = flatNode.getEndOffset();
            }
        }
    }
    // generate text
    if (insert) {
        appendSpaceBefore(node, attr.getValue(), buf);
        buf.append(attr.getValue());
    }
    return buf;
}
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.css.core.internal.util.RegionIterator) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 25 with RegionIterator

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

the class StyleDeclItemFormatter method getChildInsertPos.

/**
 */
public int getChildInsertPos(ICSSNode node) {
    int n = ((IndexedRegion) node).getEndOffset();
    if (n < 0) {
        return -1;
    }
    IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(n - 1);
    if (flatNode != null && flatNode.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
        n -= flatNode.getLength();
    }
    if (n > 0) {
        String important = ((ICSSStyleDeclItem) node).getPriority();
        if (important != null && important.length() > 0) {
            // find before "!important" position
            flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(n - 1);
            RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(n - 1));
            while (it.hasPrev()) {
                ITextRegion region = it.prev();
                if (it.getStructuredDocumentRegion() != flatNode)
                    break;
                if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_IMPORTANT)
                    return it.getStructuredDocumentRegion().getStartOffset(region);
            }
        }
        // skip last space
        flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(n - 1);
        ITextRegion region = flatNode.getRegionAtCharacterOffset(n - 1);
        if (region != null) {
            n -= region.getLength() - region.getTextLength();
        }
        return n;
    }
    return -1;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)28 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)28 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)16 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)16 BadLocationException (org.eclipse.jface.text.BadLocationException)3 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)3 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)3 Preferences (org.eclipse.core.runtime.Preferences)2 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)2 ICSSAttr (org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1