Search in sources :

Example 6 with ITextRegion

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

the class AbstractJSONCompletionProposalComputer method getCompletionRegion.

/**
 * Return the region whose content's require completion. This is something
 * of a misnomer as sometimes the user wants to be prompted for contents of
 * a non-existant ITextRegion, such as for enumerated attribute values
 * following an '=' sign.
 */
private ITextRegion getCompletionRegion(int documentPosition, IJSONNode node) {
    if (node == null) {
        return null;
    }
    ITextRegion region = null;
    int offset = documentPosition;
    IStructuredDocumentRegion flatNode = null;
    if (node.getNodeType() == IJSONNode.DOCUMENT_NODE) {
        if (node.getStructuredDocument().getLength() == 0) {
            return null;
        }
        ITextRegion result = node.getStructuredDocument().getRegionAtCharacterOffset(offset).getRegionAtCharacterOffset(offset);
        while (result == null) {
            offset--;
            result = node.getStructuredDocument().getRegionAtCharacterOffset(offset).getRegionAtCharacterOffset(offset);
        }
        return result;
    }
    IStructuredDocumentRegion startTag = node.getStartStructuredDocumentRegion();
    IStructuredDocumentRegion endTag = node.getEndStructuredDocumentRegion();
    // somewhere within the Node's JSON content.
    if ((startTag != null) && (startTag.getStartOffset() <= offset) && (offset < startTag.getStartOffset() + startTag.getLength())) {
        flatNode = startTag;
    } else if ((endTag != null) && (endTag.getStartOffset() <= offset) && (offset < endTag.getStartOffset() + endTag.getLength())) {
        flatNode = endTag;
    }
    if (flatNode != null) {
        // the offset is definitely within the start or end tag, continue
        // on and find the region
        region = getCompletionRegion(offset, flatNode);
    } else {
        // the docPosition is neither within the start nor the end, so it
        // must be content
        flatNode = node.getStructuredDocument().getRegionAtCharacterOffset(offset);
        // if (flatNode.contains(documentPosition)) {
        if ((flatNode.getStartOffset() <= documentPosition) && (flatNode.getEndOffset() >= documentPosition)) {
            // we're interesting in completing/extending the previous
            // IStructuredDocumentRegion if the current
            // IStructuredDocumentRegion isn't plain content or if it's
            // preceded by an orphan '<'
            /*
				 * if ((offset == flatNode.getStartOffset()) &&
				 * (flatNode.getPrevious() != null) && (((flatNode
				 * .getRegionAtCharacterOffset(documentPosition) != null)) ||
				 * (flatNode.getPrevious().getLastRegion() .getType() ==
				 * JSONRegionContext.JSON_TAG_OPEN) || (flatNode
				 * .getPrevious().getLastRegion().getType() ==
				 * JSONRegionContext.JSON_END_TAG_OPEN))) {
				 * 
				 * // Is the region also the start of the node? If so, the //
				 * previous IStructuredDocumentRegion is // where to look for a
				 * useful region. region =
				 * flatNode.getPrevious().getLastRegion(); } else if
				 * (flatNode.getEndOffset() == documentPosition) { region =
				 * flatNode.getLastRegion(); } else { region =
				 * flatNode.getFirstRegion(); }
				 */
            region = flatNode.getFirstRegion();
        } else {
            // catch end of document positions where the docPosition isn't
            // in a IStructuredDocumentRegion
            region = flatNode.getLastRegion();
        }
    }
    return region;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 7 with ITextRegion

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

the class JSONUtil method getRegionText.

/**
 */
public static String getRegionText(IStructuredDocumentRegion flatNode, ITextRegionList regions) {
    StringBuffer buf = new StringBuffer();
    if (regions != null) {
        for (Iterator i = regions.iterator(); i.hasNext(); ) {
            ITextRegion region = (ITextRegion) i.next();
            if (region == null) {
                continue;
            }
            buf.append(flatNode.getText(region));
        }
    }
    return buf.toString();
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator)

Example 8 with ITextRegion

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

the class CSSModelParser method insertImportRule.

/**
 */
private CSSNodeImpl insertImportRule(IStructuredDocumentRegion beginDocRegion, IStructuredDocumentRegion endDocRegion) {
    CSSNodeImpl parent = fCreationContext.getTargetNode();
    if (!fParseFloating && !(parent instanceof ICSSRuleContainer)) {
        return null;
    }
    ITextRegionList regions = new TextRegionListImpl(beginDocRegion.getRegions());
    // must be "@import"
    regions.remove(0);
    ITextRegion hrefRegion = null;
    while (!regions.isEmpty()) {
        ITextRegion textRegion = regions.remove(0);
        if (textRegion == null) {
            continue;
        }
        String type = textRegion.getType();
        if (type == CSSRegionContexts.CSS_S || type == CSSRegionContexts.CSS_COMMENT) {
            continue;
        }
        if (type == CSSRegionContexts.CSS_URI || type == CSSRegionContexts.CSS_STRING) {
            hrefRegion = textRegion;
            break;
        } else {
            break;
        }
    }
    if (hrefRegion == null) {
        return null;
    }
    CSSImportRuleImpl rule = fFeeder.getCSSImportRule();
    if (rule == null) {
        return null;
    }
    CSSUtil.stripSurroundingSpace(regions);
    MediaListImpl mediaList = (MediaListImpl) rule.getMedia();
    setMediaList(mediaList, beginDocRegion, regions);
    if (!fUpdateContext.isActive()) {
        rule.setAttribute(ICSSImportRule.HREF, beginDocRegion.getText(hrefRegion));
    }
    // setup flat container
    rule.setRangeStructuredDocumentRegion(beginDocRegion, endDocRegion);
    CSSAttrImpl attr = rule.getAttributeNode(ICSSImportRule.HREF);
    if (attr != null) {
        attr.setRangeRegion(beginDocRegion, hrefRegion, hrefRegion);
    }
    // insert to tree
    if (!fUpdateContext.isActive() && parent != null) {
        propagateRangePreInsert(parent, rule);
        CSSNodeImpl next = fCreationContext.getNextNode();
        if (next != null) {
            parent.insertBefore(rule, next);
        } else {
            parent.appendChild(rule);
        }
    }
    return rule;
}
Also used : ICSSRuleContainer(org.eclipse.wst.css.core.internal.provisional.document.ICSSRuleContainer) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) TextRegionListImpl(org.eclipse.wst.sse.core.internal.text.TextRegionListImpl) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 9 with ITextRegion

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

the class CSSModelParser method setMediaList.

/**
 * regions: surrounding spaces should be removed. Q. Why did you set
 * mediaTable ? A. MediaList may have two or more medium that have same
 * value, then searcing in MediaList is not perfect. Q.
 * fUpdateContext.isActive() is not care. Are you OK? A. OK.
 */
private void setMediaList(MediaListImpl mediaList, IStructuredDocumentRegion region, ITextRegionList textRegions) {
    if (mediaList == null || textRegions == null) {
        return;
    }
    Collection mediaTable = new HashSet();
    CSSNodeListImpl attrs = mediaList.getMedia();
    for (int i = 0; i != attrs.getLength(); i++) {
        mediaTable.add(attrs.item(i));
    }
    ITextRegion start = null;
    ITextRegion end = null;
    Iterator i = textRegions.iterator();
    ITextRegion textRegion = (ITextRegion) ((i.hasNext()) ? i.next() : null);
    while (textRegion != null) {
        if (textRegion.getType() == CSSRegionContexts.CSS_MEDIUM) {
            String mediumStr = region.getText(textRegion);
            if (0 < mediumStr.length()) {
                CSSAttrImpl attr = null;
                // is the medium already set ?
                Iterator iTable = mediaTable.iterator();
                while (iTable.hasNext()) {
                    CSSAttrImpl cai = (CSSAttrImpl) iTable.next();
                    if (mediumStr.equalsIgnoreCase(cai.getValue())) {
                        attr = cai;
                        mediaTable.remove(cai);
                        break;
                    }
                }
                if (attr == null) {
                    // is not set. create new attribute
                    // $NON-NLS-1$
                    String key = "mediumP" + mediaList.mediumCounter++;
                    mediaList.setAttribute(key, mediumStr);
                    attr = mediaList.getAttributeNode(key);
                }
                attr.setRangeRegion(region, textRegion, textRegion);
                if (start == null) {
                    start = textRegion;
                }
                end = textRegion;
            }
        }
        textRegion = (ITextRegion) ((i.hasNext()) ? i.next() : null);
    }
    if (start != null && end != null) {
        mediaList.setRangeRegion(region, start, end);
    }
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 10 with ITextRegion

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

the class CSSRegionContainer method getRegionCount.

/**
 * @return int
 */
int getRegionCount() {
    validateRange();
    if (getFirstRegion() == null)
        return 0;
    if (getFirstRegion() == getLastRegion())
        return 1;
    ITextRegionList regions = fParentRegion.getRegions();
    int j = 0;
    for (int i = 0; i < regions.size(); i++) {
        ITextRegion current = regions.get(i);
        if (j != 0 || current == getFirstRegion())
            j++;
        if (current == getLastRegion())
            break;
    }
    return j;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Aggregations

ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)447 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)179 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)174 Iterator (java.util.Iterator)75 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)58 ArrayList (java.util.ArrayList)40 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)39 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)35 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)35 List (java.util.List)33 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)31 BadLocationException (org.eclipse.jface.text.BadLocationException)30 RegionIterator (org.eclipse.wst.dtd.core.internal.text.RegionIterator)22 TextRegionListImpl (org.eclipse.wst.sse.core.internal.text.TextRegionListImpl)19 ITextRegionCollection (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)16 Node (org.w3c.dom.Node)16 ContextRegion (org.eclipse.wst.sse.core.internal.parser.ContextRegion)15 IOException (java.io.IOException)13 IRegion (org.eclipse.jface.text.IRegion)13 StyleRange (org.eclipse.swt.custom.StyleRange)13