use of org.eclipse.jface.text.ITypedRegion 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;
}
use of org.eclipse.jface.text.ITypedRegion 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;
}
use of org.eclipse.jface.text.ITypedRegion 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);
}
use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class DefaultPartitionerZeroLengthTest method assertEqualPartition.
private void assertEqualPartition(int offset, int inclusiveEnd, String type) {
int from = isOpenType(type) ? offset : offset + 1;
int to = isOpenType(type) ? inclusiveEnd : inclusiveEnd - 1;
for (int i = from; i <= to; i++) {
ITypedRegion region = fPartitioner.getPartition(i, true);
assertTypedRegion(region, offset, inclusiveEnd, type);
}
}
use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class FastPartitionerTest method assertComputePartitioning_InterleavingPartitions.
private void assertComputePartitioning_InterleavingPartitions(int startOffset, int endOffset, int[] offsets, String startType) {
ITypedRegion[] regions = fPartitioner.computePartitioning(startOffset, endOffset - startOffset);
String type = startType;
int previousOffset = startOffset;
int j = 0;
for (int i = 0; i <= offsets.length; i++) {
int currentOffset = (i == offsets.length) ? endOffset : offsets[i];
if (currentOffset - previousOffset != 0) {
// don't do empty partitions
ITypedRegion region = regions[j++];
assertTypedRegion(region, previousOffset, currentOffset, type);
}
// advance
if (type == DEFAULT)
type = COMMENT;
else
type = DEFAULT;
previousOffset = currentOffset;
}
}
Aggregations