use of org.eclipse.che.ide.api.editor.text.TypedPosition in project che by eclipse.
the class DocumentPositionMapImpl method computeIndexInPositionList.
protected int computeIndexInPositionList(final List<TypedPosition> positions, final int offset, final boolean orderedByOffset) {
if (positions.size() == 0) {
return 0;
}
int left = 0;
int right = positions.size() - 1;
int mid = 0;
Position p = null;
while (left < right) {
mid = (left + right) / 2;
p = positions.get(mid);
final int pOffset = getOffset(orderedByOffset, p);
if (offset < pOffset) {
if (left == mid) {
right = left;
} else {
right = mid - 1;
}
} else if (offset > pOffset) {
if (right == mid) {
left = right;
} else {
left = mid + 1;
}
} else if (offset == pOffset) {
left = right = mid;
}
}
int pos = left;
p = positions.get(pos);
int pPosition = getOffset(orderedByOffset, p);
if (offset > pPosition) {
// append to the end
pos++;
} else {
// entry will become the first of all entries with the same offset
do {
--pos;
if (pos < 0) {
break;
}
p = positions.get(pos);
pPosition = getOffset(orderedByOffset, p);
} while (offset == pPosition);
++pos;
}
Assert.isTrue(0 <= pos && pos <= positions.size());
return pos;
}
use of org.eclipse.che.ide.api.editor.text.TypedPosition in project che by eclipse.
the class DocumentPositionMapImpl method getPositions.
@Override
public List<TypedPosition> getPositions(String category, int offset, int length, boolean canStartBefore, boolean canEndAfter) throws BadPositionCategoryException {
if (canStartBefore && canEndAfter || (!canStartBefore && !canEndAfter)) {
List<TypedPosition> documentPositions;
if (canStartBefore && canEndAfter) {
if (offset < this.contentLength / 2) {
documentPositions = getStartingPositions(category, 0, offset + length);
} else {
documentPositions = getEndingPositions(category, offset, this.contentLength - offset + 1);
}
} else {
documentPositions = getStartingPositions(category, offset, length);
}
final List<TypedPosition> list = new ArrayList<TypedPosition>(documentPositions.size());
final Position region = new Position(offset, length);
for (final TypedPosition position : documentPositions) {
if (isWithinRegion(region, position, canStartBefore, canEndAfter)) {
list.add(position);
}
}
return list;
} else if (canStartBefore) {
final List<TypedPosition> list = getEndingPositions(category, offset, length);
return list;
} else {
Assert.isLegal(canEndAfter && !canStartBefore);
final List<TypedPosition> list = getStartingPositions(category, offset, length);
return list;
}
}
use of org.eclipse.che.ide.api.editor.text.TypedPosition in project che by eclipse.
the class DefaultPartitioner method getPartition.
@Override
public TypedRegion getPartition(final int offset) {
final int contentLength = getContentLength();
List<TypedPosition> category = null;
try {
category = getPositions();
} catch (final BadPositionCategoryException e) {
Log.warn(DefaultPartitioner.class, "Invalid position cateory... with default category! ", e);
return defaultRegion();
}
if (category == null || category.size() == 0) {
return defaultRegion();
}
Integer index = null;
try {
index = this.documentPositionMap.computeIndexInCategory(positionCategory, offset);
} catch (final BadLocationException e) {
Log.warn(DefaultPartitioner.class, "Invalid location " + offset + " (max=" + contentLength + ").");
return defaultRegion();
} catch (final BadPositionCategoryException e) {
Log.warn(DefaultPartitioner.class, "Invalid position cateory... with default category " + positionCategory + "!", e);
return defaultRegion();
}
if (index == null) {
return defaultRegion();
}
if (index < category.size()) {
final TypedPosition next = category.get(index);
if (offset == next.offset) {
return new TypedRegionImpl(next.getOffset(), next.getLength(), next.getType());
}
if (index == 0) {
return new TypedRegionImpl(0, next.offset, DEFAULT_CONTENT_TYPE);
}
final TypedPosition previous = category.get(index - 1);
if (previous.includes(offset)) {
return new TypedRegionImpl(previous.getOffset(), previous.getLength(), previous.getType());
}
final int endOffset = previous.getOffset() + previous.getLength();
return new TypedRegionImpl(endOffset, next.getOffset() - endOffset, DEFAULT_CONTENT_TYPE);
}
final TypedPosition previous = category.get(category.size() - 1);
if (previous.includes(offset)) {
return new TypedRegionImpl(previous.getOffset(), previous.getLength(), previous.getType());
}
final int endOffset = previous.getOffset() + previous.getLength();
return new TypedRegionImpl(endOffset, contentLength - endOffset, DEFAULT_CONTENT_TYPE);
}
use of org.eclipse.che.ide.api.editor.text.TypedPosition in project che by eclipse.
the class DefaultPartitioner method getFirstIndexStartingAfterOffset.
/**
* Returns the index of the first position which starts at or after the given offset.
*
* @param positions the positions in linear order
* @param offset the offset
* @return the index of the first position which starts after the offset
*/
private static int getFirstIndexStartingAfterOffset(List<TypedPosition> positions, int offset) {
int i = -1;
int j = positions.size();
while (j - i > 1) {
final int k = (i + j) >> 1;
final Position p = positions.get(k);
if (p.getOffset() >= offset) {
j = k;
} else {
i = k;
}
}
return j;
}
use of org.eclipse.che.ide.api.editor.text.TypedPosition in project che by eclipse.
the class DefaultPartitioner method computePartitioning.
private List<TypedRegion> computePartitioning(final int offset, final int length, final boolean includeZeroLengthPartitions) {
final List<TypedRegion> result = new ArrayList<>();
final int contentLength = getContentLength();
try {
final int endOffset = offset + length;
final List<TypedPosition> category = getPositions();
TypedPosition previous = null;
TypedPosition current = null;
int start, end, gapOffset;
final Position gap = new Position(0);
final int startIndex = getFirstIndexEndingAfterOffset(category, offset);
final int endIndex = getFirstIndexStartingAfterOffset(category, endOffset);
for (int i = startIndex; i < endIndex; i++) {
current = category.get(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());
result.add(new TypedRegionImpl(start, end - start, DEFAULT_CONTENT_TYPE));
}
if (current.overlapsWith(offset, length)) {
start = Math.max(offset, current.getOffset());
end = Math.min(endOffset, current.getOffset() + current.getLength());
result.add(new TypedRegionImpl(start, end - start, current.getType()));
}
previous = current;
}
if (previous != null) {
gapOffset = previous.getOffset() + previous.getLength();
gap.setOffset(gapOffset);
gap.setLength(contentLength - gapOffset);
if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
start = Math.max(offset, gapOffset);
end = Math.min(endOffset, contentLength);
result.add(new TypedRegionImpl(start, end - start, DEFAULT_CONTENT_TYPE));
}
}
if (result.isEmpty()) {
result.add(new TypedRegionImpl(offset, length, DEFAULT_CONTENT_TYPE));
}
} catch (final BadPositionCategoryException ex) {
Logger.getLogger(DefaultPartitioner.class.getName()).fine("Bad position in computePartitioning.");
} catch (final RuntimeException ex) {
Logger.getLogger(DefaultPartitioner.class.getName()).warning("computePartitioning failed.");
throw ex;
}
return result;
}
Aggregations