Eclipse 编辑器插件:“错误” 在项目外部打开文件时

发布于 2024-07-12 05:21:59 字数 607 浏览 5 评论 0原文

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

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

编辑:

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

I'm developing an editor plugin for eclipse. It works fine on files within eclipse projects, but when an external file is opened via the "File -> Open File" menu (which works file with, e.g. Java files), I get a page displaying nothing but a horizontal blue line and the word "ERROR". The Error Log of eclipse is empty, as is the log file in the .metadata directory.

What could cause this? How can I diagnose the error when I have no error message that tells me where to look? There doesn't seem to be a way to get more detailed logging from eclipse.

Edit:

I've found that the source of the problem is close to what jamesh mentioned, but not a ClassCastException - there simply is no IDocument instance for the text viewer to display because StorageDocumentProvider.createDocument() returns null. The reason for this is that it only knows how to create documents for instances of org.eclipse.ui.IStorageEditorInput, but in this case it gets an instance of org.eclipse.ui.ide.FileStoreEditorInput, which does not implement that interface, but instead implements org.eclipse.ui.IURIEditorInput

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各空 2024-07-19 05:21:59

我遇到了同样的问题,终于找到了适合我的解决方案。
您必须提供 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 编辑器插件)复制了这个解决方案

I had the same probleam and finally found solution working for me.
You have to provide 2 different document providers - first extending FileDocumentProvider for files inside your workbench, and second extending TextFileDocumentProvider for other resources outside your workspace. Then you register the right provider acording to the input in your editors doSetInput method like this:

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);
}

then in your new document provider (extending TextFileDocumentProvider) insert somethnig like this:

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;
    }

This works for me :) Finally I have to mention, that I'm not so clever and that I copied this solution from project Amateras (Opensource HTML editor plugin for eclipse)

夏雨凉 2024-07-19 05:21:59

目前我距离源代码还有一点距离,但我怀疑问题是 ClassCastException:

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

区别在于如何从 IEditorInput 获取内容>。 JDT 执行显式 instanceof 检查来进行切换。

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

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

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

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

I'm a little away from the source code at the moment, though I suspect the problem is a ClassCastException:

  • For a workspace file, the IEditorInput is org.eclipse.ui.IFileEditorInput.
  • For a local non-workspace file, the IEditorInput is org.eclipse.ui.IStorageEditorInput

The difference is in how you get the contents from the IEditorInput. The JDT does an explicit instanceof check to make the switch.

I don't think that the getAdapter(Class clazz) will return a java.io.InputStream if you offer it.

I don't quite understand why they do it like this, but it feels ugly.

Edit:
A more general point about debugging eclipse apps - it's really very useful to try and assemble all your logs into one place (i.e. the console).

To do this, make sure you use the command line options -console and -consoleLog. The latter has helped save countless hours of time. If you haven't already, learn the most basic things about how to use the console (ss and start are my most often used). This will save some more time diagnosing a certain class of problem.

爱的故事 2024-07-19 05:21:59

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

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

Did you try creating a JAVA file using the editor, outside the workspace?

When calling the editor with the file path, concat "file://" at the beginning of the file path.e.g: if the path is C://temp//Sample.java, then modify it as file://C://temp//Sample.java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文