【问题标题】:Eclipse - custom text when hovering over a markerEclipse - 悬停在标记上时的自定义文本
【发布时间】:2017-09-27 10:20:08
【问题描述】:

我正在开发一个 Eclipse 插件,目前我正在尝试让 Eclipse (Luna) 在将鼠标悬停在标记上时显示自定义文本。我知道我可以通过指定标记参数来实现这一点,但是我需要动态更改文本,例如:

我已经尝试过这些方法没有成功:

1) 自定义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!";
                    }
                };
            }
        });
    }
}

和这样的plugin.xml:

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

我的方法 getHoverInfo 永远不会被调用。相反,每当我打开任何 *.c 文件并将鼠标悬停在标记上时,都会调用 DefaultAnnotationHover 中的方法 getHoverInfo。我通过 Eclipse->Preferences->General->Editors 检查了可用的编辑器,MyEditor 在那里。

2) 动态添加自定义注解悬停。我发现here 信息表明这是可能的。所以每次我打开一个“*.c”文件时,我都有这个代码:

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 根本没有被调用。

知道我做错了什么吗?

【问题讨论】:

    标签: java c eclipse hover markers


    【解决方案1】:

    对于那些有类似问题的人 - 我找到了一个“hacky”解决方案。我创建了自己的班级MyAwesomeHover

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

    然后我使用反射将MyAweseomeHover设置为显示的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());
    

    【讨论】:

    • 在 Eclipse Neon3 中,为了让这个修复工作,必须在首选项中禁用“悬停时展开垂直标尺图标(不影响打开的编辑器)”选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2011-09-01
    • 2020-02-05
    • 1970-01-01
    相关资源
    最近更新 更多