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