8

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

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

编辑:

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

4

3 回答 3

8

我遇到了同样的问题,终于找到了适合我的解决方案。您必须提供 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)中插入如下内容:

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 的开源 HTML 编辑器插件)

于 2009-03-25T13:12:09.033 回答
2

我现在离源代码有点远,虽然我怀疑问题是ClassCastException

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

不同之处在于您如何从IEditorInput. JDT 进行显式instanceof检查以进行切换。

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

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

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

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

于 2009-01-20T08:56:29.193 回答
0

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

使用文件路径调用编辑器时,在文件路径的开头加上“file://”。例如:如果路径是C://temp//Sample.java,则修改为file://C ://temp//Sample.java。

于 2009-01-18T17:57:45.013 回答