Search in sources :

Example 11 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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 12 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class StyleDeclItemFormatter method getAttrInsertPos.

/**
 */
public int getAttrInsertPos(ICSSNode node, String attrName) {
    if (node == null || attrName == null || attrName.length() == 0)
        return -1;
    if (!ICSSStyleDeclItem.IMPORTANT.equalsIgnoreCase(attrName))
        return -1;
    ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICSSStyleDeclItem.IMPORTANT);
    if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
        return ((IndexedRegion) attr).getStartOffset();
    IndexedRegion iNode = (IndexedRegion) node;
    if (iNode.getEndOffset() <= 0)
        return -1;
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(node.getOwnerDocument().getModel().getStructuredDocument(), new FormatRegion(iNode.getStartOffset(), iNode.getEndOffset() - iNode.getStartOffset()), getCleanupStrategy(node));
    for (int i = regions.length - 1; i >= 0; i--) {
        if (regions[i].getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_IMPORTANT)
            return regions[i].getStartOffset();
    }
    return iNode.getEndOffset();
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 13 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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 14 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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 15 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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)

Aggregations

IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)148 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)32 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)31 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)27 Node (org.w3c.dom.Node)27 Region (org.eclipse.jface.text.Region)21 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)21 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)21 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)20 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)17 ArrayList (java.util.ArrayList)11 List (java.util.List)11 ITextSelection (org.eclipse.jface.text.ITextSelection)11 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)8 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)8 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)8 Preferences (org.eclipse.core.runtime.Preferences)7 IDocument (org.eclipse.jface.text.IDocument)7