Sublime Forum

Prevent concurrent run of callback?

#1

Hi,

Im new to phyton and sublime plugin development.
I am now creating a plugin and at some point I want to get info from the user, and use that info in the next lines of code.

I have figured out how to get the info, but the lines after the user input are executed before the callback function.

Here’s what I mean:

window.show_input_panel("What is the new function name?", '', self.onNewFcNameGiven, None, None)
self.runCommandLine(self.view.file_name(), self.firstLine, self.lastLine, self.newFcName)

So on line 1 I ask for user input, the self.onNewFcNameGiven function receives the user input and puts it in the variable self.newFcName, which would be used in line 2. However line 2 executes before self.newFcName is set in the callback function in line 1.

So, how can I make line 2 run only after the callback function, in line 1, is finished?

Tkx in advance for any help.

Herb

0 Likes

#2

I’m confused. If you want to run the method after the input content panel has been given, why not just put it in the on_done callback?

[code]class TestCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Your other stuff here…
window.show_input_panel(“What is the new function name?”, “”, self.onNewFcNameGive, None, None)

def onNewFcNameGive(self, content):
    # Callback stuff here
    self.runCommandLine(self.view.file_name(), self.firstLine, self.lastLine, self.newFcName)

[/code]

0 Likes

#3

I get your point, it defenitly works, however it doesn’t seem right to be forced to create a new function every time I want to get user input…

What happens if I need 10 user inputs? I have to create and chain 10 functions?
It doesn’t seem right… So beeing a rookie on this, I thought there should be some other way…

0 Likes

#4

Yes, you will need to chain several functions. This is because the entire sublime process blocks while a plugin is running. I’m guessing this vastly simplifies concurrency issues.

0 Likes

#5

If you are just storing the values, or want to perform the same actions on each input panel callback, you could probably do something like this.

[code]class TestCommand(sublime_plugin.WindowCommand):
def run(self):
# Your other stuff here…
self.map = {}
self.keys = “a”, “b”, “c”, “d”, “e”]
self.key_index = 0
self.window.show_input_panel(“What is the new function name?”, “”, self.onNewFcNameGive, None, None)

def onNewFcNameGive(self, content):
    # Callback stuff here
    self.map[self.keys[self.key_index]] = content
    if len(self.map) == len(self.keys):
        print(self.map)
    else:
        self.key_index += 1
        self.window.show_input_panel("What is the new function name?", "", self.onNewFcNameGive, None, None)

[/code]

Of course, rather than print the map, call whatever complete function you want.

0 Likes

#6

Just for the record, I followed your advice and didn’t waist more time looking for a way to do it (tkx).

However, I used a lambda function, just because it saved me from creating an extra non-lambda function (the callback can only have a string parameter and I needed to pass more parameters):
The input panel call:

window.show_input_panel("What is the new function name?", '', lambda newFcName: self.runCommandLine(self.view.file_name(), firstLine, lastLine, newFcName, execute), None, None)

The callback function signature:

def runCommandLine(self, filePath, fromLine, toLine, newFcName, execute=False):

Just tought of putting it here, it might be usefull to another rookie.

Tkx for your help.

0 Likes