Sublime Forum

View won't insert after show_quick_panel()

#1

Hi,

I’m having trouble invoking view.insert() within the “on_done” callback of show_quick_panel().

Elsewhere in my code, view.insert() and view.replace() work fine, but when called within a callback they remain silent.
Simplest non-working example included below.
Has anybody come across this before?

ST Build 3019

class TestTextCommand(sublime_plugin.TextCommand):
  def on_done(self, index):
    self.view.insert(self.edit, 0, "asdf")

  def run(self, edit):
    self.edit = edit
    self.view.window().show_quick_panel("one", "two", "buckle my", "shoe"], self.on_done)
0 Likes

#2

I don’t think hanging on to an edit object is a good thing to do.

Instead, create a separate text command class to do the action, and in your on_done, call self.view.run_command(…) with that separate command and whatever arguments you need.

0 Likes

#3

[quote=“sapphirehamster”]I don’t think hanging on to an edit object is a good thing to do.

Instead, create a separate text command class to do the action, and in your on_done, call self.view.run_command(…) with that separate command and whatever arguments you need.[/quote]

This would work in ST2, but not ST3 due to a change in the api. From the porting guide (sublimetext.com/docs/3/porting_guide.html)

[quote]
Restricted begin_edit() and end_edit()

begin_end() and end_edit() are no longer directory accessible, except in some special circumstances. The only way to get a valid instance of an Edit object is to place your code in a TextCommand subclass. In general, most code can be refactored by placing the code between begin_edit() and end_edit() in a TextCommand, and then calling run_command on that TextCommand.

This approach removes the issue of dangling Edit objects, and ensures the repeat command and macros work as they should.[/quote]

0 Likes

#4

Actually, the suggested solution works just fine in ST3 - so long as self.view.run_command(…) invokes another TextCommand:

class TestTextCommand(sublime_plugin.TextCommand):
  def on_done(self, index):
    self.view.run_command("insert_my_text", {"args":{'text':self.list[index]}})

  def run(self, edit):
    self.list = "one", "two", "buckle my", "shoe"]
    self.view.window().show_quick_panel(self.list, self.on_done)

class InsertMyText(sublime_plugin.TextCommand):
  def run(self, edit, args):
    self.view.insert(edit, 0, args'text'])

Thanks for your help!

d:)

0 Likes

#5

Ah sorry for not being clear. I meant what you had (in the initial post) would work in ST2 not ST3. I should really quote better. Glad you got it working =)

0 Likes

#6

Thanks so much - exactly what I needed. I’m selecting a date string from a pop-up list of options, and I could not see how to insert the text from the on_done method without passing the edit object somehow. Nice solution - simple. Thanks again. - Kurt

0 Likes