Search in sources :

Example 56 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project tmdm-studio-se by Talend.

the class XMLDocumentPartitioner method documentChanged2.

public IRegion documentChanged2(DocumentEvent e) {
    try {
        IDocument d = e.getDocument();
        Position[] category = d.getPositions(positionCategory);
        IRegion line = d.getLineInformationOfOffset(e.getOffset());
        int reparseStart = line.getOffset();
        int partitionStart = -1;
        String contentType = null;
        int first = d.computeIndexInCategory(positionCategory, reparseStart);
        if (first > 0) {
            TypedPosition partition = (TypedPosition) category[first - 1];
            if (partition.includes(reparseStart)) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                if (e.getOffset() == partition.getOffset() + partition.getLength()) {
                    reparseStart = partitionStart;
                }
                first--;
            } else if (reparseStart == e.getOffset() && reparseStart == partition.getOffset() + partition.getLength()) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                reparseStart = partitionStart;
                first--;
            } else {
                partitionStart = partition.getOffset() + partition.getLength();
                // $NON-NLS-1$
                contentType = "__dftl_partition_content_type";
            }
        }
        positionUpdater.update(e);
        for (int i = first; i < category.length; i++) {
            Position p = category[i];
            if (!p.isDeleted) {
                continue;
            }
            rememberDeletedOffset(e.getOffset());
            break;
        }
        category = d.getPositions(positionCategory);
        partitionScanner.setPartialRange(d, reparseStart, d.getLength() - reparseStart, contentType, partitionStart);
        int lastScannedPosition = reparseStart;
        for (IToken token = partitionScanner.nextToken(); !token.isEOF(); ) {
            contentType = getTokenContentType(token);
            if (!isSupportedContentType(contentType)) {
                token = partitionScanner.nextToken();
            } else {
                int start = partitionScanner.getTokenOffset();
                int length = partitionScanner.getTokenLength();
                lastScannedPosition = (start + length) - 1;
                for (; first < category.length; first++) {
                    TypedPosition p = (TypedPosition) category[first];
                    if (lastScannedPosition < p.offset + p.length && (!p.overlapsWith(start, length) || d.containsPosition(positionCategory, start, length) && contentType.equals(p.getType()))) {
                        break;
                    }
                    rememberRegion(p.offset, p.length);
                    d.removePosition(positionCategory, p);
                }
                if (d.containsPosition(positionCategory, start, length)) {
                    if (lastScannedPosition > e.getOffset()) {
                        return createRegion();
                    }
                    first++;
                } else {
                    try {
                        TypedPosition p = createPosition(start, length, contentType);
                        addPosition(d, positionCategory, p);
                        rememberRegion(start, length);
                    } catch (BadPositionCategoryException _ex) {
                    } catch (BadLocationException _ex) {
                    }
                }
                token = partitionScanner.nextToken();
            }
        }
        if (lastScannedPosition != reparseStart)
            lastScannedPosition++;
        for (first = d.computeIndexInCategory(positionCategory, lastScannedPosition); first < category.length; ) {
            TypedPosition p = (TypedPosition) category[first++];
            d.removePosition(positionCategory, p);
            rememberRegion(p.offset, p.length);
        }
    } catch (BadPositionCategoryException _ex) {
    } catch (BadLocationException _ex) {
    }
    return createRegion();
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) IToken(org.eclipse.jface.text.rules.IToken) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 57 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project tmdm-studio-se by Talend.

the class XMLTreeModel method createTree.

