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

1

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.

Sapajou answered 23/10, 2013 at 3:23 Comment(8)
as a bug inspection, try returning the text and calling print function in the interpreter.Luminescent
@Luminescent i'm really knew to python, have hardly used it outside of this sublime plugin. and don't quite get what you're asking me to try. like, what code to add, what command to try. Are you asking me to add a return statement to the run method to return text? (from what I recall from java, the main method was void.. maybe python is similar) and what do you mean calling 'print function' in the interpreter. Do you mean print "abcdefg" of course that works.Sapajou
What about calling the run method of an instance of blahtestCommand straight away?Tarrel
try closing and reopening sublime, and see if blahtestCommand runs...Shawntashawwal
@Shawntashawwal what? what code do you think I changed? note that in the answer to the other question(if that's what you're referring to?) it was ApplicationCommand that was subclassed in the guy's answer, I never subclassed WindowCommand and neither did he.Sapajou
@Tarrel class classabcd: blah=blahtestCommand() blah.run() <-- works so yeah " calling the run method of an instance of blahtestCommand" (while almost beyond my current python abilities!) does work. so, it's the view.run_command('..') is what isn't working on blahtest. which is an issue.Sapajou
@Shawntashawwal though I have now added regarding the WindowCommand.Sapajou
Nobody has actually said what exactly your problem is, but it's this: view .run_command('blahtest') should be sublime .run_command('blahtest'). ApplicationCommands are defined for sublime while TextCommands are defined for views.Ruyle
D
4

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.

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
Dyad answered 23/10, 2013 at 5:58 Comment(6)
Why would you need to import it multiple times?Tarrel
@Tarrel The codes were copied from multiple files. I just kept the import statement.Dyad
looks brilliant, thanks, i'll try it when I get backSapajou
sorry it took a while for me to get round to trying it. That is absolutely magnificent!Sapajou
Where in the documentation mentioned that ApplicationCommand is called by sublime.run_command ?sublimetext.com/docs/2/api_reference.html for WindowCommand it mentions the Window object - ok. For TextCommand it mentions the View object. But where with ApplicationCommand does it indicate that its run method can be called by sublime.run_command(..)?Sapajou
@Sapajou Please check run_command function doc of sublime module: Runs the named ApplicationCommand with the (optional) given arguments.Dyad

© 2022 - 2024 — McMap. All rights reserved.