Sublime Forum

Is there a way to know if a command exists?

#1

Hi!

Is there a way to know if a command exists? And if yes, where it’s defined? Because the command encode_html_entities looks like it’s doing nothing, so I was looking for the code (search in the default folder for EncodeHtmlEntitiesCommand, but I haven’t found anything.

Matt

1 Like

#2

@math2001 /Packages/HTML/encode_html_entities.py.

0 Likes

#3

You can view all the commands doing the following:

print(sublime_plugin.all_command_classes)

It will print all the classes.

<class 'Diff.diff.DiffChangesCommand'>, <class 'HTML.encode_html_entities.EncodeHtmlEntities'>

You could create a plugin that searches all the classes for a given class name if you like.

3 Likes

#4

:expressionless: I’m stupid…

@facelessuser Thanks, but do you an answer to my initial question? Woops, answered my question in the same time.

0 Likes

#5

More specifically:

The all_command_classes is an array containing arrays of all three types of commands: Application, Window, and Text. If you access __module__ you can get the module location. And if you access __name__, you will get the class name.

>>> print(sublime_plugin.all_command_classes[0][0].__module__)
Default.echo
>>> print(sublime_plugin.all_command_classes[0][0].__name__)
EchoCommand
0 Likes

#6

Thanks for the __module__, never noticed that one.

But, I realised that you can create command without a Command at the end. I thought it was mandatory…

class FmRename(sublime.ApplicationCommand):
    def run(self):
        print('rename')

is working… This is a bit weird

0 Likes

#7

Yeah, the Command part is actually optional, but I always append it (except when I accidentally forget it – which I have on occasion).

1 Like

#8

I believe that’s just a convention, but it’s not mandatory. If the class ends with Command it’s stripped away, and then a conversion is done from CamelCase to snake_case.

0 Likes

#9

The Edit Preferences package has a list of all registered commands and allows navigating to their definition. Unfortunately, it doesn’t work correctly for .sublime-package files yet and I still haven’t fixed that yet.

1 Like