Sublime 3 Plugin Storing Quick Panel Return Val
Question: I'm trying to write a simple plugin that generates a quick panel based on some
list, waits for the user to select an item, and then performs an action based
on the value the user selected. Basically, I'd like to do the following:

    
    
    class ExampleCommand(sublime_plugin.TextCommand):
        def __init__(self):
            self._return_val = None
            self._list = ['a', 'b', 'c']
        def callback(self, idx)
            self._return_val = self._list[idx]
        def run(self):
            sublime.active_window().show_quick_panel(
                options, self.callback)
            if self._return_val == 'a'
                // do something
    

However, show_quick_panel returns before anything is selected and therefore
self._return_val won't be assigned to the index selected until after the if
statement runs.

How can I solve this problem? With an event listener? I'm very new to Python
and Sublime plugin development.


Answer: Showing the quickpanel obviously does not block the program execution. I
recommend to create and pass a continuation:

    
    
    import sublime
    import sublime_plugin
    
    
    class ExampleQuickpanelCommand(sublime_plugin.WindowCommand):
        def run(self):
            # create your items
            items = ["a", "b", "c"]
    
            def on_select(index):
                if index == -1:  # canceled
                    return
                item = items[index]
                # process the selected item...
                sublime.error_message("You selected '{0}'".format(item))
    
            self.window.show_quick_panel(items, on_select)
    

