-
-
Save Enteleform/8b46387a5e0d9341fc88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified from https://github.com/titoBouzout/Open-Include/blob/master/Edit.py | |
import sublime | |
import sublime_plugin | |
from collections import defaultdict | |
try: | |
sublime.edit_storage | |
except AttributeError: | |
sublime.edit_storage = {} | |
class EditStep: | |
def __init__ ( self, cmd, *args ): | |
self.cmd = cmd | |
self.args = args | |
def run ( self, view, edit ): | |
funcs = \ | |
{ | |
'insert': view.insert, | |
'erase': view.erase, | |
'replace': view.replace, | |
} | |
func = funcs.get ( self.cmd ) | |
if func: | |
func ( edit, *self.args ) | |
class Edit: | |
defer = defaultdict ( dict ) | |
def __init__ ( self, view ): | |
self.view = view | |
self.steps = [] | |
def step ( self, cmd, *args ): | |
step = EditStep ( cmd, *args ) | |
self.steps.append ( step ) | |
def insert ( self, point, string ): | |
self.step ( 'insert', point, string ) | |
self.execute () | |
def erase ( self, region ): | |
self.step ( 'erase', region ) | |
self.execute () | |
def replace ( self, region, string ): | |
self.step ( 'replace', region, string ) | |
self.execute () | |
def run ( self, view, edit ): | |
for step in self.steps: | |
step.run ( view, edit ) | |
def execute ( self ): | |
view = self.view | |
key = str ( hash ( tuple ( self.steps ) ) ) | |
sublime.edit_storage[ key ] = self.run | |
view.run_command ( 'apply_edit', { 'key': key } ) | |
self.steps = [] | |
class apply_edit ( sublime_plugin.TextCommand ): | |
def run ( self, edit, key ): | |
sublime.edit_storage.pop ( key ) ( self.view, edit ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment