Joe
Joe

Reputation: 4514

Marker in Eclipse editor not showing message

I'm building a custom text editor plugin for a domain specific language in Eclipse.

I can detect errors in the format of the editor contents and want to use eclipse's marker's to point out the errors to the user.

I have the following code in the plugin:

public static void createMarkerForResource(int linenumber, String message) throws CoreException {
    IResource resource = getFile();
    createMarkerForResource(resource, linenumber, message);
  }

  public static void createMarkerForResource(IResource resource, int linenumber, String message)
      throws CoreException {
    HashMap<String, Object> map = new HashMap<String, Object>();
    MarkerUtilities.setLineNumber(map, linenumber);
    MarkerUtilities.setMessage(map, message); 
    MarkerUtilities.createMarker(resource, map, IMarker.PROBLEM);
    IMarker[] markers = resource.findMarkers(null, true, IResource.DEPTH_INFINITE);
    for (IMarker marker : markers){
      System.out.println("Marker contents"+MarkerUtilities.getMessage(marker));
    } 
  }

I run this code with the command:

createMarkerForResource(2, "hello");

This successfully gives me an image on the correct line

enter image description here

and if I hover over it, I get a 'you can click this thing' cursor. But I can't get the message to turn up.

The message has definitely been placed, because the:

for (IMarker marker : markers){
          System.out.println("Marker contents"+MarkerUtilities.getMessage(marker));
        } 

code produces the "Marker contentshello" output as expected. What am I doing wrong?

EDIT:

The message is appearing in the problem view:

enter image description here

Upvotes: 4

Views: 476

Answers (2)

de-jcup
de-jcup

Reputation: 1885

The answer of Njol is correct and works for me (Eclipse Neon.1).

But two additional recommendations:

  1. I am reusing a created field, so annotation hoover is not always new created (getter... not create method)
  2. Default annotation hoover does show all annotations. So when you want only markers to be shown (and no others - e.g. Diff annotations from GIT) you should override isIncluded as in my following example.

Example:

    import org.eclipse.jface.text.source.SourceViewerConfiguration;
        ...
        public class MySourceViewerConfiguration extends SourceViewerConfiguration {
        ...
    
            private IAnnotationHover annotationHoover;
        ...
    
            public MySourceViewerConfiguration(){
              this.annotationHoover=new MyAnnotationHoover();
            }
        ...
            @Override
            public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
            return annotationHoover;
        }
    }

And here the annotation hoover class

    private class MyAnnotationHoover extends DefaultAnnotationHover{
        @Override
        protected boolean isIncluded(Annotation annotation) {
             if (annotation instanceof MarkerAnnotation){
                    return true;
             }
             /* we do not support other annotations than markers*/
             return false;
        }
    }

Upvotes: 2

Njol
Njol

Reputation: 3279

You need to use a proper IAnnotationHover, which can e.g. be defined in your SourceViewerConfiguration like this:

@Override
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
    return new DefaultAnnotationHover(false);
}

Upvotes: 0

Related Questions