Eclipse 编辑器插件:请问如何调用 .getActiveEditor().getEditorInput() 等方法获取原始文本
请您帮我捕获编辑器中存在的缓冲文本,我有以下代码:
System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle() );
System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput() );
我无法遵循第一行指示的路径,并且实际上无法重新读取文件,因为我恰好需要文本缓冲区。
相反,在第二行中,我总是收到来自铸造类的路径 org.eclipse.ui.examples.rcp.texteditor.editors.PathEditorInput (我不想在我的应用程序中包含@runtime)
请帮助我,tnx
please can you help me to catch the buffered text present into the editor, I have this code:
System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle() );
System.out.println( Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput() );
I can not follow the path indicated by the first line, and pratically re-reading the file because I need exactly the text buffer.
In the second line instead I receive always a path from the casted class
org.eclipse.ui.examples.rcp.texteditor.editors.PathEditorInput
(which I don't want to include @runtime in my app)
Please help me, tnx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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。
您是否使用示例向导创建了项目? 如果是这样,那就可以解释示例用法的来源。
至于获取文本,以下代码片段将获取 Editor(如果它是 AbstractTextEditor 的实例),然后从文档中检索内容。
请注意,如果您在 SelectionService 上注册为侦听器,则此调用中存在一些不鼓励的访问,您可以跟踪活动选择并避免在工作台中查询活动编辑器。
IEditorPart.getEditorInput() returns an IEditorInput representing the editor input. If the active editor is using PathEditorInput as an input you'll need to either bundle it or refactor the code to not use the example rcp editor input - the PathEditorInput you mention is an rcp example.
For example you can use one of the standard editors like org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor or org.eclipse.ui.editors.text.TextEditor and a org.eclipse.ui.part.FileEditorInput.
Did you create your project with an example wizard? If so that would explain where the example usage has come from.
as far as getting the text, the following snippet will obtain the Editor if it is an instance of AbstractTextEditor, then will retrieve the content from the document.
Note there are some discouraged accesses in this call, if you register as a listener on the SelectionService, you can keep track of the active selection and avoid having to query the workbench for the active Editor.