*/
private Annotation getNextAnnotation(final int offset, final int length,
final boolean forward, final Position annotationPosition) {
Annotation nextAnnotation = null;
Position nextAnnotationPosition = null;
Annotation containingAnnotation = null;
Position containingAnnotationPosition = null;
boolean currentAnnotation = false;
final IDocument document = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
final int endOfDocument = document.getLength();
int distance = Integer.MAX_VALUE;
final IAnnotationModel model = editor.getDocumentProvider().getAnnotationModel(
editor.getEditorInput());
final Iterator<Annotation> e = new ErlangAnnotationIterator(model, true, true);
while (e.hasNext()) {
final Annotation a = e.next();
if (a instanceof IErlangAnnotation && ((IErlangAnnotation) a).hasOverlay()
|| !isNavigationTarget(a)) {
continue;
}
final Position p = model.getPosition(a);
if (p == null) {
continue;
}
if (forward && p.offset == offset || !forward
&& p.offset + p.getLength() == offset + length) {
// || p.includes(offset))
if (containingAnnotation == null
|| containingAnnotationPosition != null
&& (forward && p.length >= containingAnnotationPosition.length || !forward
&& p.length < containingAnnotationPosition.length)) {
containingAnnotation = a;
containingAnnotationPosition = p;
currentAnnotation = p.length == length;
}
} else {
int currentDistance = 0;
if (forward) {
currentDistance = p.getOffset() - offset;
if (currentDistance < 0) {
currentDistance = endOfDocument + currentDistance;
}
if (currentDistance < distance || currentDistance == distance
&& nextAnnotationPosition != null
&& p.length < nextAnnotationPosition.length) {
distance = currentDistance;
nextAnnotation = a;
nextAnnotationPosition = p;
}
} else {
currentDistance = offset + length - (p.getOffset() + p.length);
if (currentDistance < 0) {
currentDistance = endOfDocument + currentDistance;
}
if (currentDistance < distance || currentDistance == distance