Search in sources :

Example 11 with CustomCompletionProposal

use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.

the class AbstractContentAssistProcessor method addTagNameProposals.

protected void addTagNameProposals(ContentAssistRequest contentAssistRequest, int childPosition) {
    List cmnodes = null;
    Node parent = contentAssistRequest.getParent();
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    String error = null;
    String matchString = contentAssistRequest.getMatchString();
    if (parent.getNodeType() == Node.ELEMENT_NODE) {
        // retrieve the list of children
        // validActions = getAvailableChildrenAtIndex((Element) parent,
        // childPosition);
        cmnodes = getAvailableChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
        Iterator nodeIterator = cmnodes.iterator();
        // chop off any leading <'s and whitespace from the matchstring
        while ((matchString.length() > 0) && (Character.isWhitespace(matchString.charAt(0)) || beginsWith(matchString, "<"))) {
            // $NON-NLS-1$
            matchString = matchString.substring(1);
        }
        if (!nodeIterator.hasNext()) {
            error = NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() }));
        }
        while (nodeIterator.hasNext()) {
            CMNode elementDecl = (CMNode) nodeIterator.next();
            if (elementDecl != null) {
                // only add proposals for the child element's that begin
                // with the matchstring
                String proposedText = null;
                int cursorAdjustment = 0;
                // names are in list
                if (((node != null) && (node.getAttributes() != null) && (node.getAttributes().getLength() > 0) && attributeInList(node, parent, elementDecl)) || ((node.getNodeType() != Node.TEXT_NODE) && node.getFirstStructuredDocumentRegion().isEnded())) {
                    proposedText = getRequiredName(parent, elementDecl);
                    cursorAdjustment = proposedText.length();
                } else {
                    proposedText = getRequiredName(parent, elementDecl);
                    cursorAdjustment = proposedText.length();
                    if (elementDecl instanceof CMElementDeclaration) {
                        CMElementDeclaration ed = (CMElementDeclaration) elementDecl;
                        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
                        StringBuffer sb = new StringBuffer();
                        getContentGenerator().generateTag(parent, ed, sb);
                        // since it's a name proposal, assume '<' is
                        // already there
                        // only return the rest of the tag
                        proposedText = sb.toString().substring(1);
                        cursorAdjustment = getCursorPositionForProposedText(proposedText);
                    // cursorAdjustment = proposedText.length() +
                    // 1;
                    // proposedText += "></" +
                    // getRequiredName(parent, elementDecl) + ">";
                    // //$NON-NLS-2$//$NON-NLS-1$
                    }
                }
                if (beginsWith(proposedText, matchString)) {
                    Image image = CMImageUtil.getImage(elementDecl);
                    if (image == null) {
                        image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
                    }
                    String proposedInfo = getAdditionalInfo(getCMElementDeclaration(parent), elementDecl);
                    CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, getRequiredName(parent, elementDecl), null, proposedInfo, XMLRelevanceConstants.R_TAG_NAME);
                    contentAssistRequest.addProposal(proposal);
                }
            }
        }
        if (contentAssistRequest.getProposals().size() == 0) {
            if (error != null) {
                setErrorMessage(error);
            } else if ((contentAssistRequest.getMatchString() != null) && (contentAssistRequest.getMatchString().length() > 0)) {
                setErrorMessage(NLS.bind(XMLUIMessages.No_known_child_tag_names, (new Object[] { parent.getNodeName(), contentAssistRequest.getMatchString() })));
            // $NON-NLS-1$ = "No known child tag names of <{0}> begin with \"{1}\""
            } else {
                setErrorMessage(NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() })));
            }
        }
    } else if (parent.getNodeType() == Node.DOCUMENT_NODE) {
        List childElements = getAvailableRootChildren((Document) parent, childPosition);
        for (int i = 0; i < childElements.size(); i++) {
            CMNode ed = (CMNode) childElements.get(i);
            if (ed == null) {
                continue;
            }
            String proposedText = null;
            int cursorAdjustment = 0;
            if (ed instanceof CMElementDeclaration) {
                // proposedText = getRequiredName(parent, ed);
                StringBuffer sb = new StringBuffer();
                getContentGenerator().generateTag(parent, (CMElementDeclaration) ed, sb);
                // tag starts w/ '<', but we want to compare to name
                proposedText = sb.toString().substring(1);
                if (!beginsWith(proposedText, matchString)) {
                    continue;
                }
                cursorAdjustment = getCursorPositionForProposedText(proposedText);
                String proposedInfo = getAdditionalInfo(null, ed);
                Image image = CMImageUtil.getImage(ed);
                if (image == null) {
                    image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
                }
                CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, getRequiredName(parent, ed), null, proposedInfo, XMLRelevanceConstants.R_TAG_NAME);
                contentAssistRequest.addProposal(proposal);
            }
        }
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) Image(org.eclipse.swt.graphics.Image) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Iterator(java.util.Iterator) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Example 12 with CustomCompletionProposal

