Eclipse - 悬停在标记上时的自定义文本 [英] Eclipse - custom text when hovering over a marker
问题描述
我在Eclipse插件上工作,目前我正试图让Eclipse(Luna)在鼠标悬停时显示自定义文本。我知道我可以通过指定标记参数来实现这一点,但是我需要动态改变文本,例如:
我已经尝试过这些方法,没有成功:
< 1)使用自定义 SourceViewerConfiguration
定制 TextEditor
:
public class MyEditor extends TextEditor {
$ b $ public MyEditor(){
setSourceViewerConfiguration(new SourceViewerConfiguration(){
@Override
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer){
return new IAnnotationHover(){
@Override
public String getHoverInfo(ISourceViewer sv,int ln){
returnHel我的世界!;
}
};
}
});
}
}
和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)动态添加自定义注释悬停。我在这里找到了 信息,这是可能的。所以每次打开一个* .c文件,我都有这样的代码:
IPartListener2 partListener2 = new IPartListener2(){ ($()).equals(((EditorReference)partRef).getDescriptor()。
@Override
public void partOpened(IWorkbenchPartReference partRef){
if(org.eclipse.cdt.ui.editor.CEditor.equals .getId())){
CEditor currentCFileEditor =((CEditor)((EditorReference)partRef).getEditor(false));
currentCFileEditor.getViewer()。setAnnotationHover(new IAnnotationHover(){
@Override
public String getHoverInfo(ISourceViewer sourceViewer,int lineNumber){
returnHello world;
}
});
}
}
但是,我没有成功 - 我的 getHoverInfo
根本不被调用。
任何想法我做错了什么?
谁有类似的问题 - 我发现了一个哈克解决方案。我创建了自己的类 MyAwesomeHover
:
$ b
public class MyAwesomeHover implements IAnnotationHover {
@Override
public String getHoverInfo(ISourceViewer sw,int ln){
returnHey hoo
}
}
$ c $然后我使用反射设置 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());
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?
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());
这篇关于Eclipse - 悬停在标记上时的自定义文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!