我有一堂课blahtestCommand(sublime_plugin.ApplicationCommand),但失败了。
blahtestCommand(sublime_plugin.ApplicationCommand)
另一堂课,我有sublime_plugin.TextCommmand)作品。
sublime_plugin.TextCommmand)
我对运行定义的外观感到困惑。我知道java(十年前做过一些OOP编程,我记得很好),但是我对python的了解很少。(所以我不太了解带有参数的类,因为它不是在Java中,但是我很难猜出它有点像“扩展”-继承-或“实现”)。
我还试图确定ST2 API文档中的内容 将告诉某人,当一个类的参数sublime_plugin.TextCommand为时,def运行行应如下所示; def run(self, edit) 而当一个类的参数 sublime_plugin.ApplicationCommand 为def运行时应如下所示-我不知道知道什么。(这是一个更大的谜)
sublime_plugin.TextCommand
def run(self, edit)
sublime_plugin.ApplicationCommand
请注意,这里的代码view.run_('......') 不适用于类blahtest,它不会打印’aaaaaaaa’
view.run_('......')
blahtest
我在控制台中没有任何错误。插件-what.py加载良好。因此,一个类运行方法运行,而另一个不运行。blahtestCommand确实会加载。我可以在def run和blahtestCommand类之间放置一行以打印“ 123456789”,并且只要保存了what.py’cos,它就会立即打印并且没有错误。只是当我查看时它的run方法没有被调用。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
*通过完全怪异的运气 *添加 了我设法使其适用于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"
我可能会在将来进一步更新有关在sublime api类中运行“ run”的问题。
对于#1,您是对的。在Python中,这意味着继承。派生类定义的语法如下所示class DerivedClass(BaseClassName):。
class DerivedClass(BaseClassName):
Python中的继承
对于#2,Sublime Text 2支持三种类型的命令。run运行命令时将调用该方法。除了必需的参数之外,您还可以根据需要定义任意数量的参数run。当您运行带有附加参数的命令时,需要在映射中传递这些参数。
run
ApplicationCommand
WindowCommand
TextCommand
edit
对于#3,如何运行:
sublime.run_command('application_command_name')
run_command
window.run_command('window_command_name')
sublime.Window
view.run_command('text_command_name')
sublime.View
示例1:不带额外参数的命令
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")
运行以下命令:
>>> sublime.run_command('test_application') running TestApplicationCommand >>> window.run_command('test_window') running TestWindowCommand >>> view.run_command('test_text') running TestTextCommand
示例2:带有附加参数的命令
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)
>>> 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