Search in sources :

Example 26 with ITextRegion

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

the class HTMLContentAssistProcessor method computeCompletionProposals.

/**
 * Return a list of proposed code completions based on the specified
 * location within the document that corresponds to the current cursor
 * position within the text-editor control.
 *
 * @param documentPosition
 *            a location within the document
 * @return an array of code-assist items
 */
public ICompletionProposal[] computeCompletionProposals(ITextViewer textViewer, int documentPosition) {
    fTemplateContexts.clear();
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition);
    IDOMNode node = (IDOMNode) treeNode;
    setErrorMessage(null);
    // check if it's in a comment node
    IStructuredDocument structuredDocument = (IStructuredDocument) textViewer.getDocument();
    IStructuredDocumentRegion fn = structuredDocument.getRegionAtCharacterOffset(documentPosition);
    if (fn != null && fn.getType() == DOMRegionContext.XML_COMMENT_TEXT && documentPosition != fn.getStartOffset()) {
        return new ICompletionProposal[0];
    }
    // if it's a </script> tag, bounce back to JS ca processor...
    if (fn != null && fn.getType() == DOMRegionContext.XML_TAG_NAME && documentPosition == fn.getStartOffset()) {
        ITextRegionList v = fn.getRegions();
        if (v.size() > 1) {
            // determine that it's a close tag
            if ((v.get(0)).getType() == DOMRegionContext.XML_END_TAG_OPEN) {
                Iterator it = v.iterator();
                ITextRegion region = null;
                // search for script tag name
                while (it.hasNext()) {
                    region = (ITextRegion) it.next();
                    if (fn.getText(region).equalsIgnoreCase("script")) {
                        // $NON-NLS-1$
                        IContentAssistProcessor jsProcessor = getJSContentAssistProcessor();
                        if (jsProcessor != null) {
                            return jsProcessor.computeCompletionProposals(textViewer, documentPosition);
                        }
                        return new ICompletionProposal[0];
                    }
                }
            }
        }
    }
    isXHTML = getXHTML(node);
    // force reload of content generator
    fGenerator = null;
    // handle blank HTML document case
    if (treeNode == null || isViewerEmpty(textViewer)) {
        // cursor is at the EOF
        ICompletionProposal htmlTagProposal = getHTMLTagProposal(textViewer, documentPosition);
        ICompletionProposal[] superResults = super.computeCompletionProposals(textViewer, documentPosition);
        if (superResults != null && superResults.length > 0 && htmlTagProposal != null) {
            ICompletionProposal[] blankHTMLDocResults = new ICompletionProposal[superResults.length + 1];
            blankHTMLDocResults[0] = htmlTagProposal;
            System.arraycopy(superResults, 0, blankHTMLDocResults, 1, superResults.length);
            return blankHTMLDocResults;
        }
    }
    if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
        // check embedded CSS proposals at the beginning of the STYLE end
        // tag
        Element element = (Element) node;
        String tagName = element.getTagName();
        if (tagName != null && tagName.equalsIgnoreCase(HTML40Namespace.ATTR_NAME_STYLE)) {
            // $NON-NLS-1$
            IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.getStartOffset() == documentPosition) {
                IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
                if (startStructuredDocumentRegion != null) {
                    int offset = startStructuredDocumentRegion.getEndOffset();
                    int pos = documentPosition - offset;
                    ICompletionProposal[] proposals = getCSSProposals(textViewer, pos, node, offset, (char) 0);
                    if (proposals != null)
                        return proposals;
                }
            }
        }
        // check inline CSS proposals
        // need to find attr region from sd region
        IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(textViewer, documentPosition);
        Iterator regions = sdRegion.getRegions().iterator();
        ITextRegion styleNameRegion = null;
        ITextRegion styleValueRegion = null;
        while (regions.hasNext()) {
            styleNameRegion = (ITextRegion) regions.next();
            if (styleNameRegion.getType().equals(DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) && sdRegion.getText(styleNameRegion).equalsIgnoreCase(HTML40Namespace.ATTR_NAME_STYLE)) {
                // the next region should be "="
                if (regions.hasNext()) {
                    // skip the "="
                    regions.next();
                    // next region should be attr value region
                    if (regions.hasNext()) {
                        styleValueRegion = (ITextRegion) regions.next();
                        break;
                    }
                }
            }
        }
        if (styleValueRegion != null) {
            int offset = sdRegion.getStartOffset(styleValueRegion);
            int end = sdRegion.getTextEndOffset(styleValueRegion);
            if (documentPosition >= offset && documentPosition <= end) {
                boolean askCSS = true;
                char quote = (char) 0;
                String text = sdRegion.getText(styleValueRegion);
                int length = (text != null ? text.length() : 0);
                if (length > 0) {
                    char firstChar = text.charAt(0);
                    if (firstChar == '"' || firstChar == '\'') {
                        if (documentPosition == offset) {
                            // before quote
                            askCSS = false;
                        } else {
                            offset++;
                            quote = firstChar;
                        }
                    }
                    if (documentPosition == end) {
                        if (length > 1 && text.charAt(length - 1) == quote) {
                            // after quote
                            askCSS = false;
                        }
                    }
                }
                if (askCSS) {
                    int pos = documentPosition - offset;
                    ICompletionProposal[] proposals = getCSSProposals(textViewer, pos, node, offset, quote);
                    if (proposals != null)
                        return proposals;
                }
            }
        }
    }
    return super.computeCompletionProposals(textViewer, documentPosition);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IContentAssistProcessor(org.eclipse.jface.text.contentassist.IContentAssistProcessor) Element(org.w3c.dom.Element) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 27 with ITextRegion

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

