Search in sources :

Example 16 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class DefaultPartitioner method documentChanged2.

@Override
public IRegion documentChanged2(DocumentEvent e) {
    if (!fIsInitialized)
        return null;
    try {
        IDocument d = e.getDocument();
        Position[] category = d.getPositions(fPositionCategory);
        IRegion line = d.getLineInformationOfOffset(e.getOffset());
        int reparseStart = line.getOffset();
        int partitionStart = -1;
        String contentType = null;
        int newLength = e.getText() == null ? 0 : e.getText().length();
        int first = d.computeIndexInCategory(fPositionCategory, 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();
                contentType = IDocument.DEFAULT_CONTENT_TYPE;
            }
        }
        fPositionUpdater.update(e);
        for (int i = first; i < category.length; i++) {
            Position p = category[i];
            if (p.isDeleted) {
                rememberDeletedOffset(e.getOffset());
                break;
            }
        }
        category = d.getPositions(fPositionCategory);
        fScanner.setPartialRange(d, reparseStart, d.getLength() - reparseStart, contentType, partitionStart);
        int lastScannedPosition = reparseStart;
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            contentType = getTokenContentType(token);
            if (!isSupportedContentType(contentType)) {
                token = fScanner.nextToken();
                continue;
            }
            int start = fScanner.getTokenOffset();
            int length = fScanner.getTokenLength();
            lastScannedPosition = start + length - 1;
            // remove all affected positions
            while (first < category.length) {
                TypedPosition p = (TypedPosition) category[first];
                if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) {
                    rememberRegion(p.offset, p.length);
                    d.removePosition(fPositionCategory, p);
                    ++first;
                } else
                    break;
            }
            // area covered by the event, we are done
            if (d.containsPosition(fPositionCategory, start, length)) {
                if (lastScannedPosition >= e.getOffset() + newLength)
                    return createRegion();
                ++first;
            } else {
                // insert the new type position
                try {
                    d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
                    rememberRegion(start, length);
                } catch (BadPositionCategoryException x) {
                } catch (BadLocationException x) {
                }
            }
            token = fScanner.nextToken();
        }
        // remove all positions behind lastScannedPosition since there aren't any further types
        if (lastScannedPosition != reparseStart) {
            // if this condition is not met, nothing has been scanned because of a deletion
            ++lastScannedPosition;
        }
        first = d.computeIndexInCategory(fPositionCategory, lastScannedPosition);
        category = d.getPositions(fPositionCategory);
        TypedPosition p;
        while (first < category.length) {
            p = (TypedPosition) category[first++];
            d.removePosition(fPositionCategory, p);
            rememberRegion(p.offset, p.length);
        }
    } catch (BadPositionCategoryException x) {
    // should never happen on connected documents
    } catch (BadLocationException x) {
    }
    return createRegion();
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) 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 17 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class FastPartitioner method initialize.

/**
 * Performs the initial partitioning of the partitioner's document.
 * <p>
 * May be extended by subclasses.
 * </p>
 */
