Search in sources :

Example 11 with ICSSNode

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode 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 12 with ICSSNode

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.

the class CSSModelUpdater method valueChanged.

/**
 */
void valueChanged(CSSNodeImpl node, String oldValue) {
    if (!(node instanceof CSSRegionContainer)) {
        CSSUtil.debugOut(// $NON-NLS-1$
        "Too Bad.." + // $NON-NLS-1$
        ((node == null) ? "null" : node.getClass().toString()));
        return;
    }
    int start = node.getStartOffset();
    if (node.getNodeType() == ICSSNode.ATTR_NODE) {
        ICSSAttr attr = (ICSSAttr) node;
        CSSSourceGenerator formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) attr.getOwnerCSSNode());
        if (formatter != null)
            start = formatter.getAttrInsertPos(attr.getOwnerCSSNode(), attr.getName());
    }
    int oldLength = (oldValue == null) ? 0 : oldValue.length();
    // flash old IStructuredDocumentRegion/ITextRegion
    if (node instanceof CSSStructuredDocumentRegionContainer) {
        ((CSSStructuredDocumentRegionContainer) node).setFirstStructuredDocumentRegion(null);
        ((CSSStructuredDocumentRegionContainer) node).setLastStructuredDocumentRegion(null);
    } else if (node instanceof CSSRegionContainer) {
        ((CSSRegionContainer) node).setRangeRegion(null, null, null);
    }
    // generate new source
    String newValue = node.generateSource();
    ICSSNode parent;
    if (node.getNodeType() == ICSSNode.ATTR_NODE) {
        parent = ((ICSSAttr) node).getOwnerCSSNode();
    } else {
        parent = node.getParentNode();
    }
    fParser.setupUpdateContext(CSSModelUpdateContext.UPDATE_CHANGE_RCONTAINER, parent, node);
    insertText(start, oldLength, newValue);
    fParser.cleanupUpdateContext();
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) CSSSourceGenerator(org.eclipse.wst.css.core.internal.formatter.CSSSourceGenerator) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

Example 13 with ICSSNode

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.

the class ImportRuleFormatter method formatBefore.

/**
 */
protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) {
    // for media-type
    ICSSNode prev = (child != null) ? child.getPreviousSibling() : node.getLastChild();
    int start = (prev != null) ? ((IndexedRegion) prev).getEndOffset() : 0;
    int end = (child != null) ? ((IndexedRegion) child).getStartOffset() : 0;
    if (start > 0 && start < end) {
        // format source
        CSSCleanupStrategy stgy = getCleanupStrategy(node);
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        // get meaning regions
        CompoundRegion[] regions = null;
        if (exceptFor == null)
            regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        else {
            String pickupType = CSSRegionContexts.CSS_MEDIA_SEPARATOR;
            if (prev == null || child == null)
                pickupType = null;
            regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType);
        }
        // extract source
        for (int i = 0; i < regions.length; i++) {
            appendSpaceBefore(node, regions[i], source);
            // must
            source.append(decoratedRegion(regions[i], 0, stgy));
        // be
        // comments
        }
        appendSpaceBefore(node, toAppend, source);
    } else if (prev != null && child != null) {
        // generate source : ????
        // $NON-NLS-1$
        source.append(",");
        appendSpaceBefore(node, toAppend, source);
    } else if (child != null) {
        // generate source : between 'url()' and
        // media types
        appendSpaceBefore(node, toAppend, source);
    }
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 14 with ICSSNode

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode 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)

Example 15 with ICSSNode

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.

the class ImportRuleFormatter method getAttrInsertPos.

/**
 */
public int getAttrInsertPos(ICSSNode node, String attrName) {
    if (node == null || attrName == null || attrName.length() == 0)
        return -1;
    if (!ICSSImportRule.HREF.equalsIgnoreCase(attrName))
        return -1;
    ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICSSImportRule.HREF);
    if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
        return ((IndexedRegion) attr).getStartOffset();
    IndexedRegion iNode = (IndexedRegion) node;
    if (iNode.getEndOffset() <= 0)
        return -1;
    FormatRegion formatRegion = null;
    ICSSNode child = node.getFirstChild();
    if (child != null && ((IndexedRegion) child).getEndOffset() > 0)
        formatRegion = new FormatRegion(iNode.getStartOffset(), ((IndexedRegion) child).getStartOffset() - iNode.getStartOffset());
    else
        formatRegion = new FormatRegion(iNode.getStartOffset(), iNode.getEndOffset() - iNode.getStartOffset());
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(node.getOwnerDocument().getModel().getStructuredDocument(), formatRegion, getCleanupStrategy(node));
    boolean atrule = false;
    for (int i = 0; i < regions.length; i++) {
        if (regions[i].getType() == CSSRegionContexts.CSS_IMPORT) {
            atrule = true;
            continue;
        } else if (!atrule)
            continue;
        if (regions[i].getType() != CSSRegionContexts.CSS_COMMENT)
            return regions[i].getStartOffset();
    }
    return (child != null && ((IndexedRegion) child).getEndOffset() > 0) ? ((IndexedRegion) child).getStartOffset() : iNode.getEndOffset();
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)95 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)21 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)13 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)12 DOMException (org.w3c.dom.DOMException)12 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)11 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)11 Iterator (java.util.Iterator)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)9 Region (org.eclipse.jface.text.Region)8 CSSSourceFormatter (org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter)6 ICSSPrimitiveValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue)6 ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)6 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)6 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)5 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)5 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)5 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)5