Search in sources :

Example 71 with IndexedRegion

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

the class AddBlockCommentActionXMLDelegate 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) {
                return;
            }
            if ((selectionEndIndexedRegion == null) && (textSelection.getLength() > 0)) {
                selectionEndIndexedRegion = model.getIndexedRegion(textSelection.getOffset() + textSelection.getLength() - 1);
            }
            if (selectionEndIndexedRegion == null) {
                return;
            }
            int openCommentOffset = selectionStartIndexedRegion.getStartOffset();
            int closeCommentOffset = selectionEndIndexedRegion.getEndOffset() + OPEN_COMMENT.length();
            if ((textSelection.getLength() == 0) && (selectionStartIndexedRegion instanceof CommentImpl)) {
                return;
            }
            model.beginRecording(this, XMLUIMessages.AddBlockComment_tooltip);
            model.aboutToChangeModel();
            try {
                document.replace(openCommentOffset, 0, OPEN_COMMENT);
                document.replace(closeCommentOffset, 0, CLOSE_COMMENT);
                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)

Example 72 with IndexedRegion

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

the class AbstractXMLCompletionProposalComputer method computeCompletionProposals.

/**
 * <p>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.</p>
 *
 * @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 textViewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    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 = null;
            try {
                if (textViewer.getDocument() instanceof IStructuredDocument) {
                    sModel = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) textViewer.getDocument());
                }
                if (sModel != null) {
                    IDOMDocument docNode = ((IDOMModel) sModel).getDocument();
                    contentAssistRequest = new ContentAssistRequest(docNode, docNode, sdRegion, completionRegion, documentPosition, 0, null);
                    addEmptyDocumentProposals(contentAssistRequest, context);
                }
            } finally {
                if (sModel != null) {
                    sModel.releaseFromRead();
                }
            }
            if (contentAssistRequest == null) {
                // $NON-NLS-1$
                Logger.logException(new IllegalStateException("problem getting model"));
                return new ArrayList(0);
            }
            ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
            return (props != null) ? Arrays.asList(props) : new ArrayList(0);
        }
        // MASSIVE ERROR CONDITION
        // $NON-NLS-1$
        Logger.logException(new IllegalStateException("completion region was null"));
        setErrorMessage(XMLUIMessages.SEVERE_internal_error_occu_UI_);
        // $NON-NLS-1$
        contentAssistRequest = new ContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
        ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
        return (props != null) ? Arrays.asList(props) : new ArrayList(0);
    }
    // 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(matchString, completionRegion, (IDOMNode) treeNode, xmlnode, context);
        ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
        return (props != null) ? Arrays.asList(props) : new ArrayList(0);
    }
    // compute normal proposals
    contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IDOMNode) treeNode, xmlnode, context);
    if (contentAssistRequest == null) {
        // $NON-NLS-1$
        contentAssistRequest = new ContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
        setErrorMessage(XMLUIMessages.Content_Assist_not_availab_UI_);
    }
    /* 
		 * 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(XMLUIMessages.Content_Assist_not_availab_UI_);
    }
    ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
    return (props != null) ? Arrays.asList(props) : new ArrayList(0);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) 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) ArrayList(java.util.ArrayList) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextViewer(org.eclipse.jface.text.ITextViewer) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 73 with IndexedRegion

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

the class StructuredSelectEnclosingXMLHandler method getNewSelectionRegion.

protected Region getNewSelectionRegion(IndexedRegion indexedRegion, ITextSelection textSelection) {
    Region newRegion = null;
    if (indexedRegion instanceof Node) {
        Node cursorNode = (Node) indexedRegion;
        // use parent node for empty text node
        if ((cursorNode.getNodeType() == Node.TEXT_NODE) && (cursorNode.getNodeValue().trim().length() == 0)) {
            cursorNode = cursorNode.getParentNode();
            if (cursorNode instanceof IndexedRegion) {
                indexedRegion = (IndexedRegion) cursorNode;
            }
        }
        Region cursorNodeRegion = new Region(indexedRegion.getStartOffset(), indexedRegion.getEndOffset() - indexedRegion.getStartOffset());
        if ((cursorNodeRegion.getOffset() >= textSelection.getOffset()) && (cursorNodeRegion.getOffset() <= textSelection.getOffset() + textSelection.getLength()) && (cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= textSelection.getOffset()) && (cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= textSelection.getOffset() + textSelection.getLength())) {
            Node newNode = cursorNode.getParentNode();
            if (newNode instanceof IndexedRegion) {
                IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
                newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset());
            }
        } else {
            newRegion = cursorNodeRegion;
        }
    }
    return newRegion;
}
Also used : Node(org.w3c.dom.Node) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 74 with IndexedRegion

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

the class XMLHyperlinkDetector method getCurrentNode.

/**
 * Returns the node the cursor is currently on in the document. null if no
 * node is selected
 *
 * @param offset
 * @return Node either element, doctype, text, or null
 */