protected void initialize() {
    fIsInitialized = true;
    clearPositionCache();
    fScanner.setRange(fDocument, 0, fDocument.getLength());
    try {
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            String contentType = getTokenContentType(token);
            if (isSupportedContentType(contentType)) {
                TypedPosition p = new TypedPosition(fScanner.getTokenOffset(), fScanner.getTokenLength(), contentType);
                fDocument.addPosition(fPositionCategory, p);
            }
            token = fScanner.nextToken();
        }
    } catch (BadLocationException x) {
    // cannot happen as offsets come from scanner
    } catch (BadPositionCategoryException x) {
    // cannot happen if document has been connected before
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 18 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class FastPartitioner method computePartitioning.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 */
@Override
public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    checkInitialization();
    List<TypedRegion> list = new ArrayList<>();
    try {
        int endOffset = offset + length;
        Position[] category = getPositions();
        TypedPosition previous = null, current = null;
        int start, end, gapOffset;
        Position gap = new Position(0);
        int startIndex = getFirstIndexEndingAfterOffset(category, offset);
        int endIndex = getFirstIndexStartingAfterOffset(category, endOffset);
        for (int i = startIndex; i < endIndex; i++) {
            current = (TypedPosition) category[i];
            gapOffset = (previous != null) ? previous.getOffset() + previous.getLength() : 0;
            gap.setOffset(gapOffset);
            gap.setLength(current.getOffset() - gapOffset);
            if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, gap.getOffset() + gap.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
            if (current.overlapsWith(offset, length)) {
                start = Math.max(offset, current.getOffset());
                end = Math.min(endOffset, current.getOffset() + current.getLength());
                list.add(new TypedRegion(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            gapOffset = previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(fDocument.getLength() - gapOffset);
            if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, fDocument.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
        }
        if (list.isEmpty())
            list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE));
    } catch (BadPositionCategoryException ex) {
        // Make sure we clear the cache
        clearPositionCache();
    } catch (RuntimeException ex) {
        // Make sure we clear the cache
        clearPositionCache();
        throw ex;
    }
    TypedRegion[] result = new TypedRegion[list.size()];
    list.toArray(result);
    return result;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

Example 19 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class RuleBasedPartitioner method computePartitioning.

@Override
public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    List<TypedRegion> list = new ArrayList<>();
    try {
        int endOffset = offset + length;
        Position[] category = fDocument.getPositions(fPositionCategory);
        TypedPosition previous = null, current = null;
        int start, end, gapOffset;
        Position gap = null;
        for (Position element : category) {
            current = (TypedPosition) element;
            gapOffset = (previous != null) ? previous.getOffset() + previous.getLength() : 0;
            gap = new Position(gapOffset, current.getOffset() - gapOffset);
            if ((includeZeroLengthPartitions || gap.getLength() > 0) && gap.overlapsWith(offset, length)) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, gap.getOffset() + gap.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
            if (current.overlapsWith(offset, length)) {
                start = Math.max(offset, current.getOffset());
                end = Math.min(endOffset, current.getOffset() + current.getLength());
                list.add(new TypedRegion(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            gapOffset = previous.getOffset() + previous.getLength();
            gap = new Position(gapOffset, fDocument.getLength() - gapOffset);
            if ((includeZeroLengthPartitions || gap.getLength() > 0) && ((includeZeroLengthPartitions && offset + length == gapOffset && gap.length == 0) || gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, fDocument.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
        }
        if (list.isEmpty())
            list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE));
    } catch (BadPositionCategoryException x) {
    }
    TypedRegion[] result = new TypedRegion[list.size()];
    list.toArray(result);
    return result;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

Example 20 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class RuleBasedPartitioner method getPartition.

/*
	 * @see IDocumentPartitioner#getPartition
	 */
@Override
public ITypedRegion getPartition(int offset) {
    try {
        Position[] category = fDocument.getPositions(fPositionCategory);
        if (category == null || category.length == 0)
            return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
        int index = fDocument.computeIndexInCategory(fPositionCategory, offset);
        if (index < category.length) {
            TypedPosition next = (TypedPosition) category[index];
            if (offset == next.offset)
                return new TypedRegion(next.getOffset(), next.getLength(), next.getType());
            if (index == 0)
                return new TypedRegion(0, next.offset, IDocument.DEFAULT_CONTENT_TYPE);
            TypedPosition previous = (TypedPosition) category[index - 1];
            if (previous.includes(offset))
                return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
            int endOffset = previous.getOffset() + previous.getLength();
            return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
        }
        TypedPosition previous = (TypedPosition) category[category.length - 1];
        if (previous.includes(offset))
            return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
        int endOffset = previous.getOffset() + previous.getLength();
        return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
    } catch (BadPositionCategoryException x) {
    } catch (BadLocationException x) {
    }
    return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)42 Position (org.eclipse.jface.text.Position)30 BadLocationException (org.eclipse.jface.text.BadLocationException)22 TypedPosition (org.eclipse.jface.text.TypedPosition)18 IDocument (org.eclipse.jface.text.IDocument)14 ITypedRegion (org.eclipse.jface.text.ITypedRegion)10 TypedRegion (org.eclipse.jface.text.TypedRegion)8 ArrayList (java.util.ArrayList)7 IRegion (org.eclipse.jface.text.IRegion)7 Point (org.eclipse.swt.graphics.Point)4 Region (org.eclipse.jface.text.Region)3 Iterator (java.util.Iterator)2 List (java.util.List)2 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1