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

Mic*_*rdt 8 java eclipse-pde eclipse-plugin

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

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

编辑:

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

Mar*_*zar 9

我遇到了同样的问题,终于找到了适合我的解决方案.您必须提供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);
}
Run Code Online (Sandbox Code Playgroud)

然后在你的新文档提供程序(扩展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;
    }
Run Code Online (Sandbox Code Playgroud)

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