Search in sources :

Example 1 with ICSSStyleDeclItem

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

the class CSSModelParser method insertSemiColonForStyleDeclarationItem.

/**
 */
private CSSNodeImpl insertSemiColonForStyleDeclarationItem(IStructuredDocumentRegion region) {
    // only target/net node is changed. nothing to do.
    CSSNodeImpl targetNode = fCreationContext.getTargetNode();
    if (targetNode instanceof ICSSStyleDeclItem) {
        int offset = targetNode.getStartOffset();
        // widen document region range
        // ((CSSStyleDeclItemImpl)targetNode).setLastStructuredDocumentRegion(region);
        CSSModelUtil.expandStructuredDocumentRegionContainer((CSSStyleDeclItemImpl) targetNode, region);
        // psStructuredDocumentRegion indicates CSSStyleDeclItem
        ICSSNode parentNode = targetNode.getParentNode();
        fCreationContext.setTargetNode(parentNode);
        ICSSNode next = null;
        if (parentNode.hasChildNodes()) {
            for (ICSSNode child = targetNode.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child instanceof CSSStructuredDocumentRegionContainer && offset < ((CSSStructuredDocumentRegionContainer) child).getStartOffset()) {
                    next = child;
                    break;
                }
            }
        }
        fCreationContext.setNextNode(next);
        return targetNode;
    }
    return null;
}
Also used : ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

Example 2 with ICSSStyleDeclItem

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

the class CSSStyleDeclarationImpl method getDeclItemNode.

public ICSSStyleDeclItem getDeclItemNode(String propertyName) {
    ICSSNode node = getLastChild();
    propertyName = propertyName.trim();
    while (node != null) {
        if (node instanceof CSSStyleDeclItemImpl) {
            ICSSStyleDeclItem item = (ICSSStyleDeclItem) node;
            if (propertyName.compareToIgnoreCase(item.getPropertyName().trim()) == 0)
                return item;
        }
        node = node.getPreviousSibling();
    }
    return null;
}
Also used : ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

Example 3 with ICSSStyleDeclItem

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

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

the class StyleDeclItemFormatter method formatPre.

/**
 */
protected void formatPre(ICSSNode node, StringBuffer source) {
    int start = ((IndexedRegion) node).getStartOffset();
    int end = (node.getFirstChild() != null && ((IndexedRegion) node.getFirstChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getFirstChild()).getStartOffset() : getChildInsertPos(node);
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    if (end > 0) {
        // format source
        CSSCleanupStrategy stgy = getCleanupStrategy(node);
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        for (int i = 0; i < regions.length; i++) {
            if (i != 0)
                appendSpaceBefore(node, regions[i], source);
            source.append(decoratedPropNameRegion(regions[i], stgy));
        }
    } else {
        // generatoe source
        ICSSStyleDeclItem item = (ICSSStyleDeclItem) node;
        if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_NAME) == CSSCorePreferenceNames.UPPER)
            source.append(item.getPropertyName().toUpperCase());
        else
            source.append(item.getPropertyName());
        int k = preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
        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 + k + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) {
                source.append(getLineDelimiter(node));
                source.append(getIndent(node));
                source.append(getIndentString());
                // no space is necessary
                k = 0;
            }
        }
        // no delimiter case
        while (k-- > 0) // $NON-NLS-1$
        source.append(" ");
        // $NON-NLS-1$
        source.append(":");
    }
    if (node.getFirstChild() != null && (!isCleanup() || getCleanupStrategy(node).isFormatSource())) {
        appendAfterColonSpace(node, source);
    }
}
Also used : ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Preferences(org.eclipse.core.runtime.Preferences) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 5 with ICSSStyleDeclItem

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

the class CSSMetaModelUtil method getMetaModelNodeFor.

public CSSMMNode getMetaModelNodeFor(ICSSNode node) {
    if (node instanceof ICSSStyleDeclaration) {
        node = node.getParentNode();
    }
    if (node instanceof ICSSStyleDeclItem) {
        ICSSNode parent = node.getParentNode();
        if (parent != null) {
            parent = parent.getParentNode();
        }
        if (parent instanceof ICSSStyleRule) {
            return getProperty(((ICSSStyleDeclItem) node).getPropertyName());
        } else if (parent instanceof CSSFontFaceRule) {
            return getDescriptor(((ICSSStyleDeclItem) node).getPropertyName());
        }
    }
    if (node == null) {
        return null;
    }
    if (fTypeMap == null) {
        fTypeMap = new HashMap();
        fTypeMap.put(new Short(ICSSNode.STYLERULE_NODE), CSSMMNode.TYPE_STYLE_RULE);
        fTypeMap.put(new Short(ICSSNode.FONTFACERULE_NODE), CSSMMNode.TYPE_FONT_FACE_RULE);
        fTypeMap.put(new Short(ICSSNode.PAGERULE_NODE), CSSMMNode.TYPE_PAGE_RULE);
    }
    String nodeType = (String) fTypeMap.get(new Short(node.getNodeType()));
    if (nodeType == null) {
        return null;
    }
    Iterator iNodes = collectNodesByType(nodeType);
    if (iNodes.hasNext()) {
        CSSMMNode targetNode = (CSSMMNode) iNodes.next();
        if (!iNodes.hasNext()) {
            // it's only one
            return targetNode;
        }
    }
    return null;
}
Also used : CSSMMNode(org.eclipse.wst.css.core.internal.metamodel.CSSMMNode) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) HashMap(java.util.HashMap) Iterator(java.util.Iterator) CSSFontFaceRule(org.w3c.dom.css.CSSFontFaceRule) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Aggregations

ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)18 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)6 Preferences (org.eclipse.core.runtime.Preferences)4 Iterator (java.util.Iterator)3 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)3 CSSMMNode (org.eclipse.wst.css.core.internal.metamodel.CSSMMNode)3 ICSSPrimitiveValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue)3 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)3 ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)3 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)3 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)3 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)3 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CSSMetaModelUtil (org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil)2 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)2