Eclipse - custom text when hovering over a marker

烈酒焚心 提交于 2019-12-11 04:13:54

问题


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 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?


回答1:


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());


来源:https://stackoverflow.com/questions/46445377/eclipse-custom-text-when-hovering-over-a-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!