<s> import inspect <EOL> import sublime <EOL> import sublime_plugin <EOL> try : <EOL> sublime . sublimelint_edit_storage <EOL> except AttributeError : <EOL> sublime . sublimelint_edit_storage = { } <EOL> def run_callback ( func , * args , ** kwargs ) : <EOL> spec = inspect . getfullargspec ( func ) <EOL> if spec . args or spec . varargs : <EOL> return func ( * args , ** kwargs ) <EOL> else : <EOL> return func ( ) <EOL> class EditFuture : <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> def resolve ( self , view , edit ) : <EOL> return self . func ( view , edit ) <EOL> class EditStep : <EOL> def __init__ ( self , cmd , * args ) : <EOL> self . cmd = cmd <EOL> self . args = args <EOL> def run ( self , view , edit ) : <EOL> if self . cmd == '<STR_LIT>' : <EOL> return run_callback ( self . args [ <NUM_LIT:0> ] , view , edit ) <EOL> def insert ( edit , pos , text ) : <EOL> pos = min ( view . size ( ) , pos ) <EOL> view . insert ( edit , pos , text ) <EOL> funcs = { <EOL> '<STR_LIT>' : insert , <EOL> '<STR_LIT>' : view . erase , <EOL> '<STR_LIT:replace>' : view . replace , <EOL> } <EOL> func = funcs . get ( self . cmd ) <EOL> if func : <EOL> args = self . resolve_args ( view , edit ) <EOL> func ( edit , * args ) <EOL> def resolve_args ( self , view , edit ) : <EOL> args = [ ] <EOL> for arg in self . args : <EOL> if isinstance ( arg , EditFuture ) : <EOL> arg = arg . resolve ( view , edit ) <EOL> args . append ( arg ) <EOL> return args <EOL> class Edit : <EOL> def __init__ ( self , view ) : <EOL> self . view = view <EOL> self . steps = [ ] <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . steps ) <EOL> @ classmethod <EOL> def future ( cls , func ) : <EOL> return EditFuture ( func ) <EOL> @ classmethod <EOL> def defer ( cls , view , func ) : <EOL> with Edit ( view ) as edit : <EOL> edit . callback ( func ) <EOL> def step ( self , cmd , * args ) : <EOL> step = EditStep ( cmd , * args ) <EOL> self . steps . append ( step ) <EOL> def insert ( self , point , string ) : <EOL> self . step ( '<STR_LIT>' , point , string ) <EOL> def erase ( self , region ) : <EOL> self . step ( '<STR_LIT>' , region ) <EOL> def replace ( self , region , string ) : <EOL> self . step ( '<STR_LIT:replace>' , region , string ) <EOL> def callback ( self , func ) : <EOL> self . step ( '<STR_LIT>' , func ) <EOL> def reselect ( self , pos ) : <EOL> def select ( view , edit ) : <EOL> region = pos <EOL> if hasattr ( pos , '<STR_LIT>' ) : <EOL> region = run_callback ( pos , view ) <EOL> if isinstance ( region , int ) : <EOL> region = sublime . Region ( region , region ) <EOL> elif isinstance ( region , ( tuple , list ) ) : <EOL> region = sublime . Region ( * region ) <EOL> view . sel ( ) . clear ( ) <EOL> view . sel ( ) . add ( region ) <EOL> view . show ( region , False ) <EOL> self . callback ( select ) <EOL> def append ( self , text ) : <EOL> self . insert ( self . view . size ( ) , text ) <EOL> def run ( self , view , edit ) : <EOL> read_only = False <EOL> if view . is_read_only ( ) : <EOL> read_only = True <EOL> view . set_read_only ( False ) <EOL> for step in self . steps : <EOL> step . run ( view , edit ) <EOL> if read_only : <EOL> view . set_read_only ( True ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> view = self . view <EOL> if sublime . version ( ) . startswith ( '<STR_LIT:2>' ) : <EOL> edit = view . begin_edit ( ) <EOL> self . run ( edit ) <EOL> view . end_edit ( edit ) <EOL> else : <EOL> key = str ( hash ( tuple ( self . steps ) ) ) <EOL> sublime . sublimelint_edit_storage [ key ] = self . run <EOL> view . run_command ( '<STR_LIT>' , { '<STR_LIT:key>' : key } ) <EOL> class apply_sublimelint_edit ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , key ) : <EOL> sublime . sublimelint_edit_storage . pop ( key ) ( self . view , edit ) </s>
