Search in sources :

Example 46 with CustomCompletionProposal

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

the class HTMLContentAssistProcessor method getHTMLTagProposal.

/**
 * @return ICompletionProposal
 */
private ICompletionProposal getHTMLTagProposal(ITextViewer viewer, int documentPosition) {
    IModelManager mm = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    ICompletionProposal result = null;
    try {
        if (mm != null) {
            model = mm.getExistingModelForRead(viewer.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);
                            CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, documentPosition, /* start pos */
                            0, /* replace length */
                            requiredName.length() + 2, /*
															 * cursor position
															 * after
															 * (relavtive to
															 * start)
															 */
                            HTMLEditorPluginImageHelper.getInstance().getImage(HTMLEditorPluginImages.IMG_OBJ_TAG_GENERIC), requiredName, null, null, XMLRelevanceConstants.R_TAG_NAME);
                            result = proposal;
                        }
                    }
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
    return result;
}
Also used : HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) 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 47 with CustomCompletionProposal

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

the class AbstractXMLElementContentAssistRequest method addTagNameProposals.

/**
 * Adds proposals for the XML_TAG_NAME region.
 * @param position
 */
protected void addTagNameProposals(int position) {
    Node ancestorNode = null;
    try {
        ancestorNode = XSLTXPathHelper.selectSingleNode(getNode(), XPATH_FIRST_XSLANCESTOR_NODE);
    } catch (TransformerException ex) {
    }
    if (ancestorNode == null) {
        return;
    }
    List<CMNode> cmnodes = null;
    if (ancestorNode.getNodeType() == Node.ELEMENT_NODE) {
        cmnodes = getAvailableChildElementDeclarations((Element) ancestorNode, 0);
        Iterator<CMNode> 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$
            // $NON-NLS-1$
            matchString = matchString.substring(1);
        }
        if (!nodeIterator.hasNext()) {
            return;
        }
        while (nodeIterator.hasNext()) {
            CMNode elementDecl = nodeIterator.next();
            if (elementDecl != null) {
                // only add proposals for the child element's that begin
                // with the matchstring
                String proposedText = null;
                proposedText = contentModel.getRequiredName(ancestorNode, elementDecl);
                int cursorAdjustment = proposedText.length();
                if (elementDecl instanceof CMElementDeclaration) {
                    CMElementDeclaration ed = (CMElementDeclaration) elementDecl;
                    if (ed.getContentType() == CMElementDeclaration.EMPTY) {
                        proposedText += contentModel.getStartTagClose(ancestorNode, ed);
                        cursorAdjustment = proposedText.length();
                    } else {
                        StringBuffer sb = new StringBuffer();
                        contentModel.generateTag(ancestorNode, 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);
                    }
                }
                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(getParent()), elementDecl);
                    CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, getReplacementBeginPosition(), getReplacementLength(), cursorAdjustment, image, contentModel.getRequiredName(getParent(), elementDecl), null, proposedInfo, XMLRelevanceConstants.R_TAG_NAME);
                    addProposal(proposal);
                }
            }
        }
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Element(org.w3c.dom.Element) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Image(org.eclipse.swt.graphics.Image) TransformerException(javax.xml.transform.TransformerException)

Example 48 with CustomCompletionProposal

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

the class HrefContentAssistRequest method getCompletionProposals.

/**
 * The main method that returns an array of proposals. Returns relative paths to files in the current project.
 *
 * @return ICompletionProposal[]
 * @see org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest#getCompletionProposals()
 */
@Override
public ArrayList<ICompletionProposal> getCompletionProposals() {
    pathList.clear();
    proposals.clear();
    try {
        String text = getText();
        String precedingText;
        int length = getCursorPosition() - getStartOffset();
        if (length > 0 && text.length() > length + 1)
            precedingText = text.substring(1, length);
        else
            // $NON-NLS-1$
            precedingText = "";
        IFile editorFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(getLocation()));
        editorFile.getProject().accept(new XSLFileResourceVisitor(editorFile, precedingText));
        Collections.sort(pathList, new PathComparator());
        for (IPath path : pathList) {
            String pathString = path.toString();
            CustomCompletionProposal proposal = new CustomCompletionProposal(pathString, getStartOffset() + 1, text.length() - 2, pathString.length(), XSLPluginImageHelper.getInstance().getImage(XSLPluginImages.IMG_XSL_FILE), pathString, null, null, 0, true);
            proposals.add(proposal);
        }
    } catch (CoreException e) {
        XSLUIPlugin.log(e);
    }
    return proposals;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)

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