Eclipse 3.3 Europa JDT TextHover

时间:2023-01-14 11:59:20

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

我想在eclipse中显示我自己的文字悬停一些具体的单词?请给我一些例子

2 个解决方案

#1


You can start by looking at Koder examples.

您可以从查看Koder示例开始。

E.g. this CEditorTextHoverDispatcher or this UCTextHover

例如。这个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);
    }
}

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

您可以在SourceViewerConfiguration中设置TextHover,如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&gt;Preferences&gt;Java&gt;Code Generation&gt;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.

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

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)

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

#2


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

最好的方法是首先使用java编辑器插件和eclipse。以eclipse帮助 - > welcome-> Samples-> Java Editor插件

#1


You can start by looking at Koder examples.

您可以从查看Koder示例开始。

E.g. this CEditorTextHoverDispatcher or this UCTextHover

例如。这个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);
    }
}

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

您可以在SourceViewerConfiguration中设置TextHover,如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&gt;Preferences&gt;Java&gt;Code Generation&gt;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.

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

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)

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

#2


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

最好的方法是首先使用java编辑器插件和eclipse。以eclipse帮助 - > welcome-> Samples-> Java Editor插件