In the exalted, why def run works in one case and not in another case, and how can I make it work?

I have a blahtestCommand(sublime_plugin.ApplicationCommand) class blahtestCommand(sublime_plugin.ApplicationCommand) with start, it fails.

Another class, it works with sublime_plugin.TextCommmand) .

I am a little puzzled by what a startup definition should look like. I know java (there was OOP programming 10 years ago, which I remember well), but I know a very small python. (therefore, I don’t quite understand that the class accepts the parameter since it was not in java, but I would make a weak assumption that this is a bit like "extends" -inheritance- or "implements").

I'm also trying to determine what the ST2 API documentation will tell someone that when a class has a sublime_plugin.TextCommand parameter, this def run line should look like this: def run(self, edit) , whereas when the class has a sublime_plugin.ApplicationCommand parameter sublime_plugin.ApplicationCommand , def def should look like this: I don't know what. (so there’s still a big secret)

Note that view.run_('......') does not work for the blahtest class, it does not print 'aaaaaaaa'

There are no errors in the console. Plugin - whatever.py loads fine. Therefore, one method of starting the class is launched, although the other is not. blahtestCommand is really loading. I can put a line between def run and the blahtestCommand class to print β€œ123456789” and it prints as soon as I save any.py because it restarts and no errors. It's just that his start method is not called when I do view.run_command ('blahtest')

 import sublime, sublime_plugin class blahtestCommand(sublime_plugin.ApplicationCommand): def run(self): print "aaaaaaaaaaa" class butthiswillworkCommand(sublime_plugin.TextCommand): def run(self, edit): print "bbbb" 
 >>> view.run_command('blahtest') >>> view.run_command('butthiswillwork') bbbb 

added by complete weird luck I managed to get it to work for WindowCommand

 window.run_command('saef4',{"string":"abcd"}) , {"keys": ["ctrl+s", "ctrl+d"], "command": "saef4", "args": {"string": "abcd"} } class saef4Command(sublime_plugin.WindowCommand): def run(self,string): print "uabcccc" 

In the future, I can clarify this question regarding running "run" in lofty api classes.

+2
python sublimetext2 sublime-text-plugin
source share
1 answer

For # 1, you're right. In Python, this means inheritance. The syntax for defining a derived class looks like class DerivedClass(BaseClassName):

Python Inheritance

For # 2, Sublime Text 2 supports three types of commands. The run method is called when the command is executed. In addition to the required parameter, you can define as many parameters as you want for run . When you run the command with additional parameters, you need to transfer these parameters on the map.

  • ApplicationCommand : command for all Sublime Text 2. There is no required parameter.
  • WindowCommand : command for the window. No required parameter.
  • TextCommand : command for presentation. One required parameter, edit .

For # 3, how to run:

  • ApplicationCommand : sublime.run_command('application_command_name') . Check the run_command function for the sublime module in the API reference .
  • WindowCommand : window.run_command('window_command_name') . Check the run_command method.
  • TextCommand : view.run_command('text_command_name') . Check the run_command method

Example 1: commands without additional parameters

 import sublime, sublime_plugin class TestApplicationCommand(sublime_plugin.ApplicationCommand): def run(self): print("running TestApplicationCommand") import sublime, sublime_plugin class TestWindowCommand(sublime_plugin.WindowCommand): def run(self): print("running TestWindowCommand") import sublime, sublime_plugin class TestTextCommand(sublime_plugin.TextCommand): def run(self, edit): print("running TestTextCommand") 

Run the following commands:

 >>> sublime.run_command('test_application') running TestApplicationCommand >>> window.run_command('test_window') running TestWindowCommand >>> view.run_command('test_text') running TestTextCommand 

Example 2: commands with additional parameters

 import sublime, sublime_plugin class TestApplicationCommand(sublime_plugin.ApplicationCommand): def run(self, arg1, arg2): print("running TestApplicationCommand") print("arg1: " + arg1) print("arg2: " + arg2) import sublime, sublime_plugin class TestWindowCommand(sublime_plugin.WindowCommand): def run(self, arg1, arg2): print("running TestWindowCommand") print("arg1: " + arg1) print("arg2: " + arg2) import sublime, sublime_plugin class TestTextCommand(sublime_plugin.TextCommand): def run(self, edit, arg1, arg2): print("running TestTextCommand") print("arg1: " + arg1) print("arg2: " + arg2) 

Run the following commands:

 >>> sublime.run_command('test_application', {'arg1' : '1', 'arg2' : '2'}) running TestApplicationCommand arg1: 1 arg2: 2 >>> window.run_command('test_window', {'arg1' : '1', 'arg2' : '2'}) running TestWindowCommand arg1: 1 arg2: 2 >>> view.run_command('test_text', {'arg1' : '1', 'arg2' : '2'}) running TestTextCommand arg1: 1 arg2: 2 
+3
source share

All Articles