ZL iGfUddlZddlmZmZddlmZddlmZddlm Z m Z m Z ddl m Z mZddlmZmZmZmZmZmZmZmZmZmZddlmZdd lmZmZe eefZhd Z e e!d <d d hZ"e e!d<hdZ#e e!d<eejHejJhz Z&e e!d<dZ'dZ(dZ)eGddZ*dede deefdZ+edde,de-de de.e*fdZ/ded e0ddfd!Z1d"e,de de,fd#Z2d$ede d%ee3e0e0fddfd&Z4d'e*dede3e-e-e-ffd(Z5d'e*ded)e-d*e-de-f d+Z6ded'e*d,e0de de-f d-Z7d$ede d%ee3e0e0fde-fd.Z8d/e.ed'e*d,e0d*e-d%ee3e0e0fdeddfd0Z9ded'e*de deefd1Z:d2ede efd3Z;d4ed5ede-fd6Zd9ede de-fd:Z?d9ede de-fd;Z@de,d?eBe,de-fd@ZCy)AN) CollectionIterator) dataclass) lru_cache)FinalOptionalUnion)ModePreview) CLOSING_BRACKETSSTANDALONE_COMMENT STATEMENT WHITESPACE container_of first_leaf_ofis_type_comment_stringmake_simple_prefixpreceding_leafsyms)token)LeafNode> # fmt:off # fmt: off# yapf: disableFMT_OFFz # fmt: skipz # fmt:skipFMT_SKIP># fmt:on # fmt: on# yapf: enableFMT_ON_COMPOUND_STATEMENTSz !:#'# ;cNeZdZUdZeed<eed<eed<eed<eed<eed<y) ProtoCommentaDescribes a piece of syntax that is a comment. It's not a :class:`blib2to3.pytree.Leaf` so that: * it can be cached (`Leaf` objects should not be reused more than once as they store their lineno, column, prefix, and parent information); * `newlines` and `consumed` fields are kept separate from the `value`. This simplifies handling of special marker comments like ``# fmt: off/on``. typevaluenewlinesconsumed form_feedleading_whitespaceN)__name__ __module__ __qualname____doc__int__annotations__strboolT/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/black/comments.pyr&r&'s( I JMMOr6r&leafmodereturnc#8Kd}t|j|jtjk(|D]R}|j }t |j|j}t|j|j|Tt||yw)aClean the prefix of the `leaf` and generate comments from it, if any. Comments in lib2to3 are shoved into the whitespace prefix. This happens in `pgen2/driver.py:Driver.parse_tokens()`. This was a brilliant implementation move because it does away with modifying the grammar to include all the possible places in which comments can be placed. The sad consequence for us though is that comments don't "belong" anywhere. This is why this function generates simple parentless Leaf objects for comments. We simply don't know what the correct parent should be. No matter though, we can live without this. We really only need to differentiate between inline and standalone comments. The latter don't share the line with any code. Inline comments are emitted as regular token.COMMENT leaves. Standalone are emitted with a fake STANDALONE_COMMENT token identifier. r is_endmarkerr9)prefixN) list_commentsr>r'r ENDMARKERr*rr)r+rr(normalize_trailing_prefix)r8r9total_consumedpcr>s r7generate_commentsrD;s{&N $))u">T5#BKK>277BHHV44 5 dN3sBBi)maxsizer>r=c g}|rd|vr|Sd}d}d}d}ttjd|D]\}} |t| dzz }tjd| } | sJ| j \} } | s |dz }d| vrd}| j ds| jd r|dz }y||k(r|stj} nt} t| | }|jt| ||||| d}d}|S) zNReturn a list of :class:`ProtoComment` objects parsed from the given `prefix`.#rFz ? | z^(\s*)(\S.*|)$ T\r9)r'r(r)r*r+r,) enumerateresplitlenmatchgroups startswithendswithrCOMMENTr make_commentappendr&)r>r=r9resultr*nlines ignored_linesr+index full_linerP whitespaceline comment_typecomments r7r?r?Xs*"$F S& H FMI%bhhz6&BC!yC NQ&&*I6 u <<> D aKFy  s#}}T""  M !, ==L-Lt$/ !!##-    C!D Mr6rBc|j|d}d|vr:|jd}d|vxr|jd}t|||_yd|_y)zNormalize the prefix that's left over after generating comments. Note: don't use backslashes for formatting or you'll lose your voting rights. NrJ rI)r>countrSr)r8rB remaindernl_countr+s r7rArAs\  NO,I 9??4(I%B)*<*'>TJ #))T1   / "3=t < ' c1 5 ://#d*Z-=-=-??71:%77- =r6nodelinesc.d}|rt|||}|ryy)zJConvert content between `# fmt: off`/`# fmt: on` into standalone comments.TN)convert_one_fmt_off_pair)rsr9rt try_agains r7normalize_fmt_offrxsI ,T4?  r6r_ct|jt}t|jt}|s|sy|r|jt vryd||fS)zsCheck if comment should be processed for fmt handling. Returns (should_process, is_fmt_off, is_fmt_skip). )FFFT)rlr(rrr'r )r_r8 is_fmt_off is_fmt_skips r7_should_process_fmt_commentr|sM)@J)'--BK k"dii#33" [ ((r6rzr{c|jtk(ryt|}|sy|r&|jtvr|jtk7ry|r|jtvryy)zCheck if comment is a valid standalone fmt directive. We only want standalone comments. If there's no previous leaf or if the previous leaf is indentation, it's a standalone comment in disguise. TF)r'r rr)r_r8rzr{prevs r7 _is_valid_standalone_fmt_commentrsW||)) $ D diiz1diiCU6UtyyJ. r6previous_consumedc $t|jd|}d}d}t|D]:\}}||jtvr|}|||kDs%|jt vs8|}n||y||}||} |j} |j } | j } | | | } |jdz| z}|jdr|dd}| d|d|jzz}| j|jd}d|vr|jdd}||z }| | j d|_|j}|Jdd}t|jD] \}}||us |}n|Jd|j|tt||d y ) zvHandle fmt:off/on blocks that contain only comments. Returns True if a block was converted, False otherwise. Fr<Nrarz2INTERNAL ERROR: fmt: on/off handling (prefix only)z1INTERNAL ERROR: fmt: on/off handling (leaf index)r>fmt_pass_converted_first_leafT)r?r>rLr(rr!r*rSr)rNparentchildren insert_childrr )r8r_rr9 all_comments fmt_off_idx fmt_on_idxidxcfmt_on_commentoriginal_prefix start_posend_poscontent_between_and_fmt_on hidden_valuestandalone_comment_prefixfmt_off_prefixrleaf_idxchilds r7_handle_comment_only_fmt_blockrs!5tLLKJL)Q  177g#5K  "s['8QWW=NJ  [0;'G!*-NkkO  I%%G!07!C==4'*DDLT"#CR(  **+dW5E5E.EE%**7==9!r|r*rlistgenerate_ignored_nodesr_handle_regular_fmt_block) rsr9rtr8rr_should_processrzr{ ignored_nodess r7rvrvAs ($T[[u4P& G6Q7 3NJ "$+$4$4!3z;%,$4$4! !7gt!LMM!Z1'#4d ! &!  M& (T r6rc V|d}|j}|j}|jtvr||jd|_|r d|_|} n|d|d|j zz} dj d|D} |j|j z |jtvrqd} t|dkDrJtfd|Ds6|j|jd} d| vr| jdd} | | z } |jdz| z} |r| |j|jzz } | jdr| dd} d} |D]} | j}| |} |Jd| Jd |j| tt | | t#| y) z(Handle fmt blocks with actual AST nodes.rNrbrac32K|]}t|ywN)r3).0ns r7 z,_handle_regular_fmt_block..s9a3q69sc3JK|]}|dcxkxr|dkncyw)rrHNr5)rr]comment_linenos r7rz,_handle_regular_fmt_block..s+& 59DG~ 0a 0 0& s #rz(INTERNAL ERROR: fmt: on/off handling (1)z(INTERNAL ERROR: fmt: on/off handling (2)r)rr>r(rr*r)joinlinenorOanyrNr,rSremoverrr r)rr_rr{rtr8firstrr>rrr first_idxignoredrZrs @r7rrus ! E \\F \\F}}g..01  $*!$*+=,=$>HXHXAX$X!779=99L[[7#3#33N}} u:>#& =B& # $\\'--8;N~%!/!5!5d!;B!?!^3!}}t+l: 22W]]BB T"$CR( #I    I  III   L"LL    ,*7*>  r6c#6Kt|jtrt|||Ed{yt |}|T|j t jk7r5t||ryt||rt|jD]\}}t|tr%t||r|j tvr|y|j t jk(r:|t!|jdz krt|j|dz|ryt||ry|n:|j t j"k(r |j$y||j$}| |j t jk7r3yyyy7iw)zStarting from the container of `leaf`, generate all leaves until `# fmt: on`. If comment is skip, returns leaf only. Stops at the end of the block. NrKrH)rlr(r%_generate_ignored_nodes_from_fmt_skiprr'rr@ is_fmt_onchildren_contains_fmt_onrLr isinstancerr INDENTrODEDENT next_sibling)r8r_r9 containerrZrs r7rrs_w}}h78wMMM*40I  INNeoo$E YT *  $ID 9 ))*<*< =  ueT*yT/Jzz%55 $ JJ%,,.I$6$6 7! ;;0!**5195D +E= + .~~-)2H2H2PO!..IE  INNeoo$E $E  Ns+FFE"FFrc|jtjk7ryt|jt sy|jjtj k(rVt|jjt r2|jjjtvr |jS|jjtvr|Sy)aReturn the body node of a compound statement if we should respect fmt: skip. This handles one-line compound statements like: if condition: body # fmt: skip When Black expands such statements, they temporarily look like: if condition: body # fmt: skip In both cases, we want to return the body node (either the simple_stmt directly or the suite containing it). N)r'r simple_stmtrrrsuiter")rs r7 _find_compound_statement_contextrs{{d&&& fmmT *  djj( v}}++T 2 MM % %)= =}} }}11 r6 body_nodesimple_stmt_parentcV|jD]}|jtjk(s!|jtj k(rO|j Dcgc]!}|jtjk(r|#}}t|dk(xr|d|ucS||ucSycc}w)zCheck if a compound statement should be kept on one line. Returns True only for compound statements with semicolon-separated bodies, like: if True: print("a"); print("b") # fmt: skip rHrF) rr'rSEMIrrrrrO)rrr8r simple_stmtss r7&_should_keep_compound_statement_inliners  " 7 99 "~~+"+!3!3 zzT%5%55    <(A-W,q/EW2WW!$666 7  s&B&cvt||sgS|j}||jtvrgSg}|jD]w}||ur|St |t r?|jtjtjfvsG|j|Y|j|jy|S)zJGet header nodes for a compound statement that should be preserved inline.) rrr'r"rrrrNEWLINErrVextendr)rr compound_stmt header_leavesrs r7_get_compound_statement_headerr)s 2)=O P $$M 2 2:N N !M''1 I    eT "zz%--!>>$$U+   01 r6c#K|j}|j}g}t|jd|}|r|j|djk7ry||j|j d|_t j|vra|g}d|jvrE|j9|j}|jd|d|jvr |j9|Ed{y|}|g}|j|j |j}d|jvr|jt|jj} | r| dn|}|jtjtjfvrd|_nQ|jd||j|j |j}d|jvr |jt j|vr0t|t r t#|} | t%| |} | r| |z}|Ed{y||jt&j(k(r|jtjk(rd|_|j} | [| jt&j(k7r>|jd| | j} | | jt&j(k7r>|j} | O| jC| jjtj*k(r|jd| jt-|Ed{yyyy7h7*7w)zLGenerate all leaves that should be ignored by the `# fmt: skip` from `leaf`.Fr<rNrarrb) prev_siblingrr?r>r(r*r fix_fmt_skip_in_one_linersinsertrrr'rrrrrrrrrASYNCiter)r8r_r9rrrcommentssiblings current_node leaf_nodesr header_nodesparent_sibling grandparents r7rrBs $$L [[F MT[[u4HH w}} (9(99kk'"2"2"45  - -T 9$~HL/// --9+88 <0 L/// --9   *$ %  $ $ ,1D1D1P'..L,---,2K2K2Wl77>>@AJ-7:b>\L  U]]ELL$AA&( #  L 1((0\5H5H5T+22 ,---,2K2K2W  - - 5*VT:R8@I$=iP $0=$@M   v{{djj8TYY%--=W  ,,(^-@-@DJJ-N  N 3+88N(^-@-@DJJ-N mm  #((4((--<  K$<$< = &&&'>X8a \ !* 'sLCMM"M #C8MAM#M $B1MA+MM M MMrcd}t|jd|D],}|jtvrd}|jtvs+d}.|S)zDetermine whether formatting is switched on within a container. Determined by whether the last `# fmt:` comment is `on` or `off`. Fr<T)r?r>r(r!r)rr9fmt_onr_s r7rrsPF !1!1DQ ==F "F ]]g %F  Mr6c`|jD]}t|}|t||syy)z2Determine if children have formatting switched on.rKTF)rrr)rr9rr8s r7rrs8##U#   $T : r6 comment_listcL|D]}|jjdsyy)z Returns: True iff one of the comments in @comment_list is a pragma used by one of the more common static analysis tools for python (e.g. mypy, flake8, pylint). )z# type:z# noqaz # pylint:TF)r(rR)rr_s r7contains_pragma_commentrs.  == # #$F G r6 comment_line directivescR|g|jtddDcgc]}t|jzc}|jtjtDcgc]}t|jzc}}t fd|DScc}wcc}w)a Checks if the given comment contains format directives, alone or paired with other comments. Defaults to checking all directives (skip, off, on, yapf), but can be narrowed to specific ones. Matching styles: # foobar <-- single comment # foobar # foobar # foobar <-- multiple comments # foobar; foobar <-- list of comments (; separated) rHNc3&K|]}|v ywrr5)rr_rs r7rz*_contains_fmt_directive..sLw*$Ls)rN_COMMENT_PREFIXro_COMMENT_LIST_SEPARATORr)rrr_semantic_comment_blockss ` r7rlrls  (--o>qrB  gmmo -  (--o>DD'  gmmo -  L4KL LL  s B)B$)DrMcollections.abcrr dataclassesr functoolsrtypingrrr black.moder r black.nodesr r rrrrrrrrblib2to3.pgen2rblib2to3.pytreerrLNrr2rr! except_clause case_blockr"rprrr&rDr3r4rr?r1rArUtuplerxr|rrrvrrrrrrrrrsetrlr5r6r7rs 0!))$   !& 4:?? ,/%/;;(4+=+=t*OOeO  &4B4d4x~4: 4,#,,D,T,EW,,^ B   &#&T&c&R@ @@#-eCHo#>@ @) )!%) 4t )( !%37FJ 0I I II  I  IX1 11#-eCHo#>1 1h=8= == = eCHo & =  = =@./ ./%./-1./ b\./b&T&htn&R)- 4)- "X2Z' Z'%Z'-1Z' b\Z'z  4 D $4 $t*  /6.>.IMM#&s8M Mr6