Search in sources :

Example 16 with TypedRegion

use of org.eclipse.jface.text.TypedRegion in project xtext-eclipse by eclipse.

the class DocumentPartitioner method computePartitioning.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 *
 * @since 2.2
 */
@Override
public synchronized ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    checkInitialization();
    List<ITypedRegion> list = new ArrayList<ITypedRegion>();
    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);
            int gapLength = fDocument.getLength() - gapOffset;
            if (gapLength < 0) {
                clearPositionCache();
                return new TypedRegion[0];
            } else {
                gap.setLength(gapLength);
                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 : Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) TypedPosition(org.eclipse.jface.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

Example 17 with TypedRegion

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

the class XMLDocumentPartitioner method getPartition.

public ITypedRegion getPartition(int offset) {
    try {
        Position[] category = document.getPositions(positionCategory);
        if (category == null || category.length == 0) {
            // $NON-NLS-1$
            return new TypedRegion(0, document.getLength(), "__dftl_partition_content_type");
        }
        int index = document.computeIndexInCategory(positionCategory, 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) {
                // $NON-NLS-1$
                return new TypedRegion(0, next.offset, "__dftl_partition_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();
            // $NON-NLS-1$
            return new TypedRegion(endOffset, next.getOffset() - endOffset, "__dftl_partition_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();
        // $NON-NLS-1$
        return new TypedRegion(endOffset, document.getLength() - endOffset, "__dftl_partition_content_type");
    } catch (BadPositionCategoryException _ex) {
    } catch (BadLocationException _ex) {
    }
    // $NON-NLS-1$
    return new TypedRegion(0, document.getLength(), "__dftl_partition_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

TypedRegion (org.eclipse.jface.text.TypedRegion)17 ITypedRegion (org.eclipse.jface.text.ITypedRegion)15 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)12 Position (org.eclipse.jface.text.Position)12 TypedPosition (org.eclipse.jface.text.TypedPosition)12 BadLocationException (org.eclipse.jface.text.BadLocationException)8 ArrayList (java.util.ArrayList)6 List (java.util.List)1 BadPartitioningException (org.eclipse.jface.text.BadPartitioningException)1 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)1 IDocumentPartitionerExtension2 (org.eclipse.jface.text.IDocumentPartitionerExtension2)1