a Z^=c@sdZddlZddlmZddlmZddlmZGdddZe dd Z d d Z d d Z ddZ ddZddZedZddZGdddZGdddZGdddZedkrddlmZedd d!d"dS)#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)idleConfc@s6eZdZdZddZeddZddZd 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 ||_dSNeditwinselfrr &/usr/lib64/python3.9/idlelib/format.py__init__zFormatParagraph.__init__cCstjdddddd|_dS)NZ extensionsrz max-widthintiH)ZtypeZdefault)rZ GetOption max_width)Zclsr r r reload"szFormatParagraph.reloadcCs d|_dSrrr r r r close'rzFormatParagraph.closeNc Cs|dur|jn|}|jj}|j\}}|rF|rF|||}t|}nt||d\}}}}|rpt|||}n t ||}| ddd||kr| d|| | ||||||n | d||ddS)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. Ninsertsel1.0endbreak)rrtextget_selection_indicesgetget_comment_headerfind_paragraphindexreformat_commentreformat_paragraph tag_removemark_setundo_block_startdeleterundo_block_stopZsee) r eventlimitrfirstlastdatacomment_headernewdatar r r format_paragraph_event*s*           z&FormatParagraph.format_paragraph_eventr) __name__ __module__ __qualname____doc__r Z classmethodrrr-r r r r rs  rc Cs:tt|d\}}|d|d|}|d|ddr`t|r`|d}|d|d|}q(|}t|}t|}t||krt||ds|d}|d|d|}qtd|}|d}|d|d|}|dkrt||krt||ds|d}|d|d|}qd|d} | |||| |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.z%d.0z%d.endz        r cstdfdd|dD}t|td}t||}|d}d}|dshd}|dd}dfdd|D|S) z?Return data reformatted to specified width with comment header.r7c3s|]}|dVqdSrr Z.0r6)lcr r z#reformat_comment..iiNc3s|]}|VqdSrr rB)r+r r rDrE)r5r?r3maxr )r*r'r+Z format_widthr,Z block_suffixr )r+rCr rs   rcCstd|duS)z/Return True if line is empty or all whitespace.z^\s*$N)r;matchr6r r r r4sr4cCstd|S)z/Return the initial space or tab indent of line.z ^([ \t]*)r;rHgrouprIr r r r:sr:cCs"td|}|durdS|dS)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]*#*)NrFr2rJ)r6mr r r rs  rz[ \t]*cCs$t|}|t||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_rerHrr5rKr=)r6tabwidthrLr r r get_line_indents rOc@sleZdZdZddZddZddZdd d Zdd d Zdd dZ dddZ dddZ dddZ ddZ 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 +1cr7)rrrrrr3)r rr(r)headtailcharsr@r r r get_regions     zFormatRegion.get_regioncCsz|jj}d|}||kr&|dS|ddd|d||||||||| | d|ddS)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. r7Nrrrr) rrr?Zbellr!r"r#r$rr%Ztag_add)r rQrRrSr@rZnewcharsr r r set_regions     zFormatRegion.set_regionNc Cs||\}}}}tt|D]J}||}|rt||jj\}} | |jj} |j| ||d||<q|||||dS)z$Indent region by indentwidth spaces.Nr) rTr<r5rOrrN indentwidth _make_blanksrU r r&rQrRrSr@posr6raw 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) rTr<r5rOrrNrGrVrWrUrXr r r dedent_region_eventsz FormatRegion.dedent_region_eventcCsN|\}}}}tt|dD]}||}d|||<q |||||dS)zrComment out each line in region. ## is appended to the beginning of each line to comment it out. r2##rrTr<r5rUr r&rQrRrSr@rYr6r r r comment_region_event s z!FormatRegion.comment_region_eventcCs|\}}}}tt|D]T}||}|s.q|dddkrL|dd}n|dddkrh|dd}|||<q|||||dS)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. Nr8r^r2z#rr_r`r r r uncomment_region_event,s  z#FormatRegion.uncomment_region_eventc Cs|\}}}}|}|dur$dStt|D]L}||}|r0t||\} } t| |\} } d| d| || d||<q0|||||dS)z@Convert leading spaces to tabs for each line in selected region.Nz r9r)rT _asktabwidthr<r5rOZdivmodrU) r r&rQrRrSr@rNrYr6rZr[ZntabsZnspacesr r r tabify_region_event?s"z FormatRegion.tabify_region_eventcCs\|\}}}}|}|dur$dStt|D]}|||||<q0|||||dS)z.Expand tabs to spaces for each line in region.Nr)rTrcr<r5r=rU)r r&rQrRrSr@rNrYr r r untabify_region_eventNsz"FormatRegion.untabify_region_eventcCstdd|jj|jjdddS)zReturn value for tab width.z Tab widthzColumns per tab? (2-16)r8parentZ initialvalueZminvalueZmaxvalue)rrrrVrr r r rcYszFormatRegion._asktabwidthrrrrrr)r.r/r0r1r rTrUr\r]rarbrdrercr r r r rPs   rPc@s(eZdZdZddZddZddZdS) IndentszChange future indents.cCrrrr r r r r grzIndents.__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)rhir)rusetabsrrrV)r r&rrjr 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)r8rfrgr)rrrrVrj)r r&rrAr r r change_indentwidth_eventzsz Indents.change_indentwidth_eventN)r.r/r0r1r rkrlr r r r ridsric@seZdZddZdddZdS)RstripcCrrrr r r r r rzRstrip.__init__Nc Cs|jj}|jj}|tt|d}td|D]L}|d|d|}t |}t | }||kr4| d||fd|q4|ddkrt |jds|d d kr|d dkr| d q| dS) Nrr2z%i.0z%i.endz%i.%izend-2cr7Zinterpzend-1crzend-3c)rrundor#rZfloatrr<rr5r>r$Zhasattrr%) r r&rrnZend_lineZcurZtxtrZZcutr r r do_rstrips$    zRstrip.do_rstripr)r.r/r0r ror r r r rmsrmZ__main__)mainzidlelib.idle_test.test_formatr8F)Z verbosityZexit)r1r;Ztkinter.messageboxrZtkinter.simpledialogrZidlelib.configrrrrr rr4r:rZcompilerMrOrPrirmr.Zunittestrpr r r r Zs*   C%$  $