Search in sources :

Example 46 with ITextRegion

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

the class JSONHoverProcessor method getHoverRegion.

@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
    if ((textViewer == null) || (textViewer.getDocument() == null)) {
        return null;
    }
    IStructuredDocumentRegion flatNode = ((IStructuredDocument) textViewer.getDocument()).getRegionAtCharacterOffset(offset);
    ITextRegion region = null;
    if (flatNode != null) {
        region = flatNode.getRegionAtCharacterOffset(offset);
    }
    if (region != null) {
        // only supply hoverhelp for object key, or simple JSON value
        String regionType = region.getType();
        if ((regionType == JSONRegionContexts.JSON_OBJECT_KEY) || JSONUtil.isJSONSimpleValue(regionType)) {
            try {
                // check if we are at whitespace before or after line
                IRegion line = textViewer.getDocument().getLineInformationOfOffset(offset);
                if ((offset > (line.getOffset())) && (offset < (line.getOffset() + line.getLength()))) {
                    // (whitespace after relevant info)
                    if (offset < flatNode.getTextEndOffset(region)) {
                        return new Region(flatNode.getStartOffset(region), region.getTextLength());
                    }
                }
            } catch (BadLocationException e) {
                Logger.logException(e);
            }
        }
    }
    return null;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IRegion(org.eclipse.jface.text.IRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 47 with ITextRegion

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

the class JSPELCompletionProposalComputer method computeCompletionProposals.

/**
 * @see org.eclipse.jst.jsp.ui.internal.contentassist.JSPJavaCompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
 */
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    ITextViewer viewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    // get results from JSP completion processor
    // 3 for the "get" at the beginning of the java proposal
    List results = new ArrayList(computeJavaCompletionProposals(viewer, documentPosition, 3));
    // get the function proposals for syntax like: ${ fn:| }
    IStructuredDocumentRegion flat = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
    if (flat != null) {
        ITextRegion cursorRegion = flat.getRegionAtCharacterOffset(documentPosition);
        String elText;
        int startOffset;
        // else can use flat region
        if (cursorRegion instanceof ITextRegionContainer) {
            ITextRegionContainer container = (ITextRegionContainer) cursorRegion;
            cursorRegion = container.getRegionAtCharacterOffset(documentPosition);
            elText = container.getText(cursorRegion);
            startOffset = container.getStartOffset(cursorRegion);
        } else {
            elText = flat.getText(cursorRegion);
            startOffset = flat.getStartOffset(cursorRegion);
        }
        // sanity check that we are actually in EL region
        if (cursorRegion.getType() == DOMJSPRegionContexts.JSP_EL_CONTENT) {
            String prefix = getPrefix(documentPosition - startOffset, elText);
            if (null != prefix) {
                List proposals = getFunctionProposals(prefix, viewer, documentPosition);
                results.addAll(proposals);
            }
        }
    }
    return results;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 48 with ITextRegion

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

the class JSPTranslator method translateRegionContainer.

/**
 * translates a region container (and XML JSP container, or <% JSP
 * container)
 *
 * This method should only be called in this class and for containers in
 * the primary structured document as all buffer appends will be direct
 */
protected void translateRegionContainer(ITextRegionCollection container, int JSPType) {
    ITextRegionCollection containerRegion = container;
    Iterator regions = containerRegion.getRegions().iterator();
    ITextRegion region = null;
    while (regions.hasNext()) {
        region = (ITextRegion) regions.next();
        String type = region.getType();
        // content assist was not showing up in JSP inside a javascript region
        if (DOMRegionContext.BLOCK_TEXT == type) {
            // check if it's nested jsp in a script tag...
            if (region instanceof ITextRegionContainer) {
                // pass in block text's container & iterator
                Iterator regionIterator = ((ITextRegionCollection) region).getRegions().iterator();
                translateJSPNode(region, regionIterator, type, EMBEDDED_JSP);
            } else {
                // be sure to combine all of the text from the block region
                StringBuffer fullText = new StringBuffer(containerRegion.getFullText(region));
                while (regions.hasNext()) {
                    region = (ITextRegion) regions.next();
                    // Do not immediately translate container regions, since they may use variables declared within the full text
                    if (region.getType() == DOMRegionContext.BLOCK_TEXT) {
                        fullText.append(containerRegion.getFullText(region));
                    } else {
                        // update type for when we exit if statement for BLOCK_TEXT
                        type = region.getType();
                        break;
                    }
                }
                /**
                 * LIMITATION - Normally the script content within a
                 * script tag is a single document region with a single
                 * BLOCK_TEXT text region within it. Any JSP scripting
                 * will be within its own region container (for the sake
                 * of keeping the scripting open/content/end as a group)
                 * also of BLOCK_TEXT. That ignores custom tags that might
                 * be in there, though, as they require proper scoping and
                 * variable declaration to be performed even though
                 * they're not proper nodes in the DOM. The only way to
                 * really do this is to treat the entire script content as
                 * JSP content on its own, akin to an included segment.
                 * Further complicating this solution is that tagdependent
                 * custom tags have their comment marked as BLOCK_TEXT as
                 * well, so there's no clear way to tell the two cases
                 * apart.
                 */
                // ////////////////////////////////////////////////////////////////////////////////
                // THIS EMBEDDED JSP TEXT WILL COME OUT LATER WHEN
                // PARTITIONING HAS
                // SUPPORT FOR NESTED XML-JSP
                // CMVC 241882
                decodeScriptBlock(fullText.toString(), containerRegion.getStartOffset());
            // ////////////////////////////////////////////////////////////////////////////////
            }
        }
        // }
        if (// <%, <%=, <%!, <%@
        type != null && isJSP(type)) {
            // translateJSPNode(region, regions, type, JSPType);
            translateJSPNode(containerRegion, regions, type, JSPType);
        } else if (type != null && (type == DOMRegionContext.XML_TAG_OPEN || type == DOMRegionContext.XML_END_TAG_OPEN)) {
            translateXMLNode(containerRegion, regions);
        } else if (type != null && type == DOMRegionContext.XML_CONTENT && region instanceof ITextRegionContainer) {
            // this case was put in to parse EL that is not in an attribute
            translateXMLContent((ITextRegionContainer) region);
        } else // the end tags of these regions are "translated" in a sense
        if (type == DOMJSPRegionContexts.JSP_DIRECTIVE_CLOSE || type == DOMJSPRegionContexts.JSP_CLOSE) {
            this.fCodeTranslated = true;
        }
    }
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) ITextRegionCollection(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)

Example 49 with ITextRegion

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

the class JSPTranslator method isEmptyTag.

private boolean isEmptyTag(ITextRegionCollection customTag) {
    ITextRegion lastRegion = customTag.getLastRegion();
    // custom tag is embedded
    if (customTag instanceof ITextRegionContainer) {
        ITextRegionList regions = customTag.getRegions();
        int size = customTag.getNumberOfRegions() - 1;
        while (size > 0 && !(DOMRegionContext.XML_EMPTY_TAG_CLOSE.equals(lastRegion.getType()) || DOMRegionContext.XML_TAG_NAME.equals(lastRegion.getType()) || DOMRegionContext.XML_TAG_CLOSE.equals(lastRegion.getType()))) {
            lastRegion = regions.get(--size);
        }
    }
    return DOMRegionContext.XML_EMPTY_TAG_CLOSE.equals(lastRegion.getType());
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint)

Example 50 with ITextRegion

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

the class JSPTranslator method addImportToMap.

/**
 * new text can be something like: "import java.lang.Object;\n"
 *
 * but the reference region could have been something like: <%@page
 * import="java.lang.Object, java.io.*, java.util.List"%>
 *
 * so the exact mapping has to be calculated carefully.
 *
 * isIndirect means that the import came from an included file (if true)
 *
 * @param importName
 * @param jspReferenceRegion
 */
private void addImportToMap(String importName, ITextRegionCollection jspReferenceRegion) {
    // massage text
    // String jspText = importName.substring(importName.indexOf("import ")
    // + 7, importName.indexOf(';'));
    // String jspText = importName.trim();
    // these positions will be updated below
    Position javaRange = new Position(fOffsetInUserImports + 7, 1);
    Position jspRange = new Position(jspReferenceRegion.getStart(), jspReferenceRegion.getLength());
    // calculate JSP range by finding "import" attribute
    ITextRegionList regions = jspReferenceRegion.getRegions();
    int size = regions.size();
    int start = -1;
    int length = -1;
    ITextRegion r = null;
    for (int i = 0; i < size; i++) {
        r = regions.get(i);
        if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
            if (jspReferenceRegion.getText(r).trim().equals("import")) {
                // get the attr value region
                if (size > i + 2) {
                    r = regions.get(i + 2);
                    if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
                        String jspImportText = jspReferenceRegion.getText(r);
                        // the position in question (in the JSP) is what
                        // is bracketed below
                        // includes whitespace
                        // <%@page import="java.lang.Object,[ java.io.* ],
                        // java.util.List"%>
                        // in the java file
                        // import [ java.io.* ];
                        start = jspImportText.indexOf(importName);
                        length = importName.length();
                        // safety, don't add to map if bad positioning
                        if (start == -1 || length < 1)
                            break;
                        // update jsp range
                        jspRange.setOffset(jspReferenceRegion.getStartOffset(r) + start);
                        jspRange.setLength(length);
                        // update java range
                        javaRange.setLength(length);
                        break;
                    }
                }
            }
    }
    // safety for bad ranges
    if (start != -1 && length > 1) {
        // put ranges in java -> jsp range map
        fImportRanges.put(javaRange, jspRange);
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) Position(org.eclipse.jface.text.Position) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint)

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