Search in sources :

Example 36 with CustomCompletionProposal

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

the class JSPContentAssistProcessor method computeCompletionProposals.

/**
 * @see AbstractContentAssistProcessor#computeCompletionProposals(int,
 *      String, ITextRegion, IDOMNode, IDOMNode)
 */
protected ContentAssistRequest computeCompletionProposals(int documentPosition, String matchString, ITextRegion completionRegion, IDOMNode treeNode, IDOMNode xmlnode) {
    ContentAssistRequest request = super.computeCompletionProposals(documentPosition, matchString, completionRegion, treeNode, xmlnode);
    IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(fTextViewer, documentPosition);
    Document doc = null;
    if (xmlnode != null) {
        if (xmlnode.getNodeType() == Node.DOCUMENT_NODE)
            doc = (Document) xmlnode;
        else
            doc = xmlnode.getOwnerDocument();
    }
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String[] directiveNames = { "page", "include", "taglib" };
    // suggest JSP Expression inside of XML comments
    if (completionRegion.getType() == DOMRegionContext.XML_COMMENT_TEXT && !isXMLFormat(doc)) {
        if (request == null)
            // $NON-NLS-1$
            request = newContentAssistRequest(treeNode, xmlnode, sdRegion, completionRegion, documentPosition, 0, "");
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        request.addProposal(new CustomCompletionProposal("<%=  %>", documentPosition, 0, 4, JSPEditorPluginImageHelper.getInstance().getImage(JSPEditorPluginImages.IMG_OBJ_TAG_GENERIC), "jsp:expression", null, "&lt;%= %&gt;", XMLRelevanceConstants.R_JSP));
    } else // JSP_DIRECTIVE_NAME
    if ((completionRegion.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_OPEN && documentPosition >= sdRegion.getTextEndOffset(completionRegion)) || (completionRegion.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME && documentPosition <= sdRegion.getTextEndOffset(completionRegion))) {
        if (completionRegion.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_OPEN) {
            if (request == null)
                request = newContentAssistRequest(xmlnode, xmlnode, sdRegion, completionRegion, documentPosition, 0, matchString);
            Iterator regions = sdRegion.getRegions().iterator();
            String nameString = null;
            int begin = request.getReplacementBeginPosition();
            int length = request.getReplacementLength();
            while (regions.hasNext()) {
                ITextRegion region = (ITextRegion) regions.next();
                if (region.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME) {
                    nameString = sdRegion.getText(region);
                    begin = sdRegion.getStartOffset(region);
                    length = region.getTextLength();
                    break;
                }
            }
            if (nameString == null)
                // $NON-NLS-1$
                nameString = "";
            for (int i = 0; i < directiveNames.length; i++) {
                if (directiveNames[i].startsWith(nameString) || documentPosition <= begin)
                    request.addProposal(new CustomCompletionProposal(directiveNames[i], begin, length, directiveNames[i].length(), JSPEditorPluginImageHelper.getInstance().getImage(JSPEditorPluginImages.IMG_OBJ_TAG_GENERIC), directiveNames[i], null, null, XMLRelevanceConstants.R_JSP));
            }
        } else {
            // by default, JSP_DIRECTIVE_NAME
            if (request == null)
                request = newContentAssistRequest(xmlnode, xmlnode, sdRegion, completionRegion, sdRegion.getStartOffset(completionRegion), completionRegion.getTextLength(), matchString);
            for (int i = 0; i < directiveNames.length; i++) {
                if (directiveNames[i].startsWith(matchString))
                    request.addProposal(new CustomCompletionProposal(directiveNames[i], request.getReplacementBeginPosition(), request.getReplacementLength(), directiveNames[i].length(), JSPEditorPluginImageHelper.getInstance().getImage(JSPEditorPluginImages.IMG_OBJ_TAG_GENERIC), directiveNames[i], null, null, XMLRelevanceConstants.R_JSP));
            }
        }
    } else if ((completionRegion.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME && documentPosition > sdRegion.getTextEndOffset(completionRegion)) || (completionRegion.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_CLOSE && documentPosition <= sdRegion.getStartOffset(completionRegion))) {
        if (request == null)
            request = computeAttributeProposals(documentPosition, matchString, completionRegion, treeNode, xmlnode);
        super.addTagCloseProposals(request);
    // CMVC 274033, this is being added for all <jsp:* tags
    // in addAttributeNameProposals(contentAssistRequest)
    // super.addAttributeNameProposals(request);
    } else // no name?: <%@ %>
    if (completionRegion.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_CLOSE && documentPosition <= sdRegion.getStartOffset(completionRegion)) {
        if (request != null)
            request = computeAttributeProposals(documentPosition, matchString, completionRegion, treeNode, xmlnode);
        Iterator regions = sdRegion.getRegions().iterator();
        String nameString = null;
        while (regions.hasNext()) {
            ITextRegion region = (ITextRegion) regions.next();
            if (region.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME) {
                nameString = sdRegion.getText(region);
                break;
            }
        }
        if (nameString == null) {
            for (int i = 0; i < directiveNames.length; i++) {
                request.addProposal(new CustomCompletionProposal(directiveNames[i], request.getReplacementBeginPosition(), request.getReplacementLength(), directiveNames[i].length(), JSPEditorPluginImageHelper.getInstance().getImage(JSPEditorPluginImages.IMG_OBJ_TAG_GENERIC), directiveNames[i], null, null, XMLRelevanceConstants.R_JSP));
            }
        }
    }
    // bug115927 use original document position for all/any region
    // templates
    addTemplates(request, TemplateContextTypeIdsJSP.ALL, documentPosition);
    return request;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) Iterator(java.util.Iterator) Document(org.w3c.dom.Document) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) IDocument(org.eclipse.jface.text.IDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ContentAssistRequest(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest)

Example 37 with CustomCompletionProposal

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

the class JSPContentAssistProcessor method getJSPJavaBeanProposals.

/*
	 * This method will return JSPJava Proposals that are relevant to any java
	 * beans that in scope at the documentPosition
	 * 
	 * TODO (pa) are taglib vars getting filtered?
	 * 
	 * @param viewer @param documentPosition @return ICompletionProposal[]
	 */
private ICompletionProposal[] getJSPJavaBeanProposals(ITextViewer viewer, int documentPosition) {
    ICompletionProposal[] regularJSPResults = getJSPJavaCompletionProposals(viewer, documentPosition);
    Vector filteredProposals = new Vector();
    ICompletionProposal[] finalResults = EMPTY_PROPOSAL_SET;
    for (int i = 0; i < regularJSPResults.length; i++) {
        ICompletionProposal test = regularJSPResults[i];
        // $NON-NLS-1$
        System.out.println("proposal > " + test.getDisplayString());
        // $NON-NLS-1$
        System.out.println("relevance > " + ((CustomCompletionProposal) test).getRelevance());
        if (isRelevanceAllowed(((CustomCompletionProposal) test).getRelevance())) {
            filteredProposals.add(test);
        }
    }
    if (filteredProposals.size() > 0) {
        finalResults = new ICompletionProposal[filteredProposals.size()];
        Iterator it = filteredProposals.iterator();
        int j = 0;
        while (it.hasNext()) {
            finalResults[j++] = (ICompletionProposal) it.next();
        }
    }
    return finalResults;
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) Iterator(java.util.Iterator) Vector(java.util.Vector)

Example 38 with CustomCompletionProposal

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

the class JSPELContentAssistProcessor method getFunctionProposals.

protected List getFunctionProposals(String prefix, StructuredTextViewer viewer, int offset) {
    TLDCMDocumentManager docMgr = TaglibController.getTLDCMDocumentManager(viewer.getDocument());
    ArrayList completionList = new ArrayList();
    if (docMgr == null)
        return null;
    Iterator taglibs = docMgr.getCMDocumentTrackers(offset).iterator();
    while (taglibs.hasNext()) {
        TaglibTracker tracker = (TaglibTracker) taglibs.next();
        if (tracker.getPrefix().equals(prefix)) {
            CMDocumentImpl doc = (CMDocumentImpl) tracker.getDocument();
            List functions = doc.getFunctions();
            for (Iterator it = functions.iterator(); it.hasNext(); ) {
                TLDFunction function = (TLDFunction) it.next();
                CustomCompletionProposal proposal = new // $NON-NLS-1$
                CustomCompletionProposal(// $NON-NLS-1$
                function.getName() + "()", offset, 0, function.getName().length() + 1, null, function.getName() + " - " + function.getSignature(), null, null, // $NON-NLS-1$
                1);
                completionList.add(proposal);
            }
        }
    }
    return completionList;
}
Also used : TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ArrayList(java.util.ArrayList) List(java.util.List) TLDFunction(org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDFunction) CMDocumentImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentImpl)

