<s> """<STR_LIT>""" <EOL> import re <EOL> from idlelib . configHandler import idleConf <EOL> class FormatParagraph : <EOL> menudefs = [ <EOL> ( '<STR_LIT>' , [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] ) <EOL> ] <EOL> def __init__ ( self , editwin ) : <EOL> self . editwin = editwin <EOL> def close ( self ) : <EOL> self . editwin = None <EOL> def format_paragraph_event ( self , event , limit = None ) : <EOL> """<STR_LIT>""" <EOL> if limit is None : <EOL> limit = idleConf . GetOption ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> type = '<STR_LIT:int>' , default = <NUM_LIT> ) <EOL> text = self . editwin . text <EOL> first , last = self . editwin . get_selection_indices ( ) <EOL> if first and last : <EOL> data = text . get ( first , last ) <EOL> comment_header = get_comment_header ( data ) <EOL> else : <EOL> first , last , comment_header , data = find_paragraph ( text , text . index ( "<STR_LIT>" ) ) <EOL> if comment_header : <EOL> newdata = reformat_comment ( data , limit , comment_header ) <EOL> else : <EOL> newdata = reformat_paragraph ( data , limit ) <EOL> text . tag_remove ( "<STR_LIT>" , "<STR_LIT:1.0>" , "<STR_LIT:end>" ) <EOL> if newdata != data : <EOL> text . mark_set ( "<STR_LIT>" , first ) <EOL> text . undo_block_start ( ) <EOL> text . delete ( first , last ) <EOL> text . insert ( first , newdata ) <EOL> text . undo_block_stop ( ) <EOL> else : <EOL> text . mark_set ( "<STR_LIT>" , last ) <EOL> text . see ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> def find_paragraph ( text , mark ) : <EOL> """<STR_LIT>""" <EOL> lineno , col = map ( int , mark . split ( "<STR_LIT:.>" ) ) <EOL> line = text . get ( "<STR_LIT>" % lineno , "<STR_LIT>" % lineno ) <EOL> while text . compare ( "<STR_LIT>" % lineno , "<STR_LIT:<>" , "<STR_LIT:end>" ) and is_all_white ( line ) : <EOL> lineno = lineno + <NUM_LIT:1> <EOL> line = text . get ( "<STR_LIT>" % lineno , "<STR_LIT>" % lineno ) <EOL> first_lineno = lineno <EOL> comment_header = get_comment_header ( line ) <EOL> comment_header_len = len ( comment_header ) <EOL> while get_comment_header ( line ) == comment_header and not is_all_white ( line [ comment_header_len : ] ) : <EOL> lineno = lineno + <NUM_LIT:1> <EOL> line = text . get ( "<STR_LIT>" % lineno , "<STR_LIT>" % lineno ) <EOL> last = "<STR_LIT>" % lineno <EOL> lineno = first_lineno - <NUM_LIT:1> <EOL> line = text . get ( "<STR_LIT>" % lineno , "<STR_LIT>" % lineno ) <EOL> while lineno > <NUM_LIT:0> and get_comment_header ( line ) == comment_header and not is_all_white ( line [ comment_header_len : ] ) : <EOL> lineno = lineno - <NUM_LIT:1> <EOL> line = text . get ( "<STR_LIT>" % lineno , "<STR_LIT>" % lineno ) <EOL> first = "<STR_LIT>" % ( lineno + <NUM_LIT:1> ) <EOL> return first , last , comment_header , text . get ( first , last ) <EOL> def reformat_paragraph ( data , limit ) : <EOL> """<STR_LIT>""" <EOL> lines = data . split ( "<STR_LIT:\n>" ) <EOL> i = <NUM_LIT:0> <EOL> n = len ( lines ) <EOL> while i < n and is_all_white ( lines [ i ] ) : <EOL> i = i + <NUM_LIT:1> <EOL> if i >= n : <EOL> return data <EOL> indent1 = get_indent ( lines [ i ] ) <EOL> if i + <NUM_LIT:1> < n and not is_all_white ( lines [ i + <NUM_LIT:1> ] ) : <EOL> indent2 = get_indent ( lines [ i + <NUM_LIT:1> ] ) <EOL> else : <EOL> indent2 = indent1 <EOL> new = lines [ : i ] <EOL> partial = indent1 <EOL> while i < n and not is_all_white ( lines [ i ] ) : <EOL> words = re . split ( "<STR_LIT>" , lines [ i ] ) <EOL> for j in range ( <NUM_LIT:0> , len ( words ) , <NUM_LIT:2> ) : <EOL> word = words [ j ] <EOL> if not word : <EOL> continue <EOL> if len ( ( partial + word ) . expandtabs ( ) ) > limit and partial != indent1 : <EOL> new . append ( partial . rstrip ( ) ) <EOL> partial = indent2 <EOL> partial = partial + word + "<STR_LIT:U+0020>" <EOL> if j + <NUM_LIT:1> < len ( words ) and words [ j + <NUM_LIT:1> ] != "<STR_LIT:U+0020>" : <EOL> partial = partial + "<STR_LIT:U+0020>" <EOL> i = i + <NUM_LIT:1> <EOL> new . append ( partial . rstrip ( ) ) <EOL> new . extend ( lines [ i : ] ) <EOL> return "<STR_LIT:\n>" . join ( new ) <EOL> def reformat_comment ( data , limit , comment_header ) : <EOL> """<STR_LIT>""" <EOL> lc = len ( comment_header ) <EOL> data = "<STR_LIT:\n>" . join ( line [ lc : ] for line in data . split ( "<STR_LIT:\n>" ) ) <EOL> format_width = max ( limit - len ( comment_header ) , <NUM_LIT:20> ) <EOL> newdata = reformat_paragraph ( data , format_width ) <EOL> newdata = newdata . split ( "<STR_LIT:\n>" ) <EOL> block_suffix = "<STR_LIT>" <EOL> if not newdata [ - <NUM_LIT:1> ] : <EOL> block_suffix = "<STR_LIT:\n>" <EOL> newdata = newdata [ : - <NUM_LIT:1> ] <EOL> return '<STR_LIT:\n>' . join ( comment_header + line for line in newdata ) + block_suffix <EOL> def is_all_white ( line ) : <EOL> """<STR_LIT>""" <EOL> return re . match ( r"<STR_LIT>" , line ) is not None <EOL> def get_indent ( line ) : <EOL> """<STR_LIT>""" <EOL> return re . match ( r"<STR_LIT>" , line ) . group ( ) <EOL> def get_comment_header ( line ) : <EOL> """<STR_LIT>""" <EOL> m = re . match ( r"<STR_LIT>" , line ) <EOL> if m is None : return "<STR_LIT>" <EOL> return m . group ( <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import unittest <EOL> unittest . main ( '<STR_LIT>' , <EOL> verbosity = <NUM_LIT:2> , exit = False ) </s>
