Search in sources :

Example 11 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode 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 12 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode 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 13 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class HTMLFormatter method insertBreakAfter.

/**
 */
protected void insertBreakAfter(IDOMNode node, HTMLFormatContraints contraints) {
    if (node == null)
        return;
    if (node.getNodeType() == Node.TEXT_NODE)
        return;
    // don't insert break if node is on the last line
    int documentLength = node.getStructuredDocument().getLength();
    if (documentLength < 1 || (node.getEndOffset() >= (documentLength - 1)))
        return;
    Node parent = node.getParentNode();
    if (parent == null)
        return;
    Node next = node.getNextSibling();
    String spaces = null;
    if (next == null) {
        // If the parent is inline and its content is not formatted don't insert
        if (formattingUtil.isInline(parent) && !isContentFormatted(parent))
            return;
        // use parent indent for the end tag
        spaces = getBreakSpaces(parent);
    } else if (next.getNodeType() == Node.TEXT_NODE) {
        if (contraints != null && contraints.getFormatWithSiblingIndent()) {
            IDOMNode text = (IDOMNode) next;
            IStructuredFormatter formatter = HTMLFormatterFactory.getInstance().createFormatter(text, getFormatPreferences());
            if (formatter instanceof HTMLTextFormatter) {
                HTMLTextFormatter textFormatter = (HTMLTextFormatter) formatter;
                textFormatter.formatText(text, contraints, HTMLTextFormatter.FORMAT_HEAD);
            }
        }
        return;
    } else {
        spaces = getBreakSpaces(node);
    }
    if (spaces == null || spaces.length() == 0)
        return;
    replaceSource(node.getModel(), node.getEndOffset(), 0, spaces);
    setWidth(contraints, spaces);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IStructuredFormatter(org.eclipse.wst.sse.core.internal.format.IStructuredFormatter)

Example 14 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class HTMLFormatter method setWidth.

/**
 */
protected void setWidth(HTMLFormatContraints contraints, Node node) {
    if (contraints == null)
        return;
    if (node == null)
        return;
    IStructuredDocument structuredDocument = ((IDOMNode) node).getStructuredDocument();
    if (structuredDocument == null)
        // error
        return;
    if (!splitLines())
        return;
    int lineWidth = getLineWidth();
    if (lineWidth < 0)
        return;
    int offset = ((IDOMNode) node).getStartOffset();
    int line = structuredDocument.getLineOfOffset(offset);
    int lineOffset = 0;
    try {
        lineOffset = structuredDocument.getLineOffset(line);
    } catch (BadLocationException ex) {
        // error
        return;
    }
    if (lineOffset > offset)
        // error
        return;
    int availableWidth = lineWidth - (offset - lineOffset);
    if (availableWidth < 0)
        availableWidth = 0;
    contraints.setAvailableLineWidth(availableWidth);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 15 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class HTMLFormatter method getBreakSpaces.

/**
 */
protected String getBreakSpaces(Node node) {
    if (node == null)
        return null;
    StringBuffer buffer = new StringBuffer();
    String delim = ((IDOMNode) node).getModel().getStructuredDocument().getLineDelimiter();
    if (delim != null && delim.length() > 0)
        buffer.append(delim);
    String indent = getIndent();
    if (indent != null && indent.length() > 0) {
        for (Node parent = node.getParentNode(); parent != null; parent = parent.getParentNode()) {
            if (parent.getNodeType() != Node.ELEMENT_NODE)
                break;
            // ignore omitted tag
            if (((IDOMNode) parent).getStartStructuredDocumentRegion() == null)
                continue;
            IDOMElement element = (IDOMElement) parent;
            if (element.getPrefix() != null) {
                String localName = element.getLocalName();
                // special for html:html
                if (localName != null && !localName.equals(HTML_NAME)) {
                    buffer.append(indent);
                }
                continue;
            } else {
                String localName = element.getLocalName();
                if (HTML_NAME.equalsIgnoreCase(localName) || HEAD_NAME.equalsIgnoreCase(localName))
                    break;
            }
            CMElementDeclaration decl = getElementDeclaration(element);
            if (decl != null && decl.supports(HTMLCMProperties.SHOULD_INDENT_CHILD_SOURCE)) {
                boolean shouldIndent = isIdentable(node, parent);
                if (shouldIndent)
                    buffer.append(indent);
            }
        }
    }
    return buffer.toString();
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)250 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)91 Node (org.w3c.dom.Node)63 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)57 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)44 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)43 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)38 List (java.util.List)35 ArrayList (java.util.ArrayList)34 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)30 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)30 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)27 Element (org.w3c.dom.Element)27 NodeList (org.w3c.dom.NodeList)23 BadLocationException (org.eclipse.jface.text.BadLocationException)22 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)22 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)20 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)19 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)18 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)18