3

I'm writing a custom editor in Eclipse and just integrated custom error recognition. Now I'm facing a strange issue: I can add Markers to my editor that get displayed all fine, I can also delete them while the editor is running.

What doesn't work: When I close my editor I want the markers to disappear/get deleted.

What I'm doing right now, is

  • creating the Markers with the transient property set like this: marker.setAttribute(IMarker.TRANSIENT, true); This doesn't seem to change anything though.

  • trying to delete all Annotations via the source viewers annotation-model. This doesn't work, cause when I try to hook into my editors dispose() method or add a DisposeListener to my sourceviewers textwidget, the sourceviewer already has been disposed of and getSourceViewer().getAnnotationModel(); returns null.

My deleteMarkers method:

private void deleteMarkers() {
        IAnnotationModel anmod = getSourceViewer().getAnnotationModel();
        Iterator<Annotation> it = anmod.getAnnotationIterator();

        while (it.hasNext()) {
            SimpleMarkerAnnotation a = (SimpleMarkerAnnotation) it.next();
            anmod.removeAnnotation(a);

            try {
                a.getMarker().delete();
            } catch (CoreException e) {
                e.printStackTrace();
            }

        }
    }

Any help is appreciated ^^

greg-449
  • 109,219
  • 232
  • 102
  • 145
Jakob Sachs
  • 669
  • 4
  • 24
  • Do they have to be IMarkers? If they only exist while the editor is running you can just use annotations that aren't markers. – greg-449 Apr 30 '19 at 10:50
  • IMarker.TRANSIENT means the marker isn't saved across sessions, but doesn't mean it is deleted when the editor exits. – greg-449 Apr 30 '19 at 11:21

1 Answers1

3

Hook into your editor close event, get a reference to the IResource for the editor (i believe you get can that on IEditorInput) and call IResource#deleteMarkers() on the relevant resource which will delete them when you close your editor. By design eclipse does not delete markers when editors are closed.

Here is some reference: http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IResource.html#deleteMarkers(java.lang.String, boolean, int)

Duncan Krebs
  • 3,366
  • 2
  • 33
  • 53