Sublime Forum

Dynamic menu items

#1

Hello everyone,

I created a plugin that has a single command which accepts a single argument. The available argument values are stored in a .sublime-settings file and users can add and change them as they wish. Currently I manually add an entry in the context menu for each argument but it would be nice if this was done dynamically. However, I couldn’t find any way to do this through the API.

I’d rather not have Sublime edit the plugin’s Context.sublime-menu file each time it loads the plugin but at the moment I think this is the only solution. Any ideas how to do this in a nice way (dynamically, through the API)?

To make things clear, here’s an example:

my_plugin.py

import sublime, sublime_plugin

settings = sublime.load_settings('MySettings.sublime-settings')

class MyPluginActionCommand(sublime_plugin.TextCommand):
	def run(self, edit, argument):
		current_argument = settings.get(argument)

		# do stuff based on current_argument

MySettings.sublime-settings

{
	"first_argument": "this is the first available argument",
	"second_argument": "this is the second available argument"
}

Right now, I have to manually create a Context.sublime-menu file but I want the menu items to automatically appear in the context menu:


	{ "id": "end" },
	{ "caption": "-" },
	{
		"caption": "My Plugin",
		"id": "my_plugin",
		"children":
		
			{
				"caption": "First action",
				"command": "my_plugin_action", "args": { "argument" : "first_argument" }
			},
			{
				"caption": "Second action",
				"command": "my_plugin_action", "args": { "argument" : "second_argument" }
			}
		]
	}
]
0 Likes

#2

Maybe this discussion can help?

0 Likes

#3

So I guess the only solution would be to edit the Context.sublime-menu through python, every time my_script.py is loaded.

0 Likes

#4

Looking at Main.sublime-menu I found the following (note the $ variables which seem to generate multiple menu items):

            {
                "caption": "Syntax",
                "mnemonic": "S",
                "id": "syntax",
                "children":  { "command": "$file_types" } ]
            }
[/code][code]
            {
                "caption": "Dictionary",
                "children":  { "command": "$dictionaries" } ]
            }
[/code][code]
            {
                "caption": "Build System",
                "mnemonic": "u",
                "children":
                
                    { "command": "set_build_system", "args": { "file": "" }, "caption": "Automatic", "checkbox": true },
                    { "caption": "-" },
                    { "command": "$project_build_systems" },
                    { "command": "$build_systems" },
                    { "caption": "-" },
                    { "command": "new_build_system", "caption": "New Build System…" }
                ]
            }
[/code][code]
            {
                "caption": "Macros",
                "children":  { "command": "$macros" } ]
            }
[/code][code]
            {
                "caption": "Color Scheme",
                "children":  { "command": "$color_schemes" } ]
            }

That’s exactly what I need. :smile:

0 Likes

#5

There’s no support for this in the API, unfortunately

0 Likes

#6

Alright, thank you for the reply Jon.

0 Likes