- Code: Select all
# Clone all the regions in view.sel()
# and put them in a list called old_selection
old_selection = []
for region in view.sel():
old_selection.append(region)
{{ DO EDIT, and now the cursor positions/selections changed :( }}
# Remove new positions then add all the old regions
# back and its the same as before the edit
view.sel().clear()
for region in old_selection:
view.sel().add(region)
I noticed this logic could be much easier if there was a constructor for sublime.RegionSet() that if you pass it a RegionSet it will clone it
- Code: Select all
# Wouldn't it be nice?
old_selection = sublime.RegionSet(view.sel())
{{ DO EDIT, and now the cursor positions/selections changed }}
view.sel().clear()
view.sel().add_all(old_selection)
Further you could allow sublime.RegionSet() to return an empty Region Set.
Also to make it easier optionally you can pass a Region Set to view.sel(RegionSet) to update selection, this makes it easier to discover how to change the selection and maybe instead of above you can make view.sel() return the clone by default. Then it would be as easy as:
- Code: Select all
old_selection = view.sel()
Do edits...
view.sel(old_selection)