Get associated file extensions for an Eclipse edit

2019-06-14 04:34发布

I'm trying to programmatically retrieve the associated file extensions for a specific editor from within my (DLTK based) Eclipse plugin. The reason for it is that I only want to index files that are associated to my editor, and I need to avoid hardcoding the extensions as users are able to associate any file extension to the editor via the Eclipse preferences.

Example code:

public boolean isValidPluginFile(ISourceModule sourceModule) {

    // currently: 
    if (sourceModule.getUnderlyingResource().getFileExtension().equals("twig")) {
      return true;
    }
    return false;

    // what i would need instead (pseudocode):

    List extensions = Somehow.Retrieve.AssociatedExtensionsFor('MyEditorID');
    for (String extension : extensions) {
       if (sourceModule.getUnderlyingResource().getFileExtension().equals(extension)) {
         return true;
       }
    }

    return false;
}    

1条回答
再贱就再见
2楼-- · 2019-06-14 05:24

You can get all the file editor mappings

IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry();
IFileEditorMapping[] mappings = editorReg.getFileEditorMappings();

and then select only associated to your editorId.

查看更多
登录 后发表回答