Search in sources :

Example 26 with RegionIterator

use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.

the class MediaListFormatter method formatAttrChanged.

/**
 */
public StringBuffer formatAttrChanged(ICSSNode node, ICSSAttr attr, boolean insert, AttrChangeContext context) {
    StringBuffer buf = new StringBuffer();
    if (node == null || attr == null)
        return buf;
    boolean first = false, last = false;
    // 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();
            context.start = it.getStructuredDocumentRegion().getStartOffset(region);
            while (it.hasPrev()) {
                ITextRegion prev = it.prev();
                if (prev.getType() == CSSRegionContexts.CSS_S || prev.getType() == CSSRegionContexts.CSS_MEDIA_SEPARATOR || prev.getType() == CSSRegionContexts.CSS_COMMENT)
                    context.start = it.getStructuredDocumentRegion().getStartOffset(prev);
                else
                    break;
            }
            if (context.start < ((IndexedRegion) node).getStartOffset()) {
                context.start = ((IndexedRegion) node).getStartOffset();
                first = true;
            }
            it.reset(flatNode, region);
            context.end = it.getStructuredDocumentRegion().getEndOffset(region);
            while (it.hasNext()) {
                ITextRegion next = it.next();
                if (next.getType() == CSSRegionContexts.CSS_S || next.getType() == CSSRegionContexts.CSS_MEDIA_SEPARATOR || next.getType() == CSSRegionContexts.CSS_COMMENT)
                    context.end = it.getStructuredDocumentRegion().getEndOffset(next);
                else
                    break;
            }
            if (((IndexedRegion) node).getEndOffset() < context.end) {
                context.end = ((IndexedRegion) node).getEndOffset();
                last = true;
            }
        } else {
            last = true;
            IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
            ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
            RegionIterator it = new RegionIterator(flatNode, region);
            context.start = it.getStructuredDocumentRegion().getEndOffset(region);
            context.end = it.getStructuredDocumentRegion().getEndOffset(region);
            while (it.hasPrev()) {
                ITextRegion prev = it.prev();
                if (prev.getType() == CSSRegionContexts.CSS_S || prev.getType() == CSSRegionContexts.CSS_MEDIA_SEPARATOR || prev.getType() == CSSRegionContexts.CSS_COMMENT)
                    context.start = it.getStructuredDocumentRegion().getStartOffset(prev);
                else
                    break;
            }
            if (context.start < ((IndexedRegion) node).getStartOffset()) {
                context.start = ((IndexedRegion) node).getStartOffset();
                first = true;
            }
        }
    }
    // generate text
    if (insert && attr.getValue() != null && attr.getValue().length() > 0) {
        if (!first)
            // $NON-NLS-1$
            buf.append(",");
        appendSpaceBefore(node, attr.getValue(), buf);
        buf.append(attr.getValue());
        if (!last) {
            // $NON-NLS-1$
            buf.append(",");
            // $NON-NLS-1$
            appendSpaceBefore(node, "", buf);
        }
    } else if (!first && !last) {
        // $NON-NLS-1$
        buf.append(",");
        // $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) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 27 with RegionIterator

use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.

the class PageRuleFormatter 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 (!ICSSPageRule.SELECTOR.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).getEndOffset() - 1);
            ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) attr).getEndOffset() - 1);
            RegionIterator it = new RegionIterator(flatNode, region);
            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);
            context.start = it.getStructuredDocumentRegion().getStartOffset(region);
        } else {
            int pos = getAttrInsertPos(node, attr.getName());
            context.start = pos;
            context.end = pos;
        /*
				 * IStructuredDocumentRegion flatNode =
				 * node.getOwnerDocument().getModel().getStructuredDocument().getNodeAtCharacterOffset(((IndexedRegion)node).getStartOffset());
				 * ITextRegion region =
				 * flatNode.getRegionAtCharacterOffset(((IndexedRegion)node).getStartOffset());
				 * if (region.getType() == CSSRegionContexts.S) {
				 * context.start = region.getStartOffset(); context.end =
				 * region.getEndOffset(); } else { context.start =
				 * flatNode.getEndOffset() + 1; context.end =
				 * flatNode.getEndOffset(); }
				 */
        }
    }
    // generate text
    if (insert) {
        String val = attr.getValue();
        if (val != null && val.length() > 0) {
            buf.append(val);
            // $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) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 28 with RegionIterator

use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.

the class StyleRuleFormatter 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 (!ICSSStyleRule.SELECTOR.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).getEndOffset() - 1);
            ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) attr).getEndOffset() - 1);
            RegionIterator it = new RegionIterator(flatNode, region);
            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);
            context.start = it.getStructuredDocumentRegion().getStartOffset(region);
        } else {
            IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) node).getStartOffset());
            ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) node).getStartOffset());
            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) {
        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) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)28 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)28 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)16 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)16 BadLocationException (org.eclipse.jface.text.BadLocationException)3 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)3 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)3 Preferences (org.eclipse.core.runtime.Preferences)2 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)2 ICSSAttr (org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1