In sublime, why is def run working in one case and not another case, and how can I make it work?

后端 未结 1 1510
情歌与酒
情歌与酒 2021-01-06 20:39

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

Another class, I have with sublime_plugin.TextCommmand)

相关标签:
1条回答
  • 2021-01-06 21:12

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

    Inheritance in Python

    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'). Check run_command function for sublime module in the API reference.
    • WindowCommand: window.run_command('window_command_name'). Check run_command method of sublime.Window.
    • TextCommand: view.run_command('text_command_name'). Check run_command method of sublime.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
    
    0 讨论(0)
提交回复
热议问题