将操作悬停在 Eclipse 中自己的注释上(插件)

发布于 2024-11-19 04:12:19 字数 271 浏览 3 评论 0原文

我创建了一个使用自己的注释标记的插件。现在,当我将鼠标悬停在这个特殊标记上时,我想添加一个特殊的悬停操作。我真的不知道在哪里添加这个动作。我已经阅读了有关 IAnnotationHover 界面的内容,但是如何访问普通工作台文本编辑器的垂直标尺来添加/更改 AnnotationHover?

ps:更准确地说,我使用 Eclipse 的通用编辑器,而不是自己的编辑器...所以我认为我不应该覆盖 SourceViewerConfiguration 或 IAnnotationHover 到目前为止有什么想法吗?

I have created a Plugin that uses an own Annotation Marker. Now I want to add a special Hover Action, when I mouseover this special Marker. I don't really know where to add this action. I allready read about the IAnnotationHover Interface, but how do I access the vertical ruler of the ordinary workbench text editor to add/change the AnnotationHover?

p.s.: just to be more precise, I use the common editor of Eclipse, not an own editor... so I think I should not override SourceViewerConfiguration or the IAnnotationHover
any idea so far?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2024-11-26 04:12:19

这是在默认 Java 编辑器上创建自定义悬停的一种方法

这是针对您的特定用例的 @PKeidel 的 LangHover 类的自定义实现,结合 用于检测 java 元素的自定义脚本。请注意,您应该将 ANNOTATION_NAME 的值更改为自定义注释的值,并将 getHoverInfo 的返回值更改为您希望自定义悬停包含的任何内容。

public class LangHover implements IJavaEditorTextHover
{
    public static final String ANNOTATION_NAME = "YourCustomAnnotation";

    @SuppressWarnings("restriction")
    public static ICodeAssist getCodeAssist(IEditorPart fEditor)
    {
        if (fEditor != null) {
            IEditorInput input= fEditor.getEditorInput();
            if (input instanceof IClassFileEditorInput) {
                IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
                return cfeInput.getClassFile();
            }

            WorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
            return manager.getWorkingCopy(input, false);
        }

        return null;
    }

    public static IEditorPart getActiveEditor()
    {
        IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window != null) {
            IWorkbenchPage page= window.getActivePage();
            if (page != null) {
                return page.getActiveEditor();
            }
        }
        return null;
    } 

    // When this returns true, the custom hover should be shown.
    public static boolean elementIsCustomAnnotation(ITextViewer textViewer, IRegion hoverRegion)
    {
        IEditorPart activeEditor = getActiveEditor();
        ICodeAssist resolve = getCodeAssist(activeEditor);
        IJavaElement[] detectedJavaElements = null;

        if (resolve != null)
        {
            try
            {
                detectedJavaElements = resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
            }
            catch (JavaModelException x)
            {
                System.out.println("JavaModelException occured");
            }
        }

        for (IJavaElement javaElement : detectedJavaElements)
        {
            // If I found an element of type IJavaElement.ANNOTATION
            // and its name equals ANNOTATION_NAME, return true
            if (javaElement.getElementType() == IJavaElement.ANNOTATION && javaElement.getElementName().equals(ANNOTATION_NAME))
            {
                return true;
            }
        }
        return false;           
    }

    @Override
    public String getHoverInfo(ITextViewer textviewer, IRegion region)
    {
        if(elementIsCustomAnnotation(textviewer, region))
        {
            return "Your own hover text goes here"";
        }
        return null; // Shows the default Hover (Java Docs)
    }
}

基本上,这里发生的事情是 elementIsCustomAnnotation 正在检查用户悬停在其上的 java 元素,将它们放入 DetectedJavaElements 数组中,然后检查该数组以查找名称等于 ANNOTATION_NAME 的注释。

Here's a way to create a custom Hover on the Default Java Editor.

Here's a custom implementation of @PKeidel's LangHover class from that question for your specific use case, combined with a custom script to detect java elements. Note that you should change ANNOTATION_NAME's value to the value of your custom Annotation, and the return value of getHoverInfo to whatever you want the custom hover to contain.

public class LangHover implements IJavaEditorTextHover
{
    public static final String ANNOTATION_NAME = "YourCustomAnnotation";

    @SuppressWarnings("restriction")
    public static ICodeAssist getCodeAssist(IEditorPart fEditor)
    {
        if (fEditor != null) {
            IEditorInput input= fEditor.getEditorInput();
            if (input instanceof IClassFileEditorInput) {
                IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
                return cfeInput.getClassFile();
            }

            WorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
            return manager.getWorkingCopy(input, false);
        }

        return null;
    }

    public static IEditorPart getActiveEditor()
    {
        IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window != null) {
            IWorkbenchPage page= window.getActivePage();
            if (page != null) {
                return page.getActiveEditor();
            }
        }
        return null;
    } 

    // When this returns true, the custom hover should be shown.
    public static boolean elementIsCustomAnnotation(ITextViewer textViewer, IRegion hoverRegion)
    {
        IEditorPart activeEditor = getActiveEditor();
        ICodeAssist resolve = getCodeAssist(activeEditor);
        IJavaElement[] detectedJavaElements = null;

        if (resolve != null)
        {
            try
            {
                detectedJavaElements = resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
            }
            catch (JavaModelException x)
            {
                System.out.println("JavaModelException occured");
            }
        }

        for (IJavaElement javaElement : detectedJavaElements)
        {
            // If I found an element of type IJavaElement.ANNOTATION
            // and its name equals ANNOTATION_NAME, return true
            if (javaElement.getElementType() == IJavaElement.ANNOTATION && javaElement.getElementName().equals(ANNOTATION_NAME))
            {
                return true;
            }
        }
        return false;           
    }

    @Override
    public String getHoverInfo(ITextViewer textviewer, IRegion region)
    {
        if(elementIsCustomAnnotation(textviewer, region))
        {
            return "Your own hover text goes here"";
        }
        return null; // Shows the default Hover (Java Docs)
    }
}

Basically, what is happening here is that elementIsCustomAnnotation is checking the java element(s) the user is hovering over, puts them in the detectedJavaElements array, and then checks that array to find an Annotation with name equal to ANNOTATION_NAME.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文