B ? H°çãTã@sØdZddlZddlmZddlmZddlmZGdd„dƒZe  ¡dd „Z d d „Z d d „Z dd„Z dd„Zdd„Ze d¡Zdd„ZGdd„dƒZGdd„dƒZGdd„dƒZedkrÔddlmZedd d!d"dS)#zíFormat all or a selected region (line slice) of text. Region formatting options: paragraph, comment block, indent, deindent, comment, uncomment, tabify, and untabify. File renamed from paragraph.py with functions added from editor.py. éN)Úaskyesno)Ú askinteger)ÚidleConfc@s6eZdZdZdd„Zedd„ƒZdd„Zd d d „ZdS) ÚFormatParagraphaFormat a paragraph, comment block, 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 :-) cCs ||_dS)N)Úeditwin)Úselfr©rú3/opt/alt/python37/lib64/python3.7/idlelib/format.pyÚ__init__szFormatParagraph.__init__cCstjdddddd|_dS)NÚ extensionsrz max-widthÚintéH)ÚtypeÚdefault)rZ GetOptionÚ max_width)Úclsrrr Úreload"szFormatParagraph.reloadcCs d|_dS)N)r)rrrr Úclose'szFormatParagraph.closeNc Csà|dkr|jn|}|jj}|j ¡\}}|rF|rF| ||¡}t|ƒ}nt|| d¡ƒ\}}}}|rpt|||ƒ}n t ||ƒ}|  ddd¡||krÆ|  d|¡|  ¡|  ||¡| ||¡| ¡n |  d|¡| d¡dS)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ÚinsertÚselz1.0ÚendÚbreak)rrÚtextÚget_selection_indicesÚgetÚget_comment_headerÚfind_paragraphÚindexÚreformat_commentÚreformat_paragraphÚ tag_removeÚmark_setÚundo_block_startÚdeleterÚundo_block_stopZsee) rÚeventÚlimitrÚfirstÚlastÚdataÚcomment_headerÚnewdatarrr Úformat_paragraph_event*s(          z&FormatParagraph.format_paragraph_event)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__r Ú classmethodrrr,rrrr rs  rc CsFtt| d¡ƒ\}}| d|d|¡}x:| d|dd¡rbt|ƒrb|d}| d|d|¡}q*W|}t|ƒ}t|ƒ}x¡sz#reformat_comment..éÚéÿÿÿÿNc3s|]}ˆ|VqdS)Nr)rMr;)r*rr rO°s)r8rFr6Úmaxr)r)r&r*Z format_widthr+Z block_suffixr)r*rNr rœs   rcCst d|¡dk S)z/Return True if line is empty or all whitespace.z^\s*$N)r@Úmatch)r;rrr r7²sr7cCst d|¡ ¡S)z/Return the initial space or tab indent of line.z ^([ \t]*))r@rTÚgroup)r;rrr r?·sr?cCs"t d|¡}|dkrdS| d¡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]*#*)NrQr4)r@rTrU)r;Úmrrr r»s rz[ \t]*cCs$t |¡}| ¡t| ¡ |¡ƒfS)zÌReturn a line's indentation as (# chars, effective # of spaces). The effective # of spaces is the length after properly "expanding" the tabs into spaces, as done by str.expandtabs(tabwidth). )Ú_line_indent_rerTrr8rUrB)r;ÚtabwidthrVrrr Úget_line_indentÊs rYc@sleZdZdZdd„Zdd„Zdd„Zdd d „Zdd d „Zdd d„Z ddd„Z ddd„Z ddd„Z dd„Z dS)Ú FormatRegionzFormat selected text (region).cCs ||_dS)N)r)rrrrr r ×szFormatRegion.__init__cCsr|jj}|j ¡\}}|r<|r<| |d¡}| |d¡}n| d¡}| d¡}| ||¡}| d¡}||||fS)aqReturn line information about the selected text region. If text is selected, the first and last indices will be for the selection. If there is no text selected, the indices will be the current cursor location. Return a tuple containing (first index, last index, string representation of text, list of text lines). z linestartz-1c lineend +1czinsert linestartzinsert lineend +1cr<)rrrrrr6)rrr'r(ÚheadÚtailÚcharsrGrrr Ú get_regionÚs     zFormatRegion.get_regioncCsz|jj}d |¡}||kr&| ¡dS| ddd¡| d|¡| ¡| ||¡| ||¡|  ¡|  d|d¡dS)aaReplace the text between the given indices. Args: head: Starting index of text to replace. tail: Ending index of text to replace. chars: Expected to be string of current text between head and tail. lines: List of new lines to insert between head and tail. r<Nrz1.0rr) rrrFZbellr r!r"r#rr$Ztag_add)rr[r\r]rGrÚnewcharsrrr Ú set_regionðs     zFormatRegion.set_regionNc Cs€| ¡\}}}}xZtt|ƒƒD]J}||}|rt||jjƒ\}} | |jj} |j | ¡||d…||<qW| ||||¡dS)z$Indent region by indentwidth spaces.Nr) r^rAr8rYrrXÚ indentwidthÚ _make_blanksr`) rr%r[r\r]rGÚposr;ÚrawÚ effectiverrr Úindent_region_events  z FormatRegion.indent_region_eventc Cs†| ¡\}}}}x`tt|ƒƒD]P}||}|rt||jjƒ\}} t| |jjdƒ} |j | ¡||d…||<qW|  ||||¡dS)z$Dedent region by indentwidth spaces.rNr) r^rAr8rYrrXrSrarbr`) rr%r[r\r]rGrcr;rdrerrr Údedent_region_events z FormatRegion.dedent_region_eventcCsR| ¡\}}}}x,tt|ƒdƒD]}||}d|||<q"W| ||||¡dS)zrComment out each line in region. ## is appended to the beginning of each line to comment it out. r4z##r)r^rAr8r`)rr%r[r\r]rGrcr;rrr Úcomment_region_event s z!FormatRegion.comment_region_eventcCsŠ| ¡\}}}}xdtt|ƒƒD]T}||}|s0q|dd…dkrN|dd…}n|dd…dkrj|dd…}|||<qW| ||||¡dS)z½Uncomment each line in region. Remove ## or # in the first positions of a line. If the comment is not in the beginning position, this command will have no effect. Nr=z##r4ú#r)r^rAr8r`)rr%r[r\r]rGrcr;rrr Úuncomment_region_event,s  z#FormatRegion.uncomment_region_eventc Cs–| ¡\}}}}| ¡}|dkr$dSx\tt|ƒƒD]L}||}|r2t||ƒ\} } t| |ƒ\} } d| d| || d…||<q2W| ||||¡dS)z@Convert leading spaces to tabs for each line in selected region.Nú r>r)r^Ú _asktabwidthrAr8rYÚdivmodr`) rr%r[r\r]rGrXrcr;rdreZntabsZnspacesrrr Útabify_region_event?s$z FormatRegion.tabify_region_eventcCs`| ¡\}}}}| ¡}|dkr$dSx&tt|ƒƒD]}|| |¡||<q2W| ||||¡dS)z.Expand tabs to spaces for each line in region.Nr)r^rlrAr8rBr`)rr%r[r\r]rGrXrcrrr Úuntabify_region_eventNsz"FormatRegion.untabify_region_eventcCstdd|jj|jjdddS)zReturn value for tab width.z Tab widthzColumns per tab? (2-16)r=é)ÚparentÚ initialvalueÚminvalueÚmaxvalue)rrrra)rrrr rlYszFormatRegion._asktabwidth)N)N)N)N)N)N)r-r.r/r0r r^r`rfrgrhrjrnrorlrrrr rZÔs   rZc@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚIndentszChange future indents.cCs ||_dS)N)r)rrrrr r gszIndents.__init__cCsJ|j}|j}tddd|dd|dd|jdrF| |_d |_d S) Nz Toggle tabsz Turn tabs )ZonZoffz? Indent width )zwill bez remains atz 8.z! Note: a tab is always 8 columns)rqér)rÚusetabsrrra)rr%rrwrrr Útoggle_tabs_eventjs zIndents.toggle_tabs_eventcCs<|j}tdd|j|jddd}|r8||jkr8|js8||_dS)Nz Indent widthz6New indent width (2-16) (Always use 8 when using tabs)r=rp)rqrrrsrtr)rrrrarw)rr%rrJrrr Úchange_indentwidth_eventzsz Indents.change_indentwidth_eventN)r-r.r/r0r rxryrrrr rudsruc@seZdZdd„Zddd„ZdS)ÚRstripcCs ||_dS)N)r)rrrrr r ‰szRstrip.__init__Nc CsØ|jj}|jj}| ¡tt| d¡ƒƒ}xZtd|ƒD]L}| d|d|¡}t |ƒ}t |  ¡ƒ}||kr6|  d||fd|¡q6W| d¡dkrÌt |jdƒsÌx*| d ¡d krÊ| d ¡dkrÊ|  d ¡q¢W|  ¡dS) Nrr4z%i.0z%i.endz%i.%izend-2cr<Zinterpzend-1cz1.0zend-3c)rrÚundor"r ÚfloatrrArr8rDr#Úhasattrr$) rr%rr{Zend_lineZcurZtxtrdZcutrrr Ú do_rstripŒs   zRstrip.do_rstrip)N)r-r.r/r r~rrrr rzˆsrzÚ__main__)Úmainzidlelib.idle_test.test_formatr=F)Ú verbosityÚexit)r0r@Ztkinter.messageboxrZtkinter.simpledialogrZidlelib.configrrrrrrr7r?rÚcompilerWrYrZrurzr-Zunittestr€rrrr Ús*   C%$  $