Search in sources :

Example 81 with IStructuredDocumentRegion

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

the class JSONSyntaxColoringPage method getNamedStyleAtOffset.

private String getNamedStyleAtOffset(int offset) {
    // ensure the offset is clean
    if (offset >= fDocument.getLength())
        return getNamedStyleAtOffset(fDocument.getLength() - 1);
    else if (offset < 0)
        return getNamedStyleAtOffset(0);
    IStructuredDocumentRegion documentRegion = fDocument.getFirstStructuredDocumentRegion();
    while (documentRegion != null && !documentRegion.containsOffset(offset)) {
        documentRegion = documentRegion.getNext();
    }
    if (documentRegion != null) {
        // find the ITextRegion's Context at this offset
        ITextRegion interest = documentRegion.getRegionAtCharacterOffset(offset);
        if (interest == null)
            return null;
        if (offset > documentRegion.getTextEndOffset(interest))
            return null;
        String regionContext = interest.getType();
        if (regionContext == null)
            return null;
        // find the named style (internal/selectable name) for that
        // context
        String namedStyle = (String) fContextToStyleMap.get(regionContext);
        if (namedStyle != null) {
            return namedStyle;
        }
    }
    return null;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 82 with IStructuredDocumentRegion

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

the class JSONSyntaxColoringPage method applyStyles.

/**
 * Color the text in the sample area according to the current preferences
 */
void applyStyles() {
    if (fText == null || fText.isDisposed())
        return;
    IStructuredDocumentRegion documentRegion = fDocument.getFirstStructuredDocumentRegion();
    while (documentRegion != null) {
        ITextRegionList regions = documentRegion.getRegions();
        for (int i = 0; i < regions.size(); i++) {
            ITextRegion currentRegion = regions.get(i);
            // lookup the local coloring type and apply it
            String namedStyle = (String) fContextToStyleMap.get(currentRegion.getType());
            if (namedStyle == null)
                continue;
            TextAttribute attribute = getAttributeFor(namedStyle);
            if (attribute == null)
                continue;
            StyleRange style = new StyleRange(documentRegion.getStartOffset(currentRegion), currentRegion.getTextLength(), attribute.getForeground(), attribute.getBackground(), attribute.getStyle());
            style.strikeout = (attribute.getStyle() & TextAttribute.STRIKETHROUGH) != 0;
            style.underline = (attribute.getStyle() & TextAttribute.UNDERLINE) != 0;
            fText.setStyleRange(style);
        }
        documentRegion = documentRegion.getNext();
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) TextAttribute(org.eclipse.jface.text.TextAttribute) StyleRange(org.eclipse.swt.custom.StyleRange)

Example 83 with IStructuredDocumentRegion

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

the class StructuredTextPartitionerForJSP method getPartitionType.

public String getPartitionType(ITextRegion region, int offset) {
    String result = null;
    final String region_type = region.getType();
    if (region_type == DOMJSPRegionContexts.JSP_CONTENT) {
        result = getPartitionTypeForDocumentLanguage();
    } else if (region_type == DOMJSPRegionContexts.JSP_COMMENT_TEXT || region_type == DOMJSPRegionContexts.JSP_COMMENT_OPEN || region_type == DOMJSPRegionContexts.JSP_COMMENT_CLOSE)
        result = IJSPPartitions.JSP_COMMENT;
    else if (region_type == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME || region_type == DOMJSPRegionContexts.JSP_DIRECTIVE_OPEN || region_type == DOMJSPRegionContexts.JSP_DIRECTIVE_CLOSE)
        result = IJSPPartitions.JSP_DIRECTIVE;
    else if (region_type == DOMJSPRegionContexts.JSP_CLOSE || region_type == DOMJSPRegionContexts.JSP_SCRIPTLET_OPEN || region_type == DOMJSPRegionContexts.JSP_EXPRESSION_OPEN || region_type == DOMJSPRegionContexts.JSP_DECLARATION_OPEN)
        result = IJSPPartitions.JSP_CONTENT_DELIMITER;
    else if (region_type == DOMJSPRegionContexts.JSP_ROOT_TAG_NAME)
        result = IJSPPartitions.JSP_DEFAULT;
    else if (region_type == DOMJSPRegionContexts.JSP_EL_OPEN || region_type == DOMJSPRegionContexts.JSP_EL_CONTENT || region_type == DOMJSPRegionContexts.JSP_EL_CLOSE || region_type == DOMJSPRegionContexts.JSP_EL_DQUOTE || region_type == DOMJSPRegionContexts.JSP_EL_SQUOTE || region_type == DOMJSPRegionContexts.JSP_EL_QUOTED_CONTENT)
        result = IJSPPartitions.JSP_DEFAULT_EL;
    else if (region_type == DOMJSPRegionContexts.JSP_VBL_OPEN || region_type == DOMJSPRegionContexts.JSP_VBL_CONTENT || region_type == DOMJSPRegionContexts.JSP_VBL_CLOSE || region_type == DOMJSPRegionContexts.JSP_VBL_DQUOTE || region_type == DOMJSPRegionContexts.JSP_VBL_SQUOTE || region_type == DOMJSPRegionContexts.JSP_VBL_QUOTED_CONTENT)
        result = IJSPPartitions.JSP_DEFAULT_EL2;
    else if (region_type == DOMJSPRegionContexts.XML_TAG_ATTRIBUTE_VALUE_DQUOTE || region_type == DOMJSPRegionContexts.XML_TAG_ATTRIBUTE_VALUE_SQUOTE)
        result = IJSPPartitions.JSP_DEFAULT;
    else if (region_type == DOMRegionContext.XML_CDATA_TEXT) {
        // BUG131463: possibly between <jsp:scriptlet>, <jsp:expression>,
        // <jsp:declaration>
        IStructuredDocumentRegion sdRegion = this.fStructuredDocument.getRegionAtCharacterOffset(offset);
        if (isJspJavaActionName(getParentName(sdRegion)))
            result = getPartitionTypeForDocumentLanguage();
        else
            result = getEmbeddedPartitioner().getPartitionType(region, offset);
    } else if (region_type == DOMRegionContext.XML_CONTENT) {
        // possibly between <jsp:scriptlet>, <jsp:expression>,
        // <jsp:declaration>
        IStructuredDocumentRegion sdRegion = this.fStructuredDocument.getRegionAtCharacterOffset(offset);
        if (isJspJavaActionName(getParentName(sdRegion)))
            result = getPartitionTypeForDocumentLanguage();
        else
            result = getDefaultPartitionType();
    } else // else if (region_type == DOMRegionContext.BLOCK_TEXT) {
    // possibly between <jsp:scriptlet>, <jsp:expression>,
    // <jsp:declaration>
    // IStructuredDocumentRegion sdRegion = this.fStructuredDocument.getRegionAtCharacterOffset(offset);
    // if (isJspJavaActionName(getParentName(sdRegion)))
    // result = getPartitionTypeForDocumentLanguage();
    // else
    // result = getDefaultPartitionType();
    // }
    {
        result = getEmbeddedPartitioner().getPartitionType(region, offset);
    }
    return result;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)

Example 84 with IStructuredDocumentRegion

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

the class JSPSourceParser method parseNodes.

protected IStructuredDocumentRegion parseNodes() {
    // regions are initially reported as complete offsets within the
    // scanned input
    // they are adjusted here to be indexes from the currentNode's start
    // offset
    IStructuredDocumentRegion headNode = null;
    IStructuredDocumentRegion lastNode = null;
    ITextRegion region = null;
    // DMW: 2/12/03. Made current node local variable, since
    // we changed class to not require state
    IStructuredDocumentRegion currentNode = null;
    String type = null;
    while ((region = getNextRegion()) != null) {
        type = region.getType();
        // of them
        if (type == DOMRegionContext.BLOCK_TEXT) {
            if (currentNode != null && currentNode.getLastRegion().getType() == DOMRegionContext.BLOCK_TEXT) {
                // multiple block texts indicated embedded containers; no
                // new IStructuredDocumentRegion
                currentNode.addRegion(region);
                currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
                region.adjustStart(-currentNode.getStart());
                // region.setParent(currentNode);
                if (region instanceof ITextRegionContainer) {
                    ((ITextRegionContainer) region).setParent(currentNode);
                }
            } else {
                // not continuing a IStructuredDocumentRegion
                if (currentNode != null) {
                    // terminated
                    if (!currentNode.isEnded()) {
                        currentNode.setLength(region.getStart() - currentNode.getStart());
                    // fCurrentNode.setTextLength(region.getStart() -
                    // fCurrentNode.getStart());
                    }
                    lastNode = currentNode;
                }
                fireNodeParsed(currentNode);
                currentNode = createStructuredDocumentRegion(type);
                if (lastNode != null) {
                    lastNode.setNext(currentNode);
                }
                currentNode.setPrevious(lastNode);
                currentNode.setStart(region.getStart());
                currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
                currentNode.setEnded(true);
                region.adjustStart(-currentNode.getStart());
                currentNode.addRegion(region);
                // region.setParent(currentNode);
                if (region instanceof ITextRegionContainer) {
                    ((ITextRegionContainer) region).setParent(currentNode);
                }
            }
        } else // the following contexts OPEN new StructuredDocumentRegions
        if ((currentNode != null && currentNode.isEnded()) || (type == DOMRegionContext.XML_CONTENT) || (type == DOMRegionContext.XML_CHAR_REFERENCE) || (type == DOMRegionContext.XML_ENTITY_REFERENCE) || (type == DOMRegionContext.XML_PI_OPEN) || (type == DOMRegionContext.XML_TAG_OPEN) || (type == DOMRegionContext.XML_END_TAG_OPEN) || (type == DOMRegionContext.XML_COMMENT_OPEN) || (type == DOMRegionContext.XML_CDATA_OPEN) || (type == DOMRegionContext.XML_DECLARATION_OPEN) || (type == DOMJSPRegionContexts.JSP_COMMENT_OPEN) || (type == DOMJSPRegionContexts.JSP_DECLARATION_OPEN) || (type == DOMJSPRegionContexts.JSP_DIRECTIVE_OPEN) || (type == DOMJSPRegionContexts.JSP_EXPRESSION_OPEN) || (type == DOMJSPRegionContexts.JSP_SCRIPTLET_OPEN) || (type == DOMJSPRegionContexts.JSP_CLOSE) || type == DOMJSPRegionContexts.JSP_EL_OPEN) {
            if (currentNode != null) {
                // ensure that any existing node is at least terminated
                if (!currentNode.isEnded()) {
                    currentNode.setLength(region.getStart() - currentNode.getStart());
                // fCurrentNode.setTextLength(region.getStart() -
                // fCurrentNode.getStart());
                }
                lastNode = currentNode;
            }
            fireNodeParsed(currentNode);
            currentNode = createStructuredDocumentRegion(type);
            if (lastNode != null) {
                lastNode.setNext(currentNode);
            }
            currentNode.setPrevious(lastNode);
            currentNode.setStart(region.getStart());
            currentNode.addRegion(region);
            currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
            region.adjustStart(-currentNode.getStart());
            // region.setParent(currentNode);
            if (region instanceof ITextRegionContainer) {
                ((ITextRegionContainer) region).setParent(currentNode);
            }
        } else // StructuredDocumentRegions; just add to them
        if ((type == DOMRegionContext.XML_TAG_NAME) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) || (type == DOMRegionContext.XML_COMMENT_TEXT) || (type == DOMRegionContext.XML_PI_CONTENT) || (type == DOMRegionContext.XML_DOCTYPE_INTERNAL_SUBSET) || (type == DOMJSPRegionContexts.JSP_COMMENT_TEXT) || (type == DOMJSPRegionContexts.JSP_ROOT_TAG_NAME) || (type == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME) || type == DOMJSPRegionContexts.JSP_EL_CONTENT) {
            currentNode.addRegion(region);
            currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
            region.adjustStart(-currentNode.getStart());
            // region.setParent(currentNode);
            if (region instanceof ITextRegionContainer) {
                ((ITextRegionContainer) region).setParent(currentNode);
            }
        } else // cleanly
        if ((type == DOMRegionContext.XML_PI_CLOSE) || (type == DOMRegionContext.XML_TAG_CLOSE) || (type == DOMRegionContext.XML_EMPTY_TAG_CLOSE) || (type == DOMRegionContext.XML_COMMENT_CLOSE) || (type == DOMRegionContext.XML_CDATA_CLOSE) || (type == DOMJSPRegionContexts.JSP_CLOSE) || (type == DOMJSPRegionContexts.JSP_COMMENT_CLOSE) || (type == DOMJSPRegionContexts.JSP_DIRECTIVE_CLOSE) || (type == DOMRegionContext.XML_DECLARATION_CLOSE) || type == DOMJSPRegionContexts.JSP_EL_CLOSE) {
            currentNode.setEnded(true);
            currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
            currentNode.addRegion(region);
            region.adjustStart(-currentNode.getStart());
            // region.setParent(currentNode);
            if (region instanceof ITextRegionContainer) {
                ((ITextRegionContainer) region).setParent(currentNode);
            }
        } else // this is extremely rare, but valid
        if (type == DOMRegionContext.WHITE_SPACE) {
            ITextRegion lastRegion = currentNode.getLastRegion();
            // pack the embedded container with this region
            if (lastRegion instanceof ITextRegionContainer) {
                ITextRegionContainer container = (ITextRegionContainer) lastRegion;
                container.getRegions().add(region);
                // DW, 4/16/2003 container regions have parent. Probably a
                // better place to set,
                // but for now, will (re)set each time through
                container.setParent(currentNode);
                // DW, 4/16/2003 token regions no longer have parents
                // region.setParent(container);
                region.adjustStart(container.getLength() - region.getStart());
            }
            currentNode.getLastRegion().adjustLength(region.getLength());
            currentNode.adjustLength(region.getLength());
        } else if (type == DOMRegionContext.UNDEFINED && currentNode != null) {
            // combine with previous if also undefined
            if (currentNode.getLastRegion() != null && currentNode.getLastRegion().getType() == DOMRegionContext.UNDEFINED) {
                currentNode.getLastRegion().adjustLength(region.getLength());
                currentNode.adjustLength(region.getLength());
                // region to the container and update its start location
                if (currentNode.getLastRegion() instanceof ITextRegionContainer) {
                    region.adjustStart(-currentNode.getLastRegion().getStart() - currentNode.getStart());
                    ((ITextRegionContainer) currentNode.getLastRegion()).getRegions().add(region);
                }
            } else // previous wasn't undefined
            {
                currentNode.addRegion(region);
                currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
                region.adjustStart(-currentNode.getStart());
            }
            if (region instanceof ITextRegionContainer) {
                ((ITextRegionContainer) region).setParent(currentNode);
            }
        } else {
            // ensure that a node exists
            if (currentNode == null) {
                currentNode = createStructuredDocumentRegion(type);
                currentNode.setStart(region.getStart());
            }
            currentNode.addRegion(region);
            currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
            region.adjustStart(-currentNode.getStart());
            // region.setParent(currentNode);
            if (region instanceof ITextRegionContainer) {
                ((ITextRegionContainer) region).setParent(currentNode);
            }
            if (Debug.debugTokenizer)
                // $NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
                System.out.println(getClass().getName() + " found region of not specifically handled type " + region.getType() + " @ " + region.getStart() + "[" + region.getLength() + "]");
        }
        // ensures that they open StructuredDocumentRegions the same way
        if ((type == DOMRegionContext.XML_CONTENT) || (type == DOMRegionContext.XML_CHAR_REFERENCE) || (type == DOMRegionContext.XML_ENTITY_REFERENCE) || (type == DOMJSPRegionContexts.JSP_DECLARATION_OPEN) || (type == DOMJSPRegionContexts.JSP_EXPRESSION_OPEN) || (type == DOMJSPRegionContexts.JSP_SCRIPTLET_OPEN) || (type == DOMJSPRegionContexts.JSP_CONTENT) || (type == DOMJSPRegionContexts.JSP_CLOSE)) {
            currentNode.setEnded(true);
        }
        if (headNode == null && currentNode != null) {
            headNode = currentNode;
        }
    }
    if (currentNode != null) {
        fireNodeParsed(currentNode);
        currentNode.setPrevious(lastNode);
    }
    primReset();
    return headNode;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)

Example 85 with IStructuredDocumentRegion

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

the class StyleElementAdapter method replaceData.

/**
 * Apply changes from CSS sub-model to HTML model
 */
private void replaceData(int offset, int length, String data) {
    IDOMNode element = (IDOMNode) getElement();
    if (element == null)
        return;
    IDOMModel ownerModel = element.getModel();
    if (ownerModel == null)
        return;
    IStructuredDocument structuredDocument = ownerModel.getStructuredDocument();
    if (structuredDocument == null)
        return;
    IStructuredDocumentRegion flatNode = element.getStartStructuredDocumentRegion();
    if (flatNode == null)
        return;
    int contentOffset = flatNode.getEndOffset();
    if (data == null)
        // $NON-NLS-1$
        data = "";
    this.ignoreNotification = true;
    structuredDocument.replaceText(getRequesterC2H(), contentOffset + offset, length, data);
    this.ignoreNotification = false;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Aggregations

IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)439 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)174 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)99 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)87 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)70 List (java.util.List)40 BadLocationException (org.eclipse.jface.text.BadLocationException)39 ArrayList (java.util.ArrayList)38 Iterator (java.util.Iterator)35 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)35 Node (org.w3c.dom.Node)30 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)26 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)19 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)19 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)17 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)15 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)14 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)13 NodeList (org.w3c.dom.NodeList)13