Home Download Buy Blog Forum Support

Add new commands

Add new commands

Postby JamesAguilar on Sun Mar 13, 2011 1:01 am

This is a super basic newbie question, but I have been looking around for about an hour and I still can't figure out how to do it. The question is this: how do you add new commands? Or to put it another way, how do you connect menu items and key bindings with python commands that you have written?

For example, I was trying to find out how the toggle_comment command referred to in Main.sublime-menu connects to the ToggleCommentCommand in comment.py. I made my own file named test.py and renamed ToggleCommentCommand to ToggleCommentTestCommand. I added a menu item that was an exact copy of the Comment menu item, but I changed the names to match the names in my test.py file:

Code: Select all
            {
                "caption": "Test",
                "id": "test",
                "children":
                [
                    { "command": "toggle_comment_test", "args": {"block": false}, "caption": "Toggle Comment" },
                    { "command": "toggle_comment_test", "args": {"block": true}, "caption": "Toggle Block Comment" }
                ]
            },

The menu items get added but they are grayed out. If I use the "toggle_comment" command instead, they work.

I can't figure out how ToggleCommentCommand gets connected up to toggle_comment. Could someone explain it?
JamesAguilar
 
Posts: 5
Joined: Wed Mar 09, 2011 10:08 pm

Re: Add new commands

Postby JamesAguilar on Sun Mar 13, 2011 4:41 am

For anyone reading this, I think I've figured it out. I'm still not sure why what I was trying wasn't working, but when you make a python class inherited from one of the commands, you can refer to it from keymaps and menus via the underscore-separated version of the name. For example:

Python class: SomeTestCommand
In keymap: some_test
JamesAguilar
 
Posts: 5
Joined: Wed Mar 09, 2011 10:08 pm


Return to Plugin Development

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests