Search in sources :

Example 31 with IndexedRegion

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

the class ContentAssistUtils method getNodeAt.

/**
 * Returns the closest IndexedRegion for the offset and viewer allowing
 * for differences between viewer offsets and model positions. note: this
 * method returns an IndexedRegion for read only
 *
 * @param viewer
 *            the viewer whose document is used to compute the proposals
 * @param documentOffset
 *            an offset within the document for which completions should
 *            be computed
 * @return an IndexedRegion
 */
public static IndexedRegion getNodeAt(ITextViewer viewer, int documentOffset) {
    if (viewer == null)
        return null;
    IndexedRegion node = null;
    IModelManager mm = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    if (mm != null)
        model = mm.getExistingModelForRead(viewer.getDocument());
    try {
        if (model != null) {
            int lastOffset = documentOffset;
            node = model.getIndexedRegion(documentOffset);
            while (node == null && lastOffset >= 0) {
                lastOffset--;
                node = model.getIndexedRegion(lastOffset);
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
    return node;
}
Also used : IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 32 with IndexedRegion

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

the class AbstractStructuredSelectHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    ITextEditor textEditor = null;
    if (editor instanceof ITextEditor)
        textEditor = (ITextEditor) editor;
    else {
        Object o = editor.getAdapter(ITextEditor.class);
        if (o != null)
            textEditor = (ITextEditor) o;
    }
    if (textEditor != null) {
        ISelection selection = textEditor.getSelectionProvider().getSelection();
        IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        // determine current text selection
        if (selection instanceof ITextSelection && document != null) {
            ITextSelection textSelection = (ITextSelection) selection;
            if (textSelection.getLength() < document.getLength()) {
                // get current indexed region
                IndexedRegion cursorIndexedRegion = getCursorIndexedRegion(document, textSelection);
                // determine new selection based on current indexed region
                Region newSelectionRegion = getNewSelectionRegion(cursorIndexedRegion, textSelection);
                // select new selection
                if (newSelectionRegion != null) {
                    SelectionHistory history = editor.getAdapter(SelectionHistory.class);
                    if (history != null) {
                        history.remember(new Region(textSelection.getOffset(), textSelection.getLength()));
                        try {
                            history.ignoreSelectionChanges();
                            textEditor.selectAndReveal(newSelectionRegion.getOffset(), newSelectionRegion.getLength());
                        } finally {
                            history.listenToSelectionChanges();
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISelection(org.eclipse.jface.viewers.ISelection) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IEditorPart(org.eclipse.ui.IEditorPart) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) SelectionHistory(org.eclipse.wst.sse.ui.internal.selection.SelectionHistory)

Example 33 with IndexedRegion

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

the class HTMLSourceValidator method getCorrespondingNode.

protected IndexedRegion getCorrespondingNode(IStructuredDocumentRegion sdRegion) {
    IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument);
    IndexedRegion indexedRegion = null;
    try {
        if (sModel != null)
            indexedRegion = sModel.getIndexedRegion(sdRegion.getStart());
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
    return indexedRegion;
}
Also used : IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 34 with IndexedRegion

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

the class StructuredSelectNextCSSActionDelegate method getNewSelectionRegion.

protected Region getNewSelectionRegion(IndexedRegion indexedRegion, ITextSelection textSelection) {
    Region newRegion = null;
    if (indexedRegion instanceof ICSSNode) {
        ICSSNode cursorNode = (ICSSNode) indexedRegion;
        Region cursorNodeRegion = new Region(indexedRegion.getStartOffset(), indexedRegion.getEndOffset() - indexedRegion.getStartOffset());
        int currentOffset = textSelection.getOffset();
        int currentEndOffset = currentOffset + textSelection.getLength();
        if (cursorNodeRegion.getOffset() >= currentOffset && cursorNodeRegion.getOffset() <= currentEndOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentEndOffset) {
            ICSSNode newNode = cursorNode.getNextSibling();
            if (newNode == null) {
                newNode = cursorNode.getParentNode();
                if (newNode instanceof IndexedRegion) {
                    IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
                    newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset());
                }
            } else {
                if (newNode instanceof IndexedRegion) {
                    IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
                    newRegion = new Region(currentOffset, newIndexedRegion.getEndOffset() - currentOffset);
                }
            }
        } else
            newRegion = cursorNodeRegion;
    }
    return newRegion;
}
Also used : Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 35 with IndexedRegion

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

IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)148 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)32 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)31 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)27 Node (org.w3c.dom.Node)27 Region (org.eclipse.jface.text.Region)21 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)21 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)21 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)20 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)17 ArrayList (java.util.ArrayList)11 List (java.util.List)11 ITextSelection (org.eclipse.jface.text.ITextSelection)11 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)8 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)8 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)8 Preferences (org.eclipse.core.runtime.Preferences)7 IDocument (org.eclipse.jface.text.IDocument)7