Eclipse editor plugin: “ERROR” when opening file outside project

自闭症网瘾萝莉.ら 提交于 2019-12-05 02:58:53

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)

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!