a wayは、デフォルトのJavaエディタでカスタムホバーを作成します。
具体的な使用例についてのこの質問からの@ PKeidelのLangHoverクラスのカスタム実装は、custom script to detect java elementsと組み合わせてあります。 ANNOTATION_NAME
の値をカスタムAnnotationの値に、戻り値を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
がdetectedJavaElements
配列にそれらを置く、ユーザーがオーバーホバリングされたJava要素(複数可)をチェックし、その後に等しい名前の注釈を見つけるために、その配列を確認しているということですANNOTATION_NAME
。