Search in sources :

Example 11 with ITextRegionList

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

the class CSSDocumentCharsetDetector method getType.

private String getType(IStructuredDocumentRegion node) {
    if (node == null)
        return null;
    ITextRegionList regions = node.getRegions();
    if (regions == null || regions.size() == 0)
        return null;
    ITextRegion region = regions.get(0);
    String result = region.getType();
    return result;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 12 with ITextRegionList

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

the class StructuredTextPartitionerForHTML method getScriptingPartitionType.

private String getScriptingPartitionType(IStructuredDocumentRegion coreNode) {
    String language = null;
    String type = null;
    String result = IHTMLPartitions.SCRIPT;
    IStructuredDocumentRegion node = coreNode;
    ITextRegion attrNameRegion = null;
    while (node != null && isValidScriptingRegionType(node.getType())) {
        node = node.getPrevious();
    }
    ITextRegionList regions = node.getRegions();
    if (regions.size() > 4 && regions.get(1).getType() == DOMRegionContext.XML_TAG_NAME) {
        ITextRegion potentialLanguageRegion = regions.get(1);
        String potentialLanguageString = node.getText(potentialLanguageRegion);
        if (potentialLanguageString.equalsIgnoreCase(HTML40Namespace.ElementName.SCRIPT)) {
            for (int i = 0; i < regions.size(); i++) {
                ITextRegion region = regions.get(i);
                String regionType = region.getType();
                if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
                    attrNameRegion = region;
                else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
                    String attrName = node.getText(attrNameRegion);
                    if (attrName.equalsIgnoreCase(HTML40Namespace.ATTR_NAME_LANGUAGE))
                        language = StringUtils.strip(node.getText(region));
                    else if (attrName.equalsIgnoreCase(HTML40Namespace.ATTR_NAME_TYPE)) {
                        type = StringUtils.strip(node.getText(region));
                        /*
							 * Avoid partition names built with MIME subtypes,
							 * e.g. type="text/javascript;e4x=1"
							 */
                        if (type != null) {
                            int index = type.indexOf(';');
                            if (index > 1)
                                type = type.substring(0, index);
                        }
                        break;
                    }
                    attrNameRegion = null;
                }
            }
        }
    }
    if (type != null)
        result = lookupScriptType(type);
    else if (language != null)
        result = lookupScriptLanguage(language);
    return result;
}
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)

Example 13 with ITextRegionList

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

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

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

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