Search in sources :

Example 56 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class JSPContentAssistHelper method getResource.

/**
 * Returns project request is in
 *
 * @param request
 * @return {@link IResource} representing the project the given request was made in
 */
public static IResource getResource(ContentAssistRequest request) {
    IResource resource = null;
    String baselocation = null;
    if (request != null) {
        IStructuredDocumentRegion region = request.getDocumentRegion();
        if (region != null) {
            IDocument document = region.getParentDocument();
            IStructuredModel model = null;
            try {
                model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
                if (model != null) {
                    baselocation = model.getBaseLocation();
                }
            } finally {
                if (model != null)
                    model.releaseFromRead();
            }
        }
    }
    if (baselocation != null) {
        // copied from JSPTranslationAdapter#getJavaProject
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IPath filePath = new Path(baselocation);
        IFile file = null;
        if (filePath.segmentCount() > 1) {
            file = root.getFile(filePath);
        }
        if (file != null) {
            resource = file.getProject();
        }
    }
    return resource;
}
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) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IPath(org.eclipse.core.runtime.IPath) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Example 57 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class StructuredAutoEditStrategyJSP method customizeDocumentCommand.

public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
    if (!supportsSmartInsert(document)) {
        return;
    }
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
        if (model != null) {
            if (command.text != null) {
                smartInsertForEndTag(command, document, model);
                smartRemoveEndTag(command, document, model);
                if (command.text.equals("%") && isPreferenceEnabled(JSPUIPreferenceNames.TYPING_COMPLETE_SCRIPTLETS)) {
                    // $NON-NLS-1$
                    // scriptlet - add end %>
                    IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset);
                    if (node != null && prefixedWith(document, command.offset, "<") && !node.getSource().endsWith("%>")) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        // $NON-NLS-1$
                        command.text += " %>";
                        command.shiftsCaret = false;
                        command.caretOffset = command.offset + 1;
                        command.doit = false;
                    }
                }
                if (command.text.equals("{") && isPreferenceEnabled(JSPUIPreferenceNames.TYPING_COMPLETE_EL_BRACES)) {
                    // $NON-NLS-1$
                    IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset);
                    if (// $NON-NLS-1$ //$NON-NLS-2$
                    node != null && (prefixedWith(document, command.offset, "$") || prefixedWith(document, command.offset, "#")) && !node.getSource().endsWith("}")) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        // $NON-NLS-1$
                        command.text += " }";
                        command.shiftsCaret = false;
                        command.caretOffset = command.offset + 1;
                        command.doit = false;
                    }
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 58 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class AutoImportProposal method getInsertPosition.

/**
 * @param doc
 * @param isXml
 * @return position after <jsp:root> if xml, otherwise right before the document element
 */
