Search in sources :

Example 56 with ICSSNode

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

the class CSSProposalArranger method buildProposals.

/**
 */
void buildProposals() {
    fProposals.clear();
    /*
		 * String text; ICompletionProposal item; text = "---- Test
		 * Information ----"; item = new CompletionProposal("",
		 * fContext.getReplaceBegin(), 0, 0, null, text, null, null);
		 * fProposals.add(item);
		 * 
		 * text = "Target: \"" + fContext.getRegionText() + "\"";
		 * 
		 * item = new CompletionProposal("", fContext.getReplaceBegin(), 0, 0,
		 * null, text, null, null); fProposals.add(item);
		 * 
		 * text = fContext.getTargetNode().getClass().toString(); int
		 * lastPeriodPos = text.lastIndexOf('.'); text = "Node: " +
		 * text.substring(lastPeriodPos + 1); item = new
		 * CompletionProposal("", fContext.getReplaceBegin(), 0, 0, null,
		 * text, null, null); fProposals.add(item);
		 */
    ICSSNode targetNode = fContext.getTargetNode();
    // int targetPos = fContext.getTargetPos();
    if (targetNode instanceof ICSSStyleSheet) {
        buildProposalsForAnyRule();
    } else if ((targetNode instanceof ICSSMediaRule && fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_LBRACE)) || (targetNode instanceof ICSSStyleRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForAnyRule();
    // buildProposalsForStyleRule();
    } else if ((targetNode instanceof ICSSPageRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForPageRulePseudoClass();
    } else if ((targetNode instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule || targetNode instanceof ICSSPageRule || targetNode instanceof ICSSStyleDeclaration) && (targetNode.getOwnerDocument() instanceof ICSSStyleDeclaration || fContext.targetFollows(CSSRegionContexts.CSS_DECLARATION_DELIMITER) || fContext.targetFollows(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForDeclarationName();
    } else if (targetNode instanceof ICSSStyleDeclItem) {
        if (fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_DECLARATION_SEPARATOR)) {
            buildProposalsForDeclarationValue();
        } else {
            buildProposalsForDeclarationName();
        }
    } else if (targetNode instanceof ICSSPrimitiveValue) {
        buildProposalsForDeclarationValue();
    }
/*
		 * else if (targetNode instanceof ICSSPrimitiveValue || ((targetNode
		 * instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule ||
		 * targetNode instanceof ICSSStyleDeclaration || targetNode instanceof
		 * ICSSStyleDeclItem) &&
		 * fContext.isTargetPosAfterOf(CSSRegionContexts.COLON))) {
		 * buildProposalsForDeclarationValue(); }
		 */
// for Test
}
Also used : ICSSMediaRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule) ICSSPageRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSPageRule) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ICSSPrimitiveValue(org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue) CSSFontFaceRule(org.w3c.dom.css.CSSFontFaceRule) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Example 57 with ICSSNode

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

the class CSSProposalGeneratorForAtmarkRule method getCandidateCharsetRule.

/**
 */
private CSSCACandidate getCandidateCharsetRule() {
    // check content model
    CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
    if (!util.collectNodesByType(CSSMMNode.TYPE_CHARSET_RULE).hasNext()) {
        return null;
    }
    // check if embedded or not
    if (fContext.getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
        return null;
    }
    // check if caret precede all other rules.
    int offset = fContext.getCursorPos();
    if (0 < offset) {
        SelectionCollector trav = new SelectionCollector();
        trav.setRegion(0, offset - 1);
        trav.apply(fContext.getModel().getDocument());
        Iterator i = trav.getSelectedNodes().iterator();
        while (i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof ICSSNode && !(obj instanceof ICSSDocument)) {
                return null;
            }
        }
    }
    int cursorPos = 0;
    String ident = (fUseUpperCase) ? CHARSET.toUpperCase() : CHARSET.toLowerCase();
    StringBuffer buf = new StringBuffer();
    buf.append(ident);
    // $NON-NLS-1$
    buf.append(" ");
    cursorPos = buf.length();
    StringAndOffset sao;
    sao = generateQuotes();
    buf.append(sao.fString);
    cursorPos += sao.fOffset;
    sao = generateSemicolon();
    buf.append(sao.fString);
    String text = buf.toString();
    if (isMatch(text)) {
        CSSCACandidate item = new CSSCACandidate();
        item.setReplacementString(text);
        item.setCursorPosition(cursorPos);
        item.setDisplayString(ident);
        item.setImageType(CSSImageType.RULE_CHARSET);
        return item;
    } else {
        return null;
    }
}
Also used : CSSMetaModelUtil(org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil) Iterator(java.util.Iterator) SelectionCollector(org.eclipse.wst.css.core.internal.util.SelectionCollector) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)

