Sublime Forum

Dynamic menu caption?

#1

I have a listing in a sub menu.
I would like to display different captions for the different items, all which are dynamically obtained. Much the same way ‘Open recent’ works.

I’ve implemented a

def is_visible(self, index):

function to only show relevant menu items, and that works great.

How can I provide a custom caption?
Is there a function along the lines of has_caption(self, index): that I could implement?

1 Like

#2

There’s an API:

description(<args>)	String	Returns a description of the command with the given arguments. Used in the menu, if no caption is provided. Return None to get the default description.

So something like:

def description(self, *args): return "DESCRIPTION"

Not sure exactly when it is called (once or each times ?)

0 Likes

#3

I tried implementing a function like

def description(self, *args): return "DESCRIPTION"
but it is never called…
Do I need to put something special in my .sublime-menu file to trigger this callback?
I’m using sublime 2.

0 Likes

#4

Tried it right now and it works:
startup, version: 2221 windows x64 channel: stable

\Sublime Text 2\Packages\User\Main.sublime-menu

[code]
{
“id”: “view”,
“children”:

        {
            "command": "example"
        }
    ]
}    

][/code]

\Sublime Text 2\Packages\User\exemple.py

[code]class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
print ‘Hello’

def description(self, *args):
    return "DESCRIPTION"[/code]
0 Likes