Search in sources :

Example 26 with IStructuredDocumentRegion

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

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

the class StructuredAutoEditStrategyHTML method getElementName.

/**
 * Get the element name that will be created by closing the start tag. Defaults
 * to the node's nodeName.
 * @param node the node that is being edited
 * @param offset the offset in the document where the start tag is closed
 * @return The element name of the tag
 */
private String getElementName(IDOMNode node, int offset) {
    String name = null;
    IStructuredDocumentRegion region = node.getFirstStructuredDocumentRegion();
    ITextRegion textRegion = region.getRegionAtCharacterOffset(offset);
    if (textRegion != null && textRegion.getType() == DOMRegionContext.XML_TAG_NAME) {
        int nameStart = region.getStartOffset(textRegion);
        String regionText = region.getText(textRegion);
        int length = offset - nameStart;
        if (length <= regionText.length())
            name = regionText.substring(0, length);
    }
    // Default to the node name
    if (name == null)
        name = node.getNodeName();
    return name;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 28 with IStructuredDocumentRegion

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

the class StructuredAutoEditStrategyHTML method smartInsertForEndTag.

private void smartInsertForEndTag(DocumentCommand command, IDocument document, IStructuredModel model) {
    try {
        if (command.text.equals("/") && document.getLength() >= 1 && document.get(command.offset - 1, 1).equals("<") && isPreferenceEnabled(HTMLUIPreferenceNames.TYPING_COMPLETE_END_TAGS)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(command.offset - 1)).getParentNode();
            if (isCommentNode(parentNode)) {
                // loop and find non comment node parent
                while (parentNode != null && isCommentNode(parentNode)) {
                    parentNode = (IDOMNode) parentNode.getParentNode();
                }
            }
            if (!isDocumentNode(parentNode)) {
                // only add end tag if one does not already exist or if
                // add '/' does not create one already
                IStructuredDocumentRegion endTagStructuredDocumentRegion = parentNode.getEndStructuredDocumentRegion();
                IDOMNode ancestor = parentNode;
                boolean smartInsertForEnd = false;
                if (endTagStructuredDocumentRegion != null) {
                    // Look for ancestors by the same name that are missing end tags
                    while ((ancestor = (IDOMNode) ancestor.getParentNode()) != null) {
                        if (ancestor.getEndStructuredDocumentRegion() == null && parentNode.getNodeName().equals(ancestor.getNodeName())) {
                            smartInsertForEnd = true;
                            break;
                        }
                    }
                }
                if (endTagStructuredDocumentRegion == null || smartInsertForEnd) {
                    StringBuffer toAdd = new StringBuffer(parentNode.getNodeName());
                    if (toAdd.length() > 0) {
                        // $NON-NLS-1$
                        toAdd.append(">");
                        String suffix = toAdd.toString();
                        if ((document.getLength() < command.offset + suffix.length()) || (!suffix.equals(document.get(command.offset, suffix.length())))) {
                            command.text += suffix;
                        }
                    }
                }
            }
        }
    } catch (BadLocationException e) {
        Logger.logException(e);
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 29 with IStructuredDocumentRegion

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

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

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