ZL iΟ TddlZddlZddlmZmZmZddlmZmZddl m Z m Z m Z m Z ddlmZmZmZddlmZmZddlmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&ddl'm(Z(dd l)m*Z*dd l+m,Z,m-Z-e d Z.e/Z0e/Z1e e,e-fZ2eGd d Z3eGddZ4eGddZ5eGddZ6dee.dee7e0e.ffdZ8 d+de3de3de9e,de:ddf dZ;ddde3ded ed#e4d$e/de:fd%Z?de3d&e,d$e/de:fd'Z@de3d(e,d$e/de:fd)ZAde3de II ! (yyU1153G3GHH (DJJ$$&'    99 #(A(A BC ;;| KK:"&";";D"AYY K   |}  % %d +yy--00604D-..t4**,""4( KK  t $)c|jjdk(s|jjrA|jr t d|j r|j tk(r t d|j||y)zLike :func:`append()` but disallow invalid standalone comment structure. Raises ValueError when any `leaf` is appended after a standalone comment or when a standalone comment is not the first leaf on the line. rz$cannot append to standalone commentsz5cannot append standalone comments to a populated liner5N) r0r+any_open_for_or_lambda is_comment ValueErrorr.r;rrI)rJr4r5s rL append_safezLine.append_safe^sr  & &! +##::< !GHH{{tyy,>> K D| 4rMcvt|jdk(xr |jdjtk(S)z"Is this line a standalone comment?r)lenr.r;rrJs rLrQzLine.is_commentrs14;;1$RQ)<)<@R)RRrMcpt|xr*|jdjtjk(S)zIs this line a decorator?r)r>r.r;r#ATrWs rL is_decoratorzLine.is_decoratorws*Dz=dkk!n11UXX==rMcLt|xrt|jdS)zIs this an import line?r)r>rr.rWs rLrzLine.is_import|s Dz7i A77rMcLt|xrt|jdS)zIs this a with_stmt line?r)r>rr.rWs rLrzLine.is_with_or_async_with_stmts!DzH8QHHrMct|xrH|jdjtjk(xr|jdj dk(S)z Is this line a class definition?rclass)r>r.r;r#NAMEr?rWs rLis_classz Line.is_classsJ J 0 A##uzz1 0 A$$/ rMc|jxrA|jddtdDcgc]}ttj dc}k(Scc}w)zEIs this line a class definition with a body consisting only of "..."?N.)r`r.ranger$r#DOTrJ_s rL is_stub_classzLine.is_stub_classsN}} RS!1*/(6 %&DC 6 "  6 s!Ac |jd} |jd}|jtjk(xr|j dk(xsS|jtj k(xr4|duxr.|jtjk(xr|j dk(S#t$rYywxYw#t$rd}YwxYw)zBIs this a function definition? (Also returns True for async defs.)rFrUNdef)r. IndexErrorr;r#r_r?ASYNC)rJ first_leaf second_leafs rLis_defz Line.is_defs QJ *.++a.K5::-K*2B2Be2K OOu{{ * +4' +  EJJ. +!!U*      K s"B$B3$ B0/B03 CCc |jxr]|jddttjdgt dDcgc]}ttj dc}zk(Scc}w)zHIs this line a function definition with a body consisting only of "..."?N:rcrd)rpr.r$r#rArerfrgs rL is_stub_defzLine.is_stub_defsf{{ t{{23/Dc4J3K*/(O %&DC O 4   O s!A,ct|xrt|jdk(xr|jxr|jdjt j k(xrf|jdjdk(xrH|jdjt jk(xr|jdjdk(S)zzIs this a class with no base classes but using parentheses? Those are unnecessary and should be removed. (rc)) r>rVr.r`r;r#LPARr?RPARrWs rLrBzLine.is_class_paren_emptys J ,DKK A% ,  , A##uzz1 , A$$+  ,  A##uzz1  ,  A$$+ rMc|r*|jdjtjk7ry|jdj}|j dry|j dryy)z#Is the line a triple quoted string?rF)z"""z'''T)zr'''zr"""zR'''zR""")r.r;r#STRINGr? startswith)rJr?s rL_is_triple_quoted_stringzLine._is_triple_quoted_strings\t{{1~**ell: A$$   N +   < =rMcLt|xrt|jdS)zIs the line a docstring?r)r>rr.rWs rLrzLine.is_docstrings Dz:l4;;q>::rMc|jDcgc]}|jc}jtjdkDScc}w)z Is the line a chained assignmentrU)r.r;countr#EQUALrJr4s rLis_chained_assignmentzLine.is_chained_assignments4'+kk2d 288EII2sAct|jdk(ry|jdjtjk(S)z/Does this line open a new level of indentation.rF)rVr.r;r#rArWs rL opens_blockzLine.opens_blocks5 t{{ q {{2##u{{22rMfirst_leaf_matchesrct|jdk7ry|jd}|jtk7s |jy|duxs||jS)zIs this line converted from fmt off/skip code? If first_leaf_matches is not None, it only returns True if the first leaf of converted code matches. rUFrN)rVr.r;rfmt_pass_converted_first_leaf)rJrr4s rLis_fmt_pass_convertedzLine.is_fmt_pass_convertedsd t{{ q {{1~ II+ +119!T) -?  . ..  rMcP|jD]}|jtk(syy)z)If so, needs to be split before emitting.TF)r.r;rrs rLcontains_standalone_commentsz!Line.contains_standalone_commentss*KK Dyy.. rMctj|jdD]N\}}|tjk7rt |}t |dk(r4|D]}|j|syPy)zFChck if we have an implicit multiline string with comments on the linec|jSN)r;)r4s rLzGLine.contains_implicit_multiline_string_with_comments..s diirMrUTF) itertoolsgroupbyr.r#r}listrVcomments_after)rJ leaf_typeleaf_group_iterator leaf_listr4s rL0contains_implicit_multiline_string_with_commentsz5Line.contains_implicit_multiline_string_with_commentss|.7.?.? KK// *I*ELL(01I9~"! &&t,  rMc0t} |jd}|jt||jt j k(s)|jt jk(r5|js)|jd}|jt|d}|jjD]E\}}|D];}t||jr |st||js||vryd}=Gy#t$rYywxYw)Nrr9Fr*T)setr.addidr;r#COMMAr{r?rlr/itemsrr*r)rJ ignored_ids last_leaf comment_seenleaf_idr/comments rL$contains_uncollapsable_type_commentsz)Line.contains_uncollapsable_type_commentsse   BI OOByM *~~,%**,Y__ !KKO 9 . !%!4!4!6 $ GX# $"7;#27K#;6##  $ $)  sBD DDc^|jsytd|jDd}tdt|jDd}||k(rX|jddD]F}|jj t |gD]}t ||jsyHy)NFc3TK|] }|jdk7s|j"ywrNlineno.0r4s rL z9Line.contains_unsplittable_type_ignore..=s S4$++QRBR4;;S((rc3TK|] }|jdk7s|j"ywrrrs rLrz9Line.contains_unsplittable_type_ignore..?s OTdkkQ>NT[[ Orr9rT)r.nextreversedr/getrrr*)rJ first_line last_linenoders rL!contains_unsplittable_type_ignorez&Line.contains_unsplittable_type_ignore,s{{S4;;SUVW  OXdkk%: OQR   " BC( $#}}00D2>$G-gDIIF#$ $ rMc:td|jDS)Nc32K|]}t|ywr)rrs rLrz2Line.contains_multiline_strings..NsE&t,E)anyr.rWs rLcontains_multiline_stringszLine.contains_multiline_stringsMsEEEErMclosingc|jtvr6|jr*|jdjtjk(sy|jtj k(ry|jtj k(r|j|jjtjk(r|jyt|j||jtjtj fr8|jJ|jjtjk(sJyy|jry|j"t|j||jsyy)zReturn True if we have a magic trailing comma, that is when: - there's a trailing comma here - it's not from single-element square bracket indexing - it's not a one-tuple rFT)brackets)r;rr.r#rRBRACERSQBparentr traileropening_bracketrLSQB prev_sibling subscriptlistr)rJrs rLrFzLine.has_magic_trailing_commaPs  LL, ,  B$$ 3 <<5<< ' <<5:: %*NN''4<<7++7+++KK#jj%**5 ++777++00D4F4FFFF >>  " " .7N  # #Wdkk8 rMrc|jtk(r"|jjrd|_y|jt j k7ry|jst|_d|_y|jd}|jt jk(r|js|jrtt|jjdkrQt||js:t|jdkrt|_d|_y|jd}|jj!t#|gj%|y) z0Add an inline or standalone comment to the line.Frrcrrwr9T)r;rr0any_open_bracketsrCr#COMMENTr.r{r?rrVrrr*r/ setdefaultrrI)rJrrs rLrHzLine.append_comment|s  LL. .$$668GN <<5== ({{-GLGNKKO NNejj (OO  D))002349#G$))< 4;;!#1 !# BI   I3::7CrMcL|jjt|gS)z;Generate comments that should appear directly after `leaf`.)r/rrrs rLrzLine.comments_afters}}  D2..rMc|jj}|jjt|g}|jj t|jdgj |y)z@Remove the trailing comma and moves the comments attached to it.rN)r.popr/rrextend)rJtrailing_commatrailing_comma_commentss rLrGzLine.remove_trailing_commas]*"&--"3"3B~4F"K   DKKO!4b9@@ # rMcN|jj}|y|j}t|trG|j t jk(ry|j t jk(r t||}|duxr td|jDS)zAReturn True iff `leaf` is part of a slice with non-trivial exprs.NFc3@K|]}|jtvywr)r;r)rns rLrz,Line.is_complex_subscript..s3 +,AFF& &3 s) r0 get_open_lsqb next_sibling isinstancer%r;r listmakerrrr pre_order)rJr4 open_lsqbsubscript_starts rLrDzLine.is_complex_subscripts((668  #00 ot ,##t~~5##t'9'99"/"Fd* s3 0?0I0I0K3 0  rM is_reversedc#Kttttgtt t tff|rtnt}||jD]u\}}t|jt|jz}d|jvry|j|D]}|t|jz }|||fwyw)zReturn an enumeration of leaves with their length. Stops prematurely on multiline strings and standalone comments.  N)r rrr$rtupleIndexenumerate_reversed enumerater.rVrCr?r)rJropindexr4lengthrs rLenumerate_with_lengthzLine.enumerate_with_lengths htn%xeTk0B'CC D"- 9 dkk? &KE4%DJJ7Ftzz!..t4 -#gmm,, -v% % &sCCct|j|j|j|j|j S)N)r*r+r1r2r3)r(r*r+r1r2r3rWs rLclonez Line.clones8** 00!22!%!:!:   rMc|syd|jz}t|j}t|}|j||j }|dj d|Dz }tjj|jj}|Dcgc] }t|}}|dj |z }|dzScc}w)zRender the line.rz rc32K|]}t|ywr)strrs rLrzLine.__str__..s4Ts4y4r) r+iterr.rrCr?joinrchain from_iterabler/valuesr)rJindentr.firstres comments_iterrr/s rL__str__z Line.__str__s$**$dkk"V vhu{{m4 rww4V444!55dmm6J6J6LM 0=>WCL>> rwwx  Tz?s%CcHt|jxs |jS)z/Return True if the line has leaves or comments.)r>r.r/rWs rL__bool__z Line.__bool__sDKK04==11rM)FFF)r7N)r7r();__name__ __module__ __qualname____doc__rr*r__annotations__r+intrr.r$dictr/LeafIDrr0r1r>r2r3rrIrSpropertyrQrZrrr`rirprtrBrrrrrrrrrrrrFrHrrGrDrrrrrrrrrMrLr(r('s:EE"D$"E3Nt4FDJ4).t)DHd64:%&D&+N&KO^K!OT!"d"+/(4./MR'%'%(,'%EI'% '%R55D5T5(SDSS>d>>8488IDII $   t     $ T    d     $  ;d;;JtJJ3T33IM %-hvt|.D%E  (d $ "d"H4BFDF***X#d#t#J/4/DJ/   $ (#(&& %tS() *&*  2$2rMr(cDeZdZUdZeed<eed<eed<eed<eed<y) RHSResultz2Intermediate split result from a right hand split.headbodytailrclosing_bracketN)rrrrr(rr$rrMrLrrs < J J JrMrceZdZUdZeed<eded<eed<dZe ed<e e Z e e ed<dZe ed <d Zeed <d e e fd Zy) LinesBlockzClass that holds information about a block of formatted lines. This is introduced so that the EmptyLineTracker can look behind the standalone comments and adjust their empty lines for class or def lines. r*previous_block original_linerbeforer, content_linesafterF form_feedr7ctt|j}t|j|j |}|g|j z||jzgzS)Nr)rr(r*rrrrr)rJ empty_linerCs rL all_lineszLinesBlock.all_linessO499-. #DKKLx$,,, TZZ0G/HHHrMN)rrrrrrrr(rrrrrrrrr>rrrMrLr r s` J\**FCO$T:M49:E3NItI49IrMr c eZdZUdZeed<dZeeed<dZ ee ed<e e Z e eed<dZee ed<d ed e fd Zd ed efd Zd ed eeeffd Zd ededed eeeffdZy)EmptyLineTrackera{Provides a stateful method that returns the number of potential extra empty lines needed before and after the currently processed line. Note: this tracker works on lines that haven't been split yet. It assumes the prefix of the first leaf consists of optional newlines. Those newlines are consumed by `maybe_empty_lines()` and included in the computation. r*N previous_linerr, previous_defssemantic_leading_comment current_liner7c|jdk(xr2t|jxrd|jdjv}|j |\}}|j r|j j nd}td||z }tj|jvr|j|rd}n|j r||j j ft|j jjdk(r:|j jjr|js|j sd}t#|j|j ||||}|j$rP|j&<|j&j(sC|j&j$r|r+|j*|r||_n|j(r|rd|_||_||_|S)zReturn the number of extra empty lines before and after the `current_line`. This is for separating `def`, `async def` and `class` with extra empty lines (two on module-level). rz rUN)r*rrrrr)r+r>r.rC_maybe_empty_linesrrmaxrfix_module_docstring_detectionr*_line_is_module_docstringrVrrr`rpr rQrrZr)rJrrrrprevious_afterblocks rLmaybe_empty_linesz"EmptyLineTracker.maybe_empty_lines$s   ! # 8\(() 8,--a0777  // = 6:6I6I,,22qQ/0  1 1TYY >--l;##''66>++99@@AQF''55BB%..,2E2E..&    " "!!)&&33++66&22:f05-**f,0D ))# rMc,|j}|syt|jjdk7s.|jjr|j s |j ry|jx}r&|jjsy|jx}r&y)NFrUT)rrVrr.rr`rprQ)rJrrs rLr!z*EmptyLineTracker._line_is_module_docstring[s,, ,,33 4 9!//<<$$"" . = ==n=!//::!/ = ==n=rMcd}|jdk(r|jjrdnd}|jr>|jd}|jj d}t ||}d|_nd}t|}|j}d}|jr_|jdj|k\rC|jj}|jr|jdj|k\rC|js |jr|jj||jy|jrD|jjry|jjr|jjry||jJ|jjrN|jr|j sd}n|r)|js|jjr|rdnd}ne|rd}n`d}n]|rd}nX|sT|jrH|jdj"t$j&k(r|jdj(d vrd}nd}|j*s|js |jr|j-|||S|jj.rQ|jjdk(r8|jdk(r)|j.st0j2|jvry |jj.rC|j.s7|j5t. s!||jjk(r|xsddfS|dfS) NrUrrwrrrrrrrU)withtryforwhileifmatch)rUrr)r+r*is_pyir.rCrminr>rrrpr`rIrrrrir;r#rAr?rZ#_maybe_empty_lines_for_class_or_defrralways_one_newline_after_importr)rJr max_allowedrnruser_had_newliner+ previous_defs rLrz#EmptyLineTracker._maybe_empty_linesks    "#yy//!QK   %,,Q/J&&,,T2F-F "J F<""   T%7%7%;%A%AU%J--113L  T%7%7%;%A%AU%J   ,"7"7    % %l 3    %  $ $!!**!!--$2D2D2K2K  #%%1 11yy((1K1KF<#6#64;M;M;T;T"2QFFFF$**$++B/44 C$++A.44MNFF  $ $ (;(;|?T?T;;f&6     ( (""((A-""a' **77499D    ( ( ** 66)6T++111Ka!# #qyrMrr4c|jJ|jjr$|jjr |jryy|jj |j krM|jj s|jjr!|jjry|rddfSddfSd}|jjr|jj |j k(rr|dk(rm|j}|^|jR|jjj s2|jjjs|jdkr|}ny|jjr |j s|jj rt|jj |j krd}n|jj |j kDrd}n|jr|jjrd}nd}n|jj |j kDrd}nw|js |jr2|jjs|j r td|}n0d}n-d}n*|j rdnd}|jjr|sd}|8|j}|*t!|j||j"z |_ d}|dfS)Nr(r'rUrrw)rrZr*r/rir+r`rprQrrrrrr0rtrr)rJrrr4comment_to_add_newlinesslcnewlinesrs rLr1z4EmptyLineTracker._maybe_empty_lines_for_class_or_defs!!---    * *yyL$>$>    # #l&8&8 8    ' '4+=+=+D+Dyy(12 2a2 28<    ) )""((L,>,>>! //C&&2**88AA**88DDJJ!O*-' 99  $$(:(:(C(C%%++l.@.@@ H''-- 0B0BB H!//D4F4F4T4T H H##))L,>,>>##|'@'@((//%% #1f~H !H(..qAH!!--6F " .4CCN)/66ANDXDXX(.{rM)rrrrrrrrr(rr rrrrr$r>r!rrrr1rrMrLrrs J$(M8D>(+/NHZ(/ %d ;M4:;59hz295d5z5ndt [t[c3h[zN N*-NAEN sCxNrMrsequencer7c#^Kt|dz }t|D] }||f|dz}yw)z;Like `reversed(enumerate(sequence))` if that were possible.rUN)rVr)r:relements rLrrs< MA EH%g  s+-new_lineold_liner.r5c|D]j}t|j|j}t|||j |||j |D]}|j |dly)a Append leaves (taken from @old_line) to @new_line, making sure to fix the underlying Node structure where appropriate. All of the leaves in @leaves are duplicated. The duplicates are then appended to @new_line and used to replace their originals in the underlying Node structure. Any comments attached to the old leaves are reattached to the new leaves. Pre-conditions: set(@leaves) is a subset of set(@old_line.leaves). rOTN)r$r;r?rrIr)r=r>r.r5old_leafnew_leaf comment_leafs rL append_leavesrC!sk= x~~6h)|<$33H= =L OOLtO < = =rMr)line_strliner*rDc|s t|}tj|vr1t||jkxrd|vxr|j S|j ryd|vrt||jkS|j d^}}}t||jkDst||jkDryg}d}g}tj} t|jD]C\} } | tjk(rd} | jdzt|kDr|jdn+| jdzt|kr|j} | 1|/|j| jdzk(r| j} | dkDry| j| kr| jt j"k(rcdg} | |z } |j$s| jdkDr@| t|jdz k7s| j&| vr|| jxxdz cc<| tjk7rt)| | j} t+| sl| j,rp| j,jt.j0k(sG| j,j,r3| j,j,jt.j2k(ryt|dkDry| }| }t5||vs |j||j,*|j,}t5||vr9Ft|dk(ryt7d|DS)aFor non-multiline strings, return True if `line` is no longer than `line_length`. For multiline strings, looks at the context around `line` to determine if it should be inlined or split up. Uses the provided `line_str` rendering, if any, otherwise computes a new one. rFNrUrTc3&K|] }|dk( ywrr)rvals rLrz'is_line_short_enough..s*Csax*s)line_to_stringrmultiline_string_handlingr" line_lengthrsplitmathinfrr. bracket_depthrVrIrr;r#rr1rr0rrr test dictsetmakerrall)rEr*rDrrhlastcommasmultiline_stringmultiline_string_contextsmax_level_to_updateir4 had_comma ignore_ctxsctxs rLis_line_short_enoughr\9s !$'((4 h 4#3#3 3 8H$ 85577  ((* 8"d&6&666nnT*OEAt$***io@P@P.PF'+*,-1XXT[[)/!4 $(( *'+I!!A%F 3 a ##a'#f+5"JJL %$0$22d6H6H16LL'+&8&8#q=   !4 4ekk9Q04fK 4 4K$$(:(:Q(>S%))T->->k-Qt))*a/* $(( *"%&94;M;M"N  t ${{   DII-KK&&4;;+=+=+B+BdFWFW+W,-1# Cc(h&)005::%jj c(h&W/!f $%* *6* **rMcb|j}t|dkry|djtjk(r|djtj k(rd}d}|d}|dddD]}|jt vr|jtvry|dz }n|jtj k(r|dz }nb|jtjk(r1|jtj k(s(|jt vsy|jtvry|dkDs|dkDsyy) zReturn False if the line cannot be split *for sure*. This is not an exhaustive search but a cheap heuristic that we can use to avoid some unfortunate formattings (mostly around wrapping unsplittable code in unnecessary parentheses). rwFrrUrr9NT) r.rVr;r#r}rfrrr_)rEr. call_count dot_countrr4s rL can_be_splitr`s[[F 6{Q ay~~%&)..EII*E  bz26r6N Dyy,,99$44 a eii'Q ejj( UYY.$))?O2O "221}a! $ rMrhsrKc|j}d}t|jD]i}|r||jurd}|jt k(r|sy|r/|jt vsB|j|jvs[|jsh|}k|j}|jsy|j}|j|}|dkDry|dk(r |tk(r|jjry|tk(ryt!|jdk\sJd|jd}|jd} |jt"vr!| jt vrt%|||ry|jd } |jd } | jt&j(k(sm| jt&j*k(sP| jt&j,k(ra| j.rU| j.jt0j2k7r.| jt"vryt5|ryt7|| | ryy) aDoes `rhs.body` have a shape safe to reformat without optional parens around it? Returns True for only a subset of potentially nice looking formattings but the point is to not return false positives that end up producing lines that are too long. NFTrUrwzStranded delimiterr)rrKr9r)rSrK)r rr.rr;rrr?r0 delimitersmax_delimiter_prioritydelimiter_count_with_priorityr rrr rVr_can_omit_opening_parenr#r{rrrr rr_can_omit_closing_paren) rarKrEr r4bt max_prioritydelimiter_countrsecond penultimaterSs rLcan_omit_invisible_parensrms 88D'+O% # t'F'FF"O 99* *? --$$ 3 "O #   B ==,,.L66|DO! > )chh.Q.Q  |# t{{ q 6"66  KKNE [[^F zz%%&++=M*M "4u+ N ++b/K ;;r?D UZZ 99 $ II #    DLL0   / / u % "4d L rMrc d}d|jz}d}|jD]L\}}}|jtvr|j|urd}|s,||z }||kDry|jt vsKd}Nt |j|dzk(ryy) See `can_omit_invisible_parens`.FrvrTrU)r+rr;rrrrVr.)rErrK remainderr_indexr4 leaf_lengths rLrfrfsI ^F F%)%?%?%A!k 99( (T-A-AU-JI  k !F # yy,,!  t{{ vz ) rMrScd|jz}d}|jD]8\}}}||z }||jur |s||ks"y|jtvs7d}:y)rorvFT)r+rrr;r)rErSrKrseen_other_bracketsrqr4rrs rLrgrg8sq ^F%)%?%?%A'!k+ 4'' '"f &; YY* *"& ' rMc6t|jdS)zmReturns the string representation of @line. WARNING: This is known to be computationally expensive. r)rr@)rEs rLrIrIIs t9??4  rMr)CrrMcollections.abcrrr dataclassesrrtypingrr r r black.bracketsr r r black.moderr black.nodesrrrrrrrrrrrrrrrr r! black.stringsr"blib2to3.pgen2r#blib2to3.pytreer$r%r&rrrLNr(rr rrrrr>rCrr\r`rmrfrgrIrrMrLrs 88(11GG$&$ & CL   4: H2H2 H2V   II I* AA AH!%q/1JNS=="=,0J=FJ= =202^+ ^+^+),^+ ^+B!t!!H^ ^^ ^B$$ST2$CD"!!#!rM