* @param region can be null
*/
private void updateSectionFoldingAnnotations(IRegion region) {
if (!haveRunFolding) region = null; // Do the whole doc
if ( ! (getSourceViewer() instanceof ProjectionViewer)) return;
ProjectionViewer viewer = ((ProjectionViewer)getSourceViewer());
MarkdownPage mPage = getMarkdownPage();
List<Header> headers = mPage.getHeadings(null);
// this will hold the new annotations along
// with their corresponding positions
Map<Annotation, Position> annotations = new HashMap<Annotation, Position>();
IDocument doc = getDocument();
updateSectionFoldingAnnotations2(doc, headers, annotations, doc.getLength());
// Filter existing ones
Position[] newValues = annotations.values().toArray(POSITION_ARRAY);
List<Annotation> deletedAnnotations = new ArrayList<Annotation>();
for(Entry<Annotation, Position> ae : oldAnnotations.entrySet()) {
Position oldp = ae.getValue();
boolean stillExists = false;
for (Position newp : newValues) {
if (oldp.equals(newp)) {
annotations.remove(newp);
stillExists = true;
break;
}
}
if (!stillExists && intersectsRegion(oldp, region)) {
deletedAnnotations.add(ae.getKey());
}
}
// Filter out-of-region ones
for(Annotation a : annotations.keySet().toArray(ANNOTATION_ARRAY)) {
Position p = annotations.get(a);
if (!intersectsRegion(p , region)) annotations.remove(a);
}
// Adjust the page
ProjectionAnnotationModel annotationModel = viewer.getProjectionAnnotationModel();
if (annotationModel==null) return;
annotationModel.modifyAnnotations(deletedAnnotations.toArray(ANNOTATION_ARRAY), annotations, null);
// Remember old values
oldAnnotations.putAll(annotations);
for (Annotation a : deletedAnnotations) {