对于我的插件,我正在尝试访问 CompilationUnitEditor 中的选定代码。因此,我向上下文菜单添加了一个贡献并使用以下代码:
public class ContextMenuHandler implements IEditorActionDelegate {
    private IEditorPart editorPart;
    @Override
    public void setActiveEditor(IAction action, IEditorPart editorPart) {
        this.editorPart = editorPart;
    }
    @Override
    public void run(IAction action) {
        JavaUI.getEditorInputJavaElement(editorPart.getEditorInput());
    }
    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        if (selection instanceof TextSelection) {
            TextSelection text = (TextSelection) selection;
            System.out.println("Text: " + text.getText());
        } else {
            System.out.println(selection);
        }
    }
}
现在的问题是方法 selectionChanged(...) 仅在我真正选择某些内容时才被调用,以便我可以复制/粘贴它。但我想访问像这样突出显示的代码元素(这里我想获得“IEditorPart”)

不幸的是,我不知道我应该寻找什么。