Home Download Buy Blog Forum Support

Supporting "undo" in multiple selections

Supporting "undo" in multiple selections

Postby edanm on Sat May 08, 2010 12:39 pm

I'm writing a plugin which inserts many characters in all the selections (i.e. in several regions).
I'm using
Code: Select all
view.replace(region, "newstring")
to replace each region with the new string.

My problem is, when someone hits "undo" (ctrl+z), it will undo the replacements one at a time, and not all at once.
This is contrary to the way it work when just writing text in multiple selections, where "undo" will undo all the written lines at once.

What's the best way to make my plugin support "undo"?

Thanks,
Edan
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Supporting "undo" in multiple selections

Postby gpfsmurf on Sat May 08, 2010 6:43 pm

Funny, I just found out about this a few days ago. I don't know if the behavior's changed in the latest beta, or if I just never noticed.

Anyways, here are a few ideas:
  • Undo should undo everything the command just did, and Soft-undo should do it step by step. KISS!
  • Let the plugin decide, through the API, how to "atomicize" operations. Power to the people!
  • Nested undo? Per-region undo? The holy grail of undos?
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: Supporting "undo" in multiple selections

Postby edanm on Sat May 08, 2010 7:18 pm

The problem is, my plugin effectively "runs" the command "region.replace" several times, which is why Sublime treats it as several separate "undo" units.

I'd just add an api function which is the equivalent of SQL's "BEGIN" command, which tells Sublime to treat several commands as one.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Supporting "undo" in multiple selections

Postby adzenith on Mon May 10, 2010 4:06 pm

adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Supporting "undo" in multiple selections

Postby gpfsmurf on Mon May 10, 2010 6:43 pm

Nice! It works.

It should be added to the Commands reference, if only in the "Not Yet Documented" section :)
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: Supporting "undo" in multiple selections

Postby edanm on Mon May 10, 2010 8:32 pm

Great! Thanks a lot.
I'm still working on making it work (it behaves very oddly part of the time), but it looks like this is exactly what I need.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Supporting "undo" in multiple selections

Postby edanm on Mon May 10, 2010 8:53 pm

I've come across a problem with the commands.

It's detailed in this thread, if anyone is interested: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=1162&sid=f53467780445306c08acdebd15f178b9&sid=232cae9cdc3f0f1639e1e279378f6cce#p5171
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Supporting "undo" in multiple selections

Postby jps on Wed May 12, 2010 1:51 pm

It's worth noting that in the general case, you don't have to do anything to have this happen: any modifications to the buffer done within the run method of a TextCommand will automatically be grouped into a single undo action.
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: Supporting "undo" in multiple selections

Postby edanm on Wed May 12, 2010 5:01 pm

Why isn't it done in this case? Because it's on the "onDone" method of a QuickPanel?
It might be worth making that also group the commands into a single action.

By the way, here's a (possible) workaround: I can alway call a new "dummy" command from the onDone method (using runCommand()), and pass it the parameters somehow.
I'd just need to create a "dummy" command that does all the actual work.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm


Return to Plugin Development

Who is online

Users browsing this forum: Exabot [Bot] and 3 guests