use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.

the class AbstractContentAssistProcessor method addEndTagProposals.

/**
 * Prompt for end tags to a non-empty Node that hasn't ended Handles these
 * cases: <br>
 * <tagOpen>| <br>
 * <tagOpen>< |<br>
 * <tagOpen></ |
 *
 * @param contentAssistRequest
 */
protected void addEndTagProposals(ContentAssistRequest contentAssistRequest) {
    IDOMNode node = (IDOMNode) contentAssistRequest.getParent();
    if (isCommentNode(node)) {
        // loop and find non comment node parent
        while ((node != null) && isCommentNode(node)) {
            node = (IDOMNode) node.getParentNode();
        }
    }
    // node is already closed
    if (node.isClosed()) {
        // loop and find non comment unclose node parent
        while ((node != null) && node.isClosed()) {
            node = (IDOMNode) node.getParentNode();
        }
    }
    // there were no unclosed tags
    if (node == null) {
        return;
    }
    // data to create a CustomCompletionProposal
    // $NON-NLS-1$
    String replaceText = node.getNodeName() + ">";
    int replaceBegin = contentAssistRequest.getReplacementBeginPosition();
    int replaceLength = contentAssistRequest.getReplacementLength();
    int cursorOffset = node.getNodeName().length() + 1;
    // $NON-NLS-1$
    String displayString = "";
    // $NON-NLS-1$
    String proposedInfo = "";
    Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
    setErrorMessage(null);
    boolean addProposal = false;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        // ////////////////////////////////////////////////////////////////////////////////////
        IStructuredDocument sDoc = (IStructuredDocument) fTextViewer.getDocument();
        IStructuredDocumentRegion xmlEndTagOpen = sDoc.getRegionAtCharacterOffset(contentAssistRequest.getReplacementBeginPosition());
        // skip backward to "<", "</", or the (unclosed) start tag, null
        // if not found
        // $NON-NLS-1$
        String type = "";
        while ((xmlEndTagOpen != null) && ((type = xmlEndTagOpen.getType()) != DOMRegionContext.XML_END_TAG_OPEN) && (type != DOMRegionContext.XML_TAG_CLOSE) && !needsEndTag(xmlEndTagOpen) && (type != DOMRegionContext.XML_TAG_OPEN)) {
            xmlEndTagOpen = xmlEndTagOpen.getPrevious();
        }
        if (xmlEndTagOpen == null) {
            return;
        }
        node = (IDOMNode) node.getModel().getIndexedRegion(xmlEndTagOpen.getStartOffset());
        node = (IDOMNode) node.getParentNode();
        if (isStartTag(xmlEndTagOpen)) {
            // |
            if (needsEndTag(xmlEndTagOpen)) {
                String tagName = getTagName(xmlEndTagOpen);
                xmlEndTagOpen.getTextEndOffset();
                replaceLength = 0;
                // $NON-NLS-1$ //$NON-NLS-2$ $NON-NLS-2$
                replaceText = "</" + tagName + ">";
                // replaceText = "</" + node.getNodeName() + ">";
                // //$NON-NLS-1$ $NON-NLS-2$
                cursorOffset = tagName.length() + 3;
                displayString = NLS.bind(XMLUIMessages.End_with__, (new Object[] { tagName }));
                addProposal = true;
            }
        } else if (type == DOMRegionContext.XML_END_TAG_OPEN) {
            // this is the case for: <tag> </ |
            // possibly <tag> </ |<anotherTag>
            // should only be replacing white space...
            replaceLength = (replaceBegin > xmlEndTagOpen.getTextEndOffset()) ? replaceBegin - xmlEndTagOpen.getTextEndOffset() : 0;
            // $NON-NLS-1$
            replaceText = node.getNodeName() + ">";
            cursorOffset = replaceText.length();
            replaceBegin = xmlEndTagOpen.getTextEndOffset();
            displayString = NLS.bind(XMLUIMessages.End_with_, (new Object[] { node.getNodeName() }));
            addProposal = true;
        } else if (type == DOMRegionContext.XML_TAG_OPEN) {
            // this is the case for: <tag> < |
            // $NON-NLS-1$ //$NON-NLS-2$ $NON-NLS-2$
            replaceText = "/" + node.getNodeName() + ">";
            cursorOffset = replaceText.length();
            // replaceText = "/" + node.getNodeName() + ">"; //$NON-NLS-1$
            // $NON-NLS-2$
            // should only be replacing white space...
            replaceLength = (replaceBegin > xmlEndTagOpen.getTextEndOffset()) ? replaceBegin - xmlEndTagOpen.getTextEndOffset() : 0;
            replaceBegin = xmlEndTagOpen.getTextEndOffset();
            // $NON-NLS-1$
            displayString = NLS.bind(XMLUIMessages.End_with_, (new Object[] { "/" + node.getNodeName() }));
            addProposal = true;
        }
    } else // getNodeValue() is null, put in a null check
    if ((node.getNodeValue() != null) && (node.getNodeValue().indexOf("</") != -1)) {
        // $NON-NLS-1$
        // the case where "</" is started, but the nodes comes in as a
        // text node (instead of element)
        // like this: <tag> </|
        Node parent = node.getParentNode();
        if ((parent != null) && (parent.getNodeType() != Node.DOCUMENT_NODE)) {
            // $NON-NLS-1$
            replaceText = parent.getNodeName() + ">";
            cursorOffset = replaceText.length();
            displayString = NLS.bind(XMLUIMessages.End_with__, (new Object[] { parent.getNodeName() }));
            setErrorMessage(null);
            addProposal = true;
        }
    } else // ////////////////////////////////////////////////////////////////////////////////////
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        setErrorMessage(UNKNOWN_CONTEXT);
    }
    if (addProposal == true) {
        CustomCompletionProposal proposal = new CustomCompletionProposal(replaceText, replaceBegin, replaceLength, cursorOffset, image, displayString, null, proposedInfo, XMLRelevanceConstants.R_END_TAG);
        contentAssistRequest.addProposal(proposal);
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Image(org.eclipse.swt.graphics.Image)

Example 13 with CustomCompletionProposal

use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.

the class XPathElementContentAssist method createXPathXMLProposals.

private void createXPathXMLProposals(Node ancestorNode, Iterator<CMNode> cmNodeIt) {
    while (cmNodeIt.hasNext()) {
        CMNode cmNode = cmNodeIt.next();
        String proposedText = getRequiredName(ancestorNode, cmNode);
        if (!(proposedText.contains("xsl:") || proposedText.contains("xslt:"))) {
            // $NON-NLS-1$ //$NON-NLS-2$
            int offset = getReplacementBeginPosition();
            Image image = getCMNodeImage(cmNode);
            int startLength = getCursorPosition() - offset;
            String additionalInfo = getInfoProvider().getInfo(cmNode);
            if (matchString.length() > 0) {
                if (proposedText.startsWith(matchString)) {
                    CustomCompletionProposal proposal = createProposal(proposedText, additionalInfo, offset, image, startLength);
                    addProposal(proposal);
                }
            } else {
                CustomCompletionProposal proposal = createProposal(proposedText, additionalInfo, offset, image, startLength);
                addProposal(proposal);
            }
        }
    }
}
Also used : CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Image(org.eclipse.swt.graphics.Image)

Example 14 with CustomCompletionProposal

use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.

the class CallTemplateContentAssistRequest method getCompletionProposals.

/**
 * (non-Javadoc)
 * @see org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest#getCompletionProposals()
 */
@Override
public ArrayList<ICompletionProposal> getCompletionProposals() {
    proposals.clear();
    IFile editorFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(getLocation()));
    StylesheetModel model = XSLCore.getInstance().getStylesheet(editorFile);
    List<Template> templates = model.getTemplates();
    for (Template template : templates) {
        XSLAttribute attribute = template.getAttribute(ATTR_NAME);
        if (attribute != null) {
            String proposalInfo = getAdditionalInfo(template);
            CustomCompletionProposal proposal = new CustomCompletionProposal(attribute.getValue(), getStartOffset() + 1, 0, attribute.getValue().length(), XSLPluginImageHelper.getInstance().getImage(XSLPluginImages.IMG_TEMPLATE), attribute.getValue(), null, proposalInfo, 0);
            addProposal(proposal);
        }
    }
    return getAllCompletionProposals();
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) XSLAttribute(org.eclipse.wst.xsl.core.model.XSLAttribute) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) StylesheetModel(org.eclipse.wst.xsl.core.model.StylesheetModel) Template(org.eclipse.wst.xsl.core.model.Template)

