Search in sources :

Example 41 with CustomCompletionProposal

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

the class LibraryTagsCompletionProposalComputer method addAttributeValueProposals.

/**
 * @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
 */
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    if (!this.isXHTML) {
        IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
        ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
        if (mq != null) {
            CMDocument doc = mq.getCorrespondingCMDocument(node);
            // this shouldn't have to have the prefix coded in
            if (doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:")) {
                // $NON-NLS-1$
                return;
            }
        }
        // Find the attribute name for which this position should have a value
        IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
        ITextRegionList openRegions = open.getRegions();
        int i = openRegions.indexOf(contentAssistRequest.getRegion());
        if (i < 0) {
            return;
        }
        ITextRegion nameRegion = null;
        while (i >= 0) {
            nameRegion = openRegions.get(i--);
            if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
                break;
            }
        }
        // on an empty value, add all the JSP and taglib tags
        CMElementDeclaration elementDecl = AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(node);
        if (nameRegion != null && elementDecl != null) {
            String attributeName = open.getText(nameRegion);
            if (attributeName != null) {
                Node parent = contentAssistRequest.getParent();
                // ignore start quote in match string
                String matchString = contentAssistRequest.getMatchString().trim();
                if (matchString.startsWith("'") || matchString.startsWith("\"")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    matchString = matchString.substring(1);
                }
                // get all the proposals
                List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ALL);
                Iterator nodeIterator = additionalElements.iterator();
                // check each suggestion
                while (nodeIterator.hasNext()) {
                    CMNode additionalElementDecl = (CMNode) nodeIterator.next();
                    if (additionalElementDecl != null && additionalElementDecl instanceof CMElementDeclaration && validModelQueryNode(additionalElementDecl)) {
                        CMElementDeclaration ed = (CMElementDeclaration) additionalElementDecl;
                        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
                        StringBuffer sb = new StringBuffer();
                        getContentGenerator().generateTag(parent, ed, sb);
                        String proposedText = sb.toString();
                        // filter out any proposals that dont match matchString
                        if (beginsWith(proposedText, matchString)) {
                            // wrap with ' because JSP attributes are warped with "
                            // $NON-NLS-1$
                            proposedText = "'" + proposedText;
                            // don't want to risk injecting an extra
                            if (!(contentAssistRequest.getRegion() instanceof ITextRegionContainer)) {
                                // $NON-NLS-1$
                                proposedText += "'";
                            }
                            // get the image
                            Image image = CMImageUtil.getImage(elementDecl);
                            if (image == null) {
                                image = this.getGenericTagImage();
                            }
                            // create the proposal
                            int cursorAdjustment = getCursorPositionForProposedText(proposedText);
                            String proposedInfo = AbstractXMLModelQueryCompletionProposalComputer.getAdditionalInfo(AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(parent), elementDecl);
                            String tagname = getContentGenerator().getRequiredName(node, ed);
                            CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                            contentAssistRequest.addProposal(proposal);
                        }
                    }
                }
            }
        }
    }
}
Also used : JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNodeWrapper(org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper) 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) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) Image(org.eclipse.swt.graphics.Image) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) 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 42 with CustomCompletionProposal

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

the class HTMLTagsCompletionProposalComputer method addHTMLTagProposal.

/**
 * <p>adds HTML tag proposal for empty document</p>
 *
 * @param contentAssistRequest request to add proposal too
 * @param context context of the completion request
 */
