Search in sources :

Example 6 with RegionIterator

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

the class MediaRuleFormatter method getLengthToReformatAfter.

/**
 * @return int
 * @param node
 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
 * @param insertPos
 *            int
 */
public int getLengthToReformatAfter(ICSSNode node, int insertPos) {
    if (node == null)
        return 0;
    IndexedRegion nnode = (IndexedRegion) node;
    if (insertPos <= 0 || !nnode.contains(insertPos - 1))
        return 0;
    if (node.getFirstChild().getNextSibling() == node.getLastChild()) {
        // inserted
        // first style rule
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(insertPos);
        if (flatNode == null)
            return 0;
        ITextRegion region = flatNode.getRegionAtCharacterOffset(insertPos);
        if (region == null)
            return 0;
        RegionIterator it = new RegionIterator(flatNode, region);
        while (it.hasNext()) {
            region = it.next();
            if (region.getType() == CSSRegionContexts.CSS_LBRACE)
                break;
            if (nnode.getEndOffset() <= it.getStructuredDocumentRegion().getEndOffset(region))
                break;
        }
        int pos = it.getStructuredDocumentRegion().getStartOffset(region) - insertPos;
        return (pos >= 0) ? pos : 0;
    }
    return super.getLengthToReformatAfter(node, insertPos);
}
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 7 with RegionIterator

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

the class StructuredAutoEditStrategyCSS method getIndentFor.

/**
 */
protected String getIndentFor(CompoundRegion region, boolean indentForNextRegion) {
    if (region == null)
        return null;
    IStructuredDocumentRegion flatNode = region.getDocumentRegion();
    if (flatNode == null)
        return null;
    try {
        if (region.getType() == CSSRegionContexts.CSS_LBRACE || region.getType() == CSSRegionContexts.CSS_DELIMITER || region.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
            // get meanful flat node
            RegionIterator it = new RegionIterator(flatNode, region.getTextRegion());
            it.prev();
            while (it.hasPrev()) {
                ITextRegion r = it.prev();
                region = new CompoundRegion(it.getStructuredDocumentRegion(), r);
                if (region.getType() != CSSRegionContexts.CSS_S)
                    break;
            }
            flatNode = region.getDocumentRegion();
            // get indent string
            int position = flatNode.getStart();
            int line = structuredDocument.getLineOfOffset(position);
            int start = structuredDocument.getLineOffset(line);
            int end = findEndOfWhiteSpace(structuredDocument, start, position);
            return structuredDocument.get(start, end - start);
        } else if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_START || // region.getType() == CSSRegionContexts.CSS_PARENTHESIS_OPEN ||
        region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION || region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) {
            int position = flatNode.getStart() + region.getStart();
            int line = structuredDocument.getLineOfOffset(position);
            int start = structuredDocument.getLineOffset(line);
            int end = findEndOfWhiteSpace(structuredDocument, start, position);
            StringBuffer buf = new StringBuffer(structuredDocument.get(start, end - start));
            position += region.getText().length();
            if (indentForNextRegion) {
                int tokenStart = findEndOfWhiteSpace(structuredDocument, position, structuredDocument.getLineOffset(line) + structuredDocument.getLineLength(line) - 1);
                if (tokenStart < structuredDocument.getLineOffset(line) + structuredDocument.getLineLength(line) - 1) {
                    position = tokenStart;
                }
            }
            while (position - end > 0) {
                // $NON-NLS-1$
                buf.append(" ");
                end++;
            }
            return buf.toString();
        } else
            // $NON-NLS-1$
            return "";
    } catch (BadLocationException excp) {
        Logger.logException(excp);
    }
    return null;
}
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) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 8 with RegionIterator

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

the class CSSProposalGeneratorForDeclarationValue method checkSemiColon.

/**
 */
private void checkSemiColon() {
    fAppendSemiColon = false;
    ITextRegion targetRegion = fContext.getTargetRegion();
    if (targetRegion != null && targetRegion.getType() != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
        // find trailing ":" or ";"
        // if ":" before ";" is found, add ";"
        RegionIterator iterator = fContext.getRegionIterator();
        IStructuredDocumentRegion container = iterator.getStructuredDocumentRegion();
        while (iterator.hasNext()) {
            ITextRegion region = iterator.next();
            if (iterator.getStructuredDocumentRegion() != container) {
                break;
            }
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) {
                fAppendSemiColon = true;
                break;
            }
        }
        if (!fAppendSemiColon) {
            // second chance:
            // leading IStructuredDocumentRegion is not ";"
            IStructuredDocumentRegion nextStructuredDocumentRegion = CSSUtil.findNextSignificantNode(container);
            if (CSSUtil.getStructuredDocumentRegionType(nextStructuredDocumentRegion) != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
                fAppendSemiColon = true;
            }
        }
    }
}
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)

Example 9 with RegionIterator

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

the class CSSContentAssistContext method isTargetPosAfterOf.

/**
 */
boolean isTargetPosAfterOf(String regionType) {
    int start = ((IndexedRegion) fTargetNode).getStartOffset();
    if (start < 0 || ((IndexedRegion) fTargetNode).getEndOffset() <= 0) {
        return false;
    }
    RegionIterator iRegion = new RegionIterator(fStructuredDocument, start);
    while (iRegion.hasNext()) {
        ITextRegion region = iRegion.next();
        if (fTargetPos < iRegion.getStructuredDocumentRegion().getTextEndOffset(region)) {
            break;
        }
        if (region.getType() == regionType) {
            return true;
        }
    }
    return false;
}
Also used : 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 10 with RegionIterator

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

the class CSSContentAssistContext method targetHas.

/**
 */
boolean targetHas(String regionType) {
    int end = ((IndexedRegion) fTargetNode).getEndOffset();
    if (getTargetPos() < 0 || end <= 0) {
        return false;
    }
    RegionIterator iRegion = new RegionIterator(fStructuredDocument, getTargetPos());
    while (iRegion.hasNext()) {
        ITextRegion region = iRegion.next();
        if (end <= iRegion.getStructuredDocumentRegion().getStartOffset(region)) {
            break;
        }
        if (region.getType() == regionType) {
            return true;
        }
    }
    return false;
}
Also used : 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