我如何获得“IStructuredSelection”当用户选择文本时从编辑器中?

发布于 2024-12-19 18:05:16 字数 903 浏览 1 评论 0原文

我正在开发 eclipse 插件。

我已经成功添加了一个上下文菜单(当用户在 Java 编辑器中并在编辑器中单击鼠标右键时)并将其部分连接到我创建的操作类。

显然,当用户单击编辑器上的任何元素(任何文本)并右键单击打开上下文菜单,然后选择我创建的菜单时,插件将开始跟踪用户选择的选择。但是,它跟踪的选择始终位于 ITextSelection 的实例中,我无法用它做任何有用的事情。我想将选择作为 IStructuredSelection 的实例来获取,以便我的插件可以使用它。 无论如何,我可以做到这一点吗?

public class MyPluginAction extends ActionDelegate implements IEditorActionDelegate {

    public void run(IAction action) {

    }

    public void selectionChanged(IAction action, ISelection selection) {
        if(selection instanceof IStructuredSelection){
            System.out.println("IStructure");
        } else if(selection instanceof ITextSelection){
            System.out.println("IText");
        }
    }

    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
        // TODO Auto-generated method stub
    }
}

I am working on eclipse-plugin.

I have successfully added a context menu (while user is in Java editor and right click in the editor) and connected it partially to action class that I created.

Apparently, when user clicks on any element on the editor (any text) and right click to open the context menu, and select the menu that I created, the plugin will start tracing the selection that user selects. However, the selection that it trace is always in instance of ITextSelection which I cannot do anything useful with it. I want to get selection as an instance of IStructuredSelection so that my plugin can play with it.
Is there anyway that I can do that?

public class MyPluginAction extends ActionDelegate implements IEditorActionDelegate {

    public void run(IAction action) {

    }

    public void selectionChanged(IAction action, ISelection selection) {
        if(selection instanceof IStructuredSelection){
            System.out.println("IStructure");
        } else if(selection instanceof ITextSelection){
            System.out.println("IText");
        }
    }

    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
        // TODO Auto-generated method stub
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-12-26 18:05:16

如果您从编辑器中选择文本,您将始终获得 ITextSelection,而获得 IStructuredSelection 根本没有意义。我想这个问题会解决您的问题。

If you select text from an Editor you'll get always an ITextSelection, getting an IStructuredSelection does not make sense at all. I guess this question will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文