简体   繁体   English

如何从活动的Eclipse编辑器中获取当前方法?

[英]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. 我目前正在开发一个Eclipse插件,它可以帮助我编码。 Basically a library of String snippets. 基本上是一个String片段库。

When creating a new one, I'd love to give it an ID of sorts ClassName.MethodName.X. 在创建一个新的时,我很乐意给它一个Class of Class Class.MethodName.X的ID。

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? 现在......有没有办法使用Eclipse jdt API来获取我的文本光标当前所在方法的名称?

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. 虽然使用受限制的类是一种肮脏的解决方案。

Here the same done without using internal Eclipse APIs: 在不使用内部Eclipse API的情况下完成相同的操作:

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;

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 . 重要的方法是getElementAtgetSelection

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. 这里有趣的方法是codeSelect ,它解析给定编译单元或类文件的上下文中的当前选择。

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. 实际代码将有所不同,因为您需要在许多地方检查null,但您不需要执行任何其他实例测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM