views:

1230

answers:

3

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

A: 

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.

GreenShadow
I'm not calling the editor at all, I'm opening a file via eclipse's menu. It's a specific file type whose extension causes it to be opened in my editor plugin. I merely mentioned Java as an example of an editor where this works.
Michael Borgwardt
+1  A: 

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.

jamesh
That definitely sounds like it could be the cause - will check that when I can (I'm away from that code right now as well :)
Michael Borgwardt
+2  A: 

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)

Martin Lazar