Example 39 with CustomCompletionProposal

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

the class JSPJavaContentAssistProcessor 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 viewer, int documentPosition) {
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, documentPosition);
    // get results from JSP completion processor
    fJspCompletionProcessor = getJspCompletionProcessor();
    ICompletionProposal[] results = fJspCompletionProcessor.computeCompletionProposals(viewer, documentPosition);
    fErrorMessage = fJspCompletionProcessor.getErrorMessage();
    if (results.length == 0 && (fErrorMessage == null || fErrorMessage.length() == 0)) {
        fErrorMessage = UNKNOWN_CONTEXT;
    }
    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);
        ICompletionProposal[] newResults = new ICompletionProposal[results.length + 1];
        System.arraycopy(results, 0, newResults, 0, results.length);
        newResults[results.length] = cdataProposal;
        results = newResults;
    }
    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) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 40 with CustomCompletionProposal

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

the class JSPPropertyCompletionProposalComputer method addBeanPropertyProposals.

/**
 * <p>Add bean property proposals to the given {@link ContentAssistRequest}</p>
 *
 * @param contentAssistRequest
 * @param node
 * @param isGetProperty
 * @param matchString
 */
private void addBeanPropertyProposals(ContentAssistRequest contentAssistRequest, IDOMNode node, boolean isGetProperty, String matchString) {
    if (((Element) node).hasAttribute(JSP11Namespace.ATTR_NAME_NAME)) {
        // assumes that the node is the [gs]etProperty tag
        String useBeanName = ((Element) node).getAttribute(JSP11Namespace.ATTR_NAME_NAME);
        // properties can only be provided if a class/type/beanName has been declared
        if (useBeanName.length() > 0) {
            NodeList useBeans = node.getOwnerDocument().getElementsByTagName(JSP11Namespace.ElementName.USEBEAN);
            if (useBeans != null) {
                String typeName = null;
                for (int j = 0; j < useBeans.getLength(); j++) {
                    if (useBeans.item(j).getNodeType() != Node.ELEMENT_NODE)
                        continue;
                    Element useBean = (Element) useBeans.item(j);
                    if (useBean instanceof IndexedRegion && ((IndexedRegion) useBean).getStartOffset() < node.getStartOffset()) {
                        if (useBeanName.equals(useBean.getAttribute(JSP11Namespace.ATTR_NAME_ID))) {
                            typeName = useBean.getAttribute(JSP11Namespace.ATTR_NAME_CLASS);
                            if (!useBean.hasAttribute(JSP11Namespace.ATTR_NAME_CLASS) || typeName.length() < 1) {
                                typeName = useBean.getAttribute(JSP11Namespace.ATTR_NAME_TYPE);
                            }
                            if (!useBean.hasAttribute(JSP11Namespace.ATTR_NAME_TYPE) || typeName.length() < 1) {
                                typeName = useBean.getAttribute(JSP11Namespace.ATTR_NAME_BEAN_NAME);
                            }
                        }
                    }
                }
                if (typeName != null && typeName.length() > 0) {
                    // find the class/type/beanName definition and obtain the list of properties
                    IBeanInfoProvider provider = new BeanInfoProvider();
                    IResource resource = JSPContentAssistHelper.getResource(contentAssistRequest);
                    IJavaPropertyDescriptor[] descriptors = provider.getRuntimeProperties(resource, typeName);
                    CustomCompletionProposal proposal = null;
                    // $NON-NLS-1$
                    String displayString = "";
                    for (int j = 0; j < descriptors.length; j++) {
                        IJavaPropertyDescriptor pd = descriptors[j];
                        // check whether it's get or set kinda property
                        if (pd.getReadable() && isGetProperty || pd.getWriteable() && !isGetProperty) {
                            // filter attr value name
                            if (matchString.length() == 0 || pd.getName().toLowerCase().startsWith(matchString.toLowerCase())) {
                                displayString = pd.getDisplayName();
                                if (pd.getDeclaredType() != null && pd.getDeclaredType().length() > 0)
                                    // $NON-NLS-1$
                                    displayString += " - " + pd.getDeclaredType();
                                proposal = new // $NON-NLS-1$ //$NON-NLS-2$
                                CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
                                "\"" + pd.getName() + "\"", contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), pd.getName().length() + 2, SharedXMLEditorPluginImageHelper.getImage(SharedXMLEditorPluginImageHelper.IMG_OBJ_ATTRIBUTE), displayString, null, pd.getDeclaredType(), XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                                contentAssistRequest.addProposal(proposal);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IResource(org.eclipse.core.resources.IResource)

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