2
votes

I'm currently writing an Eclipse plugin. In it, I want to programmatically open an editor and select a portion of the text. The opened file does not have to be imported into the workspace (that's why I'm using IFileStore in the code below).

I'm using code similar to this:

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));

For Java files it works fine, but for XML Schema (XSD), it doesn't. The editor opens, but no text is selected.

From debugging, I can tell that the part is of type org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor, and the selection manager is an org.eclipse.wst.xsd.ui.internal.adt.editor.CommonSelectionManager

I'm targetting Eclipse Mars and Neon, it does not seem to work for both.

What can I do to make it work? Or at least find some further information?

1
You are going to have to read the source code of the editor to see what it supports. - greg-449
Thanks for the reply. Do you have any clue where I can find the source repository for these classes? I found a download with source JARs, but not the git repo. From what I've seen so far, it looks like it's currently not supported, but I'd like to investigate this a bit further. - Tobias B.
I think this is part of Eclipse WTP (Web Tools Project). The git locations for that are listed here - greg-449

1 Answers

0
votes

After having a look at the WTP code, this does not seem to be supported at the moment. But I found a workaround by explicitly checking if the editor is a multi part editor:

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);
    }
}

I was not sure whether it is better to send the selection to all sub parts or only to one specific, but so far sending it to all seems to work.