Example 15 with CustomCompletionProposal

use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.

the class ExcludeResultPrefixesContentAssist method getCompletionProposals.

/**
 * (non-Javadoc)
 * @see org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest#getCompletionProposals()
 */
@Override
public ArrayList<ICompletionProposal> getCompletionProposals() {
    proposals.clear();
    IDOMAttr attrNode = (IDOMAttr) ((IDOMElement) getNode()).getAttributeNode(EXCLUDE_RESULT_PREFIXES);
    String excludeResultPrefixes = attrNode.getValue();
    int offset = getCursorPosition();
    if (excludeResultPrefixes == null || excludeResultPrefixes.equals(DEFAULT)) {
        return getAllCompletionProposals();
    }
    // $NON-NLS-1$
    tokens = excludeResultPrefixes.split("\\s");
    if (tokens[0].equals("")) {
        // $NON-NLS-1$
        CustomCompletionProposal proposal = new CustomCompletionProposal(DEFAULT, offset, 0, DEFAULT.length(), XSLPluginImageHelper.getInstance().getImage(XSLPluginImages.IMG_PREFIX), DEFAULT, null, null, 0);
        addProposal(proposal);
    }
    Collection<NamespaceInfo> namespaces = this.getNamespaces((IDOMElement) node);
    for (NamespaceInfo namespace : namespaces) {
        if (includePrefix(namespace)) {
            CustomCompletionProposal proposal = new CustomCompletionProposal(namespace.prefix, offset, 0, namespace.prefix.length(), XSLPluginImageHelper.getInstance().getImage(XSLPluginImages.IMG_PREFIX), namespace.prefix, null, namespace.uri, 0);
            addProposal(proposal);
        }
    }
    return getAllCompletionProposals();
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)

Aggregations

CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)48 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)22 Image (org.eclipse.swt.graphics.Image)20 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)17 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)17 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)14 ArrayList (java.util.ArrayList)13 Iterator (java.util.Iterator)13 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)13 List (java.util.List)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)12 Node (org.w3c.dom.Node)11 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)9 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)9 NodeList (org.w3c.dom.NodeList)8 Document (org.w3c.dom.Document)7 Element (org.w3c.dom.Element)7 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)6 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)6