Sublime Forum

Write user-input to editor window

#1

As silly as the below example might seem, it’s just a stripped down version of what I would love to achieve: writing user input into the editor window.

class MyDialogCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        selection = self.view.substr(sublime.Region(0, self.view.size()))

        # self.view.window().run_command('hide_panel');
        self.view.window().show_input_panel("Replace selection:", selection, self.on_done, None, None)

    def on_done(self, user_input):
        sublime.status_message("Hello: " + user_input)

        selection = sublime.Region(0, self.view.size())
        self.view.replace(edit, selection, user_input)

I have tried many variations (working with global vars, splitting the on_done function into multiple), but none allowed me to achieve my goal. In pretty much every case I ended up with the following error:

TypeError: replace() missing 1 required positional argument: 'edit'

Is there some limitation that does not allow me to this? Is this possible? What am I doing wrong? (Yeah, I’m not a Python guy)

0 Likes

#2

The actual exception is a but odd, but this shouldn’t work for two reasons:

  1. The edit obejct is “closed” once the TextCommand’s run method exits and can not be used anymore.
  2. edit is undefined in on_done.

You can just use the buitt-in insert command in your on_done method, since it will replace the current selection with its argument.

0 Likes