Eclipse Editor插件:请如何获取原始文本调用类似.getActiveEditor()的内容.getEditorInput()

Ste*_*ume 2 plugins editor eclipse-rcp

请你能帮我把编辑的缓存文本抓到编辑器中,我有这个代码:

System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle() );
System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput() );
Run Code Online (Sandbox Code Playgroud)

我不能按照第一行指示的路径,并且实际上重新读取文件,因为我需要完全文本缓冲区.

在第二行,我总是从类似的org.eclipse.ui.examples.rcp.texteditor.editors.PathEditorInput(我不希望在我的应用程序中包含@runtime)接收路径

请帮助我,tnx

Ric*_*ler 5

IEditorPart.getEditorInput()返回表示编辑器输入的IEditorInput.如果活动编辑器使用PathEditorInput作为输入,则需要将其捆绑或重构代码以不使用示例rcp编辑器输入 - 您提到的PathEditorInput是一个rcp示例.

例如,您可以使用其中一个标准编辑器,如org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor或org.eclipse.ui.editors.text.TextEditor和org.eclipse.ui.part.FileEditorInput.

您是否使用示例向导创建了项目?如果是这样,那将解释示例用法的来源.

至于获取文本,如果它是AbstractTextEditor的实例,则以下代码段将获取编辑器,然后将从文档中检索内容.

请注意,此调用中有一些不鼓励的访问,如果您在SelectionService上注册为侦听器,则可以跟踪活动选择并避免必须查询活动编辑器的工作台.

AbstractTextEditor part = (AbstractTextEditor) Workbench.getInstance()
        .getActiveWorkbenchWindow().getActivePage().getActiveEditor()
        .getAdapter(AbstractTextEditor.class);

if (part != null) {

    IDocument document = part.getDocumentProvider().getDocument(
            part.getEditorInput());

    String content = document.get();

    //do something with the text
}
Run Code Online (Sandbox Code Playgroud)