Eclipse 3.3 Europa JDT TextHover

发布于 2024-07-20 02:51:33 字数 46 浏览 6 评论 0原文

我想在 Eclipse 中显示我自己的文本悬停某些特定单词? 请给我一些例子

I want to show my own text hover in eclipse for some specific words? Please provide me some examples

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

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

发布评论

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

评论(2

小耗子 2024-07-27 02:51:33

您可以首先查看 Koder 示例

例如这个 CEditorTextHoverDispatcher 或这个 UCTextHover

package com.ut2003.uceditor;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.graphics.Point;

public class UCTextHover implements ITextHover
{

    /* (non-Javadoc)
     * Method declared on ITextHover
     */
    public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion)
    {
        if (hoverRegion != null)
        {
            try
            {
                if (hoverRegion.getLength() > -1)
                    return textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
            }
            catch (BadLocationException x)
            {
            }
        }
        return "Empty Selection";
    }

    /* (non-Javadoc)
     * Method declared on ITextHover
     */
    public IRegion getHoverRegion(ITextViewer textViewer, int offset)
    {
        Point selection = textViewer.getSelectedRange();
        if (selection.x <= offset && offset < selection.x + selection.y)
            return new Region(selection.x, selection.y);
        return new Region(offset, 0);
    }
}

您可以在 SourceViewerConfiguration 中设置 TextHover,如下所示 < a href="http://www.koders.com/java/fid441801AFBF843A71F77B2AD1DCF4FE3FEED01A64.aspx?s=ITextHover#L20" rel="nofollow noreferrer">GasSourceViewerConfiguration 或此 CalcSourceViewerConfiguration

package com.example.calc.ui.editors;

import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

/**
 * @author cdaly
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CalcSourceViewerConfiguration extends SourceViewerConfiguration {

    private CalcEditor _editor;

    public CalcSourceViewerConfiguration(CalcEditor editor){
        _editor = editor;
    }



    /* (non-Javadoc)
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
     */
    public IReconciler getReconciler(ISourceViewer sourceViewer) {
        return new MonoReconciler(_editor.getReconcilingStrategy(), false);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
     */
    public ITextHover getTextHover(
        ISourceViewer sourceViewer,
        String contentType) {
        ITextHover hover;
        if (_editor != null && _editor instanceof CalcEditor) {
            hover = new CalcTextHover((CalcEditor)_editor);
        } else {
            hover = null;
        }
        return hover;
    }

}

除此之外,我没有太多更多信息:我发现的示例更多的是编程性而不是声明性(即“plugin.xml”),因此您可能需要探索更多代码。

另一个很好的例子:Eclipse:Rich Hovers Redux (虽然它是针对 eclipse3.4 的,但完整的示例可以给您另一个关于如何将自定义 ITextHover 添加到当前编辑器的提示)

You can start by looking at Koder examples.

E.g. this CEditorTextHoverDispatcher or this UCTextHover

package com.ut2003.uceditor;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.graphics.Point;

public class UCTextHover implements ITextHover
{

    /* (non-Javadoc)
     * Method declared on ITextHover
     */
    public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion)
    {
        if (hoverRegion != null)
        {
            try
            {
                if (hoverRegion.getLength() > -1)
                    return textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
            }
            catch (BadLocationException x)
            {
            }
        }
        return "Empty Selection";
    }

    /* (non-Javadoc)
     * Method declared on ITextHover
     */
    public IRegion getHoverRegion(ITextViewer textViewer, int offset)
    {
        Point selection = textViewer.getSelectedRange();
        if (selection.x <= offset && offset < selection.x + selection.y)
            return new Region(selection.x, selection.y);
        return new Region(offset, 0);
    }
}

You would set a TextHover in a SourceViewerConfiguration like this GasSourceViewerConfiguration or this CalcSourceViewerConfiguration

package com.example.calc.ui.editors;

import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

/**
 * @author cdaly
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CalcSourceViewerConfiguration extends SourceViewerConfiguration {

    private CalcEditor _editor;

    public CalcSourceViewerConfiguration(CalcEditor editor){
        _editor = editor;
    }



    /* (non-Javadoc)
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
     */
    public IReconciler getReconciler(ISourceViewer sourceViewer) {
        return new MonoReconciler(_editor.getReconcilingStrategy(), false);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
     */
    public ITextHover getTextHover(
        ISourceViewer sourceViewer,
        String contentType) {
        ITextHover hover;
        if (_editor != null && _editor instanceof CalcEditor) {
            hover = new CalcTextHover((CalcEditor)_editor);
        } else {
            hover = null;
        }
        return hover;
    }

}

Beyond that, I have not much more information: the examples I have found are more programmatic than declarative (i.e. "plugin.xml"), so you may want to explore some more code.

Another good example: Eclipse: Rich Hovers Redux (it is for eclipse3.4 though, but the full example can give you another hint at how a custom ITextHover is added to the current editor)

摘星┃星的人 2024-07-27 02:51:33

最好的办法是首先使用 java 编辑器插件和 eclipse。 获取eclipse帮助->欢迎->示例->Java编辑器插件

The best thing is to use the java editor plugin along with eclipse first. Take eclipse help ->welcome->Samples->Java Editor plugin

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