Eclipse 编辑器插件:请问如何调用 .getActiveEditor().getEditorInput() 等方法获取原始文本

发布于 2024-07-30 18:48:22 字数 496 浏览 0 评论 0原文

请您帮我捕获编辑器中存在的缓冲文本,我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-08-06 18:48:22

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 上注册为侦听器,则此调用中存在一些不鼓励的访问,您可以跟踪活动选择并避免在工作台中查询活动编辑器。

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
}

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.

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文