use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.
the class ProjectionMapping method toClosestImageLine.
@Override
public int toClosestImageLine(int originLine) throws BadLocationException {
try {
int imageLine = toImageLine(originLine);
if (imageLine > -1)
return imageLine;
Position[] fragments = getFragments();
if (fragments.length == 0)
return -1;
IRegion originLineRegion = fMasterDocument.getLineInformation(originLine);
int index = fMasterDocument.computeIndexInCategory(fFragmentsCategory, originLineRegion.getOffset());
if (0 < index && index < fragments.length) {
Fragment left = (Fragment) fragments[index - 1];
int leftDistance = originLineRegion.getOffset() - (exclusiveEnd(left));
Fragment right = (Fragment) fragments[index];
int rightDistance = right.getOffset() - (exclusiveEnd(originLineRegion));
if (leftDistance <= rightDistance)
originLine = fMasterDocument.getLineOfOffset(left.getOffset() + Math.max(left.getLength() - 1, 0));
else
originLine = fMasterDocument.getLineOfOffset(right.getOffset());
} else if (index == 0) {
Fragment right = (Fragment) fragments[index];
originLine = fMasterDocument.getLineOfOffset(right.getOffset());
} else if (index == fragments.length) {
Fragment left = (Fragment) fragments[index - 1];
originLine = fMasterDocument.getLineOfOffset(exclusiveEnd(left));
}
return toImageLine(originLine);
} catch (BadPositionCategoryException x) {
}
return -1;
}
use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.
the class LinkedModeModel method register.
/**
* Registers a <code>LinkedPosition</code> with this model. Called
* by <code>PositionGroup</code>.
*
* @param position the position to register
* @throws BadLocationException if the position cannot be added to its
* document
*/
void register(LinkedPosition position) throws BadLocationException {
Assert.isNotNull(position);
IDocument document = position.getDocument();
manageDocument(document);
try {
document.addPosition(getCategory(), position);
} catch (BadPositionCategoryException e) {
// won't happen as the category has been added by manageDocument()
Assert.isTrue(false);
}
int seqNr = position.getSequenceNumber();
if (seqNr != LinkedPositionGroup.NO_STOP) {
fPositionSequence.add(position);
}
}
use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.
the class ContentFormatter method getPartitioning.
/**
* Returns the partitioning of the given region of the document to be formatted.
* As one partition after the other will be formatted and formatting will
* probably change the length of the formatted partition, it must be kept
* track of the modifications in order to submit the correct partition to all
* formatting strategies. For this, all partitions are remembered as positions
* in a dedicated position category. (As formatting strategies might rely on each
* other, calling them in reversed order is not an option.)
*
* @param region the region for which the partitioning must be determined
* @return the partitioning of the specified region
* @exception BadLocationException of region is invalid in the document
* @since 3.0
*/
private TypedPosition[] getPartitioning(IRegion region) throws BadLocationException {
ITypedRegion[] regions = TextUtilities.computePartitioning(fDocument, fPartitioning, region.getOffset(), region.getLength(), false);
TypedPosition[] positions = new TypedPosition[regions.length];
for (int i = 0; i < regions.length; i++) {
positions[i] = new TypedPosition(regions[i]);
try {
fDocument.addPosition(PARTITIONING, positions[i]);
} catch (BadPositionCategoryException x) {
// should not happen
}
}
return positions;
}
use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.
the class ContentFormatter method updateAffectedPositions.
/**
* Updates all the overlapping positions. Note, all other positions are
* automatically updated by their document position updaters.
*
* @param document the document to has been formatted
* @param positions the adapted character positions to be used to update the document positions
* @param offset the offset of the document region that has been formatted
*/
protected void updateAffectedPositions(IDocument document, int[] positions, int offset) {
if (document != fDocument)
return;
if (positions.length == 0)
return;
for (int i = 0; i < positions.length; i++) {
PositionReference r = fOverlappingPositionReferences.get(i);
if (r.refersToOffset())
r.setOffset(offset + positions[i]);
else
r.setLength((offset + positions[i]) - r.getOffset());
Position p = r.getPosition();
String category = r.getCategory();
if (!document.containsPosition(category, p.offset, p.length)) {
try {
if (positionAboutToBeAdded(document, category, p))
document.addPosition(r.getCategory(), p);
} catch (BadPositionCategoryException x) {
// can not happen
} catch (BadLocationException x) {
// should not happen
}
}
}
fOverlappingPositionReferences = null;
}
use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.
the class DefaultPartitioner method computePartitioning.
@Override
public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
checkInitialization();
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 = 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 x) {
}
TypedRegion[] result = new TypedRegion[list.size()];
list.toArray(result);
return result;
}
Aggregations