java - Eclipse 编辑器插件 : "ERROR" when opening file outside project

标签 java eclipse-plugin eclipse-pde

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

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

编辑:

我发现问题的根源与 jamesh 提到的很接近,但不是 ClassCastException - 文本查看器根本没有要显示的 IDocument 实例,因为 StorageDocumentProvider. createDocument() 返回 null。这样做的原因是它只知道如何为 org.eclipse.ui.IStorageEditorInput 的实例创建文档,但在这种情况下它获取 org.eclipse.ui.ide 的实例.FileStoreEditorInput,它不实现该接口(interface),而是实现 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)中插入如下内容:

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 编辑器插件)复制了这个解决方案

关于java - Eclipse 编辑器插件 : "ERROR" when opening file outside project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/455210/

相关文章:

java - 作业 : Magic Plant Recursion Exercise in Java

javascript - Eclipse Javascript 插件扩展

java - 带有非 OSGi 应用程序的 Eclipse native 启动器

tomcat - 如何编译从底层 webapp 引用额外系统包的 Eclipse 插件项目?

java - 似乎 xpath 翻译在 java8 中没有按预期工作

java - 使用 CAMERA 时应用程序崩溃 - Android

java - 如何让 Tortoise 在 bin 中看不到 .java?

eclipse-plugin - 错误 : No repository found at http://rlogiacco. github.com/Natural

c# - 从 C# 程序调用 Eclipse 插件中的 Java 方法

eclipse-rcp - Eclipse PDE headless 构建失败,出现 : failed to create task or type eclipse. generateFeature