Search in sources :

Example 66 with IndexedRegion

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

the class StructuredSelectNextXMLHandler method getCursorIndexedRegion.

protected IndexedRegion getCursorIndexedRegion(IDocument document, ITextSelection textSelection) {
    int offset = textSelection.getOffset() + textSelection.getLength() - 1;
    if (offset < 0) {
        offset = 0;
    }
    IndexedRegion indexedRegion = null;
    indexedRegion = getIndexedRegion(document, offset);
    return indexedRegion;
}
Also used : IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 67 with IndexedRegion

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

the class DefaultXMLPartitionFormatter method format.

public TextEdit format(IStructuredModel model, int start, int length, XMLFormattingPreferences preferences) {
    setFormattingPreferences(preferences);
    TextEdit edit = new MultiTextEdit();
    IStructuredDocument document = model.getStructuredDocument();
    // get initial document region
    IStructuredDocumentRegion currentRegion = document.getRegionAtCharacterOffset(start);
    if (currentRegion != null) {
        int startOffset = currentRegion.getStartOffset();
        // get initial dom node
        IndexedRegion currentIndexedRegion = model.getIndexedRegion(startOffset);
        if (currentIndexedRegion instanceof IDOMNode) {
            // set up domRegion which will contain current region to be
            // formatted
            IDOMNode currentDOMNode = (IDOMNode) currentIndexedRegion;
            DOMRegion domRegion = new DOMRegion();
            domRegion.documentRegion = currentRegion;
            domRegion.domNode = currentDOMNode;
            XMLFormattingConstraints parentConstraints = getRegionConstraints(currentDOMNode);
            /* if the whitespace strategy is declared as default, get it from the preferences */
            if (XMLFormattingConstraints.DEFAULT.equals(parentConstraints.getWhitespaceStrategy()))
                parentConstraints.setWhitespaceStrategy(preferences.getElementWhitespaceStrategy());
            // TODO: initialize indentLevel
            // initialize available line width
            int lineWidth = getFormattingPreferences().getMaxLineWidth();
            try {
                IRegion lineInfo = document.getLineInformationOfOffset(startOffset);
                lineWidth = lineWidth - (startOffset - lineInfo.getOffset());
            } catch (BadLocationException e) {
                Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
            }
            parentConstraints.setAvailableLineWidth(lineWidth);
            // format all siblings (and their children) as long they
            // overlap with start/length
            Position formatRange = new Position(start, length);
            formatSiblings(edit, domRegion, parentConstraints, formatRange);
        }
    }
    return edit;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Position(org.eclipse.jface.text.Position) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 68 with IndexedRegion

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

the class AbstractContentAssistProcessor method computeCompletionProposals.

/**
 * CONTENT ASSIST STARTS HERE
 *
 * 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 textViewer
 * @param documentPosition -
 *            the cursor location within the document
 *
 * an array of ICompletionProposals
 */
public ICompletionProposal[] computeCompletionProposals(ITextViewer textViewer, int documentPosition) {
    setErrorMessage(null);
    fTextViewer = textViewer;
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition);
    Node node = (Node) treeNode;
    while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
        node = node.getParentNode();
    }
    IDOMNode xmlnode = (IDOMNode) node;
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
    String matchString = getMatchString(sdRegion, completionRegion, documentPosition);
    // Handle empty Documents
    if (completionRegion == null) {
        if (((treeNode == null) || (((Node) treeNode).getNodeType() == Node.DOCUMENT_NODE)) && (completionRegion == null) && ((xmlnode == null) || (xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) {
            IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(textViewer.getDocument());
            try {
                if (sModel != null) {
                    IDOMDocument docNode = ((IDOMModel) sModel).getDocument();
                    contentAssistRequest = newContentAssistRequest(docNode, docNode, sdRegion, completionRegion, documentPosition, 0, null);
                    addEmptyDocumentProposals(contentAssistRequest);
                }
            } finally {
                if (sModel != null) {
                    sModel.releaseFromRead();
                }
            }
            if (contentAssistRequest == null) {
                // $NON-NLS-1$
                Logger.logException(new IllegalStateException("problem getting model"));
                return new ICompletionProposal[0];
            }
            return contentAssistRequest.getCompletionProposals();
        }
        // MASSIVE ERROR CONDITION
        // $NON-NLS-1$
        Logger.logException(new IllegalStateException("completion region was null"));
        setErrorMessage(INTERNALERROR);
        // $NON-NLS-1$
        contentAssistRequest = newContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
        return contentAssistRequest.getCompletionProposals();
    }
    // catch documents where no region can be determined
    if ((xmlnode.getNodeType() == Node.DOCUMENT_NODE) && ((completionRegion == null) || (xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) {
        contentAssistRequest = computeStartDocumentProposals(documentPosition, matchString, completionRegion, (IDOMNode) treeNode, xmlnode);
        return contentAssistRequest.getCompletionProposals();
    }
    // compute normal proposals
    contentAssistRequest = computeCompletionProposals(documentPosition, matchString, completionRegion, (IDOMNode) treeNode, xmlnode);
    if (contentAssistRequest == null) {
        // $NON-NLS-1$
        contentAssistRequest = newContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
        if (Debug.displayWarnings) {
            // $NON-NLS-2$//$NON-NLS-1$
            System.out.println(UNKNOWN_CONTEXT + " " + completionRegion.getType() + "@" + documentPosition);
        }
        setErrorMessage(UNKNOWN_CONTEXT);
    }
    /* 
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=123892
		 * Only set this error message if nothing else was already set 
		 **/
    if (contentAssistRequest.getProposals().size() == 0 && getErrorMessage() == null) {
        setErrorMessage(UNKNOWN_CONTEXT);
    }
    return contentAssistRequest.getCompletionProposals();
}
Also used : 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) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 69 with IndexedRegion

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

the class DesignPageNavigationLocation method getSelection.

private ISelection getSelection() {
    ISelection selection = null;
    IStructuredModel model = null;
    final ITextEditor editor = getTextEditorPart();
    if (editor != null) {
        try {
            final IDocument document = getDocument(editor);
            if (document instanceof IStructuredDocument) {
                model = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) document);
                if (model != null) {
                    final IndexedRegion region = model.getIndexedRegion(fPosition.offset);
                    if (region != null) {
                        selection = new StructuredSelection(region);
                    }
                }
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    return selection;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISelection(org.eclipse.jface.viewers.ISelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDocument(org.eclipse.jface.text.IDocument)

Example 70 with IndexedRegion

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

the class RemoveBlockCommentActionXMLDelegate method processAction.

void processAction(IDocument document, ITextSelection textSelection) {
    IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document);
    if (model != null) {
        try {
            IndexedRegion selectionStartIndexedRegion = model.getIndexedRegion(textSelection.getOffset());
            IndexedRegion selectionEndIndexedRegion = model.getIndexedRegion(textSelection.getOffset() + textSelection.getLength());
            if ((selectionStartIndexedRegion == null) || (selectionEndIndexedRegion == null)) {
                return;
            }
            int openCommentOffset = selectionStartIndexedRegion.getStartOffset();
            int closeCommentOffset = selectionEndIndexedRegion.getEndOffset() - OPEN_COMMENT.length() - CLOSE_COMMENT.length();
            model.beginRecording(this, XMLUIMessages.RemoveBlockComment_tooltip);
            model.aboutToChangeModel();
            try {
                if (textSelection.getLength() == 0) {
                    if (selectionStartIndexedRegion instanceof CommentImpl) {
                        // $NON-NLS-1$
                        document.replace(openCommentOffset, OPEN_COMMENT.length(), "");
                        // $NON-NLS-1$
                        document.replace(closeCommentOffset, CLOSE_COMMENT.length(), "");
                    }
                } else {
                    if (selectionStartIndexedRegion instanceof CommentImpl) {
                        // $NON-NLS-1$
                        document.replace(openCommentOffset, OPEN_COMMENT.length(), "");
                    }
                    if (selectionEndIndexedRegion instanceof CommentImpl) {
                        // $NON-NLS-1$
                        document.replace(closeCommentOffset, CLOSE_COMMENT.length(), "");
                    }
                }
                removeOpenCloseComments(document, openCommentOffset + OPEN_COMMENT.length(), closeCommentOffset - openCommentOffset - CLOSE_COMMENT.length());
            } catch (BadLocationException e) {
                Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
            } finally {
                model.changedModel();
                model.endRecording(this);
            }
        } finally {
            model.releaseFromEdit();
        }
    }
}
Also used : CommentImpl(org.eclipse.wst.xml.core.internal.document.CommentImpl) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) 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