问题
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