3

I 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.

4

1 回答 1

4

对于#1,你是对的。在 Python 中,这意味着继承。派生类定义的语法如下所示class DerivedClass(BaseClassName):

Python中的继承

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

对于#3,如何运行:

  • ApplicationCommand: sublime.run_command('application_command_name'). 在API 参考中检查run_commandsublime 模块的功能。
  • WindowCommand: window.run_command('window_command_name'). 的检查run_command方法sublime.Window
  • TextCommand: view.run_command('text_command_name'). 检查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
于 2013-10-23T05:58:30.913 回答