î Òê[wã@sµdZddlZddlmZGdd„dƒZdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z e dkr±ddl Z e j dddddƒndS)aÐExtension to format a paragraph or selection to a max width. Does basic, standard text formatting, and also understands Python comment blocks. Thus, for editing Python source code, this extension is really only suitable for reformatting these comment blocks or triple-quoted strings. Known problems with comment reformatting: * If there is a selection marked, and the first line of the selection is not complete, the block will probably not be detected as comments, and will have the normal "text formatting" rules applied. * If a comment block has leading whitespace that mixes tabs and spaces, they will not be considered part of the same block. * Fancy comments, like this bulleted list, aren't handled :-) éN)ÚidleConfc@sIeZdZdd gfgZdd„Zdd„Zdd d „ZdS) ÚFormatParagraphÚformatúFormat Paragraphú<>cCs ||_dS)N)Úeditwin)Úselfr©r ú-/usr/lib/python3.4/idlelib/FormatParagraph.pyÚ__init__szFormatParagraph.__init__cCs d|_dS)N)r)rr r r Úclose szFormatParagraph.closeNc CsS|dkr0tjdddddddƒ}n|jj}|jjƒ\}}|r~|r~|j||ƒ}t|ƒ}n$t||jd ƒƒ\}}}}|r½t |||ƒ}nt ||ƒ}|j d d d ƒ||kr2|j d |ƒ|j ƒ|j||ƒ|j||ƒ|jƒn|j d |ƒ|jd ƒd S)aÕFormats paragraph to a max width specified in idleConf. If text is selected, format_paragraph_event will start breaking lines at the max width, starting from the beginning selection. If no text is selected, format_paragraph_event uses the current cursor location to determine the paragraph (lines of text surrounded by blank lines) and formats it. The length limit parameter is for testing with a known value. NÚ extensionsrz max-widthÚtypeÚintÚdefaultéHÚinsertZselz1.0ÚendÚbreak)rZ GetOptionrÚtextZget_selection_indicesÚgetÚget_comment_headerÚfind_paragraphÚindexÚreformat_commentÚreformat_paragraphZ tag_removeZmark_setZundo_block_startZdeleterZundo_block_stopZsee) rZeventÚlimitrÚfirstÚlastÚdataÚcomment_headerÚnewdatar r r Úformat_paragraph_event#s.    $    z&FormatParagraph.format_paragraph_event)rr)Ú__name__Ú __module__Ú __qualname__Zmenudefsr r r"r r r r rs   rc Cs²tt|jdƒƒ\}}|jd|d|ƒ}xM|jd|ddƒr‡t|ƒr‡|d}|jd|d|ƒ}q;W|}t|ƒ}t|ƒ}xQt|ƒ|krùt||d…ƒ rù|d}|jd|d|ƒ}q©Wd|}|d}|jd|d|ƒ}x]|dkr‡t|ƒ|kr‡t||d…ƒ r‡|d}|jd|d|ƒ}q+Wd|d} | |||j| |ƒfS) z»Returns the start/stop indices enclosing the paragraph that mark is in. Also returns the comment format string, if any, and paragraph of text between the start/stop indices. Ú.z%d.0z%d.endú›sz#reformat_comment..éÚr(Nc3s|]}ˆ|VqdS)Nr )rBr0)r r r rDªséÿÿÿÿrG)r,r;r*Úmaxr)rrr Z format_widthr!Z block_suffixr )r rCr r–s + rcCstjd|ƒdk S)z/Return True if line is empty or all whitespace.z^\s*$N)r5Úmatch)r0r r r r+¬sr+cCstjd|ƒjƒS)z/Return the initial space or tab indent of line.z ^([ \t]*))r5rIÚgroup)r0r r r r4±sr4cCs/tjd|ƒ}|dkr"dS|jdƒS)aReturn string with leading whitespace and '#' from line or ''. A null return indicates that the line is not a comment line. A non- null return, such as ' #', will be used to find the other lines of a comment block with the same indent. z ^([ \t]*#*)NrFr()r5rIrJ)r0Úmr r r rµs rÚ__main__z&idlelib.idle_test.test_formatparagraphÚ verbosityr2ÚexitF)Ú__doc__r5Zidlelib.configHandlerrrrrrr+r4rr#ZunittestÚmainr r r r Ús 8 % $