在 sublime 中,为什么 def run 在一种情况下工作而不是另一种情况下工作,我该如何使它工作? [英] In sublime, why is def run working in one case and not another case, and how can I make it work?
问题描述
我有一个类 blahtestCommand(sublime_plugin.ApplicationCommand)
运行,但失败了.
另一个类,我用 sublime_plugin.TextCommmand)
工作.
我对运行定义应该是什么样子感到有点困惑.我知道 java(10 年前做过一些 OOP 编程,我记得很清楚),但我对 python 知之甚少.(所以我不太了解一个带参数的类,因为它不在 Java 中,但我会做出一个弱的猜测,它有点像扩展"-继承-或实现").
我还试图确定 ST2 API 文档 中的内容告诉某人,当一个类具有 sublime_plugin.TextCommand
参数时,def 运行行应该看起来像这样 def run(self, edit)
而当一个类具有参数 sublime_plugin.ApplicationCommand
def 运行应该看起来像 - 我不知道是什么.(所以这是一个更大的谜)
注意这里 view.run_('......')
对 blahtest
类不起作用,它不会打印 'aaaaaaaa'
我在控制台中没有收到任何错误.插件-whatever.py 加载良好.因此,一个类运行方法运行,而另一个没有.blahtestCommand 确实加载.我可以在 def run 和 class blahtestCommand 之间放一条线来打印123456789",它会在我保存whatever.py后立即打印,因为它重新加载并且没有错误.只是当我执行 view.run_command('blahtest')
时没有调用它的 run 方法import sublime, sublime_plugin类 blahtestCommand(sublime_plugin.ApplicationCommand):定义运行(自我):打印aaaaaaaaaa"class butthiswillworkCommand(sublime_plugin.TextCommand):def运行(自我,编辑):打印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"} }类 saef4Command(sublime_plugin.WindowCommand):定义运行(自我,字符串):打印uabcccc"
关于在 sublime api 类中运行run",我将来可能会进一步更新这个问题.
对于 #1,你是对的.在 Python 中,这意味着继承.派生类定义的语法类似于 class DerivedClass(BaseClassName):
.
对于#2,Sublime Text 2 支持三种类型的命令.run
方法在您运行命令时被调用.除了必需的参数之外,您还可以为run
定义任意数量的参数.当您运行带有额外参数的命令时,您需要在映射中传递这些参数.
ApplicationCommand
:命令整个 Sublime Text 2. 不需要参数.WindowCommand
:命令对于一个窗口.不需要参数.TextCommand
:命令为了一个观点.一个必需的参数,edit
.
对于#3,如何运行:
ApplicationCommand
:sublime.run_command('application_command_name')
.在 API 参考中检查 sublime 模块的run_command
函数.WindowCommand
:window.run_command('window_command_name')
.检查sublime.Window 的
.run_command
方法TextCommand
:view.run_command('text_command_name')
.检查sublime.View 的
run_command
方法
示例 1:没有额外参数的命令
import sublime, sublime_plugin类 TestApplicationCommand(sublime_plugin.ApplicationCommand):定义运行(自我):打印(运行TestApplicationCommand")导入 sublime, sublime_plugin类 TestWindowCommand(sublime_plugin.WindowCommand):定义运行(自我):打印(运行测试窗口命令")导入 sublime, sublime_plugin类 TestTextCommand(sublime_plugin.TextCommand):def运行(自我,编辑):打印(运行测试文本命令")
运行这些命令:
<预><代码>>>>sublime.run_command('test_application')运行 TestApplicationCommand>>>window.run_command('test_window')运行测试窗口命令>>>view.run_command('test_text')运行 TestTextCommand示例 2:带有额外参数的命令
import sublime, sublime_plugin类 TestApplicationCommand(sublime_plugin.ApplicationCommand):定义运行(自我,arg1,arg2):打印(运行TestApplicationCommand")打印(arg1:"+arg1)打印(arg2:"+ arg2)导入 sublime, sublime_plugin类 TestWindowCommand(sublime_plugin.WindowCommand):定义运行(自我,arg1,arg2):打印(运行测试窗口命令")打印(arg1:"+arg1)打印(arg2:"+arg2)导入 sublime, sublime_plugin类 TestTextCommand(sublime_plugin.TextCommand):def运行(自我,编辑,arg1,arg2):打印(运行测试文本命令")打印(arg1:"+arg1)打印(arg2:"+ arg2)
运行这些命令:
<预><代码>>>>sublime.run_command('test_application', {'arg1' : '1', 'arg2' : '2'})运行 TestApplicationCommand参数 1:1参数 2:2>>>window.run_command('test_window', {'arg1' : '1', 'arg2' : '2'})运行测试窗口命令参数 1:1参数 2:2>>>view.run_command('test_text', {'arg1' : '1', 'arg2' : '2'})运行 TestTextCommand参数 1:1参数 2:2I have a class blahtestCommand(sublime_plugin.ApplicationCommand)
with a run, it fails.
Another class, I have with sublime_plugin.TextCommmand)
works.
I am a little bit baffled with what the run definition should look like. I know java(did some OOP programming 10 years ago which I remember well), but I know very little python. (so I don't quite get about a class taking a parameter, as it wasn't in java but i'd make a weak guess it's a bit like 'extends'-inheritance- or 'implements').
I'm also trying to determine what in the ST2 API documentation would tell somebody that when a class has parameter of sublime_plugin.TextCommand
, that the def run line should look like this def run(self, edit)
whereas when a class has parameter sublime_plugin.ApplicationCommand
the def run should look like - I don't know what. (so that's an even bigger mystery)
Notice here the view.run_('......')
doesn't work for class blahtest
, it's not printing 'aaaaaaaa'
I get no errors at all in the console. The plugin - whatever.py is loading fine. Hence one class run method runs, though the other's doesn't. blahtestCommand does load. I can put a line between def run and class blahtestCommand to print "123456789" and it prints as soon as I save whatever.py 'cos it reloads and no errors. It's just its run method isn't getting 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 working 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"
I might update this question further in future regarding running 'run' in the sublime api classes.
For #1, you are right. In Python, this means inheritance. The syntax for a derived class definition looks like class DerivedClass(BaseClassName):
.
For #2, Sublime Text 2 supports three types of commands. The run
method is invoked when you run a command. Besides the required parameter, you can define as many parameters as you want for run
. When you run a command with extra parameters, you need to pass these parameters in a map.
ApplicationCommand
: command for the whole Sublime Text 2. No required parameter.WindowCommand
: command for a window. No required parameter.TextCommand
: command for a view. One required parameter,edit
.
For #3, how to run:
ApplicationCommand
:sublime.run_command('application_command_name')
. Checkrun_command
function for sublime module in the API reference.WindowCommand
:window.run_command('window_command_name')
. Checkrun_command
method ofsublime.Window
.TextCommand
:view.run_command('text_command_name')
. Checkrun_command
method ofsublime.View
Example 1: commands without extra 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 these 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 extra 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 these 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
这篇关于在 sublime 中,为什么 def run 在一种情况下工作而不是另一种情况下工作,我该如何使它工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!