Search in sources :

Example 11 with RegionIterator

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

the class CSSProposalGeneratorForDeclarationValue method addSemiColon.

/**
 */
private void addSemiColon(List candidates) {
    ICSSNode targetNode = fContext.getTargetNode();
    if (targetNode instanceof ICSSStyleDeclItem) {
        ICSSNode firstChild = targetNode.getFirstChild();
        if (firstChild == null) {
            return;
        }
        if (firstChild instanceof IndexedRegion) {
            int startOffset = ((IndexedRegion) firstChild).getStartOffset();
            if (fContext.getCursorPos() <= startOffset) {
                return;
            }
        }
    }
    boolean bAddCloser = 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) {
                bAddCloser = true;
                break;
            }
        }
        if (!bAddCloser) {
            // second chance:
            // leading IStructuredDocumentRegion is not ";"
            IStructuredDocumentRegion nextStructuredDocumentRegion = CSSUtil.findNextSignificantNode(container);
            if (CSSUtil.getStructuredDocumentRegionType(nextStructuredDocumentRegion) != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
                bAddCloser = true;
            }
        }
    }
    if (bAddCloser) {
        CSSCACandidate item = new CSSCACandidate();
        // $NON-NLS-1$
        String text = fContext.getTextToReplace() + ";";
        item.setReplacementString(text);
        item.setCursorPosition(text.length());
        // $NON-NLS-1$
        item.setDisplayString(";");
        item.setImageType(null);
        candidates.add(item);
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) 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 12 with RegionIterator

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

the class CSSContentAssistContext method getTargetRegionPrevious.

ITextRegion getTargetRegionPrevious() {
    ITextRegion previousRegion = null;
    ITextRegion targetRegion = getTargetRegion();
    RegionIterator iterator = null;
    if (targetRegion == null) {
        if (0 < fCursorPos) {
            iterator = new RegionIterator(fStructuredDocument, fCursorPos - 1);
        }
    } else {
        iterator = getRegionIterator();
        if (iterator.hasPrev()) {
            iterator.prev();
        } else {
            iterator = null;
        }
    }
    if (iterator != null) {
        while (iterator.hasPrev()) {
            ITextRegion region = iterator.prev();
            String type = region.getType();
            if (type != CSSRegionContexts.CSS_S && type != CSSRegionContexts.CSS_COMMENT && type != CSSRegionContexts.CSS_CDO && type != CSSRegionContexts.CSS_CDC) {
                previousRegion = region;
                break;
            }
        }
    }
    return previousRegion;
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator)

Example 13 with RegionIterator

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

the class CSSContentAssistContext method targetFollows.

/**
 * @return boolean
 * @param regionType
 *            java.lang.String
 */
boolean targetFollows(String regionType) {
    RegionIterator iRegion;
    ITextRegion region = null;
    if (fStructuredDocument.getLength() <= fTargetPos) {
        iRegion = new RegionIterator(fStructuredDocument, fStructuredDocument.getLength() - 1);
    } else {
        iRegion = new RegionIterator(fStructuredDocument, fTargetPos);
        try {
            if (!Character.isWhitespace(fStructuredDocument.getChar(fTargetPos)) && iRegion.hasPrev()) {
                region = iRegion.prev();
            }
        } catch (BadLocationException e) {
            iRegion = new RegionIterator(fStructuredDocument, fStructuredDocument.getLength() - 1);
        }
    }
    while (iRegion.hasPrev()) {
        region = iRegion.prev();
        String type = region.getType();
        if (type == CSSRegionContexts.CSS_S || type == CSSRegionContexts.CSS_COMMENT) {
            continue;
        } else {
            break;
        }
    }
    if (region != null && region.getType() == regionType) {
        return true;
    } else {
        return false;
    }
}
Also used : 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 14 with RegionIterator

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

the class StructuredAutoEditStrategyCSS method prevCorrespondence.

/**
 */
protected CompoundRegion prevCorrespondence(int position, String regionType) {
    RegionIterator it = new RegionIterator(structuredDocument, position - 1);
    ITextRegion region = null;
    int nest = 1;
    if (regionType == CSSRegionContexts.CSS_RBRACE) {
        // skip to LBRACE
        while (it.hasPrev()) {
            region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_LBRACE)
                nest--;
            else if (region.getType() == CSSRegionContexts.CSS_RBRACE)
                nest++;
            if (nest <= 0)
                break;
        }
        if (nest == 0)
            return new CompoundRegion(it.getStructuredDocumentRegion(), region);
    }
    if (regionType == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END) {
        // skip to BRACKET_OPEN
        while (it.hasPrev()) {
            region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_START)
                nest--;
            else if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END)
                nest++;
            if (nest <= 0)
                break;
        }
        if (nest == 0)
            return new CompoundRegion(it.getStructuredDocumentRegion(), region);
    }
    if (regionType == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
        // skip to PARENTHESIS_OPEN
        while (it.hasPrev()) {
            region = it.prev();
            if (// CSSRegionContexts.CSS_PARENTHESIS_OPEN ||
            region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                nest--;
            else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE)
                nest++;
            if (nest <= 0)
                break;
        }
        if (nest == 0)
            return new CompoundRegion(it.getStructuredDocumentRegion(), region);
    }
    return null;
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator)

Example 15 with RegionIterator

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

the class StructuredAutoEditStrategyCSS method setRangeForClose.

/**
 * Insert the method's description here.
 *
 * @return boolean
 * @param command
 *            org.eclipse.jface.text.DocumentCommand
 */
protected boolean setRangeForClose(DocumentCommand command) {
    int position = command.offset;
    if (position == -1 || structuredDocument.getLength() == 0) {
        return false;
    }
    try {
        // find start of line
        int p = (position == structuredDocument.getLength() ? position - 1 : position);
        int line = structuredDocument.getLineOfOffset(p);
        int start = structuredDocument.getLineOffset(line);
        RegionIterator it = new RegionIterator(structuredDocument, start);
        boolean allWhiteSpace = false;
        // or not
        while (it.hasNext()) {
            ITextRegion region = it.next();
            if (region.getType() != CSSRegionContexts.CSS_S)
                break;
            if (it.getStructuredDocumentRegion().getEndOffset(region) > p) {
                allWhiteSpace = true;
                break;
            }
        }
        if (allWhiteSpace) {
            command.length = command.length - (start - command.offset);
            command.offset = start;
            return true;
        }
    } catch (BadLocationException excp) {
        Logger.logException(excp);
    }
    return false;
}
Also used : 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)

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