private Node getCurrentNode(IDocument document, int offset) {
    // get the current node at the offset (returns either: element,
    // doctype, text)
    IndexedRegion inode = null;
    IStructuredModel sModel = null;
    try {
        sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
        if (sModel != null) {
            inode = sModel.getIndexedRegion(offset);
            if (inode == null) {
                inode = sModel.getIndexedRegion(offset - 1);
            }
        }
    } finally {
        if (sModel != null) {
            sModel.releaseFromRead();
        }
    }
    if (inode instanceof Node) {
        return (Node) inode;
    }
    return null;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 75 with IndexedRegion

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

the class DelegatingSourceValidator method computeStartAndEndLocation.

/**
 * Calculates the "better" offsets.
 *
 * @param startOffset -
 *            the offset given by Xerces
 * @param errorMessage -
 *            the Xerces error Message
 * @param selectionStrategy -
 *            the selectionStrategy
 * @param document -
 *            the document
 * @return int[] - position 0 has the start offset of the squiggle range,
 *         position 1 has the endOffset
 */
/*
	 * The way the offsets is calculated is: - find the indexed region
	 * (element) closest to the given offset - if we are between two elements,
	 * choosing left or right element will depend on parameter 'errorSide' -
	 * based on the selectionStrategy choose the underlining strategy (eg
	 * START_TAG means underline the start tag of that element) - use
	 * information from nameOrValue and the DOM to get better offsets
	 * 
	 */
protected int[] computeStartAndEndLocation(int startOffset, String selectionStrategy, String errorSide, String nameOrValue, IDOMDocument document) {
    try {
        int[] startEndPositions = new int[2];
        IndexedRegion region = document.getModel().getIndexedRegion(startOffset);
        IndexedRegion prevRegion = document.getModel().getIndexedRegion(startOffset - 1);
        if (prevRegion != region) {
            // exactly where we need to be.
            if (ERROR_SIDE_LEFT.equals(errorSide)) {
                region = prevRegion;
            }
        }
        // the start of the region where the error was
        if (region != null) {
            startEndPositions[0] = region.getStartOffset();
            startEndPositions[1] = startEndPositions[0];
        } else {
            // this will message will not get added to the IReporter
            // since the length is 0
            startEndPositions[0] = 0;
            startEndPositions[1] = 0;
        }
        if (region instanceof Node) {
            Node node = (Node) region;
            if (START_TAG.equals(selectionStrategy)) {
                // underline the opening tag
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    startEndPositions[0] = element.getStartOffset() + 1;
                    startEndPositions[1] = startEndPositions[0] + element.getTagName().length();
                }
            } else if (END_TAG.equals(selectionStrategy)) {
                // underline the end tag
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    startEndPositions[0] = element.getEndStartOffset();
                    startEndPositions[1] = element.getEndOffset();
                }
            } else if (ATTRIBUTE_NAME.equals(selectionStrategy)) {
                // underline the attribute's name
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    IDOMNode attributeNode = (IDOMNode) (element.getAttributeNode(nameOrValue));
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getStartOffset();
                        startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
                    }
                }
            } else if (ATTRIBUTE_VALUE.equals(selectionStrategy)) {
                // underline the attribute's value
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    IDOMAttr attributeNode = (IDOMAttr) (element.getAttributeNode(nameOrValue));
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getValueRegionStartOffset();
                        String valueRegionText = attributeNode.getValueRegionText();
                        int valueRegionLength = valueRegionText == null ? 0 : valueRegionText.length();
                        startEndPositions[1] = startEndPositions[0] + valueRegionLength;
                    }
                }
            } else if (ALL_ATTRIBUTES.equals(selectionStrategy)) {
                // underline all attributes
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    NamedNodeMap attributes = element.getAttributes();
                    if (attributes != null) {
                        IDOMNode first = (IDOMNode) attributes.item(0);
                        IDOMNode last = (IDOMNode) attributes.item(attributes.getLength() - 1);
                        if ((first != null) && (last != null)) {
                            startEndPositions[0] = first.getStartOffset();
                            startEndPositions[1] = last.getEndOffset();
                        }
                    }
                }
            } else if (TEXT.equals(selectionStrategy)) {
                // underline the text between the tags
                if (node.getNodeType() == Node.TEXT_NODE) {
                    IDOMText textNode = (IDOMText) node;
                    int start = textNode.getStartOffset();
                    String value = textNode.getNodeValue();
                    int index = 0;
                    char curChar = value.charAt(index);
                    // whitespace:
                    while ((curChar == '\n') || (curChar == '\t') || (curChar == '\r') || (curChar == ' ')) {
                        curChar = value.charAt(index);
                        index++;
                    }
                    if (index > 0) {
                        index--;
                    }
                    start = start + index;
                    startEndPositions[0] = start;
                    startEndPositions[1] = start + value.trim().length();
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    Node child = element.getFirstChild();
                    if (child instanceof IDOMNode) {
                        IDOMNode xmlChild = ((IDOMNode) child);
                        startEndPositions[0] = xmlChild.getStartOffset();
                        startEndPositions[1] = xmlChild.getEndOffset();
                    }
                }
            } else if (FIRST_NON_WHITESPACE_TEXT.equals(selectionStrategy)) {
                // text node
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NodeList nodes = node.getChildNodes();
                    for (int i = 0; i < nodes.getLength(); i++) {
                        Node currentNode = nodes.item(i);
                        if (currentNode.getNodeType() == Node.TEXT_NODE) {
                            // TODO (Trung) I don't think we should call
                            // getNodeValue(), trim(), length()
                            // repeatedly.
                            // This is inefficient, to improve use local
                            // variables to store values.
                            IDOMText textNode = (IDOMText) currentNode;
                            if (textNode.getNodeValue().trim().length() > 0) {
                                String value = textNode.getNodeValue();
                                int index = 0;
                                int start = textNode.getStartOffset();
                                char curChar = value.charAt(index);
                                // skipping over whitespace:
                                while ((curChar == '\n') || (curChar == '\t') || (curChar == '\r') || (curChar == ' ')) {
                                    curChar = value.charAt(index);
                                    index++;
                                }
                                if (index > 0) {
                                    index--;
                                }
                                start = start + index;
                                startEndPositions[0] = start;
                                startEndPositions[1] = start + value.trim().length();
                                break;
                            }
                        }
                    }
                }
            } else if (TEXT_ENTITY_REFERENCE.equals(selectionStrategy)) {
                if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                    startEndPositions[0] = region.getStartOffset();
                    startEndPositions[1] = region.getEndOffset();
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    /*
						 * In this case the undeclared entity might be in one
						 * of the attribute values. Search through the
						 * attributes to find the range of the undeclared
						 * entity.
						 */
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String entity = "&" + nameOrValue + ";";
                    NamedNodeMap attributes = node.getAttributes();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        IDOMAttr attr = (IDOMAttr) attributes.item(i);
                        String nodeValue = attr.getNodeValue();
                        int index = nodeValue.indexOf(entity);
                        if (index != -1) {
                            startEndPositions[0] = attr.getValueRegionStartOffset() + index + 1;
                            startEndPositions[1] = startEndPositions[0] + entity.length();
                        }
                    }
                }
            } else if (VALUE_OF_ATTRIBUTE_WITH_GIVEN_VALUE.equals(selectionStrategy)) {
                // ATTRIBUTE_VALUE ?
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    // here we will search through all attributes for the
                    // one with the
                    // with the value we want:
                    // TODO (Trung) I see a potential problem here.
                    // What happens when there is another attribute having
                    // the same value
                    // with this attribute's buggy value ?
                    // Need to solve when time permits.
                    NamedNodeMap attributes = node.getAttributes();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        IDOMAttr attr = (IDOMAttr) attributes.item(i);
                        String nodeValue = attr.getNodeValue().trim();
                        if (nodeValue.equals(nameOrValue)) {
                            startEndPositions[0] = attr.getValueRegionStartOffset() + 1;
                            startEndPositions[1] = startEndPositions[0] + nodeValue.length();
                            break;
                        }
                    }
                }
            } else if (ATTRIBUTE_NAME_LAST.equals(selectionStrategy)) {
                // underline the last attribute's name
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NamedNodeMap attributeMap = node.getAttributes();
                    final int length = attributeMap.getLength();
                    Node tempNode = null;
                    Node attrNode = null;
                    for (int i = 0; i < length; i++) {
                        tempNode = attributeMap.item(i);
                        if (tempNode != null && tempNode.getNodeName().equals(nameOrValue)) {
                            attrNode = tempNode;
                        }
                    }
                    IDOMNode attributeNode = (IDOMNode) (attrNode);
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getStartOffset();
                        startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
                    }
                }
            }
        }
        return startEndPositions;
    } finally // catch (Exception e) { // e.printStackTrace();
    // }
    {
    }
// return null;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) NodeList(org.w3c.dom.NodeList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

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