Yy+2 dZddlZddlmZddlmZddlmZGddZejdZ d Z d Z d Z d Zd Zej dZdZGddZGddZGddZedk(rddlmZedddyy)zFormat 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)idleConfc4eZdZdZdZedZdZddZy)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 :-) c||_yNeditwinselfr s 5/opt/alt/python312/lib64/python3.12/idlelib/format.py__init__zFormatParagraph.__init__  cBtjddddd|_y)N extensionsrz max-widthintH)typedefault)r GetOption max_width)clss rreloadzFormatParagraph.reload"s! **<9J+6UBP rcd|_yr r r s rclosezFormatParagraph.close's  rNcj| |jn|}|jj}|jj\}}|r |r|j ||}t |}n t ||jd\}}}}|rt|||}n t||}|jddd||k7rW|jd||j|j|||j|||jn|jd||j!dy)aFormats 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. insertsel1.0endbreak)rr textget_selection_indicesgetget_comment_headerfind_paragraphindexreformat_commentreformat_paragraph tag_removemark_setundo_block_startdeleter undo_block_stopsee) r eventlimitr%firstlastdatacomment_headernewdatas rformat_paragraph_eventz&FormatParagraph.format_paragraph_event*s #(-U||  ll88: t T88E4(D/5N#4H)=> .E4 &tUNCG(u5G ue, d? MM(E *  ! ! # KKt $ KKw '  " MM(D ) rr ) __name__ __module__ __qualname____doc__r classmethodrrr:rrrrs, PP$rrcRtt|jd\}}|jd|zd|z}|j d|zddrJt |r?|dz}|jd|zd|z}|j d|zddr t |r?|}t |}t|}t ||k(rHt ||ds:|dz}|jd|zd|z}t ||k(rt ||ds:d|z}|dz }|jd|zd|z}|dkDr[t ||k(rMt ||ds?|dz }|jd|zd|z}|dkDrt ||k(rt ||ds?d|dzz} | |||j| |fS) zReturns 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>+,!ns*GsSZE!A#J#$5!C-) aC a% U1X.JJw~~ JJuQRy 99S>rc.tdjfd|jdD}t|tz d}t ||}|jd}d}|dsd}|dd}djfd|D|zS)z?Return data reformatted to specified width with comment header.rQc3(K|] }|d ywr r@).0rMlcs r z#reformat_comment..s<+;4T"#Y+;Nc3(K|] }|z ywr r@)rhrMr8s rrjz#reformat_comment..s=WT^D(Wrk)rIr[rFmaxr,)r7r4r8 format_widthr9 block_suffixris ` @rr+r+s ^ B 99<4::d+;< 22B7L |4GmmD!G L 2; #2, 99=W= = LLrc2tjd|duS)z/Return True if line is empty or all whitespace.z^\s*$N)rUmatchrMs rrHrHs 88Hd #4 //rcJtjd|jS)z/Return the initial space or tab indent of line.z ^([ \t]*)rUrtgrouprus rrTrTs 88L$ ' - - //rcVtjd|}|y|jdS)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]*#*)rmrDrw)rMms rr(r(s) &Ay 771:rz[ \t]*ctj|}|jt|j j |fS)zReturn 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_rertr#rIrxrW)rMtabwidthrzs rget_line_indentr~s; d#A 557C ,,X67 77rcXeZdZdZdZdZdZd dZd dZd dZ d d Z d d Z d d Z d Z y) FormatRegionzFormat selected text (region).c||_yr r r s rrzFormatRegion.__init__rrcX|jj}|jj\}}|r+|r)|j|dz}|j|dz}n"|jd}|jd}|j ||}|j 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 +1crQ)r r%r&r*r'rF)r r%r5r6headtailcharsr\s r get_regionzFormatRegion.get_regions||  ll88: t T::el23D::d%667D::01D::23Dt$ D!T5%''rcv|jj}dj|}||k(r|jy|j ddd|j d||j |j|||j|||j|jd|dy)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. rQNr!r"r#r ) r r%r[bellr-r.r/r0r r1tag_add)r rrrr\r%newcharss r set_regionzFormatRegion.set_regions||  99U# u  IIK  ue, h%  D$ D(#  UD(+rNcX|j\}}}}tt|D]j}||}|s t||jj \}} | |jj z} |jj| ||dz||<l|j||||y)z$Indent region by indentwidth spaces.Nr$) rrVrIr~r r} indentwidth _make_blanksr r r3rrrr\posrMraw effectives rindent_region_eventz FormatRegion.indent_region_events#'??#4 dE5U$C:D!0t||7L7L!MY% (@(@@ !\\66yADJNc % dE51rcl|j\}}}}tt|D]t}||}|s t||jj \}} t | |jjz d} |jj| ||dz||<v|j||||y)z$Dedent region by indentwidth spaces.rNr$) rrVrIr~r r}rprrrrs rdedent_region_eventz FormatRegion.dedent_region_events#'??#4 dE5U$C:D!0t||7L7L!MY DLL,D,D DaH !\\66yADJNc % dE51rc|j\}}}}tt|dz D]}||}d|z||<|j||||y)zrComment out each line in region. ## is appended to the beginning of each line to comment it out. rD##r$rrVrIrr r3rrrr\rrMs rcomment_region_eventz!FormatRegion.comment_region_event s] $(??#4 dE5Ua(C:DE#J) dE51rc|j\}}}}tt|D]*}||}|s |dddk(r|dd}n |dddk(r|dd}|||<,|j||||y)zUncomment 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. NrRrrD#r$rrs runcomment_region_eventz#FormatRegion.uncomment_region_event,s $(??#4 dE5U$C:DBQx4ABxbqSABxE#J% dE51rc"|j\}}}}|j}|ytt|D]<}||}|s t ||\} } t | |\} } d| zd| zz|| dz||<>|j ||||y)z@Convert leading spaces to tabs for each line in selected region.N rSr$)r _asktabwidthrVrIr~divmodr) r r3rrrr\r}rrMrrntabsnspacess rtabify_region_eventz FormatRegion.tabify_region_event?s#'??#4 dE5$$&   U$C:D!0x!@Y!' 8!<w!E\C'M9DJFc % dE51rc|j\}}}}|j}|ytt|D]}||j |||<|j ||||y)z.Expand tabs to spaces for each line in region.Nr$)rrrVrIrWr)r r3rrrr\r}rs runtabify_region_eventz"FormatRegion.untabify_region_eventNsn#'??#4 dE5$$&   U$Cs..x8E#J% dE51rcttdd|jj|jjddS)zReturn value for tab width.z Tab widthzColumns per tab? (2-16)rRparent initialvalueminvaluemaxvalue)rr r%rrs rrzFormatRegion._asktabwidthYs5  %<<$$11  rr )r;r<r=r>rrrrrrrrrrr@rrrrs7$(,,0   &  rrc"eZdZdZdZdZdZy)IndentszChange future indents.c||_yr r r s rrzIndents.__init__grrc|j}|j}tddd|zdzd|zdzdz|jr| |_d |_y ) Nz Toggle tabsz Turn tabs )onoffz? Indent width )zwill bez remains atz 8.z! Note: a tab is always 8 columns)rr$)r usetabsrr%r)r r3r rs rtoggle_tabs_eventzIndents.toggle_tabs_eventjst,,// ]733 !'0138933\\  ##*kGO#$G rc|j}tdd|j|jdd}|r"||jk7r|js||_y)Nz Indent widthz6New indent width (2-16) (Always use 8 when using tabs)rRrrr$)r rr%rr)r r3r ras rchange_indentwidth_eventz Indents.change_indentwidth_eventzsT,, K &22  3'---goo"%G rN)r;r<r=r>rrrr@rrrrds rrceZdZdZddZy)Rstripc||_yr r r s rrzRstrip.__init__rrNc|jj}|jj}|jt t |j d}td|D]^}|jd|zd|z}t|}t|j}||ksE|jd||fzd|z`|jddk(rxt|jdsb|j d d kDrN|jd dk(r:|jd |j d d kDr|jd dk(r:|jy) Nr#rDz%i.0z%i.endz%i.%izend-2crQinterpzend-1cr"zend-3c)r r%undor/rfloatr*rVr'rIrYr0hasattrr1) r r3r%rend_linecurtxtrcuts r do_rstripzRstrip.do_rstrips+||  ||   uTZZ./0H%C((6C<C8Cc(Ccjjl#CSy GsCj0(S.A& HHX $ &DLL(3::h'%/xx)T1 H%::h'%/xx)T1 rr )r;r<r=rrr@rrrrs rr__main__)mainzidlelib.idle_test.test_formatrRF) verbosityexit)r>rUtkinter.messageboxrtkinter.simpledialogridlelib.configrrrr)r,r+rHrTr(compiler|r~rrrr;unittestrr@rrrs '+#@@F">J"HM,0 0 "**Y'8MM`!!H@ z (AEBr