Example 58 with ICSSNode

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

the class AbstractCssTraverser method traverse.

private final short traverse(ICSSNode node) {
    if (node == null)
        return TRAV_CONT;
    travStack.push(node);
    // pre-action
    short ret = preNode(node);
    if (ret == TRAV_CONT) {
        if (fTraverseImported && (node.getNodeType() == ICSSNode.IMPORTRULE_NODE)) {
            ICSSImportRule rule = (ICSSImportRule) node;
            // traverse external style-sheet
            ICSSStyleSheet sheet = (ICSSStyleSheet) rule.getStyleSheet();
            if (sheet != null && !traversedSheets.contains(sheet)) {
                // prevent
                // loop
                traversedSheets.add(sheet);
                short retExt = traverse(sheet);
                if (retExt == TRAV_STOP) {
                    travStack.pop();
                    return retExt;
                }
            }
        }
        // collect children
        ArrayList children = new ArrayList();
        ICSSNode child = node.getFirstChild();
        if (fTraverseImportFirst) {
            ArrayList others = new ArrayList();
            while (child != null) {
                if (child.getNodeType() == ICSSNode.IMPORTRULE_NODE)
                    children.add(child);
                else
                    others.add(child);
                child = child.getNextSibling();
            }
            children.addAll(others);
        } else {
            while (child != null) {
                children.add(child);
                child = child.getNextSibling();
            }
        }
        // traverse children
        for (int i = 0; i < children.size(); i++) {
            child = (ICSSNode) children.get(i);
            short retChild = traverse(child);
            if (retChild == TRAV_STOP) {
                travStack.pop();
                return retChild;
            }
        }
    } else if (ret == TRAV_STOP) {
        travStack.pop();
        return ret;
    }
    // post-action
    ret = postNode(node);
    travStack.pop();
    return (ret == TRAV_PRUNE) ? TRAV_CONT : ret;
}
Also used : ArrayList(java.util.ArrayList) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)

Example 59 with ICSSNode

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

the class CSSProposalGeneratorForDeclarationName method getCandidates.

/**
 * getCandidates method comment.
 */
