Eclipse编辑器插件:在项目外打开文件时出现“ERROR”

我正在为eclipse开发一个编辑器插件。 它可以在eclipse项目中的文件上正常工作,但是当通过“文件 – >打开文件”菜单(使用例如Java文件工作文件)打开外部文件时,我得到的页面只显示水平蓝线和单词“ERROR”。 eclipse的错误日志为空,与.metadata目录中的日志文件一样。

什么可能导致这个? 当我没有错误消息告诉我在哪里查看时,如何诊断错误? 似乎没有办法从eclipse获得更详细的日志记录。

编辑:

我发现问题的根源与jamesh提到的相近,但不是ClassCastException – 因为StorageDocumentProvider.createDocument()返回null,因此文本查看器根本没有IDocument实例。 这样做的原因是它只知道如何为org.eclipse.ui.IStorageEditorInput实例创建文档,但在这种情况下,它获取了一个org.eclipse.ui.ide.FileStoreEditorInput的实例,它没有实现该接口,但是实现了org.eclipse.ui.IURIEditorInput

我遇到了同样的问题,最后找到了适合我的解决方案。 您必须提供2个不同的文档提供程序 – 首先为工作台中的文件扩展FileDocumentProvider ,然后为工作区外的其他资源扩展TextFileDocumentProvider 。 然后根据编辑器doSetInput方法中的输入注册正确的提供程序,如下所示:

 private IDocumentProvider createDocumentProvider(IEditorInput input) { if(input instanceof IFileEditorInput){ return new XMLTextDocumentProvider(); } else if(input instanceof IStorageEditorInput){ return new XMLFileDocumentProvider(); } else { return new XMLTextDocumentProvider(); } } @Override protected final void doSetInput(IEditorInput input) throws CoreException { setDocumentProvider(createDocumentProvider(input)); super.doSetInput(input); } 

然后在你的新文档提供程序(扩展TextFileDocumentProvider)中插入somethnig,如下所示:

 protected FileInfo createFileInfo(Object element) throws CoreException { FileInfo info = super.createFileInfo(element); if(info==null){ info = createEmptyFileInfo(); } IDocument document = info.fTextFileBuffer.getDocument(); if (document != null) { /* register your partitioner and other things here same way as in your fisrt document provider */ } return info; } 

这对我有用:)最后我要提一下,我不是那么聪明,我从项目Amateras(eclipse的Opensource HTML编辑器插件)复制了这个解决方案

我目前离源代码有点远,但我怀疑问题是ClassCastException

  • 对于工作空间文件, IEditorInputorg.eclipse.ui.IFileEditorInput
  • 对于本地非工作区文件, IEditorInputorg.eclipse.ui.IStorageEditorInput

区别在于如何从IEditorInput获取内容。 JDT执行明确的检查instanceof以进行切换。

如果你提供它,我认为getAdapter(Class clazz)不会返回java.io.InputStream

我不太明白为什么他们这样做,但感觉很难看。

编辑:关于调试eclipse应用程序的更一般的观点 – 尝试将所有日志组装到一个地方(即控制台)非常有用。

为此,请确保使用命令行选项-console-consoleLog 。 后者帮助节省了无数小时的时间。 如果您还没有,请了解有关如何使用控制台的最基本的事情( ssstart是我最常用的)。 这将节省更多时间来诊断某类问题。

您是否尝试使用工作区外的编辑器创建JAVA文件?

当使用文件路径调用编辑器时,在文件path.eg的开头连接“file://”:如果路径是C://temp//Sample.java,则将其修改为file:// C ://temp//Sample.java。