weekapaug
weekapaug

Reputation: 332

Adding DYNAMIC arguments to WindowCommand class

THE PROBLEM

In short, below works but only when hardcoded like this.

class GetWindowCommand(sublime_plugin.WindowCommand):

    #NEED VARIABLE INSTEAD OF THE HARDCODED "qv" string
    sublime.active_window().run_command("get_window",{"qualifier": "qv"})
    
    def __init__(self,window):
        self.window=window

    def run(self,qualifier):
        self.projectFolders(qualifier)

    def projectFolders(self,qualifier):
        print(qualifier)

My goal is that when plugin is loaded, it reads the project folders and looks for specific files depending on the folder. As such, I need to access an external variable AS WELL AS the the WindowCommandClass

When doing print(WindowCommandClass) I notice it populates all the methods with the self,window variables and everything works.

In theory I thought I could introduce variables as shown below

qualifier="qv"
print(WindowCommandClass.projectFolders(qualifier))

However, introducing arguments to any method on that class seems to destroy the self and window arguments from the WindowCommandClass. I've only been using python & sublime text api for a couple days, so I have no idea if I'm missing something small or attempting the impossible. Any ideas?

Upvotes: 0

Views: 42

Answers (2)

weekapaug
weekapaug

Reputation: 332

In case anyone is looking to pass values to a class in Sublime Text 3 plugin

Given a class that starts out

class GetWindow(sublime_plugin.WindowCommand)

    def __init__(self,window):
        self.window=window

    def results(self)
        return("just testing")

I was calling INCORRECTLY as

print(GetWindow().results())

What I had to do was supply the function with the class like this.

print(GetWindow(sublime_plugin.WindowCommand).results())

Now to get the variable as mentioned in original post I can do this

qualifier='qv'
results=SublimeID(sublime.window,qualifier).results()

Also modify class & methods to include the variable

def __init__(self,window,qualifier):
    self.qualifier=qualifier

def results(self)
    qualifer=self.qualifier
    # You can now work with this external variable #

Upvotes: 0

mattst
mattst

Reputation: 13970

Your question does not make it clear what the problem is. But have a look at this example and the notes I've made in it, perhaps it will help.

class GetWindowCommand(sublime_plugin.WindowCommand):

    # The following call should not be anywhere in your plugin at all
    # unless for some reason you want to restart your plugin:
    # sublime.active_window().run_command("get_window", {"qualifier": "qv"})

    def run(self, qualifier=None):
        """ Called by Sublime Text when the plugin is run. """

        # self.window is set by Sublime - do NOT set it in a __init__() method.

        # The 'qualifier' variable will hold whatever the 'qualifier' arg
        # was when the plugin was launched, e.g. 'foo' in my example below,
        # or None if the plugin was started without the 'qualifier' arg set.

        project_data = self.window.project_data()
        if not project_data:
            return

        project_folders = project_data.get("folders", [])
        if not project_folders:
            print("No project folders have been set.")
            return

        for folder in project_folders:
            print(folder)

You could launch your plugin by assigning a key binding in your user keys file:

{ "keys": ["ctrl+f0"], "command": "get_window", "args": {"qualifier": "foo" } },

Upvotes: 1

Related Questions