-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dictionary mapping command_id
to its associated MenuCommand
s for each plugin
#348
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jni
reviewed
Jun 21, 2024
src/npe2/_plugin_manager.py
Outdated
Comment on lines
370
to
386
for command in manifest.contributions.commands or (): | ||
# command IDs are keys in map | ||
# each value is a dict menu_id: list of MenuCommands | ||
# for the command and menu | ||
associated = self._get_associated_menus(manifest, command.id) | ||
self._command_menu_map[manifest.name][command.id] = associated | ||
|
||
def _get_associated_menus( | ||
self, manifest: PluginManifest, command_id: str | ||
) -> Dict[str, List[MenuCommand]]: | ||
menus = manifest.contributions.menus or dict() | ||
associated_menus = defaultdict(list) | ||
for menu_id, items in menus.items(): | ||
for item in items: | ||
if getattr(item, "command", "") == command_id: | ||
associated_menus[menu_id].append(item) | ||
return associated_menus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah this is backwards. You should be able to do this in a single pass of the menus:
- iterate over the menus, but
- map commands back to menus
- since you use a defaultdict it's ok if some menu-less commands are missed, but you could add them by keeping a set of already-seen commands and then putting in the missed ones at the end.
So:
Suggested change
for command in manifest.contributions.commands or (): | |
# command IDs are keys in map | |
# each value is a dict menu_id: list of MenuCommands | |
# for the command and menu | |
associated = self._get_associated_menus(manifest, command.id) | |
self._command_menu_map[manifest.name][command.id] = associated | |
def _get_associated_menus( | |
self, manifest: PluginManifest, command_id: str | |
) -> Dict[str, List[MenuCommand]]: | |
menus = manifest.contributions.menus or dict() | |
associated_menus = defaultdict(list) | |
for menu_id, items in menus.items(): | |
for item in items: | |
if getattr(item, "command", "") == command_id: | |
associated_menus[menu_id].append(item) | |
return associated_menus | |
menu_map = self._command_menu_map[manifest.name] # just for conciseness below | |
for menu_id, menu_items in manifest.contributions.menus.items() or (): | |
# command IDs are keys in map | |
# each value is a dict menu_id: list of MenuCommands | |
# for the command and menu | |
for item in menu_items: | |
if command_id := getattr(item, "command", None) is not None: | |
menu_map[command_id].append(item) |
jni
approved these changes
Jun 21, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adding this dictionary to support quick mapping of widgets/commands to their menus for implementing contributable menus . Prior to this PR, you would have to search all menu contributions for the command of the contribution you were currently processing. This map allows you direct access to the menus this command needs to live in.
This is a precursor to potentially rearchitecting the manifest schema entirely to be "command-first".