如何在Eclipse插件中以编程方式设置TextEditor的String输入? [英] How to programatically set String input of TextEditor in Eclipse plug-in?
问题描述
我想要的东西:我想为我的Eclipse编辑器插件编写JUnit测试。为此,我想设置我的 TextEditor
扩展的实例。然后,我想从字符串中设置此 TextEditor
的输入,以便该字符串的内容是编辑器实例的输入。然后,我想运行一些测试并断言编辑器标记了错误,依此类推。
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.
失败的地方:我不知道如何从字符串设置编辑器的输入。编辑器唯一要做的功能就是 setInput(IEditorInput input)
,但是我不知道如何创建ÌEditorInput
带有字符串作为内容。
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?
推荐答案
您可以创建一个(工作区)文件为 Greg 建议或实施专门的 IEditorInput
并使用此输入打开文本编辑器。
You can either create a (workspace) file as Greg suggested or implement a specialized IEditorInput
and open a text editor with this input.
例如:
// 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 );
这篇关于如何在Eclipse插件中以编程方式设置TextEditor的String输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!