java - Eclipse 插件开发:如何访问在 Eclipse 编辑器中编写的代码
问题描述
我正在制作一个需要访问在 Eclipse 编辑器中编写的代码的 Eclipse 插件。我已经按照链接中提到的过程进行了操作。 访问 Eclipse 编辑器代码 但它在消息框中显示文件路径而不是代码。IEditorEditor类的getEditorInput()没有按照链接做它应该做的事情。这是我的代码。请帮我找出我做错了什么。
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
IEditorInput input = (IEditorInput) editor.getEditorInput(); // accessing code from eclipse editor
String code = input.toString();
MessageDialog.openInformation(
window.getShell(),
"Project",
code);
return null;
}
解决方案
你可以用两种不同的方式来做到这一点。无论内容是否由磁盘上的文件支持,这种方式都有效。
此方法从编辑器获取文本IDocument
,这是大多数用途存储和访问内容的方式。StyledText
是一个小部件,除非您使用 Widgets 和 Controls 做某事,否则它不是正确的方法。为此,您将从编辑器部分开始,通过ITextEditor
界面,然后将IDocumentProvider
与当前编辑器输入一起使用。这会跳过instanceof
您事先想要做的检查,以及如果这是 a 中的一个页面,您可能需要做的任何事情MultiPageEditorPart
(没有处理这些的标准方法)。
org.eclipse.jface.text.IDocument document =
((org.eclipse.ui.texteditor.ITextEditor)editor).
getDocumentProvider().
getDocument(input);
您可以通过IDocument
.
推荐阅读
- angular - 将角度容器连接到同一主机上的 laravel 容器
- typescript - puppeteer:如何编辑和删除一行?
- python - 如何在 Foundry Nuke Python 中创建弹跳动画?
- java - 使用常规 JDK 编译子项目,将结果添加到我的 Android App
- javascript - 当我将线宽的值分配为 100 时,它会在浏览器中显示矩形的默认线宽
- r - 图例标签组合图(ggplot 和拼凑)
- docker - GCE 上的容器。大容器怎么处理?
- vuetify.js - Vuetify v-container 与填充高度项目对齐
- flutter - 如何修复地理编码器插件安装错误?
- javascript - 在完整日历中刷新特定资源及其事件