public void createTree(IDocument document) {
    this.fireDocumentAboutToBeChanged(this.getTree());
    IDocumentPartitioner partitioner = document.getDocumentPartitioner();
    String[] categories = ((IDocumentPartitionerExtension2) partitioner).getManagingPositionCategories();
    this.sourceViewer.getCodeTagContainersRegistry().clear();
    for (int iCat = 0; iCat < categories.length; iCat++) {
        String category = categories[iCat];
        Position[] positions = null;
        try {
            positions = document.getPositions(category);
            this.rootNode = new XMLRootNode(0, 0, IXMLPartitions.XML_TAG, document, sourceViewer);
            ElementsToRelate.currentNode = this.rootNode;
            ElementsToRelate.parentNode = null;
            for (int j = 0; j < positions.length; j++) {
                Position pos = positions[j];
                ElementsToRelate.node = (XMLNode) pos;
                ElementsToRelate.node.getChildren().clear();
                ElementsToRelate.node.setCorrespondingNode(null);
                ElementsToRelate.node.setSourceViewer(this.sourceViewer);
                if (ElementsToRelate.currentNode != null && ElementsToRelate.currentNode.isTag()) {
                    String nodeType = ElementsToRelate.node.getType();
                    RelateElementsCommand command = this.commands.get(nodeType);
                    if (command != null) {
                        command.excecute(this.rootNode, this.sourceViewer.getCodeTagContainersRegistry());
                    }
                }
            }
        } catch (BadPositionCategoryException e) {
            e.printStackTrace();
        }
    }
    this.fireDocumentChanged(this.getTree());
}
Also used : IDocumentPartitionerExtension2(org.eclipse.jface.text.IDocumentPartitionerExtension2) RelateElementsCommand(com.amalto.workbench.widgets.xmlviewer.model.commands.RelateElementsCommand) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException)

Example 58 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project webtools.sourceediting by eclipse.

the class SemanticHighlightingPresenter method updatePresentation.

/**
 * Invalidate the presentation of the positions based on the given added positions and the existing deleted positions.
 * Also unregisters the deleted positions from the document and patches the positions of this presenter.
 * <p>
 * NOTE: Indirectly called from background thread by UI runnable.
 * </p>
 * @param textPresentation the text presentation or <code>null</code>, if the presentation should computed in the UI thread
 * @param addedPositions the added positions
 * @param removedPositions the removed positions
 */
public void updatePresentation(TextPresentation textPresentation, HighlightedPosition[] addedPositions, HighlightedPosition[] removedPositions) {
    if (fSourceViewer == null)
        return;
    // TODO: reuse removed positions
    if (isCanceled())
        return;
    IDocument document = fSourceViewer.getDocument();
    if (document == null)
        return;
    String positionCategory = getPositionCategory();
    List removedPositionsList = Arrays.asList(removedPositions);
    try {
        synchronized (fPositionLock) {
            List oldPositions = fPositions;
            int newSize = Math.max(fPositions.size() + addedPositions.length - removedPositions.length, 10);
            /*
				 * The following loop is a kind of merge sort: it merges two List<Position>, each
				 * sorted by position.offset, into one new list. The first of the two is the
				 * previous list of positions (oldPositions), from which any deleted positions get
				 * removed on the fly. The second of two is the list of added positions. The result
				 * is stored in newPositions.
				 */
            List newPositions = new ArrayList(newSize);
            Position position = null;
            Position addedPosition = null;
            for (int i = 0, j = 0, n = oldPositions.size(), m = addedPositions.length; i < n || position != null || j < m || addedPosition != null; ) {
                // a) find the next non-deleted Position from the old list
                while (position == null && i < n) {
                    position = (Position) oldPositions.get(i++);
                    if (position.isDeleted() || contain(removedPositionsList, position)) {
                        document.removePosition(positionCategory, position);
                        position = null;
                    }
                }
                // b) find the next Position from the added list
                if (addedPosition == null && j < m) {
                    addedPosition = addedPositions[j++];
                    document.addPosition(positionCategory, addedPosition);
                }
                // c) merge: add the next of position/addedPosition with the lower offset
                if (position != null) {
                    if (addedPosition != null)
                        if (position.getOffset() <= addedPosition.getOffset()) {
                            newPositions.add(position);
                            position = null;
                        } else {
                            newPositions.add(addedPosition);
                            addedPosition = null;
                        }
                    else {
                        newPositions.add(position);
                        position = null;
                    }
                } else if (addedPosition != null) {
                    newPositions.add(addedPosition);
                    addedPosition = null;
                }
            }
            fPositions = newPositions;
            Collections.sort(fPositions, new Comparator() {

                public int compare(Object arg0, Object arg1) {
                    Position p1 = (Position) arg0;
                    Position p2 = (Position) arg1;
                    return p1.offset - p2.offset;
                }
            });
        }
    } catch (BadPositionCategoryException e) {
        // Should not happen
        Logger.logException(e);
    } catch (BadLocationException e) {
        // Should not happen
        Logger.logException(e);
    }
    if (textPresentation != null)
        fSourceViewer.changeTextPresentation(textPresentation, false);
    else
        fSourceViewer.invalidateTextPresentation();
}
Also used : HighlightedPosition(org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ArrayList(java.util.ArrayList) List(java.util.List) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) Comparator(java.util.Comparator)

