Home Download Buy Blog Forum Support

view won't insert after show_quick_panel()

view won't insert after show_quick_panel()

Postby dhodges on Wed Mar 13, 2013 11:35 am

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

Code: Select all
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)
dhodges
 
Posts: 2
Joined: Wed Mar 13, 2013 11:13 am

Re: view won't insert after show_quick_panel()

Postby sapphirehamster on Wed Mar 13, 2013 7:05 pm

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.
sapphirehamster
 
Posts: 74
Joined: Sun Jul 01, 2012 11:19 pm

Re: view won't insert after show_quick_panel()

Postby skuroda on Wed Mar 13, 2013 11:27 pm

sapphirehamster wrote: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.


This would work in ST2, but not ST3 due to a change in the api. From the porting guide (http://www.sublimetext.com/docs/3/porting_guide.html)
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.
skuroda
 
Posts: 674
Joined: Fri Oct 05, 2012 8:11 am

Re: view won't insert after show_quick_panel()

Postby dhodges on Thu Mar 14, 2013 7:08 am

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

Code: Select all
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:)
dhodges
 
Posts: 2
Joined: Wed Mar 13, 2013 11:13 am

Re: view won't insert after show_quick_panel()

Postby skuroda on Thu Mar 14, 2013 7:45 am

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 =)
skuroda
 
Posts: 674
Joined: Fri Oct 05, 2012 8:11 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests

cron