private int getInsertPosition(IDocument doc, boolean isXml) {
    int pos = 0;
    IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
    try {
        if (sModel != null) {
            if (sModel instanceof IDOMModel) {
                IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
                /*
					 * document element must be sole Element child of Document
					 * to remain valid
					 */
                Node targetElement = null;
                if (isXml) {
                    targetElement = documentNode.getDocumentElement();
                }
                if (targetElement == null)
                    targetElement = getInsertNode(documentNode);
                if (targetElement != null) {
                    IStructuredDocumentRegion sdRegion = ((IDOMNode) targetElement).getFirstStructuredDocumentRegion();
                    if (isXml) {
                        /*
							 * document Element must be sole Element child of
							 * Document to remain valid, so insert after
							 */
                        pos = sdRegion.getEndOffset();
                        try {
                            while (pos < doc.getLength() && (doc.getChar(pos) == '\r' || doc.getChar(pos) == '\n')) {
                                pos++;
                            }
                        } catch (BadLocationException e) {
                        // not important, use pos as determined earlier
                        }
                    } else {
                        // insert before target element
                        pos = sdRegion.getStartOffset();
                    }
                } else {
                    pos = 0;
                }
            }
        }
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
    return pos;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 59 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class AutoImportProposal method isXmlFormat.

/**
 * @param doc
 * @return true if this document is xml-jsp syntax, otherwise false
 */
private boolean isXmlFormat(IDocument doc) {
    boolean isXml = false;
    IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
    try {
        if (sModel != null) {
            if (!isXml) {
                if (sModel instanceof IDOMModel) {
                    IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
                    Element docElement = documentNode.getDocumentElement();
                    // $NON-NLS-1$  //$NON-NLS-2$
                    isXml = docElement != null && ((docElement.getNodeName().equals("jsp:root")) || docElement.getAttributeNode("xmlns:jsp") != null || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() == null && ((IDOMNode) docElement).getEndStructuredDocumentRegion() == null)));
                }
            }
        }
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
    return isXml;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 60 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class CSSCompletionProposalComputer method computeCompletionProposals.

/**
 * @see org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
 */
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    ITextViewer viewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    IndexedRegion indexedNode = ContentAssistUtils.getNodeAt(viewer, documentPosition);
    IDOMNode xNode = null;
    IDOMNode parent = null;
    CSSProposalArranger arranger = null;
    // If there is a selected region, we'll need to replace the text
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    // if(indexedNode == null) return new ICompletionProposal[0];
    if (indexedNode instanceof IDOMNode) {
        xNode = (IDOMNode) indexedNode;
        parent = (IDOMNode) xNode.getParentNode();
    }
    // case
    if ((xNode != null) && xNode.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag w/out region
        IStructuredModel cssModel = getCSSModel(xNode);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = documentPosition;
            int pos = 0;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, selection.getLength(), (char) 0);
        }
    } else if ((parent != null) && parent.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag with a region
        // use the parent because that will be the <style> tag
        IStructuredModel cssModel = getCSSModel(parent);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = indexedNode.getStartOffset();
            int pos = documentPosition - offset;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, selection.getLength(), (char) 0);
        }
    } else if (indexedNode instanceof IDOMNode) {
        IDOMNode domNode = ((IDOMNode) indexedNode);
        // get model for node w/ style attribute
        IStructuredModel cssModel = getCSSModel(domNode);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int textRegionStartOffset = getTextRegionStartOffset(domNode, documentPosition);
            int pos = documentPosition - textRegionStartOffset;
            char quote = (char) 0;
            try {
                quote = context.getDocument().get(textRegionStartOffset, 1).charAt(0);
            } catch (BadLocationException e) {
                Logger.logException("error getting quote character", e);
            }
            // get css indexed region
            IndexedRegion cssIndexedNode = cssModel.getIndexedRegion(pos);
            if (cssIndexedNode == null) {
                cssIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            if (cssIndexedNode instanceof ICSSNode) {
                // inline style for a tag, not embedded
                arranger = new CSSProposalArranger(pos, (ICSSNode) cssIndexedNode, textRegionStartOffset, selection.getLength(), quote);
            }
        }
    } else if (indexedNode instanceof ICSSNode) {
        // when editing external CSS using CSS Designer, ICSSNode is passed.
        ICSSDocument cssdoc = ((ICSSNode) indexedNode).getOwnerDocument();
        if (cssdoc != null) {
            IStructuredModel cssModel = cssdoc.getModel();
            if (cssModel != null) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, 0, selection.getLength(), (char) 0);
                }
            }
        }
    } else if ((indexedNode == null) && ContentAssistUtils.isViewerEmpty(viewer)) {
        // the top of empty CSS Document
        IStructuredModel cssModel = null;
        try {
            cssModel = StructuredModelManager.getModelManager().getExistingModelForRead(viewer.getDocument());
            if (cssModel instanceof ICSSModel) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, 0, (char) 0);
                }
            }
        } finally {
            if (cssModel != null)
                cssModel.releaseFromRead();
        }
    }
    ICompletionProposal[] proposals = new ICompletionProposal[0];
    if (arranger != null) {
        proposals = arranger.getProposals();
        ICompletionProposal[] newfileproposals = new ICompletionProposal[0];
        ICompletionProposal[] anyproposals = new ICompletionProposal[0];
        // add end tag if parent is not closed
        ICompletionProposal endTag = XMLContentAssistUtilities.computeXMLEndTagProposal(viewer, documentPosition, indexedNode, HTML40Namespace.ElementName.STYLE, SharedXMLEditorPluginImageHelper.IMG_OBJ_TAG_GENERIC);
        // add the additional proposals
        int additionalLength = newfileproposals.length + anyproposals.length;
        additionalLength = (endTag != null) ? ++additionalLength : additionalLength;
        if (additionalLength > 0) {
            ICompletionProposal[] plusOnes = new ICompletionProposal[proposals.length + additionalLength];
            int appendPos = proposals.length;
            // add end tag proposal
            if (endTag != null) {
                System.arraycopy(proposals, 0, plusOnes, 1, proposals.length);
                plusOnes[0] = endTag;
                ++appendPos;
            } else {
                System.arraycopy(proposals, 0, plusOnes, 0, proposals.length);
            }
            // add items in newfileproposals
            for (int i = 0; i < newfileproposals.length; ++i) {
                plusOnes[appendPos + i] = newfileproposals[i];
            }
            // add items in anyproposals
            appendPos = appendPos + newfileproposals.length;
            for (int i = 0; i < anyproposals.length; ++i) {
                plusOnes[appendPos + i] = anyproposals[i];
            }
            proposals = plusOnes;
        }
    }
    return Arrays.asList(proposals);
}
Also used : ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection) ITextViewer(org.eclipse.jface.text.ITextViewer) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)482 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)110 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)102 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)81 IFile (org.eclipse.core.resources.IFile)75 IOException (java.io.IOException)69 CoreException (org.eclipse.core.runtime.CoreException)49 IDocument (org.eclipse.jface.text.IDocument)46 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)42 InputStream (java.io.InputStream)40 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)36 Path (org.eclipse.core.runtime.Path)35 BadLocationException (org.eclipse.jface.text.BadLocationException)34 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)32 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)32 IProject (org.eclipse.core.resources.IProject)31 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)31 IPath (org.eclipse.core.runtime.IPath)27 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Document (org.w3c.dom.Document)19