<s> '''<STR_LIT>''' <EOL> import sublime , sublime_plugin , re , string <EOL> class IncDecValueCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , action ) : <EOL> self . edit = edit <EOL> self . action = action <EOL> self . load_settings ( ) <EOL> self . delta = self . settings . get ( "<STR_LIT>" + action ) <EOL> self . insert_counter = None <EOL> for index , region in enumerate ( self . view . sel ( ) ) : <EOL> self . region = region <EOL> self . region_index = index <EOL> if len ( region ) == <NUM_LIT:0> : <EOL> self . word_reg = self . view . word ( region ) <EOL> else : <EOL> self . word_reg = region <EOL> if self . action [ : <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> self . apply_insert ( ) <EOL> elif not self . word_reg . empty ( ) : <EOL> ( <EOL> self . apply_date ( ) or <EOL> self . apply_hex_color ( ) or <EOL> self . apply_floating_point ( ) or <EOL> self . apply_integer ( ) or <EOL> self . apply_enums ( ) or <EOL> self . apply_string ( ) <EOL> ) <EOL> if self . settings . get ( "<STR_LIT>" ) : <EOL> self . view . run_command ( '<STR_LIT>' ) <EOL> def load_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> defaults = { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : - <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:10> , <EOL> "<STR_LIT>" : - <NUM_LIT:10> , <EOL> "<STR_LIT>" : <NUM_LIT:10> , <EOL> "<STR_LIT>" : <NUM_LIT:100> , <EOL> "<STR_LIT>" : - <NUM_LIT:100> , <EOL> "<STR_LIT>" : <NUM_LIT:100> , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False <EOL> } <EOL> self . settings = { } <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> for setting in defaults : <EOL> self . settings [ setting ] = settings . get ( setting , defaults . get ( setting ) ) <EOL> def apply_insert ( self ) : <EOL> """<STR_LIT>""" <EOL> tmp_reg = self . word_reg <EOL> if self . insert_counter is None : <EOL> word = self . get_word ( tmp_reg ) <EOL> match = re . match ( '<STR_LIT>' , word ) <EOL> if match : <EOL> self . insert_counter = int ( match . group ( <NUM_LIT:1> ) ) <EOL> else : <EOL> self . insert_counter = self . delta <EOL> if tmp_reg . empty ( ) : <EOL> self . view . insert ( self . edit , tmp_reg . begin ( ) , str ( self . insert_counter ) ) <EOL> else : <EOL> self . replace ( str ( self . insert_counter ) , tmp_reg ) <EOL> self . insert_counter += self . delta <EOL> def apply_date ( self ) : <EOL> """<STR_LIT>""" <EOL> word = self . get_word ( ) <EOL> re_date = re . compile ( '<STR_LIT>' ) <EOL> if re . match ( '<STR_LIT>' , word ) : <EOL> date = self . get_word ( self . word_reg . begin ( ) , self . word_reg . begin ( ) + <NUM_LIT:10> ) <EOL> if re_date . match ( date ) : <EOL> return False <EOL> elif re . match ( '<STR_LIT>' , word ) : <EOL> date = self . get_word ( self . word_reg . begin ( ) - <NUM_LIT:5> , self . word_reg . begin ( ) + <NUM_LIT:5> ) <EOL> if re_date . match ( date ) : <EOL> self . replace ( self . cycle ( word , <NUM_LIT:12> , self . delta ) ) <EOL> return True <EOL> else : <EOL> date = self . get_word ( self . word_reg . begin ( ) - <NUM_LIT:8> , self . word_reg . begin ( ) + <NUM_LIT:2> ) <EOL> if re_date . match ( date ) : <EOL> self . replace ( self . cycle ( word , <NUM_LIT> , self . delta ) ) <EOL> return True <EOL> def cycle ( self , word , max , delta ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( ( int ( word ) + max - <NUM_LIT:1> + delta ) % max + <NUM_LIT:1> ) <EOL> def apply_hex_color ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . action in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> pos_rgba_beg = self . find_left ( "<STR_LIT:r>" ) <EOL> pos_rgba_end = self . find_right ( "<STR_LIT:)>" ) <EOL> if pos_rgba_beg and pos_rgba_end : <EOL> pos_rgba_end = pos_rgba_end + <NUM_LIT:1> <EOL> if self . view . substr ( pos_rgba_end ) == "<STR_LIT:;>" : <EOL> pos_rgba_end = pos_rgba_end + <NUM_LIT:1> <EOL> rgba_str = self . get_word ( pos_rgba_beg , pos_rgba_end ) <EOL> re_rgba = re . compile ( '<STR_LIT>' ) <EOL> match = re_rgba . match ( rgba_str ) <EOL> if match : <EOL> alpha = match . group ( <NUM_LIT:4> ) . strip ( ) if match . group ( <NUM_LIT:4> ) else "<STR_LIT:1>" <EOL> alpha_txt = ( "<STR_LIT>" + alpha + "<STR_LIT>" ) if alpha != "<STR_LIT:1>" else "<STR_LIT:;>" <EOL> r = self . int_to_hex ( match . group ( <NUM_LIT:1> ) ) <EOL> g = self . int_to_hex ( match . group ( <NUM_LIT:2> ) ) <EOL> b = self . int_to_hex ( match . group ( <NUM_LIT:3> ) ) <EOL> if ( r [ <NUM_LIT:0> : <NUM_LIT:1> ] == r [ <NUM_LIT:1> : <NUM_LIT:2> ] ) and ( g [ <NUM_LIT:0> : <NUM_LIT:1> ] == g [ <NUM_LIT:1> : <NUM_LIT:2> ] ) and ( b [ <NUM_LIT:0> : <NUM_LIT:1> ] == b [ <NUM_LIT:1> : <NUM_LIT:2> ] ) : <EOL> r = r [ <NUM_LIT:1> : ] <EOL> g = g [ <NUM_LIT:1> : ] <EOL> b = b [ <NUM_LIT:1> : ] <EOL> hex_str = "<STR_LIT:#>" + r + g + b + alpha_txt <EOL> self . view . sel ( ) . subtract ( sublime . Region ( self . region . begin ( ) , self . region . end ( ) ) ) <EOL> self . view . replace ( self . edit , sublime . Region ( pos_rgba_beg , pos_rgba_end ) , hex_str ) <EOL> self . view . sel ( ) . add ( sublime . Region ( pos_rgba_beg , pos_rgba_beg ) ) <EOL> return True <EOL> pos_hex_beg = self . word_reg . begin ( ) <EOL> pos_hex_end = self . word_reg . end ( ) <EOL> word = self . get_word ( ) <EOL> if word [ - <NUM_LIT:1> ] == "<STR_LIT:#>" : <EOL> word2_reg = self . view . word ( self . word_reg . end ( ) + <NUM_LIT:1> ) <EOL> pos_hex_beg = self . word_reg . end ( ) - <NUM_LIT:1> <EOL> pos_hex_end = word2_reg . end ( ) <EOL> else : <EOL> prev = self . prev ( ) <EOL> if prev [ '<STR_LIT>' ] == "<STR_LIT:#>" : <EOL> pos_hex_beg = self . word_reg . begin ( ) - <NUM_LIT:1> <EOL> pos_hex_end = self . word_reg . end ( ) <EOL> alpha = "<STR_LIT>" <EOL> pos_alpha1 = self . find_right ( "<STR_LIT:/>" , pos_hex_end ) <EOL> if pos_alpha1 : <EOL> pos_alpha2 = self . find_right ( "<STR_LIT:/>" , pos_alpha1 + <NUM_LIT:1> ) <EOL> if pos_alpha2 : <EOL> alpha_str = self . get_word ( pos_alpha1 , pos_alpha2 + <NUM_LIT:1> ) <EOL> re_alpha = re . compile ( '<STR_LIT>' ) <EOL> match_alpha = re_alpha . match ( alpha_str ) <EOL> if match_alpha : <EOL> alpha = match_alpha . group ( <NUM_LIT:1> ) <EOL> word = self . get_word ( pos_hex_beg , pos_hex_end ) <EOL> if self . view . substr ( pos_hex_end ) == "<STR_LIT:;>" : <EOL> pos_hex_end = pos_hex_end + <NUM_LIT:1> <EOL> re_hex_color = re . compile ( '<STR_LIT>' ) <EOL> match = re_hex_color . match ( word ) <EOL> if match : <EOL> r = str ( int ( match . group ( <NUM_LIT:1> ) or match . group ( <NUM_LIT:4> ) + match . group ( <NUM_LIT:4> ) , <NUM_LIT:16> ) ) <EOL> g = str ( int ( match . group ( <NUM_LIT:2> ) or match . group ( <NUM_LIT:5> ) + match . group ( <NUM_LIT:5> ) , <NUM_LIT:16> ) ) <EOL> b = str ( int ( match . group ( <NUM_LIT:3> ) or match . group ( <NUM_LIT:6> ) + match . group ( <NUM_LIT:6> ) , <NUM_LIT:16> ) ) <EOL> rgba_alpha = "<STR_LIT:1>" if alpha == "<STR_LIT>" else alpha <EOL> sp = "<STR_LIT:U+0020>" if self . settings . get ( "<STR_LIT>" ) else "<STR_LIT>" <EOL> if self . settings . get ( "<STR_LIT>" ) : <EOL> rgba_str = "<STR_LIT>" + r + "<STR_LIT:U+002C>" + sp + g + "<STR_LIT:U+002C>" + sp + b + "<STR_LIT>" <EOL> else : <EOL> rgba_str = "<STR_LIT>" + r + "<STR_LIT:U+002C>" + sp + g + "<STR_LIT:U+002C>" + sp + b + "<STR_LIT:U+002C>" + sp + rgba_alpha + "<STR_LIT>" <EOL> self . view . sel ( ) . subtract ( sublime . Region ( self . region . begin ( ) , self . region . end ( ) ) ) <EOL> self . view . replace ( self . edit , sublime . Region ( pos_hex_beg , pos_hex_end if alpha == "<STR_LIT>" else pos_alpha2 + <NUM_LIT:1> ) , rgba_str ) <EOL> self . view . sel ( ) . add ( sublime . Region ( pos_hex_beg , pos_hex_beg ) ) <EOL> return True <EOL> else : <EOL> word = self . get_word ( ) <EOL> re_hex_color = re . compile ( '<STR_LIT>' ) <EOL> match = re_hex_color . match ( word ) <EOL> prev = self . prev ( ) <EOL> if match and prev [ '<STR_LIT>' ] == "<STR_LIT:#>" : <EOL> tmp_reg = self . word_reg <EOL> if self . action in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if tmp_reg . end ( ) == self . region . end ( ) : <EOL> tmp_reg = sublime . Region ( self . region . begin ( ) - <NUM_LIT:1> , self . region . begin ( ) ) <EOL> else : <EOL> tmp_reg = sublime . Region ( self . region . begin ( ) , self . region . begin ( ) + <NUM_LIT:1> ) <EOL> re_hex = re . compile ( '<STR_LIT>' ) <EOL> else : <EOL> re_hex = re_hex_color <EOL> word = self . get_word ( tmp_reg ) <EOL> match = re_hex . match ( word ) <EOL> if match : <EOL> delta = <NUM_LIT:1> if self . delta > <NUM_LIT:0> else - <NUM_LIT:1> <EOL> new_word = "<STR_LIT>" <EOL> for char in word : <EOL> char = hex ( int ( char , <NUM_LIT:16> ) + delta & <NUM_LIT> ) [ <NUM_LIT:2> : ] <EOL> new_word += char <EOL> if self . settings . get ( "<STR_LIT>" ) : <EOL> new_word = new_word . upper ( ) <EOL> self . replace ( new_word , tmp_reg ) <EOL> return True <EOL> def apply_floating_point ( self ) : <EOL> """<STR_LIT>""" <EOL> prev = self . prev ( ) <EOL> if prev [ '<STR_LIT>' ] == "<STR_LIT:.>" : <EOL> while True : <EOL> prev = self . prev ( prev [ '<STR_LIT>' ] - <NUM_LIT:1> ) <EOL> if not re . match ( '<STR_LIT>' , prev [ '<STR_LIT>' ] ) : <EOL> break <EOL> if prev [ '<STR_LIT>' ] != "<STR_LIT:->" : <EOL> prev = self . prev ( prev [ '<STR_LIT>' ] + <NUM_LIT:1> ) <EOL> tmp_reg = sublime . Region ( prev [ '<STR_LIT>' ] , self . word_reg . end ( ) ) <EOL> word = self . get_word ( tmp_reg ) <EOL> match = re . match ( '<STR_LIT>' , word ) <EOL> if match : <EOL> float_len = len ( match . group ( <NUM_LIT:2> ) ) <EOL> result = float ( match . group ( <NUM_LIT:1> ) ) + float ( self . delta ) / ( <NUM_LIT:10> ** float_len ) <EOL> match3 = match . group ( <NUM_LIT:3> ) if match . group ( <NUM_LIT:3> ) else "<STR_LIT>" <EOL> self . replace ( ( '<STR_LIT>' % float_len ) % result + match3 , tmp_reg ) <EOL> return True <EOL> def apply_integer ( self ) : <EOL> """<STR_LIT>""" <EOL> prev = self . prev ( ) <EOL> tmp_reg = self . word_reg <EOL> if prev [ '<STR_LIT>' ] == "<STR_LIT:->" : <EOL> tmp_reg = sublime . Region ( prev [ '<STR_LIT>' ] , self . word_reg . end ( ) ) <EOL> word = self . get_word ( tmp_reg ) <EOL> match = re . match ( '<STR_LIT>' , word ) <EOL> if match : <EOL> orig_int = int ( match . group ( <NUM_LIT:1> ) ) <EOL> result = orig_int + self . delta <EOL> match2 = match . group ( <NUM_LIT:2> ) or "<STR_LIT>" <EOL> old_pos = self . view . sel ( ) [ self . region_index ] <EOL> self . replace ( str ( result ) + match2 , tmp_reg ) <EOL> pos = self . view . sel ( ) [ self . region_index ] <EOL> if orig_int >= <NUM_LIT:0> and result < <NUM_LIT:0> and pos . a == tmp_reg . a and old_pos . a == old_pos . b : <EOL> self . view . sel ( ) . subtract ( tmp_reg ) <EOL> self . view . sel ( ) . add ( sublime . Region ( tmp_reg . a + <NUM_LIT:1> , tmp_reg . a + <NUM_LIT:1> ) ) <EOL> return True <EOL> def apply_enums ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . action not in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return <EOL> word = self . get_word ( ) <EOL> prev = self . prev ( ) <EOL> if prev [ '<STR_LIT>' ] == "<STR_LIT:->" : <EOL> prev = self . prev ( prev [ '<STR_LIT>' ] - <NUM_LIT:1> ) <EOL> while re . match ( '<STR_LIT>' , prev [ '<STR_LIT>' ] ) : <EOL> prev = self . prev ( prev [ '<STR_LIT>' ] - <NUM_LIT:1> ) <EOL> prev = self . prev ( prev [ '<STR_LIT>' ] + <NUM_LIT:1> ) <EOL> self . word_reg = sublime . Region ( prev [ '<STR_LIT>' ] , self . word_reg . end ( ) ) <EOL> word = self . get_word ( ) <EOL> last = self . prev ( self . word_reg . end ( ) ) <EOL> if last [ '<STR_LIT>' ] == "<STR_LIT:->" : <EOL> last = self . prev ( last [ '<STR_LIT>' ] + <NUM_LIT:1> ) <EOL> while re . match ( '<STR_LIT>' , last [ '<STR_LIT>' ] ) : <EOL> last = self . prev ( last [ '<STR_LIT>' ] + <NUM_LIT:1> ) <EOL> self . word_reg = sublime . Region ( self . word_reg . begin ( ) , last [ '<STR_LIT>' ] ) <EOL> word = self . get_word ( ) <EOL> found = False <EOL> enums = self . settings . get ( "<STR_LIT>" ) + self . settings . get ( "<STR_LIT>" ) <EOL> for enum in enums : <EOL> if word in enum : <EOL> found = True <EOL> fn = lambda s : s <EOL> if not found : <EOL> fn = lambda s : s . lower ( ) <EOL> if re . match ( '<STR_LIT>' , word ) : <EOL> fn = lambda s : s . upper ( ) <EOL> if re . match ( '<STR_LIT>' , word ) : <EOL> fn = lambda s : s . capitalize ( ) <EOL> word = word . lower ( ) <EOL> for enum in enums : <EOL> if word in enum : <EOL> delta = <NUM_LIT:1> if self . delta > <NUM_LIT:0> else - <NUM_LIT:1> <EOL> new_value = enum [ ( enum . index ( word ) + delta ) % len ( enum ) ] <EOL> self . replace ( fn ( new_value ) ) <EOL> return True <EOL> def apply_string ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . action in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return <EOL> word = self . get_word ( ) <EOL> fn = { <EOL> "<STR_LIT>" : lambda s : s [ <NUM_LIT:0> ] . capitalize ( ) + s [ <NUM_LIT:1> : ] if s [ <NUM_LIT:0> ] . islower ( ) else s . upper ( ) , <EOL> "<STR_LIT>" : lambda s : s . capitalize ( ) if s . isupper ( ) else s . lower ( ) , <EOL> "<STR_LIT>" : lambda s : s . upper ( ) , <EOL> "<STR_LIT>" : lambda s : s . lower ( ) , <EOL> } . get ( self . action , None ) <EOL> if fn : <EOL> new_word = fn ( word ) <EOL> if word != new_word : <EOL> self . replace ( new_word ) <EOL> return True <EOL> def prev ( self , pos = None ) : <EOL> """<STR_LIT>""" <EOL> pos = pos or self . word_reg . begin ( ) - <NUM_LIT:1> <EOL> sym = self . view . substr ( pos ) <EOL> return { '<STR_LIT>' : pos , '<STR_LIT>' : sym } <EOL> def next ( self , pos = None ) : <EOL> """<STR_LIT>""" <EOL> pos = pos or self . word_reg . begin ( ) + <NUM_LIT:1> <EOL> sym = self . view . substr ( pos ) <EOL> return { '<STR_LIT>' : pos , '<STR_LIT>' : sym } <EOL> def replace ( self , text , region = None ) : <EOL> """<STR_LIT>""" <EOL> old_pos = self . view . sel ( ) [ self . region_index ] <EOL> if not region : <EOL> region = self . word_reg <EOL> old_len = len ( self . view . substr ( region ) ) <EOL> self . view . replace ( self . edit , region , text ) <EOL> offset = len ( text ) - old_len <EOL> self . view . sel ( ) . subtract ( sublime . Region ( region . end ( ) + offset , region . end ( ) + offset ) ) <EOL> offset_start = old_pos . begin ( ) + offset <EOL> offset_end = old_pos . end ( ) + offset <EOL> if old_pos . begin ( ) == region . begin ( ) : <EOL> offset_start = old_pos . begin ( ) <EOL> if old_pos . begin ( ) == old_pos . end ( ) : <EOL> offset_end = old_pos . end ( ) <EOL> self . view . sel ( ) . add ( sublime . Region ( offset_start , offset_end ) ) <EOL> def get_word ( self , reg_begin = None , reg_end = None ) : <EOL> """<STR_LIT>""" <EOL> if not reg_begin : <EOL> return self . view . substr ( self . word_reg ) <EOL> if not reg_end : <EOL> return self . view . substr ( reg_begin ) <EOL> return self . view . substr ( sublime . Region ( reg_begin , reg_end ) ) <EOL> def find_right ( self , sym , pos = None ) : <EOL> pos = pos or self . region . begin ( ) <EOL> line = self . view . line ( pos ) <EOL> end_pos = line . end ( ) <EOL> while pos < end_pos : <EOL> pos_sym = self . view . substr ( pos ) <EOL> if pos_sym == sym : <EOL> return pos <EOL> pos = pos + <NUM_LIT:1> <EOL> def find_left ( self , sym , pos = None ) : <EOL> pos = pos or self . region . begin ( ) <EOL> line = self . view . line ( pos ) <EOL> pos_beg = line . begin ( ) <EOL> while pos > pos_beg : <EOL> pos_sym = self . view . substr ( pos ) <EOL> if pos_sym == sym : <EOL> return pos <EOL> pos = pos - <NUM_LIT:1> <EOL> def int_to_hex ( self , int_str , digits = <NUM_LIT:2> ) : <EOL> hex_str = hex ( int ( int_str ) ) [ <NUM_LIT:2> : ] <EOL> if len ( hex_str ) < digits : <EOL> hex_str = "<STR_LIT:0>" + hex_str <EOL> return hex_str </s>
