Java – how do I get the current method from the active eclipse editor?

I'm using an eclipse plug - in that will help me code It is basically a library of string fragments

When creating a new one, I want to give it a category classname MethodName. ID of X

The editor is simple:

IWorkbenchPage page = PlatformUI.getWorkbench()
                        .getActiveWorkbenchWindow().getActivePage();
IEditorPart activeEditor = page.getActiveEditor();
if(activeEditor.getClass().getName().endsWith("CompilationUnitEditor")){
// do something
}

Now... Is there any way to use the eclipse JDT API to get the name of the method where my text cursor is currently located?

Editor: OK With Andrew's help, this is what I got:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart activeEditor = page.getActiveEditor();

if(activeEditor instanceof JavaEditor) {
    ICompilationUnit root = (ICompilationUnit) EditorUtility.getEditorInputJavaElement(activeEditor,false);
    try {
        ITextSelection sel = (ITextSelection) ((JavaEditor) activeEditor)
            .getSelectionProvider().getSelection();
        int offset = sel.getOffset();
        IJavaElement element = root.getElementAt(offset);
        if(element.getElementType() == IJavaElement.METHOD){
            return element.getElementName());
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

It works well, although it's a dirty solution to using restricted classes

Solution

It is uncertain whether a method around the current insertion position is required, or the method being selected by the insertion position I'll give you two

1、 Peripheral method:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart activeEditor = page.getActiveEditor();
if(activeEditor instanceof JavaEditor) {
    IJavaElement elt = ((JavaEditor) activeEditor).getElementAt(((TextSelection) activeEditor.getSelection()).getOffset(),true);
    if (elt.getElementType == IJavaElement.METHOD) {
        return (IMethod) elt;
    }
}
return null;

The important methods are getelementat and getselection

Here is how to find the method currently selected by the caret:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart activeEditor = page.getActiveEditor();
if(activeEditor instanceof JavaEditor) {
    ITypeRoot root = EditorUtility.getEditorInputJavaElement(this,false);
    TextSelection sel = ((TextSelection) activeEditor.getSelection());
    IJavaElement elt = root.codeSelect(sel.getOffset(),sel.getLength();
    if (elt.getElementType == IJavaElement.METHOD) {
        return (IMethod) elt;
    }
}
return null;

The interesting method here is codeselect, which parses the current selection in the context of a given compilation unit or class file

The actual code will be different because you need to check for nulls in many places, but you don't need to perform any other instanceof tests

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>