Search in sources :

Example 11 with TypedRegion

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

the class XMLDocumentPartitioner method computePartitioning.

public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    List<TypedRegion> list = new ArrayList<TypedRegion>();
    try {
        int endOffset = offset + length;
        Position[] category = document.getPositions(positionCategory);
        TypedPosition previous = null;
        TypedPosition current = null;
        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];
            int gapOffset = previous == null ? 0 : previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(current.getOffset() - gapOffset);
            if (includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length) || gap.getLength() > 0 && gap.overlapsWith(offset, length)) {
                int start = Math.max(offset, gapOffset);
                int end = Math.min(endOffset, gap.getOffset() + gap.getLength());
                // $NON-NLS-1$
                list.add(new TypedRegion(start, end - start, "__dftl_partition_content_type"));
            }
            if (current.overlapsWith(offset, length)) {
                int start = Math.max(offset, current.getOffset());
                int end = Math.min(endOffset, current.getOffset() + current.getLength());
                list.add(new TypedRegion(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            int gapOffset = previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(document.getLength() - gapOffset);
            if (includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length) || gap.getLength() > 0 && gap.overlapsWith(offset, length)) {
                int start = Math.max(offset, gapOffset);
                int end = Math.min(endOffset, document.getLength());
                // $NON-NLS-1$
                list.add(new TypedRegion(start, end - start, "__dftl_partition_content_type"));
            }
        }
        if (list.isEmpty()) {
            // $NON-NLS-1$
            list.add(new TypedRegion(offset, length, "__dftl_partition_content_type"));
        }
    } catch (BadPositionCategoryException _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 12 with TypedRegion

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

the class DefaultPartitioner method getPartition.

@Override
public ITypedRegion getPartition(int offset) {
    checkInitialization();
    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)

Example 13 with TypedRegion

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

the class FastPartitioner method getPartition.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 */
@Override
public ITypedRegion getPartition(int offset) {
    checkInitialization();
    try {
        Position[] category = getPositions();
        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)

Example 14 with TypedRegion

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

the class TypedRegionMerger method merge.

public ITypedRegion[] merge(ITypedRegion[] original) {
    if (original == null || original.length == 0)
        return original;
    ITypedRegion[] result = new ITypedRegion[original.length];
    String contentType = original[0].getType();
    result[0] = original[0];
    for (int i = 1; i < original.length; i++) {
        ITypedRegion copyMe = original[i];
        result[i] = new TypedRegion(copyMe.getOffset(), copyMe.getLength(), contentType);
    }
    return result;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 15 with TypedRegion

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

the class TaskHighlightingTest method addPosition.

@Override
public void addPosition(int offset, int length, String... id) {
    Assert.assertEquals(1, id.length);
    TypedRegion region = new TypedRegion(offset, length, id[0]);
    Assert.assertFalse(region.toString(), expectedRegions.isEmpty());
    Assert.assertTrue("expected: " + expectedRegions.toString() + " but was: " + region, expectedRegions.remove(region));
}
Also used : TypedRegion(org.eclipse.jface.text.TypedRegion)

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