<s> import sublime , sublime_plugin <EOL> import string <EOL> import textwrap <EOL> import re <EOL> import comment <EOL> def previous_line ( view , sr ) : <EOL> """<STR_LIT>""" <EOL> if sr . begin ( ) == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> return view . full_line ( sr . begin ( ) - <NUM_LIT:1> ) <EOL> def next_line ( view , sr ) : <EOL> """<STR_LIT>""" <EOL> if sr . end ( ) == view . size ( ) : <EOL> return None <EOL> else : <EOL> return view . full_line ( sr . end ( ) ) <EOL> separating_line_pattern = re . compile ( "<STR_LIT>" ) <EOL> def is_paragraph_separating_line ( view , sr ) : <EOL> return separating_line_pattern . match ( view . substr ( sr ) ) != None <EOL> def has_prefix ( view , line , prefix ) : <EOL> if not prefix : <EOL> return True <EOL> line_start = view . substr ( sublime . Region ( line . begin ( ) , <EOL> line . begin ( ) + len ( prefix ) ) ) <EOL> return line_start == prefix <EOL> def expand_to_paragraph ( view , tp ) : <EOL> sr = view . full_line ( tp ) <EOL> if is_paragraph_separating_line ( view , sr ) : <EOL> return sublime . Region ( tp , tp ) <EOL> required_prefix = None <EOL> ( line_comments , block_comments ) = comment . build_comment_data ( view , tp ) <EOL> dataStart = comment . advance_to_first_non_white_space_on_line ( view , sr . begin ( ) ) <EOL> for c in line_comments : <EOL> ( start , disable_indent ) = c <EOL> comment_region = sublime . Region ( dataStart , <EOL> dataStart + len ( start ) ) <EOL> if view . substr ( comment_region ) == start : <EOL> required_prefix = view . substr ( sublime . Region ( sr . begin ( ) , comment_region . end ( ) ) ) <EOL> break <EOL> first = sr . begin ( ) <EOL> prev = sr <EOL> while True : <EOL> prev = previous_line ( view , prev ) <EOL> if ( prev == None or is_paragraph_separating_line ( view , prev ) or <EOL> not has_prefix ( view , prev , required_prefix ) ) : <EOL> break <EOL> else : <EOL> first = prev . begin ( ) <EOL> last = sr . end ( ) <EOL> next = sr <EOL> while True : <EOL> next = next_line ( view , next ) <EOL> if ( next == None or is_paragraph_separating_line ( view , next ) or <EOL> not has_prefix ( view , next , required_prefix ) ) : <EOL> break <EOL> else : <EOL> last = next . end ( ) <EOL> return sublime . Region ( first , last ) <EOL> def all_paragraphs_intersecting_selection ( view , sr ) : <EOL> paragraphs = [ ] <EOL> para = expand_to_paragraph ( view , sr . begin ( ) ) <EOL> if not para . empty ( ) : <EOL> paragraphs . append ( para ) <EOL> while True : <EOL> line = next_line ( view , para ) <EOL> if line == None or line . begin ( ) >= sr . end ( ) : <EOL> break ; <EOL> if not is_paragraph_separating_line ( view , line ) : <EOL> para = expand_to_paragraph ( view , line . begin ( ) ) <EOL> paragraphs . append ( para ) <EOL> else : <EOL> para = line <EOL> return paragraphs <EOL> class ExpandSelectionToParagraphCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> regions = [ ] <EOL> for s in self . view . sel ( ) : <EOL> regions . append ( sublime . Region ( <EOL> expand_to_paragraph ( self . view , s . begin ( ) ) . begin ( ) , <EOL> expand_to_paragraph ( self . view , s . end ( ) ) . end ( ) ) ) <EOL> for r in regions : <EOL> self . view . sel ( ) . add ( r ) <EOL> class WrapLinesCommand ( sublime_plugin . TextCommand ) : <EOL> line_prefix_pattern = re . compile ( "<STR_LIT>" ) <EOL> def extract_prefix ( self , sr ) : <EOL> lines = self . view . split_by_newlines ( sr ) <EOL> if len ( lines ) == <NUM_LIT:0> : <EOL> return None <EOL> initial_prefix_match = self . line_prefix_pattern . match ( self . view . substr ( <EOL> lines [ <NUM_LIT:0> ] ) ) <EOL> if not initial_prefix_match : <EOL> return None <EOL> prefix = self . view . substr ( sublime . Region ( lines [ <NUM_LIT:0> ] . begin ( ) , <EOL> lines [ <NUM_LIT:0> ] . begin ( ) + initial_prefix_match . end ( ) ) ) <EOL> for line in lines [ <NUM_LIT:1> : ] : <EOL> if self . view . substr ( sublime . Region ( line . begin ( ) , <EOL> line . begin ( ) + len ( prefix ) ) ) != prefix : <EOL> return None <EOL> return prefix <EOL> def width_in_spaces ( self , str , tab_width ) : <EOL> sum = <NUM_LIT:0> ; <EOL> for c in str : <EOL> if c == '<STR_LIT:\t>' : <EOL> sum += tab_width - <NUM_LIT:1> <EOL> return sum <EOL> def run ( self , edit , width = <NUM_LIT:0> ) : <EOL> if width == <NUM_LIT:0> and self . view . settings ( ) . get ( "<STR_LIT>" ) : <EOL> try : <EOL> width = int ( self . view . settings ( ) . get ( "<STR_LIT>" ) ) <EOL> except TypeError : <EOL> pass <EOL> if width == <NUM_LIT:0> and self . view . settings ( ) . get ( "<STR_LIT>" ) : <EOL> try : <EOL> width = int ( self . view . settings ( ) . get ( "<STR_LIT>" ) [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> pass <EOL> except TypeError : <EOL> pass <EOL> if width == <NUM_LIT:0> : <EOL> width = <NUM_LIT> <EOL> tab_width = <NUM_LIT:8> <EOL> if self . view . settings ( ) . get ( "<STR_LIT>" ) : <EOL> try : <EOL> tab_width = int ( self . view . settings ( ) . get ( "<STR_LIT>" ) ) <EOL> except TypeError : <EOL> pass <EOL> if tab_width == <NUM_LIT:0> : <EOL> tab_width == <NUM_LIT:8> <EOL> paragraphs = [ ] <EOL> for s in self . view . sel ( ) : <EOL> paragraphs . extend ( all_paragraphs_intersecting_selection ( self . view , s ) ) <EOL> if len ( paragraphs ) > <NUM_LIT:0> : <EOL> self . view . sel ( ) . clear ( ) <EOL> for p in paragraphs : <EOL> self . view . sel ( ) . add ( p ) <EOL> for s in self . view . sel ( ) : <EOL> wrapper = textwrap . TextWrapper ( ) <EOL> wrapper . expand_tabs = False <EOL> wrapper . width = width <EOL> prefix = self . extract_prefix ( s ) <EOL> if prefix : <EOL> wrapper . initial_indent = prefix <EOL> wrapper . subsequent_indent = prefix <EOL> wrapper . width -= self . width_in_spaces ( prefix , tab_width ) <EOL> if wrapper . width < <NUM_LIT:0> : <EOL> continue <EOL> txt = self . view . substr ( s ) <EOL> if prefix : <EOL> txt = txt . replace ( prefix , u"<STR_LIT>" ) <EOL> txt = string . expandtabs ( txt , tab_width ) <EOL> txt = wrapper . fill ( txt ) + u"<STR_LIT:\n>" <EOL> self . view . replace ( edit , s , txt ) <EOL> ends = [ s . end ( ) - <NUM_LIT:1> for s in self . view . sel ( ) ] <EOL> self . view . sel ( ) . clear ( ) <EOL> for pt in ends : <EOL> self . view . sel ( ) . add ( sublime . Region ( pt ) ) </s>