private void addHTMLTagProposal(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    IStructuredModel model = null;
    try {
        if (context.getDocument() instanceof IStructuredDocument) {
            model = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) context.getDocument());
        }
        if (model != null) {
            IDOMDocument doc = ((IDOMModel) model).getDocument();
            ModelQuery mq = ModelQueryUtil.getModelQuery(doc);
            if (mq != null) {
                // XHTML requires lowercase tagname for lookup
                CMDocument correspondingCMDocument = mq.getCorrespondingCMDocument(doc);
                if (correspondingCMDocument != null) {
                    CMElementDeclaration htmlDecl = (CMElementDeclaration) correspondingCMDocument.getElements().getNamedItem(HTML40Namespace.ElementName.HTML.toLowerCase());
                    if (htmlDecl != null) {
                        StringBuffer proposedTextBuffer = new StringBuffer();
                        getContentGenerator().generateTag(doc, htmlDecl, proposedTextBuffer);
                        String proposedText = proposedTextBuffer.toString();
                        String requiredName = getContentGenerator().getRequiredName(doc, htmlDecl);
                        IStructuredDocumentRegion region = contentAssistRequest.getDocumentRegion();
                        if (region != null) {
                            if (region.getFirstRegion() != null && region.getFirstRegion().getType().equals(DOMRegionContext.XML_TAG_OPEN)) {
                                // in order to differentiate between content assist on
                                // completely empty document and the one with xml open tag
                                proposedText = proposedText.substring(1);
                            }
                        }
                        if (!beginsWith(proposedText, contentAssistRequest.getMatchString())) {
                            return;
                        }
                        int cursorAdjustment = getCursorPositionForProposedText(proposedText);
                        CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, HTMLEditorPluginImageHelper.getInstance().getImage(HTMLEditorPluginImages.IMG_OBJ_TAG_GENERIC), requiredName, null, null, XMLRelevanceConstants.R_TAG_NAME);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 43 with CustomCompletionProposal

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

the class HTMLTagsCompletionProposalComputer method addDocTypeProposal.

/**
 * @param contentAssistRequest
 * @param isXHTML
 */
private void addDocTypeProposal(ContentAssistRequest contentAssistRequest, boolean isXHTML) {
    // if a DocumentElement exists, use that for the root Element name
    // $NON-NLS-1$
    String rootname = "unspecified";
    if (contentAssistRequest.getNode().getOwnerDocument().getDocumentElement() != null) {
        rootname = contentAssistRequest.getNode().getOwnerDocument().getDocumentElement().getNodeName();
    }
    // decide which entry to use
    HTMLDocumentTypeEntry entry;
    if (isXHTML) {
        entry = HTMLDocumentTypeRegistry.getInstance().getXHTMLDefaultEntry();
    } else {
        entry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry();
    }
    // create the content assist string and proposal
    String proposedText = // $NON-NLS-1$ //$NON-NLS-2$
    "<!DOCTYPE " + rootname + " PUBLIC \"" + entry.getPublicId() + "\" \"" + entry.getSystemId() + // $NON-NLS-1$ //$NON-NLS-2$
    "\">";
    ICompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), 10, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DOCTYPE), // $NON-NLS-1$
    entry.getDisplayName() + " " + HTMLUIMessages.Expandable_label_document_type, null, null, XMLRelevanceConstants.R_DOCTYPE);
    contentAssistRequest.addProposal(proposal);
}
Also used : HTMLDocumentTypeEntry(org.eclipse.wst.html.core.internal.document.HTMLDocumentTypeEntry) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)

Example 44 with CustomCompletionProposal

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

the class TemplateNameAttributeContentAssist method getCompletionProposals.

/* (non-Javadoc)
	 * @see org.eclipse.wst.xsl.ui.internal.contentassist.AbstractXSLContentAssistRequest#getCompletionProposals()
	 */
@Override
public ArrayList<ICompletionProposal> getCompletionProposals() {
    proposals.clear();
    StylesheetModel model = getStylesheetModel();
    List<CallTemplate> templates = model.getCallTemplates();
    for (CallTemplate template : templates) {
        CustomCompletionProposal proposal = createProposal(template);
        addUniqueProposal(proposal);
    }
    return getAllCompletionProposals();
}
Also used : CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) StylesheetModel(org.eclipse.wst.xsl.core.model.StylesheetModel) CallTemplate(org.eclipse.wst.xsl.core.model.CallTemplate)

Example 45 with CustomCompletionProposal

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

the class JSPJavaCompletionProposalComputer method computeCompletionProposals.

/**
 * <p>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.</p>
 *
 * @see org.eclipse.wst.xml.ui.internal.contentassist.AbstractXMLCompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
 */
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    List results = new ArrayList(0);
    if (isValidContext(context)) {
        ITextViewer viewer = context.getViewer();
        int documentPosition = context.getInvocationOffset();
        IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, documentPosition);
        // get results from JSP completion processor
        results = computeJavaCompletionProposals(viewer, documentPosition, 0);
        IDOMNode xNode = null;
        IStructuredDocumentRegion flat = null;
        if (treeNode instanceof IDOMNode) {
            xNode = (IDOMNode) treeNode;
            flat = xNode.getFirstStructuredDocumentRegion();
            if (flat != null && flat.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
                flat = flat.getPrevious();
            }
        }
        // this is in case it's a <%@, it will be a region container...
        ITextRegion openRegion = null;
        if (flat != null && flat instanceof ITextRegionContainer) {
            ITextRegionList v = ((ITextRegionContainer) flat).getRegions();
            if (v.size() > 0)
                openRegion = v.get(0);
        }
        // ADD CDATA PROPOSAL IF IT'S AN XML-JSP TAG
        if (flat != null && flat.getType() != DOMJSPRegionContexts.JSP_SCRIPTLET_OPEN && flat.getType() != DOMJSPRegionContexts.JSP_DECLARATION_OPEN && flat.getType() != DOMJSPRegionContexts.JSP_EXPRESSION_OPEN && flat.getType() != DOMRegionContext.BLOCK_TEXT && (openRegion != null && openRegion.getType() != DOMJSPRegionContexts.JSP_DIRECTIVE_OPEN) && !inAttributeRegion(flat, documentPosition)) {
            // determine if cursor is before or after selected range
            int adjustedDocPosition = documentPosition;
            int realCaretPosition = viewer.getTextWidget().getCaretOffset();
            int selectionLength = viewer.getSelectedRange().y;
            if (documentPosition > realCaretPosition) {
                adjustedDocPosition -= selectionLength;
            }
            CustomCompletionProposal cdataProposal = createCDATAProposal(adjustedDocPosition, selectionLength);
            results.add(cdataProposal);
        }
    }
    return results;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ArrayList(java.util.ArrayList) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) List(java.util.List) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextViewer(org.eclipse.jface.text.ITextViewer)

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