napets napets - 3 years ago 225
C Question

Eclipse - custom text when hovering over a marker

I working on an Eclipse plugin and currently I am trying to make Eclipse (Luna) to show customized text when hovering over a marker. I know that I could achieve this by specifying the marker arguments, but I need to change the text dynamically, e.g:

Use case

I have already tried these approaches without success:

1) Having a custom

TextEditor
with custom
SourceViewerConfiguration
:

public class MyEditor extends TextEditor {

public MyEditor() {
setSourceViewerConfiguration(new SourceViewerConfiguration() {
@Override
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
return new IAnnotationHover() {
@Override
public String getHoverInfo(ISourceViewer sv, int ln) {
return "Hello world!";
}
};
}
});
}
}


and the plugin.xml like this:

<extension
point="org.eclipse.ui.editors">
<editor
id="test"
name="MyEditor"
extensions="c"
class="foo.bar.editors.MyEditor">
</editor>
</extension>


My method getHoverInfo is never called. Instead, the method getHoverInfo from the DefaultAnnotationHover is being called any time I open any *.c file and hover over the marker. I checked the available editors via the Eclipse->Preferences->General->Editors and the
MyEditor
is there.

2) Adding the custom annotation hover dynamically. I found here information that it is possible. So every time I open a "*.c" file, I have this code:

IPartListener2 partListener2 = new IPartListener2() {
@Override
public void partOpened(IWorkbenchPartReference partRef) {
if ("org.eclipse.cdt.ui.editor.CEditor".equals(((EditorReference) partRef).getDescriptor().getId())) {
CEditor currentCFileEditor = ((CEditor)((EditorReference) partRef).getEditor(false));
currentCFileEditor.getViewer().setAnnotationHover(new IAnnotationHover() {
@Override
public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
return "Hello world";
}
});
}
}


But again, I was not successful - my
getHoverInfo
is not called at all.

Any idea what I am doing wrong?

Answer Source

For those who have a similar problem - I have found a "hacky" solution. I have created my own class MyAwesomeHover:

public class MyAwesomeHover implements IAnnotationHover {
    @Override
    public String getHoverInfo(ISourceViewer sw, int ln) {
        return "Hey hoo"
    }   
}

And then I use the reflection to set MyAweseomeHover as the displayed AnnotationHover.

    Field hm= SourceViewer.class.getDeclaredField("fVerticalRulerHoveringController");
    hm.setAccessible(true);
    AnnotationBarHoverManager ma= (AnnotationBarHoverManager) hm.get(sourceViewer);
    Field ah= AnnotationBarHoverManager.class.getDeclaredField("fAnnotationHover");
    ah.setAccessible(true);
    ah.set(ma, MyAwesomeHover());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download