Search in sources :

Example 1 with RegionIterator

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

the class AbstractCSSSourceFormatter method appendSpaceBefore.

/**
 */
protected void appendSpaceBefore(ICSSNode node, CompoundRegion toAppend, StringBuffer source) {
    if (node == null || toAppend == null || source == null)
        return;
    if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
        // for not formatting case on cleanup action
        return;
    String type = toAppend.getType();
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    boolean needIndent = !(node instanceof ICSSStyleSheet);
    if (type == CSSRegionContexts.CSS_COMMENT) {
        // check whether previous region is 'S' and has CR-LF
        String delim = getLineDelimiter(node);
        RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
        it.prev();
        ITextRegion prev = it.prev();
        // bug390904
        if (prev.getType() == CSSRegionContexts.CSS_LBRACE && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getFullText(prev), 0)[0] > 0) {
            source.append(delim);
            source.append(getIndent(node));
            source.append(getIndentString());
        } else 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 {
            appendSpaceBefore(node, toAppend.getText(), source);
        }
    } else if (type == CSSRegionContexts.CSS_LBRACE && preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) {
        String delim = getLineDelimiter(node);
        source.append(delim);
        source.append(getIndent(node));
    // } else if (type == CSSRegionContexts.CSS_CURLY_BRACE_CLOSE) {
    // } else if (type == CSSRegionContexts.CSS_INCLUDES || type ==
    // CSSRegionContexts.CSS_DASHMATCH) {
    } else if (type == CSSRegionContexts.CSS_DECLARATION_SEPARATOR && node instanceof ICSSStyleDeclItem) {
        int n = preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
        // no delimiter case
        while (n-- > 0) // $NON-NLS-1$
        source.append(" ");
    } else if (type == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR || type == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
        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 (CSSRegionContexts.CSS_FOREIGN_ELEMENT == type || CSSRegionContexts.CSS_DECLARATION_DELIMITER == type) {
        return;
    } else
        appendSpaceBefore(node, toAppend.getText(), source);
}
Also used : 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) Preferences(org.eclipse.core.runtime.Preferences) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)

Example 2 with RegionIterator

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

the class CounterFormatter method getAttrInsertPos.

/**
 */
public int getAttrInsertPos(ICSSNode node, String attrName) {
    if (node == null || attrName == null || attrName.length() == 0)
        return -1;
    IndexedRegion iNode = (IndexedRegion) node;
    if (ICounter.IDENTIFIER.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.IDENTIFIER);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        if (iNode.getEndOffset() <= 0)
            return -1;
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else if (ICounter.LISTSTYLE.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.LISTSTYLE);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR)
                return it.getStructuredDocumentRegion().getEndOffset(region);
            else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else if (ICounter.SEPARATOR.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.SEPARATOR);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        boolean hasComma = false;
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR) {
                if (!hasComma)
                    hasComma = true;
                else
                    return it.getStructuredDocumentRegion().getEndOffset(region);
            } else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else
        return -1;
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) 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 3 with RegionIterator

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

the class StyleDeclItemFormatter method getFirstChildRegionLength.

/**
 */
private int getFirstChildRegionLength(ICSSNode node) {
    ICSSNode firstChild = node.getFirstChild();
    if (firstChild == null)
        return 1;
    int start = ((IndexedRegion) firstChild).getStartOffset();
    RegionIterator itr = new RegionIterator(node.getOwnerDocument().getModel().getStructuredDocument(), start);
    while (itr.hasNext()) {
        ITextRegion reg = itr.next();
        if (reg != null && reg.getType() != CSSRegionContexts.CSS_S)
            return reg.getTextLength();
    }
    return 1;
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 4 with RegionIterator

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

the class StyleDeclItemFormatter 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 (!ICSSStyleDeclItem.IMPORTANT.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.getEndOffset(region);
            } else {
                context.start = flatNode.getEndOffset();
                context.end = flatNode.getEndOffset();
            }
        }
    }
    // generate text
    if (insert && attr.getValue() != null && attr.getValue().length() > 0) {
        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 5 with RegionIterator

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

the class ImportRuleFormatter 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 (!ICSSImportRule.HREF.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);
                it.next();
            } else
                context.start = it.getStructuredDocumentRegion().getStartOffset(region);
            it.next();
            it.next();
            if (it.hasNext()) {
                ITextRegion next = it.next();
                if (next.getType() == CSSRegionContexts.CSS_S)
                    context.end = it.getStructuredDocumentRegion().getEndOffset(next);
                else
                    context.end = it.getStructuredDocumentRegion().getEndOffset(region);
            } else
                context.end = it.getStructuredDocumentRegion().getEndOffset(region);
        } else {
            ICSSNode child = node.getFirstChild();
            IStructuredDocumentRegion flatNode = null;
            ITextRegion region = null;
            if (child == null) {
                flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
                region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
            } else {
                flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) child).getStartOffset() - 1);
                region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) child).getStartOffset() - 1);
            }
            if (region.getType() == CSSRegionContexts.CSS_S) {
                context.start = flatNode.getStartOffset(region);
                context.end = flatNode.getEndOffset(region);
            } else {
                context.start = flatNode.getEndOffset();
                context.end = flatNode.getEndOffset();
            }
        }
    }
    // generate text
    if (insert && attr.getValue() != null && attr.getValue().length() > 0) {
        appendSpaceBefore(node, attr.getValue(), buf);
        buf.append(attr.getValue());
        // $NON-NLS-1$
        appendSpaceBefore(node, "", buf);
    }
    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) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) 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