Search in sources :

Example 6 with TypedRegion

use of org.eclipse.jface.text.TypedRegion 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)

Example 7 with TypedRegion

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

the class JavaHeuristicScanner method getPartition.

/**
 * Returns the partition at <code>position</code>.
 *
 * @param position
 *            the position to get the partition for
 * @return the partition at <code>position</code> or a dummy zero-length
 *         partition if accessing the document fails
 */
private ITypedRegion getPartition(int position) {
    Assert.isTrue(position >= 0);
    Assert.isTrue(position <= fDocument.getLength());
    try {
        return TextUtilities.getPartition(fDocument, fPartitioning, position, false);
    } catch (BadLocationException e) {
        // $NON-NLS-1$
        return new TypedRegion(position, 0, "__no_partition_at_all");
    }
}
Also used : TypedRegion(org.eclipse.jface.text.TypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 8 with TypedRegion

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

the class BasicStructuredDocument method getPartition.

public ITypedRegion getPartition(String partitioning, int offset, boolean preferOpenPartitions) throws BadLocationException, BadPartitioningException {
    if ((0 > offset) || (offset > getLength()))
        throw new BadLocationException();
    ITypedRegion result = null;
    IDocumentPartitioner partitioner = getDocumentPartitioner(partitioning);
    if (partitioner instanceof IDocumentPartitionerExtension2) {
        result = ((IDocumentPartitionerExtension2) partitioner).getPartition(offset, preferOpenPartitions);
    } else if (partitioner != null) {
        result = partitioner.getPartition(offset);
    } else if (IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING.equals(partitioning)) {
        result = new TypedRegion(0, getLength(), DEFAULT_CONTENT_TYPE);
    } else
        throw new BadPartitioningException();
    return result;
}
Also used : BadPartitioningException(org.eclipse.jface.text.BadPartitioningException) IDocumentPartitionerExtension2(org.eclipse.jface.text.IDocumentPartitionerExtension2) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with TypedRegion

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

the class DocumentPartitioner method getPartition.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 *
 * @since 2.2
 */
@Override
public synchronized 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());
        }
        if (isOpenSingleLineCommentPartition(previous, offset)) {
            return new TypedRegion(previous.getOffset(), previous.getLength() + 1, 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 : Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) 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 10 with TypedRegion

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

the class SemanticHighlightingTest method addPosition.

@Override
public void addPosition(int offset, int length, String... id) {
    assertEquals(1, id.length);
    TypedRegion region = new TypedRegion(offset, length, id[0]);
    assertFalse(region.toString(), expectedRegions.isEmpty());
    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