<s> """<STR_LIT>""" <EOL> import re <EOL> import sublime <EOL> import sublime_plugin <EOL> try : <EOL> from . helpers import BaseBlockCommand <EOL> except ValueError : <EOL> from helpers import BaseBlockCommand <EOL> ORDER_LIST_PATTERN = re . compile ( r"<STR_LIT>" ) <EOL> UNORDER_LIST_PATTERN = re . compile ( r"<STR_LIT>" ) <EOL> EMPTY_LIST_PATTERN = re . compile ( r"<STR_LIT>" ) <EOL> NONLIST_PATTERN = re . compile ( r"<STR_LIT>" ) <EOL> ROMAN_PATTERN = re . compile ( r"<STR_LIT>" , <EOL> re . IGNORECASE ) <EOL> ROMAN_MAP = ( ( '<STR_LIT:M>' , <NUM_LIT:1000> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT:D>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT:C>' , <NUM_LIT:100> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT:L>' , <NUM_LIT:50> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT:X>' , <NUM_LIT:10> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:9> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:5> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:4> ) , <EOL> ( '<STR_LIT:I>' , <NUM_LIT:1> ) ) <EOL> class RomanError ( Exception ) : pass <EOL> class NotIntegerError ( RomanError ) : pass <EOL> class InvalidRomanNumeralError ( RomanError ) : pass <EOL> def to_roman ( n ) : <EOL> """<STR_LIT>""" <EOL> if not ( <NUM_LIT:0> < n < <NUM_LIT> ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> result = "<STR_LIT>" <EOL> for numeral , integer in ROMAN_MAP : <EOL> while n >= integer : <EOL> result += numeral <EOL> n -= integer <EOL> return result <EOL> def from_roman ( s ) : <EOL> """<STR_LIT>""" <EOL> result = <NUM_LIT:0> <EOL> index = <NUM_LIT:0> <EOL> for numeral , integer in ROMAN_MAP : <EOL> while s [ index : index + len ( numeral ) ] == numeral : <EOL> result += integer <EOL> index += len ( numeral ) <EOL> return result <EOL> class SmartListCommand ( BaseBlockCommand ) : <EOL> def run ( self , edit ) : <EOL> def update_ordered_list ( lines ) : <EOL> new_lines = [ ] <EOL> next_num = None <EOL> kind = lambda a : a <EOL> for line in lines : <EOL> match = ORDER_LIST_PATTERN . match ( line ) <EOL> if not match : <EOL> new_lines . append ( line ) <EOL> continue <EOL> new_line = match . group ( <NUM_LIT:1> ) + ( kind ( next_num ) or match . group ( <NUM_LIT:2> ) ) + match . group ( <NUM_LIT:3> ) + match . group ( <NUM_LIT:4> ) <EOL> new_lines . append ( new_line ) <EOL> if not next_num : <EOL> try : <EOL> next_num = int ( match . group ( <NUM_LIT:2> ) ) <EOL> kind = str <EOL> except ValueError : <EOL> next_num = ord ( match . group ( <NUM_LIT:2> ) ) <EOL> kind = chr <EOL> next_num += <NUM_LIT:1> <EOL> return new_lines <EOL> def update_roman_list ( lines ) : <EOL> new_lines = [ ] <EOL> next_num = None <EOL> kind = lambda a : a <EOL> for line in lines : <EOL> match = ROMAN_PATTERN . match ( line ) <EOL> if not match : <EOL> new_lines . append ( line ) <EOL> continue <EOL> new_line = match . group ( <NUM_LIT:1> ) + ( kind ( next_num ) or match . group ( <NUM_LIT:2> ) ) + match . group ( <NUM_LIT:3> ) + match . group ( <NUM_LIT:4> ) <EOL> new_lines . append ( new_line ) <EOL> if not next_num : <EOL> actual = match . group ( <NUM_LIT:2> ) <EOL> next_num = from_roman ( actual . upper ( ) ) <EOL> if actual == actual . lower ( ) : <EOL> kind = lambda a : to_roman ( a ) . lower ( ) <EOL> else : <EOL> kind = to_roman <EOL> next_num += <NUM_LIT:1> <EOL> return new_lines <EOL> for region in self . view . sel ( ) : <EOL> line_region = self . view . line ( region ) <EOL> before_point_region = sublime . Region ( line_region . a , <EOL> region . a ) <EOL> before_point_content = self . view . substr ( before_point_region ) <EOL> folded = False <EOL> for i in self . view . folded_regions ( ) : <EOL> if i . contains ( before_point_region ) : <EOL> self . view . insert ( edit , region . a , '<STR_LIT:\n>' ) <EOL> folded = True <EOL> if folded : <EOL> break <EOL> match = EMPTY_LIST_PATTERN . match ( before_point_content ) <EOL> if match : <EOL> insert_text = match . group ( <NUM_LIT:1> ) + re . sub ( r'<STR_LIT>' , '<STR_LIT:U+0020>' , str ( match . group ( <NUM_LIT:2> ) ) ) + match . group ( <NUM_LIT:3> ) <EOL> self . view . erase ( edit , before_point_region ) <EOL> self . view . insert ( edit , line_region . a , insert_text ) <EOL> break <EOL> match = ROMAN_PATTERN . match ( before_point_content ) <EOL> if match : <EOL> actual = match . group ( <NUM_LIT:2> ) <EOL> next_num = to_roman ( from_roman ( actual . upper ( ) ) + <NUM_LIT:1> ) <EOL> if actual == actual . lower ( ) : <EOL> next_num = next_num . lower ( ) <EOL> insert_text = match . group ( <NUM_LIT:1> ) + next_num + match . group ( <NUM_LIT:3> ) <EOL> self . view . insert ( edit , region . a , "<STR_LIT:\n>" + insert_text ) <EOL> pos = self . view . sel ( ) [ <NUM_LIT:0> ] . a <EOL> region , lines , indent = self . get_block_bounds ( ) <EOL> new_list = update_roman_list ( lines ) <EOL> self . view . replace ( edit , region , '<STR_LIT:\n>' . join ( new_list ) + '<STR_LIT:\n>' ) <EOL> self . view . sel ( ) . clear ( ) <EOL> self . view . sel ( ) . add ( sublime . Region ( pos , pos ) ) <EOL> self . view . show ( pos ) <EOL> break <EOL> match = ORDER_LIST_PATTERN . match ( before_point_content ) <EOL> if match : <EOL> try : <EOL> next_num = str ( int ( match . group ( <NUM_LIT:2> ) ) + <NUM_LIT:1> ) <EOL> except ValueError : <EOL> next_num = chr ( ord ( match . group ( <NUM_LIT:2> ) ) + <NUM_LIT:1> ) <EOL> insert_text = match . group ( <NUM_LIT:1> ) + next_num + match . group ( <NUM_LIT:3> ) <EOL> self . view . insert ( edit , region . a , "<STR_LIT:\n>" + insert_text ) <EOL> pos = self . view . sel ( ) [ <NUM_LIT:0> ] . a <EOL> region , lines , indent = self . get_block_bounds ( ) <EOL> new_list = update_ordered_list ( lines ) <EOL> self . view . replace ( edit , region , '<STR_LIT:\n>' . join ( new_list ) + '<STR_LIT:\n>' ) <EOL> self . view . sel ( ) . clear ( ) <EOL> self . view . sel ( ) . add ( sublime . Region ( pos , pos ) ) <EOL> self . view . show ( pos ) <EOL> break <EOL> match = UNORDER_LIST_PATTERN . match ( before_point_content ) <EOL> if match : <EOL> insert_text = match . group ( <NUM_LIT:1> ) + match . group ( <NUM_LIT:2> ) <EOL> self . view . insert ( edit , region . a , "<STR_LIT:\n>" + insert_text ) <EOL> break <EOL> match = NONLIST_PATTERN . match ( before_point_content ) <EOL> if match : <EOL> insert_text = match . group ( <NUM_LIT:1> ) + match . group ( <NUM_LIT:2> ) <EOL> self . view . insert ( edit , region . a , "<STR_LIT:\n>" + insert_text ) <EOL> break <EOL> self . view . insert ( edit , region . a , '<STR_LIT:\n>' + re . sub ( r'<STR_LIT>' , '<STR_LIT>' , before_point_content ) ) <EOL> self . adjust_view ( ) <EOL> def adjust_view ( self ) : <EOL> for region in self . view . sel ( ) : <EOL> self . view . show ( region ) </s>
