Dynamic menu items
Importance: minor
Description: Fill a menu with items that are generated on the fly by plugins, like the syntax or color scheme selection, or recent files/projects in the main menu.
Motivation: Many plugins would like to add items to a menu dynamically, which is the only thing you can’t do with menus right now.
Details:
In a .sublime-menu
file, you provide a { "source": "key" }
entry for a menu item. If a menu is to be displayed with an entry like this, ST would go through all EventListeners and call their on_query_menu
callback with the value of "source"
as the first parameter, similar to completions. Plugins would then be able to provide a list of menu items of the same structure as in .sublime-menu
files, including the ability to define more { "source": "key" }
items.
Alternatively, the { "command": "$key" }
syntax, as found in Main.sublime-menu
, could be used.
Example:
Main.sublime-menu
[
{
"id": "preferences",
"children": [
{
"caption": "Theme",
"children": [
{ "source": "my_menu" }
]
}
]
}
]
some.py
class MenuExpander(sublime_plugin.EventListener):
def on_query_menu(self, key):
if key != "my_menu":
return
theme_files = sublime.find_resources("*.sublime-theme")
theme_names = set(path.rpartition("/")[2] for path in theme_files)
return [{"command": "set_theme", "args": {"name": name}}
for name in sorted(theme_names)]
class SetThemeCommand(sublime_plugin.ApplicationCommand):
def description(self, name):
return name
def run(self, name):
prefs = sublime.load_settings("Preferences.sublime-settings")
prefs.set("theme", name)
sublime.save_settings("Preferences.sublime-settings")