Get associated file extensions for an Eclipse editor

送分小仙女□ 提交于 2019-12-08 11:35:05

问题


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:


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.



来源:https://stackoverflow.com/questions/10052864/get-associated-file-extensions-for-an-eclipse-editor

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