Example 59 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project webtools.sourceediting by eclipse.

the class BasicStructuredDocument method clearReadOnly.

public void clearReadOnly(int startOffset, int length) {
    // remove any that overlap.
    try {
        Position[] positions = getPositions(READ_ONLY_REGIONS_CATEGORY);
        for (int i = 0; i < positions.length; i++) {
            Position position = positions[i];
            if (position.overlapsWith(startOffset, length)) {
                String effectedText = this.get(startOffset, length);
                // fDocumentEvent = new DocumentEvent(this, startOffset,
                // length, effectedText);
                fireReadOnlyAboutToBeChanged();
                position.delete();
                NoChangeEvent noChangeEvent = new NoChangeEvent(this, null, effectedText, startOffset, length);
                noChangeEvent.reason = NoChangeEvent.READ_ONLY_STATE_CHANGE;
                fireReadOnlyStructuredDocumentEvent(noChangeEvent);
            }
        }
    } catch (BadPositionCategoryException e) {
    // just means no readonly regions been defined yet
    // so nothing to do.
    }
}
Also used : Position(org.eclipse.jface.text.Position) NoChangeEvent(org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException)

Example 60 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project webtools.sourceediting by eclipse.

the class BasicStructuredDocument method makeReadOnly.

public void makeReadOnly(int startOffset, int length, boolean canInsertBefore, boolean canInsertAfter) {
    // so we'll ignore those requests
    if (length <= 0)
        return;
    String affectedText = this.get(startOffset, length);
    // a document event for "read only" change ... must
    // be followed by "no change" structuredDocument event
    // fDocumentEvent = new DocumentEvent(this, startOffset, length,
    // affectedText);
    fireReadOnlyAboutToBeChanged();
    // if (containsReadOnly(startOffset, length)) {
    // adjustReadOnlyRegions(startOffset, length);
    // } else {
    // we can blindly add category, since no harm done if already
    // exists.
    addPositionCategory(READ_ONLY_REGIONS_CATEGORY);
    Position newPosition = new ReadOnlyPosition(startOffset, length, canInsertBefore);
    try {
        addPosition(READ_ONLY_REGIONS_CATEGORY, newPosition);
        // FIXME: need to change API to pass in requester, so this event
        // can be
        // created correctly, instead of using null.
        NoChangeEvent noChangeEvent = new NoChangeEvent(this, null, affectedText, startOffset, length);
        noChangeEvent.reason = NoChangeEvent.READ_ONLY_STATE_CHANGE;
        fireReadOnlyStructuredDocumentEvent(noChangeEvent);
    } catch (BadLocationException e) {
        // for now, log and ignore. Perhaps later we
        // could adjust to handle some cases?
        // $NON-NLS-1$ //$NON-NLS-2$
        Logger.logException(("could not create readonly region at " + startOffset + " to " + length), e);
    } catch (BadPositionCategoryException e) {
        // should never occur, since we add category
        Logger.logException(e);
    }
}
Also used : Position(org.eclipse.jface.text.Position) NoChangeEvent(org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)66 Position (org.eclipse.jface.text.Position)46 BadLocationException (org.eclipse.jface.text.BadLocationException)38 TypedPosition (org.eclipse.jface.text.TypedPosition)27 IDocument (org.eclipse.jface.text.IDocument)21 ArrayList (java.util.ArrayList)14 ITypedRegion (org.eclipse.jface.text.ITypedRegion)14 TypedRegion (org.eclipse.jface.text.TypedRegion)12 IRegion (org.eclipse.jface.text.IRegion)8 List (java.util.List)6 Point (org.eclipse.swt.graphics.Point)5 IToken (org.eclipse.jface.text.rules.IToken)4 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)3 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)3 Region (org.eclipse.jface.text.Region)3 Iterator (java.util.Iterator)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 NoChangeEvent (org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent)2 HighlightedPosition (org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition)2 XMLNode (com.amalto.workbench.widgets.xmlviewer.model.XMLNode)1