Search in sources :

Example 1 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project liferay-ide by liferay.

the class KaleoEdits method insertAt.

public static Element insertAt(Element newElement, int offset) {
    Document doc = newElement.getOwnerDocument();
    if (doc instanceof IDOMDocument) {
        IDOMDocument domDoc = (IDOMDocument) doc;
        IndexedRegion ir = domDoc.getModel().getIndexedRegion(offset);
        Node parent = ((Node) ir).getParentNode();
        if (ir instanceof Text) {
            Text txt = (Text) ir;
            String data = txt.getData();
            int dataSplitIndex = offset - ir.getStartOffset();
            String beforeText = data.substring(0, dataSplitIndex);
            String afterText = data.substring(dataSplitIndex);
            Text after = doc.createTextNode(afterText);
            Text before = doc.createTextNode(beforeText);
            parent.replaceChild(after, txt);
            parent.insertBefore(newElement, after);
            parent.insertBefore(before, newElement);
        } else if (ir instanceof Element) {
            ((Element) ir).appendChild(newElement);
        } else {
            throw new IllegalArgumentException();
        }
    } else {
        throw new IllegalArgumentException();
    }
    return newElement;
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Text(org.w3c.dom.Text) IDocument(org.eclipse.jface.text.IDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 2 with IndexedRegion

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

the class AbstractJSONCompletionProposalComputer method computeCompletionProposals.

@Override
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 <= 0 ? 0 : documentPosition - 1);
    IJSONNode node = (IJSONNode) treeNode;
    while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
        node = node.getParentNode();
    }
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
    // Fix completion region in case of JSON_OBJECT_CLOSE
    if (completionRegion != null && completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_CLOSE && documentPosition > 0) {
        completionRegion = getCompletionRegion(documentPosition, node);
    }
    String matchString = EMPTY;
    if (completionRegion != null) {
        if (isPairValue(context, node)) {
            try {
                String nodeText = getNodeText(node);
                int colonIndex = nodeText.indexOf(COLON);
                int offset = documentPosition - node.getStartOffset();
                if (colonIndex >= 0 && offset >= 0) {
                    String str = nodeText.substring(colonIndex + 1, offset);
                    // $NON-NLS-1$
                    str = str.replaceAll(",", BLANK);
                    matchString = str;
                }
            } catch (BadLocationException e) {
            // ignore
            }
        } else {
            matchString = getMatchString(sdRegion, completionRegion, documentPosition);
        }
    }
    // compute normal proposals
    contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IJSONNode) treeNode, node != null ? node.getParentNode() : null, context);
    if (contentAssistRequest == null) {
        contentAssistRequest = new ContentAssistRequest((IJSONNode) treeNode, node != null ? node.getParentNode() : null, sdRegion, completionRegion, documentPosition, 0, EMPTY);
        setErrorMessage(JSONUIMessages.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(JSONUIMessages.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) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 3 with IndexedRegion

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

the class StructureSelectAction method run.

public void run() {
    Region currentRegion = new Region(fViewer.getSelectedRange().x, fViewer.getSelectedRange().y);
    if (currentRegion.getLength() == fViewer.getDocument().getLength())
        return;
    IndexedRegion cursorIndexedRegion = getCursorIndexedRegion();
    if (cursorIndexedRegion instanceof Node) {
        Node cursorNode = (Node) cursorIndexedRegion;
        // 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)
                cursorIndexedRegion = (IndexedRegion) cursorNode;
        }
        Region cursorNodeRegion = new Region(cursorIndexedRegion.getStartOffset(), cursorIndexedRegion.getEndOffset() - cursorIndexedRegion.getStartOffset());
        Region newRegion = null;
        if (cursorNodeRegion.getOffset() >= currentRegion.getOffset() && cursorNodeRegion.getOffset() <= currentRegion.getOffset() + currentRegion.getLength() && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentRegion.getOffset() && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentRegion.getOffset() + currentRegion.getLength())
            newRegion = getNewSelectionRegion(cursorNode, currentRegion);
        else
            newRegion = cursorNodeRegion;
        if (newRegion != null) {
            fHistory.remember(currentRegion);
            try {
                fHistory.ignoreSelectionChanges();
                fEditor.selectAndReveal(newRegion.getOffset(), newRegion.getLength());
            } finally {
                fHistory.listenToSelectionChanges();
            }
        }
    }
}
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 4 with IndexedRegion

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

the class StructureSelectAction method getIndexedRegion.

protected IndexedRegion getIndexedRegion(int offset) {
    IndexedRegion indexedRegion = null;
    int lastOffset = offset;
    IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
    IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
    if (model != null) {
        try {
            indexedRegion = model.getIndexedRegion(lastOffset);
            while (indexedRegion == null && lastOffset >= 0) {
                lastOffset--;
                indexedRegion = model.getIndexedRegion(lastOffset);
            }
        } finally {
            model.releaseFromRead();
        }
    }
    return indexedRegion;
}
Also used : 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 5 with IndexedRegion

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

the class StructuredSelectActionDelegate method getIndexedRegion.

/**
 * This method will probably be removed and replaced by using new selection provider
 * @param document
 * @param offset
 * @return
 */
protected IndexedRegion getIndexedRegion(IDocument document, int offset) {
    IndexedRegion indexedRegion = null;
    int lastOffset = offset;
    IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
    if (model != null) {
        try {
            indexedRegion = model.getIndexedRegion(lastOffset);
            while (indexedRegion == null && lastOffset >= 0) {
                lastOffset--;
                indexedRegion = model.getIndexedRegion(lastOffset);
            }
        } finally {
            model.releaseFromRead();
        }
    }
    return indexedRegion;
}
Also used : IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

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