Search code examples
javaeclipse-plugineclipse-rcp

My custom Eclipse IMarkers are not being showed after I reopen the file


Currently I have my own custom IMarker:

<extension
    point="org.eclipse.core.resources.markers"
    id="com.test.package.custommarker"
    name="Custom Markers">
    <super type="org.eclipse.core.resources.problemmarker" />
    <persistent value="true" />
</extension>

And the IMarkers are created by this code:

IMarker marker = this.resource.createMarker("com.test.package.custommarker");
marker.setAttribute(IMarker.MESSAGE, description);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
marker.setAttribute(IMarker.LOCATION, "line: " + line + ", col: " + col);
marker.setAttribute(IMarker.LINE_NUMBER, line);     
marker.setAttribute(IMarker.CHAR_START, col);

At the 1st execution the Editor is open and the IMarkers are displayed in the Editor and in the Problem view, the issue is if I reopen the file the IMarkers are only showed in the Problem view but not in the editor. When I said the IMarkers are displayed in the Editor I mean to this: Editor with error.

Here is how the Editor looks when I reopen the file: Editor after the reopen.

My question is, why is this happen?


Solution

  • You need to set both CHAR_START and CHAR_END, or only LINE_NUMBER, in order for the corresponding text Annotation to be created. Also, CHAR_START and CHAR_END are document-relative, not line-relative. Step through org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel#createPositionFromMarker(IMarker) if you want to see how that works.