protected Iterator getCandidates() {
    List candidates = new ArrayList();
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    // $NON-NLS-1$
    String preDelim = "";
    for (int i = 0; i < preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM); i++) {
        // $NON-NLS-1$
        preDelim += ' ';
    }
    // $NON-NLS-1$
    String postDelim = "";
    for (int i = 0; i < preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_POST_DELIM); i++) {
        // $NON-NLS-1$
        postDelim += ' ';
    }
    ICSSNode targetNode = fContext.getTargetNode();
    boolean bFontRule = false;
    for (ICSSNode node = targetNode; node != null; node = node.getParentNode()) {
        if (node instanceof org.w3c.dom.css.CSSFontFaceRule) {
            bFontRule = true;
            break;
        }
    }
    List names = new ArrayList();
    CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
    Iterator iNames = util.collectNodesByType((bFontRule) ? CSSMMNode.TYPE_DESCRIPTOR : CSSMMNode.TYPE_PROPERTY);
    while (iNames.hasNext()) {
        CSSMMNode node = (CSSMMNode) iNames.next();
        names.add(node);
    }
    sortNames(names);
    // Collections.sort(names);
    boolean bAddColon = true;
    if ((targetNode instanceof ICSSStyleRule || targetNode instanceof ICSSStyleDeclItem || targetNode instanceof ICSSStyleDeclaration) && fContext.targetHas(CSSRegionContexts.CSS_DECLARATION_SEPARATOR)) {
        bAddColon = false;
    }
    Iterator i = names.iterator();
    while (i.hasNext()) {
        CSSMMNode node = (CSSMMNode) i.next();
        String text = node.getName();
        text = (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_NAME) == CSSCorePreferenceNames.UPPER) ? text.toUpperCase() : text.toLowerCase();
        if (!isMatch(text)) {
            continue;
        }
        int cursorPos = 0;
        StringBuffer buf = new StringBuffer();
        buf.append(text);
        buf.append(preDelim);
        cursorPos = buf.length();
        if (bAddColon) {
            // $NON-NLS-1$
            buf.append(':');
            buf.append(postDelim);
            cursorPos += 1 + postDelim.length();
        }
        // if (! (targetNode instanceof ICSSStyleDeclItem)) {
        // buf.append(';');//$NON-NLS-1$
        // }
        CSSCACandidate item = new CSSCACandidate();
        item.setReplacementString(buf.toString());
        item.setCursorPosition(cursorPos);
        item.setDisplayString(text);
        item.setMMNode(node);
        item.setImageType(getCategoryImageType(node));
        candidates.add(item);
    }
    return candidates.iterator();
}
Also used : CSSMetaModelUtil(org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ArrayList(java.util.ArrayList) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule) CSSMMNode(org.eclipse.wst.css.core.internal.metamodel.CSSMMNode) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Preferences(org.eclipse.core.runtime.Preferences)

Example 60 with ICSSNode

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

the class MediaRuleFormatter method formatBefore.

/**
 */
protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) {
    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) {
        // source formatting
        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 = null;
            if ((prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE) || (prev == null && (child == null || child.getNodeType() != ICSSNode.MEDIALIST_NODE))) {
                pickupType = CSSRegionContexts.CSS_LBRACE;
            }
            regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType);
        }
        // extract source
        if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
            // "@media" and mediatype
            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 (child != null && (child.getPreviousSibling() == null || child.getPreviousSibling().getNodeType() == ICSSNode.MEDIALIST_NODE)) {
            // mediatype and the first style rule
            for (int i = 0; i < regions.length; i++) {
                appendSpaceBefore(node, regions[i], source);
                // must
                source.append(decoratedRegion(regions[i], 0, stgy));
            // be comments
            }
            appendDelimBefore(node, null, source);
        } else {
            // between styles
            for (int i = 0; i < regions.length; i++) {
                appendDelimBefore(node, regions[i], source);
                // must
                source.append(decoratedRegion(regions[i], 0, stgy));
            // be comments
            }
            appendDelimBefore(node, null, source);
        }
    } else {
        // source generation
        if (child == null && prev != null && prev.getNodeType() != ICSSNode.MEDIALIST_NODE) {
            // after
            // the last style rule
            appendDelimBefore(node.getParentNode(), null, source);
        } else if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
            // between
            // "@media" and mediatype
            appendSpaceBefore(node, toAppend, source);
        } else if (prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) prev).getLength() > 0) {
            // between
            // mediatype and the first style rule
            // $NON-NLS-1$
            appendSpaceBefore(node, "{", source);
            // $NON-NLS-1$
            source.append("{");
            if (child != null)
                appendDelimBefore(node, null, source);
            else
                appendDelimBefore(node.getParentNode(), null, source);
        } else {
            // normal case
            appendDelimBefore(node, null, source);
        }
    }
}
Also used : MediaList(org.w3c.dom.stylesheets.MediaList) 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)

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