在 Sublime 中,为什么 def run 在一种情况下起作用,而在另一种情况下不起作用,我怎样才能让它工作?

2023-11-29

我有课blahtestCommand(sublime_plugin.ApplicationCommand)运行时,它会失败。

另一堂课,我和sublime_plugin.TextCommmand) works.

我对运行定义应该是什么样子有点困惑。我了解java(10年前做过一些OOP编程,我记得很清楚),但我对Python知之甚少。 (所以我不太了解一个带有参数的类,因为它不是在java中,但我猜测它有点像“扩展”-继承-或“实现”)。

我也在尝试确定其中的内容ST2 API 文档会告诉某人,当一个类的参数为sublime_plugin.TextCommand, def run 行应该是这样的def run(self, edit)而当一个类有参数时sublime_plugin.ApplicationCommanddef 运行应该看起来像 - 我不知道是什么。 (所以这是一个更大的谜)

注意这里view.run_('......')不适合上课blahtest,它没有打印“aaaaaaaa”

我在控制台中没有收到任何错误。插件 -whatever.py 加载正常。因此,一个类的 run 方法会运行,而另一个类的 run 方法不会运行。 blahtestCommand 确实加载。我可以在 def run 和 class blahtestCommand 之间放一行来打印“123456789”,一旦我保存whatever.py,它就会打印,因为它会重新加载并且没有错误。只是当我执行 view.run_command('blahtest') 时,它的 run 方法没有被调用

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完全奇怪的运气我设法让它为 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):.

Python 中的继承

对于#2,Sublime Text 2 支持三种类型的命令。这run当您运行命令时会调用方法。除了所需的参数外,您还可以定义任意数量的参数run。当您运行带有额外参数的命令时,您需要在映射中传递这些参数。

  • ApplicationCommand:整个 Sublime Text 2 的命令。无需参数。
  • WindowCommand:窗口命令。没有必需的参数。
  • TextCommand:查看命令。一个必需的参数,edit.

对于#3,如何运行:

  • ApplicationCommand: sublime.run_command('application_command_name'). Check run_commandsublime 模块中的函数API参考.
  • WindowCommand: window.run_command('window_command_name'). Check run_command的方法sublime.Window.
  • TextCommand: view.run_command('text_command_name'). Check run_command的方法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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Sublime 中,为什么 def run 在一种情况下起作用,而在另一种情况下不起作用,我怎样才能让它工作? 的相关文章

随机推荐