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:
I have already tried these approaches without success:
1) Having a custom
TextEditor
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!";
}
};
}
});
}
}
<extension
point="org.eclipse.ui.editors">
<editor
id="test"
name="MyEditor"
extensions="c"
class="foo.bar.editors.MyEditor">
</editor>
</extension>
MyEditor
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";
}
});
}
}
getHoverInfo
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());