Technically speaking, plugin classes that you create are generally creating a Command
of some sort (TextCommand
, WindowCommand
or ApplicationCommand
), which has a specific name, and which can be executed by referencing it by name, such as:
- From a menu item (Main menu or context menu)
- From a key binding
- From an entry in a command palette
To do that, your command class would take an argument that allows it to know how you want it to proceed, and then you pass the argument when you bind the button in the menu.
As a simple example:
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit, button):
if button == "first":
print("The first button")
elif button == "second":
print("The second button")
else:
print("Unknown button")
This creates a command named example
(it's based on the name of the class) which requires you to pass it an argument named button
that tells it what to do.
In the menu, you would specify the name of the command and the arguments to pass. For example:
{
"caption": "The first thing",
"command": "example",
"args": { "button": "first" }
}
Of course, you could use a name for the argument that was more descriptive than button
, it could be a number instead of a string, and so on.
Note that when you define a command like this, it absolutely requires that you pass it the button
argument; If you forget, seemingly nothing will happen, but you'll see something like this in the console:
Traceback (most recent call last):
File "/home/tmartin/local/sublime_text_3_3141/sublime_plugin.py", line 818, in run_
return self.run(edit)
TypeError: run() missing 1 required positional argument: 'button'