U .‘c¡=ã@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/python38/lib64/python3.8/idlelib/format.pyÚ__init__szFormatParagraph.__init__cCstjdddddd|_dS)NÚ extensionsrz max-widthÚintéH)ÚtypeÚdefault)rZ GetOptionÚ max_width)Úclsr r r Úreload"s ÿzFormatParagraph.reloadcCs d|_dSrr©r r r r Ú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Úselú1.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Únewdatar r r Úformat_paragraph_event*s*   ÿ        z&FormatParagraph.format_paragraph_event)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__r Ú classmethodrrr1r r r r rs  rc Cs:tt| d¡ƒ\}}| d|d|¡}| d|dd¡r`t|ƒr`|d}| d|d|¡}q(|}t|ƒ}t|ƒ}t|ƒ|kr®t||d…ƒs®|d}| d|d|¡}qtd|}|d}| d|d|¡}|dkrt|ƒ|krt||d…ƒs|d}| d|d|¡}qÒd|d} | ||| | |¡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ú    ÿ    r$cs„tˆƒ‰d ‡fdd„| d¡Dƒ¡}t|tˆƒdƒ}t||ƒ}| d¡}d}|dshd}|dd…}d ‡fdd„|Dƒ¡|S) z?Return data reformatted to specified width with comment header.rAc3s|]}|ˆd…VqdSrr ©Ú.0r@)Úlcr r Ú ¡sz#reformat_comment..éÚéÿÿÿÿNc3s|]}ˆ|VqdSrr rR)r/r r rU°s)r=rKr;Úmaxr$)r.r+r/Z format_widthr0Z block_suffixr )r/rTr r#œs   r#cCst d|¡dk S)z/Return True if line is empty or all whitespace.z^\s*$N)rEÚmatch©r@r r r r<²sr<cCst d|¡ ¡S)z/Return the initial space or tab indent of line.z ^([ \t]*)©rErZÚgroupr[r r r rD·srDcCs"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]*#*)NrWr9r\)r@Úmr r r r »s r z[ \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_rerZrr=r]rG)r@Útabwidthr^r r r Úget_line_indentÊs rac@sleZdZdZdd„Zdd„Zdd„Zdd d „Zdd d „Zdd d„Z ddd„Z ddd„Z ddd„Z dd„Z dS)Ú FormatRegionzFormat selected text (region).cCs ||_dSrrr r r r 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 +1crA)rrrr"rr;)r rr,r-ÚheadÚtailÚcharsrLr r r Ú 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. rANrrrr) rrrKZbellr%r&r'r(rr)Ztag_add)r rcrdrerLrÚnewcharsr r r Ú set_regionðs     zFormatRegion.set_regionNc Cs|| ¡\}}}}tt|ƒƒD]J}||}|rt||jjƒ\}} | |jj} |j | ¡||d…||<q| ||||¡dS)z$Indent region by indentwidth spaces.Nr) rfrFr=rarr`Ú indentwidthÚ _make_blanksrh© r r*rcrdrerLÚposr@ÚrawÚ effectiver r r Úindent_region_events z FormatRegion.indent_region_eventc Cs‚| ¡\}}}}tt|ƒƒD]P}||}|rt||jjƒ\}} t| |jjdƒ} |j | ¡||d…||<q|  ||||¡dS)z$Dedent region by indentwidth spaces.rNr) rfrFr=rarr`rYrirjrhrkr r r Údedent_region_eventsz FormatRegion.dedent_region_eventcCsN| ¡\}}}}tt|ƒdƒD]}||}d|||<q | ||||¡dS)zrComment out each line in region. ## is appended to the beginning of each line to comment it out. r9ú##r©rfrFr=rh©r r*rcrdrerLrlr@r r r Úcomment_region_event s z!FormatRegion.comment_region_eventcCs†| ¡\}}}}tt|ƒƒD]T}||}|s.q|dd…dkrL|dd…}n|dd…dkrh|dd…}|||<q| ||||¡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. NrBrqr9ú#rrrrsr r r Úuncomment_region_event,s  z#FormatRegion.uncomment_region_eventc Cs’| ¡\}}}}| ¡}|dkr$dStt|ƒƒD]L}||}|r0t||ƒ\} } t| |ƒ\} } d| d| || d…||<q0| ||||¡dS)z@Convert leading spaces to tabs for each line in selected region.Nú rCr)rfÚ _asktabwidthrFr=raÚdivmodrh) r r*rcrdrerLr`rlr@rmrnZntabsZnspacesr r r Útabify_region_event?s"z FormatRegion.tabify_region_eventcCs\| ¡\}}}}| ¡}|dkr$dStt|ƒƒD]}|| |¡||<q0| ||||¡dS)z.Expand tabs to spaces for each line in region.Nr)rfrxrFr=rGrh)r r*rcrdrerLr`rlr r r Úuntabify_region_eventNsz"FormatRegion.untabify_region_eventcCstdd|jj|jjdddS)zReturn value for tab width.z Tab widthzColumns per tab? (2-16)rBé©ÚparentZ initialvalueZminvalueZmaxvalue)rrrrirr r r rxYsúzFormatRegion._asktabwidth)N)N)N)N)N)N)r2r3r4r5r rfrhrorprtrvrzr{rxr r r r rbÔs   rbc@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚIndentszChange future indents.cCs ||_dSrrr r r r 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)r~ér)rÚusetabsrrri)r r*rrr r r Ú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)rBr|r}r)rrrrir)r r*rrOr r r Úchange_indentwidth_eventzsúz Indents.change_indentwidth_eventN)r2r3r4r5r r‚rƒr r r r rdsrc@seZdZdd„Zddd„ZdS)ÚRstripcCs ||_dSrrr r r r r ‰szRstrip.__init__Nc CsÐ|jj}|jj}| ¡tt| d¡ƒƒ}td|ƒD]L}| d|d|¡}t |ƒ}t |  ¡ƒ}||kr4|  d||fd|¡q4| d¡dkrÄt |jdƒsÄ| d ¡d krÄ| d ¡dkrÄ|  d ¡qœ|  ¡dS) Nrr9z%i.0z%i.endz%i.%izend-2crAZinterpzend-1crzend-3c)rrÚundor'rÚfloatr"rFrr=rIr(Úhasattrr)) r r*rr…Zend_lineZcurZtxtrmZcutr r r Ú do_rstripŒs$  ÿ ÿ zRstrip.do_rstrip)N)r2r3r4r rˆr r r r r„ˆsr„Ú__main__)Úmainzidlelib.idle_test.test_formatrBF)Ú verbosityÚexit)r5rEZtkinter.messageboxrZtkinter.simpledialogrZidlelib.configrrrr!r$r#r<rDr Úcompiler_rarbrr„r2ZunittestrŠr r r r Ús*   C%$  $