use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.
the class ProjectionDocument method addMasterDocumentRange.
/**
* Ensures that the given range of the master document is part of this
* projection document.
*
* @param offsetInMaster the offset of the master document range
* @param lengthInMaster the length of the master document range
* @param masterDocumentEvent the master document event which causes this
* projection change, or <code>null</code> if none
* @throws BadLocationException in case the master event is not valid
*/
private void addMasterDocumentRange(int offsetInMaster, int lengthInMaster, DocumentEvent masterDocumentEvent) throws BadLocationException {
/*
* Calling internalAddMasterDocumentRange may cause other master ranges
* to become unfolded, resulting in re-entrant calls to this method. In
* order to not add a region twice, we have to compute the next region
* to add in every iteration.
*
* To place an upper bound on the number of iterations, we use the number
* of fragments * 2 as the limit.
*/
int limit = Math.max(getFragments().length * 2, 20);
while (true) {
if (limit-- < 0)
// $NON-NLS-1$
throw new IllegalArgumentException("safety loop termination");
IRegion gap = computeFirstUnprojectedMasterRegion(offsetInMaster, lengthInMaster);
if (gap == null)
return;
internalAddMasterDocumentRange(gap.getOffset(), gap.getLength(), masterDocumentEvent);
}
}
use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.
the class ProjectionDocument method removeMasterDocumentRange.
/**
* Ensures that the given range of the master document is not part of this
* projection document.
*
* @param offsetInMaster the offset of the master document range
* @param lengthInMaster the length of the master document range
* @throws BadLocationException in case the master event is not valid
*/
public void removeMasterDocumentRange(int offsetInMaster, int lengthInMaster) throws BadLocationException {
IRegion[] fragments = computeProjectedMasterRegions(offsetInMaster, lengthInMaster);
if (fragments == null || fragments.length == 0)
return;
for (int i = 0; i < fragments.length; i++) {
IRegion fragment = fragments[i];
internalRemoveMasterDocumentRange(fragment.getOffset(), fragment.getLength());
}
}
use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.
the class ProjectionDocument method normalize.
/**
* Transforms a document event of the master document into a projection
* document based document event.
*
* @param masterEvent the master document event
* @return the slave document event
* @throws BadLocationException in case the master event is not valid
*/
private ProjectionDocumentEvent normalize(DocumentEvent masterEvent) throws BadLocationException {
if (!isUpdating()) {
IRegion imageRegion = fMapping.toExactImageRegion(new Region(masterEvent.getOffset(), masterEvent.getLength()));
if (imageRegion != null)
return new ProjectionDocumentEvent(this, imageRegion.getOffset(), imageRegion.getLength(), masterEvent.getText(), masterEvent);
return null;
}
ProjectionDocumentEvent event = new ProjectionDocumentEvent(this, fOriginalEvent.getOffset(), fOriginalEvent.getLength(), fOriginalEvent.getText(), masterEvent);
fOriginalEvent = null;
return event;
}
use of org.eclipse.jface.text.IRegion 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.IRegion in project eclipse.platform.text by eclipse.
the class ProjectionMapping method getExactCoverage.
@Override
public IRegion[] getExactCoverage(IRegion originRegion) throws BadLocationException {
int originOffset = originRegion.getOffset();
int originLength = originRegion.getLength();
if (originLength == 0) {
int imageOffset = toImageOffset(originOffset);
return imageOffset > -1 ? new IRegion[] { new Region(originOffset, 0) } : null;
}
int endOffset = originOffset + originLength;
Position[] fragments = getFragments();
int firstIndex = findFragmentIndex(originOffset, RIGHT);
int lastIndex = findFragmentIndex(endOffset - 1, LEFT);
if (firstIndex == -1 || firstIndex > lastIndex)
return null;
int resultLength = lastIndex - firstIndex + 1;
IRegion[] result = new IRegion[resultLength];
// first
result[0] = createOriginStartRegion((Fragment) fragments[firstIndex], originOffset - fragments[firstIndex].getOffset());
// middles
for (int i = 1; i < resultLength - 1; i++) result[i] = createOriginRegion((Fragment) fragments[firstIndex + i]);
// last
Fragment last = (Fragment) fragments[lastIndex];
int fragmentEndOffset = exclusiveEnd(last);
IRegion lastRegion = createOriginEndRegion(last, fragmentEndOffset - endOffset);
if (resultLength > 1) {
// first != last
result[resultLength - 1] = lastRegion;
} else {
// merge first and last
IRegion intersection = getIntersectingRegion(result[0], lastRegion);
if (intersection == null)
return null;
result[0] = intersection;
}
return result;
}
Aggregations