【问题标题】:Eclipse: Select text in XSD programmatically from pluginEclipse:从插件中以编程方式选择 XSD 中的文本
【发布时间】:2016-12-04 23:49:57
【问题描述】:

我目前正在编写一个 Eclipse 插件。在其中,我想以编程方式打开一个编辑器并选择一部分文本。打开的文件不必导入工作区(这就是我在下面的代码中使用 IFileStore 的原因)。

我正在使用类似这样的代码:

IFileStore fileStore = EFS.getLocalFileSystem().getStore(localPath);
IEditorPart part = IDE.openEditorOnFileStore(page, fileStore);
final int posStart = ...;
final int posEnd = ...;
part.getEditorSite().getSelectionProvider().setSelection(
                    new TextSelection(posStart, posEnd - posStart));

对于 Java 文件,它可以正常工作,但对于 XML Schema (XSD),它就不行了。编辑器打开,但没有选择文本。

通过调试,我可以看出该部件的类型是 org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor,选择管理器是 org.eclipse.wst.xsd.ui.internal.adt .editor.CommonSelectionManager

我的目标是 Eclipse Mars 和 Neon,它似乎对两者都不起作用。

我该怎么做才能让它发挥作用?或者至少找到一些进一步的信息?

【问题讨论】:

  • 您必须阅读编辑器的源代码才能了解它支持什么。
  • 感谢您的回复。你有什么线索可以找到这些类的源代码库吗?我找到了带有源 JAR 的下载,但没有找到 git repo。从我目前看到的情况来看,它目前似乎不受支持,但我想进一步调查一下。
  • 我认为这是 Eclipse WTP(Web 工具项目)的一部分。列出的 git 位置 here

标签: eclipse xsd eclipse-plugin


【解决方案1】:

查看了 WTP 代码后,目前似乎不支持此功能。但是我通过明确检查编辑器是否是多部分编辑器找到了一种解决方法:

private static void setSelection(IEditorPart part, TextSelection textSelection) {
    if (part instanceof MultiPageEditorPart) {
        final MultiPageEditorPart multiPage = (MultiPageEditorPart) part;
        for (final IEditorPart subPart : multiPage.findEditors(multiPage.getEditorInput())) {
            setSelection(subPart, textSelection);
        }
    } else {
        part.getEditorSite().getSelectionProvider().setSelection(textSelection);
    }
}

我不确定将选择发送到所有子部分还是只发送到特定部分更好,但到目前为止,将其发送给所有子部分似乎都有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    相关资源
    最近更新 更多