the class AnchorHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (textViewer != null && region != null) {
        IDocument document = textViewer.getDocument();
        if (document != null) {
            Node currentNode = getCurrentNode(document, region.getOffset());
            if (currentNode != null && currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) currentNode;
                IStructuredDocumentRegion documentRegion = ((IStructuredDocument) document).getRegionAtCharacterOffset(region.getOffset());
                ITextRegion textRegion = documentRegion.getRegionAtCharacterOffset(region.getOffset());
                ITextRegion nameRegion = null;
                ITextRegion valueRegion = null;
                String name = null;
                String value = null;
                /*
					 * http://bugs.eclipse.org/475680 - possible after last offset in the file or
					 * by a faulty implementation
					 */
                if (textRegion != null) {
                    if (DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE.equals(textRegion.getType())) {
                        ITextRegionList regions = documentRegion.getRegions();
                        /*
							 * Could use 2, but there needs to be the tag open
							 * and name regions
							 */
                        int index = regions.indexOf(textRegion);
                        if (index >= 4) {
                            nameRegion = regions.get(index - 2);
                            valueRegion = textRegion;
                            name = documentRegion.getText(nameRegion);
                            value = StringUtils.strip(documentRegion.getText(valueRegion));
                        }
                    } else if (DOMRegionContext.XML_TAG_ATTRIBUTE_NAME.equals(textRegion.getType())) {
                        ITextRegionList regions = documentRegion.getRegions();
                        int index = regions.indexOf(textRegion);
                        // minus 3 to leave equal and value regions
                        if (index <= regions.size() - 3) {
                            nameRegion = textRegion;
                            valueRegion = regions.get(index + 2);
                            name = documentRegion.getText(nameRegion);
                            value = StringUtils.strip(documentRegion.getText(valueRegion));
                        }
                    }
                }
                if (name != null && value != null) {
                    int idx = -1;
                    if (HTML40Namespace.ATTR_NAME_HREF.equalsIgnoreCase(name) && (idx = value.indexOf("#")) >= 0) {
                        // $NON-NLS-1$
                        String filename = value.substring(0, idx);
                        final String anchorName = idx < value.length() - 1 ? value.substring(idx + 1) : null;
                        if (anchorName != null) {
                            final IPath basePath = new Path(((IDOMNode) element).getModel().getBaseLocation());
                            if (basePath.segmentCount() > 1) {
                                if (filename.length() == 0) {
                                    filename = basePath.lastSegment();
                                }
                                final IPath resolved = ModuleCoreSupport.resolve(basePath, filename);
                                final IFile targetFile = ResourcesPlugin.getWorkspace().getRoot().getFile(resolved);
                                if (targetFile.isAccessible())
                                    return createHyperlinksToAnchorNamed(textViewer, createHyperlinkRegion(documentRegion, valueRegion), element, value, canShowMultipleHyperlinks);
                            }
                        }
                    }
                    if (HTML40Namespace.ATTR_NAME_NAME.equalsIgnoreCase(name) || HTML40Namespace.ATTR_NAME_ID.equalsIgnoreCase(name)) {
                        return createReferrerHyperlinks(textViewer, createHyperlinkRegion(documentRegion, valueRegion), element, value, canShowMultipleHyperlinks);
                    }
                }
            }
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDocument(org.eclipse.jface.text.IDocument)

Example 28 with ITextRegion

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

the class NamespaceValidator method validate.

public void validate(IndexedRegion node) {
    Element target = (Element) node;
    if (isXMLElement(target) && hasUnknownPrefix(target)) {
        IDOMElement e = (IDOMElement) target;
        if (!isValidPrefix(e.getPrefix(), target) && !e.isCommentTag()) {
            // report unknown tag error.
            Segment errorSeg = null;
            if (e.hasStartTag())
                errorSeg = FMUtil.getSegment(e, FMUtil.SEG_START_TAG);
            else if (e.hasEndTag())
                errorSeg = FMUtil.getSegment(e, FMUtil.SEG_END_TAG);
            if (errorSeg != null)
                reporter.report(new ErrorInfoImpl(UNDEFINED_NAME_ERROR, errorSeg, e));
        }
    }
    // (2) check prefix of each attr
    NamedNodeMap attrs = target.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node n = attrs.item(i);
        // some containers will contain languages that also use ':'
        if (!(n instanceof IDOMAttr) || ((IDOMAttr) n).getNameRegion() instanceof ITextRegionContainer)
            continue;
        IDOMAttr a = (IDOMAttr) n;
        String prefix = a.getPrefix();
        if ((prefix != null) && isUnknownAttr(a, target)) {
            // The attr has unknown prefix.  So, check it.
            if (!isValidPrefix(prefix, target)) {
                // report unknown attr error.
                ITextRegion r = a.getNameRegion();
                if (r == null)
                    continue;
                int a_offset = a.getNameRegionStartOffset();
                int a_length = a.getNameRegion().getLength();
                reporter.report(new ErrorInfoImpl(UNDEFINED_NAME_ERROR, new Segment(a_offset, a_length), a));
            }
        }
    }
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Node(org.w3c.dom.Node) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 29 with ITextRegion

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

the class SyntaxValidator method getTagName.

private static String getTagName(IStructuredDocumentRegion tag) {
    ITextRegionList regions = tag.getRegions();
    Iterator iter = regions.iterator();
    while (iter.hasNext()) {
        ITextRegion rgn = (ITextRegion) iter.next();
        if (rgn.getType() == DOMRegionContext.XML_TAG_NAME)
            return tag.getText(rgn);
    }
    // $NON-NLS-1$
    return "";
}
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)

Example 30 with ITextRegion

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

the class SyntaxValidator method isEndTagCorrupted.

private boolean isEndTagCorrupted(ElementInfo info) {
    ITextRegionList regions = info.endTag.getRegions();
    if (regions == null || regions.isEmpty())
        return false;
    Iterator iter = regions.iterator();
    while (iter.hasNext()) {
        ITextRegion rgn = (ITextRegion) iter.next();
        if (!isValidRegion(rgn))
            // found invalid region type.
            return true;
    }
    // all regions are valid.
    return false;
}
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

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