While porting some of my plugins to ST3, I was annoyed enough with the edit changes (view.insert, replace, erase can only be used in a TextCommand now) to make an edit abstraction with easy syntax that works with both ST2 and ST3.
https://github.com/lunixbochs/SublimeXiki/blob/st3/edit.py
This module allows use of insert, replace, and erase. It also batches all edits made inside the “with” block so they appear as one “edit” to Sublime.
Hopefully this makes your life easier if you enjoy writing text manipulation plugins as much as me
Example (should work in both ST2 and ST3):
with Edit(view) as edit:
edit.insert(0, 'hi\n')
edit.insert(0, 'more stuff\n')
Compare to:
ST2 edit:
edit = view.begin_edit()
view.insert(edit, 0, 'hi\n')
view.insert(edit, 0, 'more stuff\n')
view.end_edit(edit)
ST3 edit (repeat for erase, replace? or roll your own / use callbacks if you want multiple operations in a single edit):
[code]class InsertCommand(sublime_plugin.TextCommand):
def run(self, edit, pos, text):
self.view.insert(edit, pos, text)
view.run_command(‘insert’, {‘pos’: 0, ‘text’: ‘hi\n’})
view.run_command(‘insert’, {‘pos’: 0, ‘text’: ‘more stuff\n’})[/code]