Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current method from the active Eclipse editor?

I'm currently working on an Eclipse addon which would help me coding. Basically a library of String snippets.

When creating a new one, I'd love to give it an ID of sorts ClassName.MethodName.X.

Getting the editor is pretty straightforward:

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 APIs to get the name of the method my text cursor is currently in?

Edit: Ok. With the help of Andrew, here's 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();
    }
}

Works pretty good. Although it's kind of a dirty solution to use restricted classes.

like image 727
Michael Avatar asked Mar 12 '12 13:03

Michael


2 Answers

Here the same done without using internal Eclipse APIs:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ITextEditor editor = (ITextEditor) page.getActiveEditor();
IJavaElement elem = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
if (elem instanceof ICompilationUnit) {
    ITextSelection sel = (ITextSelection) editor.getSelectionProvider().getSelection();
    IJavaElement selected = ((ICompilationUnit) elem).getElementAt(sel.getOffset());
    if (selected != null && selected.getElementType() == IJavaElement.METHOD) {
         return (IMethod) selected;
    }
}
return null;
like image 74
sevenforce Avatar answered Sep 19 '22 22:09

sevenforce


Not sure if you are asking for the method surrounding the current caret location, or the method that the caret location is selecting. I'll show you both.

First, surrounding 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.

And here is how to find the method that is 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 resolves the current selection in the context of the given compilation unit or class file.

Actual code will be different since you need to check for null in many places, but you should not need to do any other instanceof tests.

like image 21
Andrew Eisenberg Avatar answered Sep 19 '22 22:09

Andrew Eisenberg