Search in sources :

Example 36 with ITextRegionList

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

the class JSPJavaCompletionProposalComputer method isValidContext.

/**
 * <p>Determines if the context is a valid one for JSP Java proposals.
 * The default result is <code>true</code></p>
 *
 * @param context check this context to see if it is valid for JSP
 * Java proposals
 * @return <code>true</code> if the given context is a valid one for
 * JSP Java proposals, <code>false</code> otherwise.  <code>true</code>
 * is the default response if a specific case for <code>false</code> is
 * not found.
 */
private boolean isValidContext(CompletionProposalInvocationContext context) {
    ITextViewer viewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    String partitionType = getPartitionType(viewer, documentPosition);
    if (partitionType == IJSPPartitions.JSP_CONTENT_JAVA)
        return true;
    IStructuredDocument structuredDocument = (IStructuredDocument) viewer.getDocument();
    IStructuredDocumentRegion fn = structuredDocument.getRegionAtCharacterOffset(documentPosition);
    IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
    // check for xml-jsp tags...
    if (partitionType == IJSPPartitions.JSP_DIRECTIVE && fn != null) {
        IStructuredDocumentRegion possibleXMLJSP = ((fn.getType() == DOMRegionContext.XML_CONTENT) && fn.getPrevious() != null) ? fn.getPrevious() : fn;
        ITextRegionList regions = possibleXMLJSP.getRegions();
        if (regions.size() > 1) {
            // check bounds cases
            ITextRegion xmlOpenOrClose = regions.get(0);
            if (xmlOpenOrClose.getType() != DOMRegionContext.XML_TAG_OPEN && documentPosition != possibleXMLJSP.getStartOffset() && xmlOpenOrClose.getType() != DOMRegionContext.XML_END_TAG_OPEN && documentPosition <= possibleXMLJSP.getStartOffset()) {
                // possible xml-jsp
                ITextRegion nameRegion = regions.get(1);
                String name = possibleXMLJSP.getText(nameRegion);
                if (name.equals("jsp:scriptlet") || name.equals("jsp:expression") || name.equals("jsp:declaration")) {
                    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    return true;
                }
            }
        }
    }
    // check for XML-JSP in a <script> region
    if (partitionType == IJSPPartitions.JSP_CONTENT_JAVASCRIPT || partitionType == IHTMLPartitions.SCRIPT) {
        // fn should be block text
        IStructuredDocumentRegion decodedSDRegion = decodeScriptBlock(fn.getFullText());
        // decodedSDRegion.getEndOffset()));
        if (decodedSDRegion != null) {
            IStructuredDocumentRegion sdr = decodedSDRegion;
            while (sdr != null) {
                // sdr.getEndOffset()));
                if (sdr.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
                    if (documentPosition >= fn.getStartOffset() + sdr.getStartOffset() && documentPosition <= fn.getStartOffset() + sdr.getEndOffset()) {
                        return true;
                    }
                } else if (sdr.getType() == DOMRegionContext.XML_TAG_NAME) {
                    if (documentPosition > fn.getStartOffset() + sdr.getStartOffset() && documentPosition < fn.getStartOffset() + sdr.getEndOffset()) {
                        return false;
                    } else if (documentPosition == fn.getStartOffset() + sdr.getEndOffset() && sdr.getNext() != null && sdr.getNext().getType() == DOMJSPRegionContexts.JSP_CONTENT) {
                        // <jsp:scriptlet>| blah </jsp:scriptlet>
                        return true;
                    } else if (documentPosition == fn.getStartOffset() + sdr.getStartOffset() && sdr.getPrevious() != null && sdr.getPrevious().getType() == DOMRegionContext.XML_TAG_NAME) {
                        return true;
                    }
                }
                sdr = sdr.getNext();
            }
        }
    }
    // check special JSP delimiter cases
    if (fn != null && partitionType == IJSPPartitions.JSP_CONTENT_DELIMITER) {
        IStructuredDocumentRegion fnDelim = fn;
        // if it's a nested JSP region, need to get the correct
        // StructuredDocumentRegion
        // not sure why this check was there...
        // if (fnDelim.getType() == XMLRegionContext.BLOCK_TEXT) {
        Iterator blockRegions = fnDelim.getRegions().iterator();
        ITextRegion temp = null;
        ITextRegionContainer trc;
        while (blockRegions.hasNext()) {
            temp = (ITextRegion) blockRegions.next();
            // we hit a nested
            if (temp instanceof ITextRegionContainer) {
                trc = (ITextRegionContainer) temp;
                // it's in this region
                if (documentPosition >= trc.getStartOffset() && documentPosition < trc.getEndOffset()) {
                    Iterator nestedJSPRegions = trc.getRegions().iterator();
                    while (nestedJSPRegions.hasNext()) {
                        temp = (ITextRegion) nestedJSPRegions.next();
                        if (XMLContentAssistUtilities.isJSPOpenDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
                            // adapter
                            if (documentPosition > 0) {
                                partitionType = getPartitionType(viewer, documentPosition - 1);
                                break;
                            }
                        } else if (XMLContentAssistUtilities.isJSPCloseDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
                            // JSP content assist
                            return true;
                        }
                    }
                }
            }
        // }
        }
        // take care of XML-JSP delimter cases
        if (XMLContentAssistUtilities.isXMLJSPDelimiter(fnDelim)) {
            // since it's a delimiter, we know it's a ITextRegionContainer
            ITextRegion firstRegion = fnDelim.getRegions().get(0);
            if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN)) {
            // |<jsp:scriptlet> </jsp:scriptlet>
            // (pa) commented out so that we get regular behavior JSP
            // macros etc...
            // return getHTMLCompletionProposals(viewer,
            // documentPosition);
            } else if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_END_TAG_OPEN)) {
                // adapter get the proposals
                if (documentPosition > 0) {
                    String checkType = getPartitionType(viewer, documentPosition - 1);
                    if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
                        // check is failing for XML-JSP (region is not javascript...)
                        return true;
                    }
                    partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
                }
            } else if ((firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN) && documentPosition >= fnDelim.getEndOffset()) {
                // anything else inbetween
                return true;
            }
        } else if (XMLContentAssistUtilities.isJSPDelimiter(fnDelim)) {
            // the delimiter <%, <%=, <%!, ...
            if (XMLContentAssistUtilities.isJSPCloseDelimiter(fnDelim)) {
                if (documentPosition == fnDelim.getStartOffset()) {
                    // JAVASCRIPT adapter get the proposals
                    if (documentPosition > 0) {
                        String checkType = getPartitionType(viewer, documentPosition - 1);
                        if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
                            return true;
                        }
                        partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
                    }
                }
            } else if (XMLContentAssistUtilities.isJSPOpenDelimiter(fnDelim)) {
                // use embedded HTML results
                if (documentPosition == fnDelim.getEndOffset()) {
                    // it's at the EOF <%|
                    return true;
                }
            }
        }
    }
    // <!-- <% |%> -->
    if (fn != null && (fn.getType() == DOMRegionContext.XML_CDATA_TEXT || fn.getType() == DOMRegionContext.XML_COMMENT_TEXT)) {
        if (fn instanceof ITextRegionContainer) {
            Object[] cdataRegions = fn.getRegions().toArray();
            ITextRegion r = null;
            ITextRegion jspRegion = null;
            for (int i = 0; i < cdataRegions.length; i++) {
                r = (ITextRegion) cdataRegions[i];
                if (r instanceof ITextRegionContainer) {
                    // CDATA embedded container, or comment container
                    Object[] jspRegions = ((ITextRegionContainer) r).getRegions().toArray();
                    for (int j = 0; j < jspRegions.length; j++) {
                        jspRegion = (ITextRegion) jspRegions[j];
                        if (jspRegion.getType() == DOMJSPRegionContexts.JSP_CLOSE) {
                            if (sdRegion.getStartOffset(jspRegion) == documentPosition) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    // check if it's in an attribute value, if so, don't add CDATA
    // proposal
    ITextRegion attrContainer = (fn != null) ? fn.getRegionAtCharacterOffset(documentPosition) : null;
    if (attrContainer != null && attrContainer instanceof ITextRegionContainer) {
        if (attrContainer.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
            // test location of the cursor
            // return null if it's in the middle of an open/close delimiter
            Iterator attrRegions = ((ITextRegionContainer) attrContainer).getRegions().iterator();
            ITextRegion testRegion = null;
            while (attrRegions.hasNext()) {
                testRegion = (ITextRegion) attrRegions.next();
                // need to check for other valid attribute regions
                if (XMLContentAssistUtilities.isJSPOpenDelimiter(testRegion.getType())) {
                    if (!(((ITextRegionContainer) attrContainer).getEndOffset(testRegion) <= documentPosition))
                        return false;
                } else if (XMLContentAssistUtilities.isJSPCloseDelimiter(testRegion.getType())) {
                    if (!(((ITextRegionContainer) attrContainer).getStartOffset(testRegion) >= documentPosition))
                        return false;
                }
            }
            // TODO: handle non-Java code such as nested tags
            if (testRegion.getType().equals(DOMJSPRegionContexts.JSP_CONTENT)) {
                return true;
            }
            return false;
        }
    }
    return true;
}
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) Iterator(java.util.Iterator) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 37 with ITextRegionList

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

the class TaglibHelper method extractTagData.

/**
 * Returns all attribute -> value pairs for the tag in a Hashtable.
 *
 * @param customTag
 * @return
 */
private Hashtable extractTagData(ITextRegionCollection customTag) {
    Hashtable tagDataTable = new Hashtable();
    ITextRegionList regions = customTag.getRegions();
    ITextRegion r = null;
    // $NON-NLS-1$
    String attrName = "";
    // $NON-NLS-1$
    String attrValue = "";
    final int size = regions.size();
    for (int i = 2; i < size; i++) {
        r = regions.get(i);
        // check if attr name
        if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            attrName = customTag.getText(r);
            // check equals is next region
            if (size > ++i) {
                r = regions.get(i);
                if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS && size > ++i) {
                    // get attr value
                    r = regions.get(i);
                    final String type = r.getType();
                    if (type == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
                        // attributes in our document have quotes, so we
                        // need to strip them
                        attrValue = StringUtils.stripQuotes(customTag.getText(r));
                        tagDataTable.put(attrName, attrValue);
                    } else if (type == DOMJSPRegionContexts.JSP_TAG_ATTRIBUTE_VALUE_DQUOTE || type == DOMJSPRegionContexts.JSP_TAG_ATTRIBUTE_VALUE_SQUOTE) {
                        final StringBuffer buffer = new StringBuffer();
                        while (size > ++i && (r = regions.get(i)).getType() != type) {
                            buffer.append(customTag.getText(r));
                        }
                        tagDataTable.put(attrName, buffer.toString());
                    }
                }
            }
        }
    }
    // $NON-NLS-1$
    tagDataTable.put("jsp:id", customTag.getText(regions.get(1)) + "_" + customTag.getStartOffset());
    return tagDataTable;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) Hashtable(java.util.Hashtable) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 38 with ITextRegionList

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

the class TaglibHyperlinkDetector method getNameRegion.

private IRegion getNameRegion(ITextRegionCollection containerRegion) {
    ITextRegionList regions = containerRegion.getRegions();
    ITextRegion nameRegion = null;
    for (int i = 0; i < regions.size(); i++) {
        ITextRegion r = regions.get(i);
        if (r.getType() == DOMRegionContext.XML_TAG_NAME) {
            nameRegion = r;
            break;
        }
    }
    if (nameRegion != null)
        return new Region(containerRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
    return null;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 39 with ITextRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList 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 40 with ITextRegionList

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

the class MetaDataAdapter method getData.

/**
 */
private String getData(IStructuredDocumentRegion flatNode) {
    if (flatNode == null)
        return null;
    ITextRegionList regions = flatNode.getRegions();
    if (regions == null)
        return null;
    String data = null;
    Iterator e = regions.iterator();
    while (e.hasNext()) {
        ITextRegion region = (ITextRegion) e.next();
        String regionType = region.getType();
        if (isCommentText(regionType)) {
            data = flatNode.getText(region);
            break;
        }
    }
    if (data == null)
        return null;
    int length = data.length();
    int offset = 0;
    for (; offset < length; offset++) {
        char c = data.charAt(offset);
        if (c == '\r' || c == '\n') {
            offset++;
            break;
        }
    }
    for (; offset < length; offset++) {
        char c = data.charAt(offset);
        if (c != '\r' && c != '\n') {
            break;
        }
    }
    return data.substring(offset);
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator)

Aggregations

ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)193 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)171 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)74 Iterator (java.util.Iterator)46 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)27 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)24 ArrayList (java.util.ArrayList)21 List (java.util.List)18 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)16 StyleRange (org.eclipse.swt.custom.StyleRange)13 TextAttribute (org.eclipse.jface.text.TextAttribute)12 LocalizedMessage (org.eclipse.wst.validation.internal.operations.LocalizedMessage)12 TextRegionListImpl (org.eclipse.wst.sse.core.internal.text.TextRegionListImpl)10 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)9 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)8 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)8 DOMException (org.w3c.dom.DOMException)8 BadLocationException (org.eclipse.jface.text.BadLocationException)7 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)7 ITextRegionCollection (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)7