In Short:
Is there any way to force Sublime Text to add individual events performed in a TextCommand plugin to its soft-undo buffer?
In Long:
I’ve written a plugin which clears multiple selections leaving only one selection, either the top or bottom one.
Most of the time I want the selections totally cleared leaving just a cursor at the sel.b
position (i.e. where the actual cursor is) of the top or bottom selection but sometimes I want any selected text in the top or bottom selection to remain intact.
The plugin clears all the selections before adding just the one that is wanted so I had the idea of using soft-undo so that I can easily get to what I want. I added code to clear the selections, add the region of the top or bottom selection intact, assuming sel.size() > 0
, then to clear the selections again before finally adding the sel.b
position to add just the cursor. That way if I want the selected text I could just hit the soft-undo keys. i.e.
bottom_sel = sels[sels_len - 1]
sels.clear()
sels.add(bottom_sel)
sels.clear()
sels.add(sublime.Region(bottom_sel.b, bottom_sel.b))
Unfortunately when I hit the soft-undo keys Sublime Text just undoes everything my plugin did in one go.
So I thought I’d use a dedicated TextCommand, like this:
bottom_sel = sels[sels_len - 1]
if bottom_sel.a == bottom_sel.b:
self.view.run_command("multiple_selection_clear_add_sel",
{"sel_a": bottom_sel.a, "sel_b": bottom_sel.b})
else:
# Selection text intact:
self.view.run_command("multiple_selection_clear_add_sel",
{"sel_a": bottom_sel.a, "sel_b": bottom_sel.b})
# Selection with cursor at the non-cursor end:
self.view.run_command("multiple_selection_clear_add_sel",
{"sel_a": bottom_sel.a, "sel_b": bottom_sel.a})
# Selection with cursor at the cursor end:
self.view.run_command("multiple_selection_clear_add_sel",
{"sel_a": bottom_sel.b, "sel_b": bottom_sel.b})
class MultipleSelectionClearAddSelCommand(sublime_plugin.TextCommand):
def run(self, edit, sel_a, sel_b):
sels = self.view.sel()
sels.clear()
sels.add(sublime.Region(sel_a, sel_b))
i.e. 1) Cursor is at the cursor end of bottom selection
2) Hit the soft-undo keys
3) Cursor is at the non-cursor end of bottom selection
4) Hit the soft-undo keys
5) Bottom selection (size > 0) with selected text intact.
I thought that by calling another TextCommand the individual states would be stored by Sublime Text and I could soft-undo through them one by one. Unfortunately Sublime Text just does a soft-undo of everything on one go and, as before, I end up with all the original selections intact.
Is there any way to force Sublime Text to add the individual events to its soft-undo buffer?
Thanks.