I have a plugin which modifies text in multiple steps. E.g. first it moves the cursor, then it selects a block of text, then it replaces that text. If I want to revert that operation by pressing CTRL+Z (“undo”), ST undoes the single operations one by one (replace text, unselect, move cursor), so in order to undo the whole thing I have to press CTRL+Z 3 times. Instead I would like to press it once. Is there an API to tell ST to treat multiple commands as a single atomic operation?
Undo multiple commands as a single atomic operation
bschaaf
#2
You need to do all of those steps from within a TextCommand
. That will ensure they all get grouped into a single action.
0 Likes
giampaolo
#3
Thanks for your help Benjamin. I am using a TextCommand
. I think the problem is that I do the text manipulation by using sub-commands, e.g.:
# add \n
view.run_command("insert", {"characters": "\n"})
# move cursor up
view.run_command("move", {"by": "lines", "forward": False})
view.run_command("insert", {"characters": f"{spaces}@classmethod"})
I guess I should use APIs instead?
0 Likes
bschaaf
#4
That’s working fine here:
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command("insert", {"characters": "\n"})
# move cursor up
self.view.run_command("move", {"by": "lines", "forward": False})
self.view.run_command("insert", {"characters": f" @classmethod"})
0 Likes
giampaolo
#5
Sorry, you’re right. I understand what’s causing the problem. It’s that between those commands I invoke an LSP command. What I actually do is this:
self.view.run_command("insert", {"characters": "\n"})
# move cursor up
self.view.run_command("move", {"by": "lines", "forward": False})
self.view.run_command("insert", {"characters": f" @classmethod"})
self.view.run_command(
"lsp_symbol_rename", {"new_name": "cls", "position": position}
)
What “splits” the operation in 2 is the last command.
0 Likes