How to programatically set String input of TextEditor in Eclipse plug-in?

怎甘沉沦 提交于 2019-12-12 22:14:55

问题


What I want: I want to write JUnit tests for my Eclipse editor plug-in. For this I want to set up an instance of my TextEditor extension. Then I want to set the input of this TextEditor from a String, so that the content of the String is the input of the editor instance. Then I want to run some tests and assert that the editor marks errors and so on.

Where I fail: I don't know how to set up the input of the editor from a String. The only function the editor has to do so is setInput(IEditorInput input), but I don't know how to create an ÌEditorInput with a String as it's content.

Is there any possibility to do so and, if not, any other way to set the input of the editor to a given String?


回答1:


You can either create a (workspace) file as Greg suggested or implement a specialized IEditorInput and open a text editor with this input.

For example:

// uninteresting methods left out for brevity

class StringEditorInput implements IStorageEditorInput {
  @Override
  public boolean exists() {
    return true;
  }

  @Override
  public IStorage getStorage() throws CoreException {
    return new StringStorage();
  }
}

class StringStorage implements IStorage {
  @Override
  public InputStream getContents() throws CoreException {
    return new ByteArrayInputStream( "Hello editor!".getBytes( StandardCharsets.UTF_8 ) );
  }
}

String editorId = "org.eclipse.ui.DefaultTextEditor";
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = IDE.openEditor( page , new StringEditorInput(), editorId );


来源:https://stackoverflow.com/questions/42027933/how-to-programatically-set-string-input-of-texteditor-in-eclipse-plug-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!