I’m developing a plugin that asks for user input with successive instances of sublime_plugin.TextInputHandler
and sublime_plugin.ListInputHandler
. These are fine for single line input, but there is a part that I’d like to allow for multiline input. I’ve read that show_input_panel
can accept multiline input, but I’m not really sure how to insert that into the chain of TextInputHandler
and ListInputHandler
that I have. Any suggestions are welcome. Even any tutorials or videos on how to use show_input_panel
because I’m finding virtually no examples of using that method, let alone in this context. Thank you.
How to have multiline input for plugin?
I’ve figured out a bit more. I need to call show_input_panel
in the run
method of my plugin’s base sublime_plugin.TextCommand
. My question now is how do I get the inputted text into a variable? The show_input_panel
returns a view instead of the text. I have successfully implemented the on_done
function to return the text but cannot figure out how to pass that to a variable in my sublime_plugin.TextCommand
.
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit, my_arg=None):
print(my_arg)
view.run_command("example", my_arg="blah")
Thanks, I guess my question is how do I access the inputted text outside of the on_done
function? Or does all subsequent logic using the inputted text have to occur inside the on_done
function?
That sounds like a wrong workflow, but sometimes you have to, I guess… global variables.
That’s how callback works. Pass the variable as another function’s arg in the callback.
Thank you for the replies. After some sleep, I was able to grab the inputted text with the following:
my_input_view = sublime.active_window().show_input_panel('Caption', 'Initial text', on_done, on_change, on_cancel)
my_input_text = (my_input_view .substr(sublime.Region(0, my_input_view .size())))
Since show_input_panel
returns a view, I had to call the substr()
method to get the text of that view. Fortunately, I am still able to grab the text from the view after the input panel is dismissed with Enter. In this case, I don’t really have any use for the show_input_panel
method arguments (on_done
, on_change
, on_cancel
), but they still have to be supplied to the show_input_panel
method.
Also, for anyone reading this in future, when editing text in the input panel, new lines can be made with Command+Return (on Mac at least, probably Ctrl+Enter on Windows but haven’t tested that).