Search in sources :

Example 46 with IStructuredDocumentRegion

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

the class JSONFoldingStrategy method calcNewFoldPosition.

/**
 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 */
protected Position calcNewFoldPosition(IndexedRegion indexedRegion) {
    Position retPos = null;
    // only want to fold regions of the valid type and with a valid range
    if (indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) {
        IJSONNode node = (IJSONNode) indexedRegion;
        IStructuredDocumentRegion startRegion = node.getStartStructuredDocumentRegion();
        IStructuredDocumentRegion endRegion = node.getEndStructuredDocumentRegion();
        // don't fold it
        if (startRegion != null && endRegion != null) {
            if (startRegion.getEndOffset() == endRegion.getStartOffset())
                return null;
            if (endRegion.getEndOffset() >= startRegion.getStartOffset() && endRegion.getEndOffset() <= getDocument().getLength())
                retPos = new JSONObjectFoldingPosition(startRegion, endRegion);
        }
    // else if(startRegion != null && indexedRegion instanceof
    // CommentImpl) {
    // retPos = new JSONCommentFoldingPosition(startRegion);
    // }
    }
    return retPos;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) Position(org.eclipse.jface.text.Position) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 47 with IStructuredDocumentRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion 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 48 with IStructuredDocumentRegion

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

the class JSPELCompletionProposalComputer method computeCompletionProposals.

/**
 * @see org.eclipse.jst.jsp.ui.internal.contentassist.JSPJavaCompletionProposalComputer#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();
    // get results from JSP completion processor
    // 3 for the "get" at the beginning of the java proposal
    List results = new ArrayList(computeJavaCompletionProposals(viewer, documentPosition, 3));
    // get the function proposals for syntax like: ${ fn:| }
    IStructuredDocumentRegion flat = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
    if (flat != null) {
        ITextRegion cursorRegion = flat.getRegionAtCharacterOffset(documentPosition);
        String elText;
        int startOffset;
        // else can use flat region
        if (cursorRegion instanceof ITextRegionContainer) {
            ITextRegionContainer container = (ITextRegionContainer) cursorRegion;
            cursorRegion = container.getRegionAtCharacterOffset(documentPosition);
            elText = container.getText(cursorRegion);
            startOffset = container.getStartOffset(cursorRegion);
        } else {
            elText = flat.getText(cursorRegion);
            startOffset = flat.getStartOffset(cursorRegion);
        }
        // sanity check that we are actually in EL region
        if (cursorRegion.getType() == DOMJSPRegionContexts.JSP_EL_CONTENT) {
            String prefix = getPrefix(documentPosition - startOffset, elText);
            if (null != prefix) {
                List proposals = getFunctionProposals(prefix, viewer, documentPosition);
                results.addAll(proposals);
            }
        }
    }
    return results;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 49 with IStructuredDocumentRegion

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

the class StructuredAutoEditStrategyJSP method smartInsertForEndTag.

private void smartInsertForEndTag(DocumentCommand command, IDocument document, IStructuredModel model) {
    try {
        if (command.text.equals("/") && (document.getLength() >= 1) && document.get(command.offset - 1, 1).equals("<") && HTMLUIPlugin.getDefault().getPreferenceStore().getBoolean(HTMLUIPreferenceNames.TYPING_COMPLETE_END_TAGS)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(command.offset - 1)).getParentNode();
            if (isCommentNode(parentNode)) {
                // loop and find non comment node parent
                while ((parentNode != null) && isCommentNode(parentNode)) {
                    parentNode = (IDOMNode) parentNode.getParentNode();
                }
            }
            if (!isDocumentNode(parentNode)) {
                // only add end tag if one does not already exist or if
                // add '/' does not create one already
                IStructuredDocumentRegion endTagStructuredDocumentRegion = parentNode.getEndStructuredDocumentRegion();
                IDOMNode ancestor = parentNode;
                boolean smartInsertForEnd = false;
                if (endTagStructuredDocumentRegion != null) {
                    // Look for ancestors by the same name that are missing end tags
                    while ((ancestor = (IDOMNode) ancestor.getParentNode()) != null) {
                        if (ancestor.getEndStructuredDocumentRegion() == null && parentNode.getNodeName().equals(ancestor.getNodeName())) {
                            smartInsertForEnd = true;
                            break;
                        }
                    }
                }
                if (endTagStructuredDocumentRegion == null || smartInsertForEnd) {
                    StringBuffer toAdd = new StringBuffer(parentNode.getNodeName());
                    if (toAdd.length() > 0) {
                        // $NON-NLS-1$
                        toAdd.append(">");
                        String suffix = toAdd.toString();
                        if ((document.getLength() < command.offset + suffix.length()) || (!suffix.equals(document.get(command.offset, suffix.length())))) {
                            command.text += suffix;
                        }
                    }
                }
            }
        }
    } catch (BadLocationException e) {
        Logger.logException(e);
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 50 with IStructuredDocumentRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion 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)

Aggregations

IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)439 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)174 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)99 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)87 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)70 List (java.util.List)40 BadLocationException (org.eclipse.jface.text.BadLocationException)39 ArrayList (java.util.ArrayList)38 Iterator (java.util.Iterator)35 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)35 Node (org.w3c.dom.Node)30 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)26 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)19 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)19 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)17 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)15 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)14 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)13 NodeList (org.w3c.dom.NodeList)13