Search in sources :

Example 16 with ICSSStyleSheet

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

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

the class CSSStyleSheetImpl method getRules.

private void getRules(CSSRuleListImpl list, ICSSStyleSheet sheet, Stack refs) {
    String href = sheet.getHref();
    if (href != null) {
        // Avoid circular @imports
        if (refs.contains(href))
            return;
        refs.push(href);
    }
    boolean acceptImports = true;
    for (ICSSNode node = sheet.getFirstChild(); node != null; node = node.getNextSibling()) {
        // @import rules must precede all other rules, according to the spec
        if (node.getNodeType() == ICSSNode.IMPORTRULE_NODE && acceptImports) {
            CSSStyleSheet importSheet = ((ICSSImportRule) node).getStyleSheet();
            if (importSheet instanceof ICSSStyleSheet)
                getRules(list, (ICSSStyleSheet) importSheet, refs);
            else
                list.appendNode(node);
        } else // Add the rule to the list
        if (node instanceof CSSRule) {
            list.appendNode(node);
            if (node.getNodeType() != ICSSNode.CHARSETRULE_NODE)
                acceptImports = false;
        }
    }
    if (href != null)
        refs.pop();
}
Also used : CSSRule(org.w3c.dom.css.CSSRule) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) CSSStyleSheet(org.w3c.dom.css.CSSStyleSheet) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)

Example 18 with ICSSStyleSheet

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

the class CSSImportRuleImpl method releaseRule.

/**
 */
void releaseRule() {
    if (fStyleSheet != null) {
        ICSSStyleSheet sheet = fStyleSheet;
        // to prevent loop, we must reset fStyleSheet
        fStyleSheet = null;
        // before all closing action
        sheet.getModel().removeStyleListener(getOwnerDocument().getModel());
        // get ModelProvideAdapter
        IModelProvideAdapter adapter = (IModelProvideAdapter) getAdapterFor(IModelProvideAdapter.class);
        // }
        if (adapter != null)
            adapter.modelReleased(sheet.getModel());
        sheet.getModel().releaseFromRead();
    }
}
Also used : IModelProvideAdapter(org.eclipse.wst.css.core.internal.provisional.adapters.IModelProvideAdapter) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)

Example 19 with ICSSStyleSheet

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

the class CSSImportRuleImpl method closeStyleSheet.

/**
 */
void closeStyleSheet() {
    if (fStyleSheet != null) {
        ICSSStyleSheet sheet = fStyleSheet;
        // to prevent loop, we must reset fStyleSheet
        fStyleSheet = null;
        // before all closing action
        sheet.getModel().removeStyleListener(getOwnerDocument().getModel());
        // get ModelProvideAdapter
        IModelProvideAdapter adapter = (IModelProvideAdapter) getAdapterFor(IModelProvideAdapter.class);
        // }
        if (adapter != null)
            adapter.modelRemoved(sheet.getModel());
        sheet.getModel().releaseFromRead();
    }
}
Also used : IModelProvideAdapter(org.eclipse.wst.css.core.internal.provisional.adapters.IModelProvideAdapter) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)

Aggregations

ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)19 CSSRule (org.w3c.dom.css.CSSRule)9 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)8 CSSRuleList (org.w3c.dom.css.CSSRuleList)8 IFile (org.eclipse.core.resources.IFile)5 Path (org.eclipse.core.runtime.Path)5 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)5 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)5 Iterator (java.util.Iterator)3 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)3 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)3 MediaList (org.w3c.dom.stylesheets.MediaList)3 ArrayList (java.util.ArrayList)2 Preferences (org.eclipse.core.runtime.Preferences)2 IModelProvideAdapter (org.eclipse.wst.css.core.internal.provisional.adapters.IModelProvideAdapter)2 ICSSMediaRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule)2 ICSSSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector)2 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)2 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)2 ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)2