o ­=?h¡=ã@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ƒZedkrlddlmZedd d!d"dSdS)#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 :-) cCó ||_dS©N©Úeditwin©Úselfr ©r ú5/opt/alt/python310/lib64/python3.10/idlelib/format.pyÚ__init__ó zFormatParagraph.__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'rzFormatParagraph.closeNc Csà|dur|jn|}|jj}|j ¡\}}|r#|r#| ||¡}t|ƒ}n t|| d¡ƒ\}}}}|r8t|||ƒ}nt ||ƒ}|  ddd¡||krc|  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_eventr) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚ classmethodrrr3r r r r rs rc Csˆtt| d¡ƒ\}}| d|d|¡}| d|dd¡rÚ get_indentÚreÚrangeÚ expandtabsÚappendÚrstripÚextendÚjoin) r0r-ÚlinesÚiÚnZindent1Zindent2ÚnewÚpartialZwordsÚjZwordr r r r&xsB ÿ    €ò r&cs„tˆƒ‰d ‡fdd„| d¡Dƒ¡}t|tˆƒdƒ}t||ƒ}| d¡}d}|ds4d}|dd…}d ‡fdd„|Dƒ¡|S) z?Return data reformatted to specified width with comment header.rCc3s|] }|ˆd…VqdSrr ©Ú.0rB)Úlcr r Ú ¡s€z#reformat_comment..éÚéÿÿÿÿNc3s|]}ˆ|VqdSrr rT)r1r r rW°s€)r?rMr=Úmaxr&)r0r-r1Z format_widthr2Z block_suffixr )r1rVr r%œs   r%cCst d|¡duS)z/Return True if line is empty or all whitespace.z^\s*$N)rGÚmatch©rBr r r r>²sr>cCst d|¡ ¡S)z/Return the initial space or tab indent of line.z ^([ \t]*)©rGr\Úgroupr]r r r rF·srFcCs"t d|¡}|dur dS| 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]*#*)NrYr;r^)rBÚ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_rer\rr?r_rI)rBÚtabwidthr`r r r Úget_line_indentÊs rcc@sleZdZdZdd„Zdd„Zdd„Zdd d „Zdd d „Zdd d„Z ddd„Z ddd„Z ddd„Z dd„Z dS)Ú FormatRegionzFormat selected text (region).cCrrrr r r r r×rzFormatRegion.__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 +1crC)r rr r$r!r=)r rr.r/ÚheadÚtailÚcharsrNr 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. rCNrrrr) r rrMZbellr'r(r)r*rr+Ztag_add)r rerfrgrNrÚnewcharsr r r Ú set_regionðs    zFormatRegion.set_regionNc Cs|| ¡\}}}}tt|ƒƒD]%}||}|r3t||jjƒ\}} | |jj} |j | ¡||d…||<q| ||||¡dS)z$Indent region by indentwidth spaces.Nr) rhrHr?rcr rbÚ indentwidthÚ _make_blanksrj© r r,rerfrgrNÚposrBÚrawÚ effectiver r r Úindent_region_events €z FormatRegion.indent_region_eventc Cs‚| ¡\}}}}tt|ƒƒD](}||}|r6t||jjƒ\}} t| |jjdƒ} |j | ¡||d…||<q|  ||||¡dS)z$Dedent region by indentwidth spaces.rNr) rhrHr?rcr rbr[rkrlrjrmr r r Údedent_region_events€z 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. r;ú##r©rhrHr?rj©r r,rerfrgrNrnrBr r r Úcomment_region_event s z!FormatRegion.comment_region_eventcCs†| ¡\}}}}tt|ƒƒD]*}||}|sq|dd…dkr&|dd…}n|dd…dkr4|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. NrDrsr;ú#rrtrur r r Úuncomment_region_event,s  z#FormatRegion.uncomment_region_eventc Cs’| ¡\}}}}| ¡}|durdStt|ƒƒD]&}||}|r>t||ƒ\} } t| |ƒ\} } d| d| || d…||<q| ||||¡dS)z@Convert leading spaces to tabs for each line in selected region.Nú rEr)rhÚ _asktabwidthrHr?rcÚdivmodrj) r r,rerfrgrNrbrnrBrorpZntabsZnspacesr r r Útabify_region_event?s €z FormatRegion.tabify_region_eventcCs\| ¡\}}}}| ¡}|durdStt|ƒƒD] }|| |¡||<q| ||||¡dS)z.Expand tabs to spaces for each line in region.Nr)rhrzrHr?rIrj)r r,rerfrgrNrbrnr r r Úuntabify_region_eventNsz"FormatRegion.untabify_region_eventcCstdd|jj|jjdddS)zReturn value for tab width.z Tab widthzColumns per tab? (2-16)rDé©ÚparentZ initialvalueZminvalueZmaxvalue)rr rrkrr r r rzYsúzFormatRegion._asktabwidthr)r4r5r6r7rrhrjrqrrrvrxr|r}rzr r r r rdÔs    rdc@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚIndentszChange future indents.cCrrrr r r r rgrzIndents.__init__cCsJ|j}|j}tddd|dd|dd|jdr#| |_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 Úusetabsrrrk)r r,r rƒr r r Útoggle_tabs_eventjs$ ÿþþýúzIndents.toggle_tabs_eventcCs<|j}tdd|j|jddd}|r||jkr|js||_dS)Nz Indent widthz6New indent width (2-16) (Always use 8 when using tabs)rDr~rr)r rrrkrƒ)r r,r rQr r r Úchange_indentwidth_eventzsúz Indents.change_indentwidth_eventN)r4r5r6r7rr„r…r r r r rds  rc@seZdZdd„Zddd„ZdS)ÚRstripcCrrrr r r r r‰rzRstrip.__init__Nc Csê|jj}|jj}| ¡tt| d¡ƒƒ}td|ƒD]&}| d|d|¡}t |ƒ}t |  ¡ƒ}||kr@|  d||fd|¡q| d¡dkrot |jdƒso| d ¡d kro| d ¡dkro|  d ¡| d ¡d kro| d ¡dks\|  ¡dS) Nrr;z%i.0z%i.endz%i.%izend-2crCZinterpzend-1crzend-3c)r rÚundor)rÚfloatr$rHr!r?rKr*Úhasattrr+) r r,rr‡Zend_lineZcurZtxtroZcutr r r Ú do_rstripŒs( € ÿ þ zRstrip.do_rstripr)r4r5r6rrŠr r r r r†ˆsr†Ú__main__)Úmainzidlelib.idle_test.test_formatrDF)Ú verbosityÚexit)r7rGZtkinter.messageboxrZtkinter.simpledialogrZidlelib.configrrrr#r&r%r>rFr"Úcompilerarcrdrr†r4ZunittestrŒr r r r Ús.   C%$  $ þ