
    !.d                        d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZmZmZ d dlmZmZ d dlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d d	l+m,Z,m-Z-  ed
          Z.e/Z0e/Z1ee,e-f         Z2e G d d                      Z3e G d d                      Z4e G d d                      Z5e G d d                      Z6dee.         de	ee0e.f                  fdZ7	 d+de3de3de
e,         de8ddf
dZ9ddde3ded e:de8fd!Z;de3de8fd"Z<d#e4d$e/de8fd%Z=de3d&e,d$e/de8fd'Z>de3d(e,d$e/de8fd)Z?de3de:fd*Z@dS ),    N)	dataclassfield)
CallableDictIteratorListOptionalSequenceTupleTypeVarUnioncast)COMMA_PRIORITYDOT_PRIORITYBracketTracker)ModePreview)BRACKETSCLOSING_BRACKETSOPENING_BRACKETSSTANDALONE_COMMENTTEST_DESCENDANTSchild_towards	is_importis_multiline_stringis_one_sequence_betweenis_type_commentis_with_or_async_with_stmtreplace_childsyms
whitespace)	str_width)token)LeafNodeTc            	       ^   e Zd ZU dZeed<   dZeed<    ee	          Z
ee         ed<    ee          Zeeee         f         ed<    ee          Zeed<   d	Zeed
<   d	Zeed<   dZee         ed<   	 d3dedededdfdZd4dededdfdZedefd            Zedefd            Zedefd            Zedefd            Zedefd            Zedefd            Z edefd            Z!edefd            Z"edefd            Z#edefd            Z$dddee%egef                  defd Z&e'j(        fd!edefd"Z)defd#Z*defd$Z+defd%Z,	 d4d&ed'edefd(Z-d)edefd*Z.dedee         fd+Z/d5d,Z0dedefd-Z1	 d4d.ede2e3e4eef                  fd/Z5d6d0Z6de7fd1Z8defd2Z9dS )7Linez;Holds leaves and comments. Can be printed with `str(line)`.moder   depthdefault_factoryleavescommentsbracket_trackerFinside_bracketsshould_split_rhsNmagic_trailing_commaleafpreformattedtrack_bracketreturnc                    |j         t          v p%t          |j                                                  }|sdS t
          j        |j         k    r| j        r
| j        dd= | j        r4|s2|xj	        t          ||                     |                    z  c_	        | j        s|r|rn| j                            |           | j        j        r|                     |          r|| _        n+|                     |d          r|                                  |                     |          s| j                            |           dS dS )ag  Add a new `leaf` to the end of the line.

        Unless `preformatted` is True, the `leaf` will receive a new consistent
        whitespace prefix and metadata applied by :class:`BracketTracker`.
        Trailing commas are maybe removed, unpacked for loop variables are
        demoted from being delimiters.

        Inline comments are put aside.
        N)complex_subscriptT)ensure_removable)typer   boolvaluestripr#   COLONis_class_paren_emptyr-   prefixr!   is_complex_subscriptr0   r/   markr)   r2   has_magic_trailing_commaremove_trailing_commaappend_commentappend)selfr3   r4   r5   	has_values        +lib/python3.11/site-packages/black/lines.pyrG   zLine.append=   sk    I)ET$*2B2B2D2D-E-E	 	F;$)##(A#BCC ; 	| 	 KK:(A(A$(G(G   KK  	-| 	-} 	- %%d+++y- -0066 504D-..td.KK -**,,,""4(( 	%Kt$$$$$	% 	%    c                     | j         j        dk    r<| j        rt          d          | j        r|j        t          k    rt          d          |                     ||           dS )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.
        r   z$cannot append to standalone commentsz5cannot append standalone comments to a populated liner4   N)r/   r*   
is_comment
ValueErrorr-   r;   r   rG   )rH   r3   r4   s      rJ   append_safezLine.append_safe_   sy     %** I !GHHH{ ty,>>> K   	D|44444rK   c                 h    t          | j                  dk    o| j        d         j        t          k    S )z"Is this line a standalone comment?   r   )lenr-   r;   r   rH   s    rJ   rN   zLine.is_commentp   s-     4;1$RQ)<@R)RRrK   c                 `    t          |           o| j        d         j        t          j        k    S )zIs this line a decorator?r   )r<   r-   r;   r#   ATrT   s    rJ   is_decoratorzLine.is_decoratoru   s%     Dzz=dk!n1UX==rK   c                 T    t          |           ot          | j        d                   S )zIs this an import line?r   )r<   r   r-   rT   s    rJ   r   zLine.is_importz   s#     Dzz7iA777rK   c                 T    t          |           ot          | j        d                   S )zIs this a with_stmt line?r   )r<   r   r-   rT   s    rJ   r   zLine.is_with_or_async_with_stmt   s$     DzzH8QHHHrK   c                     t          |           o5| j        d         j        t          j        k    o| j        d         j        dk    S )z Is this line a class definition?r   class)r<   r-   r;   r#   NAMEr=   rT   s    rJ   is_classzLine.is_class   s@     JJ 0A#uz10A$/	
rK   c                 d    | j         o)| j        dd         d t          d          D             k    S )zEIs this line a class definition with a body consisting only of "..."?Nc                 B    g | ]}t          t          j        d           S ).)r$   r#   DOT).0_s     rJ   
<listcomp>z&Line.is_stub_class.<locals>.<listcomp>   s1     6
 6
 6
%&DC  6
 6
 6
rK      )r]   r-   rangerT   s    rJ   is_stub_classzLine.is_stub_class   sG     } 
RSS!1 6
 6
*/((6
 6
 6
 "
 	
rK   c                 6   	 | j         d         }n# t          $ r Y dS w xY w	 | j         d         }n# t          $ r d}Y nw xY w|j        t          j        k    r|j        dk    p8|j        t          j        k    o#|duo|j        t          j        k    o
|j        dk    S )zBIs this a function definition? (Also returns True for async defs.)r   FrR   Ndef)r-   
IndexErrorr;   r#   r\   r=   ASYNC)rH   
first_leafsecond_leafs      rJ   is_defzLine.is_def   s    	QJJ 	 	 	55		*.+a.KK 	 	 	KKK	5:-K*2Be2K 
Ou{* +4'+ EJ.+ !U*		
s    
0 ??c                 6   t          |           ot          | j                  dk    or| j        ok| j        d         j        t
          j        k    oK| j        d         j        dk    o5| j        d         j        t
          j        k    o| j        d         j        dk    S )zzIs this a class with no base classes but using parentheses?

        Those are unnecessary and should be removed.
              (rf   ))	r<   rS   r-   r]   r;   r#   LPARr=   RPARrT   s    rJ   r@   zLine.is_class_paren_empty   s     JJ ,DK  A%,, A#uz1, A$+	,
 A#uz1, A$+	
rK   c                     t          |           oD| j        d         j        t          j        k    o$| j        d         j                            d          S )z#Is the line a triple quoted string?r   )z"""z''')r<   r-   r;   r#   STRINGr=   
startswithrT   s    rJ   is_triple_quoted_stringzLine.is_triple_quoted_string   sK     JJ @A#u|3@A$//??	
rK   c                 v    t          | j                  dk    rdS | j        d         j        t          j        k    S )z/Does this line open a new level of indentation.r   F)rS   r-   r;   r#   r?   rT   s    rJ   opens_blockzLine.opens_block   s4     t{q  5{2#u{22rK   first_leaf_matchesr   c                    t          | j                  dk    rdS | j        d         }|j        t          k    s|j        dS |du p ||j                  S )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.
        rR   Fr   N)rS   r-   r;   r   fmt_pass_converted_first_leaf)rH   r   r3   s      rJ   is_fmt_pass_convertedzLine.is_fmt_pass_converted   sm     t{q  5{1~I+++195!T) 
-?-?..
 .
 	
rK   depth_limitc                 V    | j         D ] }|j        t          k    r|j        |k    r dS !dS )z)If so, needs to be split before emitting.TF)r-   r;   r   bracket_depth)rH   r   r3   s      rJ   contains_standalone_commentsz!Line.contains_standalone_comments   s;    K 	 	Dy...43E3T3TtturK   c                    t                      }	 | j        d         }|                    t          |                     |j        t
          j        k    s|j        t
          j        k    r6|j        s/| j        d         }|                    t          |                     n# t          $ r Y dS w xY wd}| j
                                        D ]5\  }}|D ]-}t          |          r|st          |d          s||vr  dS d}.6dS )Nr|   r8   F ignoreT)setr-   addidr;   r#   COMMArv   r=   rk   r.   itemsr   )rH   ignored_ids	last_leafcomment_seenleaf_idr.   comments          rJ   $contains_uncollapsable_type_commentsz)Line.contains_uncollapsable_type_comments   s)   ee	BIOOByMM***~,,%*,,Y_, !KO	9... 	 	 	55	 !%!4!4!6!6 		$ 		$GX# $ $"7++ $# $+GY??$#;66#ttt#$ us   BB   
B.-B.c                 \   | j         sdS t          d | j         D             d          }t          d t          | j                   D             d          }||k    rQ| j         dd          D ]A}| j                            t          |          g           D ]}t          |d          r  dS BdS )NFc              3   :   K   | ]}|j         d k    |j         V  dS r   Nlinenorc   r3   s     rJ   	<genexpr>z9Line.contains_unsplittable_type_ignore.<locals>.<genexpr>  s1      SS4$+QRBRBR4;BRBRBRBRSSrK   r   c              3   :   K   | ]}|j         d k    |j         V  dS r   r   r   s     rJ   r   z9Line.contains_unsplittable_type_ignore.<locals>.<genexpr>  s0      OOTdkQ>N>NT[>N>N>N>NOOrK   r8   r   T)r-   nextreversedr.   getr   r   )rH   
first_line	last_linenoder   s        rJ   !contains_unsplittable_type_ignorez&Line.contains_unsplittable_type_ignore  s    { 	5 SS4;SSSUVWW
OOXdk%:%:OOOQR
 
	 "" BCC( $ $#}00D2>> $ $G&w	:: $#ttt$$ urK   c                 >    t          d | j        D                       S )Nc              3   4   K   | ]}t          |          V  d S N)r   r   s     rJ   r   z2Line.contains_multiline_strings.<locals>.<genexpr>)  s+      EE&t,,EEEEEErK   )anyr-   rT   s    rJ   contains_multiline_stringszLine.contains_multiline_strings(  s!    EEEEEEEErK   closingr:   c                    |j         t          v r'| j        r | j        d         j         t          j        k    sdS |j         t          j        k    rdS |j         t          j        k    r|j        rV|j        j         t          j	        k    r<|j
        r5t          |j
        || j        t          j        t          j        f          rdS |sdS | j        d         }|j        dS |j        j         t          j        k    p<|j
        du p3t          |j
        || j        t          j        t          j        f           S | j        rdS |j
        t          |j
        || j                  sdS dS )aT  Return True if we have a magic trailing comma, that is when:
        - there's a trailing comma here
        - it's not a one-tuple
        - it's not a single-element subscript
        Additionally, if ensure_removable:
        - it's not from square bracket indexing
        (specifically, single-element square bracket indexing)
        r|   FT)bracketsN)r;   r   r-   r#   r   RBRACERSQBparentr    traileropening_bracketr   LSQBsubscriptlistr   )rH   r   r:   commas       rJ   rD   zLine.has_magic_trailing_comma+  s{    L,,, -B$335<5<''4<5:%%N'4<77+ 8++K#j%*5	   8 u# tKOE|#u!T%77 *d2.+K#j%*5	   	 > 	4".7N#Wdk8
 8
. 4urK   r   c                    |j         t          k    r"| j                                        r	d|_        dS |j         t
          j        k    rdS | j        st          |_         d|_        dS | j        d         }|j         t
          j        k    r|j	        s|j
        rt          t          |j
                                                            dk    rIt          |          s:t          | j                  dk     rt          |_         d|_        dS | j        d         }| j                            t!          |          g                               |           dS )z0Add an inline or standalone comment to the line. Fr|   rf   rr   r8   T)r;   r   r/   any_open_bracketsrA   r#   COMMENTr-   rv   r=   r   rS   listr   r.   
setdefaultr   rG   )rH   r   r   s      rJ   rF   zLine.append_commenti  s9    L...$6688 /  GN5<5=((5{ 	-GLGN5KO	Nej((O )  ) D)0022334499#G,, : 4;!##1!#uBI  I33::7CCCtrK   c                 R    | j                             t          |          g           S )z;Generate comments that should appear directly after `leaf`.)r.   r   r   )rH   r3   s     rJ   comments_afterzLine.comments_after  s     }  D2...rK   c                    | j                                         }| j                            t          |          g           }| j                            t          | j         d                   g                               |           dS )z@Remove the trailing comma and moves the comments attached to it.r|   N)r-   popr.   r   r   extend)rH   trailing_commatrailing_comma_commentss      rJ   rE   zLine.remove_trailing_comma  sx    **"&-"3"3B~4F4F"K"K  DKO!4!4b99@@#	
 	
 	
 	
 	
rK   c                 J   | j                                         }|dS |j        }t          |t                    r<|j        t          j        k    rdS |j        t          j        k    rt          ||          }|duo*t          d |                                D                       S )zAReturn True iff `leaf` is part of a slice with non-trivial exprs.NFc              3   2   K   | ]}|j         t          v V  d S r   )r;   r   )rc   ns     rJ   r   z,Line.is_complex_subscript.<locals>.<genexpr>  s;       3
 3
+,AF&&3
 3
 3
 3
 3
 3
rK   )r/   get_open_lsqbnext_sibling
isinstancer%   r;   r    	listmakerr   r   r   	pre_order)rH   r3   	open_lsqbsubscript_starts       rJ   rB   zLine.is_complex_subscript  s    (6688	5#0ot,, 	G#t~55u#t'999"/"F"Fd* 
s 3
 3
0?0I0I0K0K3
 3
 3
 0
 0
 	
rK   r   c              #     K   t          t          t          t                   gt          t
          t          t          f                  f         |rt          nt                    } || j	                  D ]p\  }}t          |j                  t          |j                  z   }d|j        v r dS |                     |          D ]}|t          |j                  z  }|||fV  qdS )zReturn an enumeration of leaves with their length.

        Stops prematurely on multiline strings and standalone comments.
        
N)r   r   r
   r$   r   r   Indexenumerate_reversed	enumerater-   rS   rA   r=   r   )rH   r   opindexr3   lengthr   s          rJ   enumerate_with_lengthzLine.enumerate_with_length  s       htn%xeTk0B'CCD"*9	
 
 2dk?? 	& 	&KE4%%DJ7Ftz!!..t44 - -#gm,,,v%%%%%	& 	&rK   c                 \    t          | j        | j        | j        | j        | j                  S )N)r)   r*   r0   r1   r2   )r(   r)   r*   r0   r1   r2   rT   s    rJ   clonez
Line.clone  s5    * 0!2!%!:
 
 
 	
rK   c                 V   | sdS d| j         z  }t          | j                  }t          |          }|j         | |j         }|D ]}|t          |          z  }t          j        	                    | j
                                                  D ]}|t          |          z  }|dz   S )zRender the line.r   z    )r*   iterr-   r   rA   r=   str	itertoolschainfrom_iterabler.   values)rH   indentr-   firstresr3   r   s          rJ   __str__zLine.__str__  s     	4$*$dk""V4v4u{44 	 	D3t99CC 44T]5I5I5K5KLL 	  	 G3w<<CCTzrK   c                 8    t          | j        p| j                  S )z/Return True if the line has leaves or comments.)r<   r-   r.   rT   s    rJ   __bool__zLine.__bool__  s    DK04=111rK   )FFF)r6   N)r6   r(   ):__name__
__module____qualname____doc__r   __annotations__r*   intr   r   r-   r   r$   dictr.   r   LeafIDr   r/   r0   r<   r1   r2   r	   rG   rP   propertyrN   rW   r   r   r]   rh   ro   r@   rz   r}   r   r   sysmaxsizer   r   r   r   rD   rF   r   rE   rB   r   r   r   r   r   r   r   r    rK   rJ   r(   r(   /   s        EE
JJJE3NNNt444FDJ444).t)D)D)DHd64:%&DDD&+eN&K&K&KO^KKK!OT!!!"d"""+/(4./// MR %  % %(, %EI %	 %  %  %  %D5 5 5D 5T 5 5 5 5" SD S S S XS >d > > > X> 84 8 8 8 X8 ID I I I XI 
$ 
 
 
 X
 
t 
 
 
 X
 
 
 
 
 X
$ 
d 
 
 
 X
 
 
 
 
 X
 3T 3 3 3 X3 IM
 
 
%-hvt|.D%E
	
 
 
 
( ?Bk   d    "d " " " "H4    BFD F F F F 7<< <</3<	< < < <|#d #t # # # #J/4 /DJ / / / /
 
 
 

 
$ 
 
 
 
&  %& &&	%tS()	*& & & &*
 
 
 
     2$ 2 2 2 2 2 2rK   r(   c                   F    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   dS )	RHSResultz2Intermediate split result from a right hand split.headbodytailr   closing_bracketN)r   r   r   r   r(   r   r$   r   rK   rJ   r   r     sK         <<
JJJ
JJJ
JJJrK   r   c                       e Zd ZU dZeed<   ed          ed<   eed<   dZe	ed<    e
e          Zee         ed<   dZe	ed	<   d
ee         fdZdS )
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_liner   beforer+   content_linesafterr6   c                     t          t          | j                            }|| j        z  g| j        z   || j        z  gz   S )N)r)   )r   r(   r)   r   r   r   )rH   
empty_lines     rJ   	all_lineszLinesBlock.all_lines  sE    49---..
$+%&);;zDJ?V>WW	
rK   N)r   r   r   r   r   r   r	   r(   r   r   r   r   r   r   r   r   r   r   rK   rJ   r   r     s           JJJ\****FCOOO$uT:::M49:::E3NNN
49 
 
 
 
 
 
rK   r   c                       e Zd ZU dZeed<   dZee         ed<   dZ	ee
         ed<    ee          Zee         ed<   dZee
         ed<   d	ed
e
fdZd	ed
eeef         fdZd	eded
eeef         fdZdS )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_liner   r+   previous_defssemantic_leading_commentcurrent_liner6   c                 f   |                      |          \  }}| j        r| j        j        nd}| j        dn||z
  }t	          | j        | j        |||          }|j        r2| j        #| j        j        s| j        j        r|r| j        |r|| _        n|j        r|rd| _        || _        || _        |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).
        r   N)r)   r   r   r   r   )	_maybe_empty_linesr   r   r  r   r)   rN   rW   r  )rH   r  r   r   previous_afterblocks         rJ   maybe_empty_linesz"EmptyLineTracker.maybe_empty_lines  s    //==6:6IP,22q
 !) A.( 	 .&
 
 
 " 
	1!)&3 * +6 * ;A * 2:f:05-* 	1f 	1,0D))#rK   c                 ~   d}|j         dk    r| j        j        rdnd}|j        r?|j        d         }|j                            d          }t          ||          }d|_        nd}|j         }| j        r| j        d         j         |k    r| j        j        rq| j        J |r$|j	        s| j        j	        rt          d|          }nt          j        | j        v r'| j        d         j        r| j        d         j        sd}nZ|rd}nUd}nR|rd}nM|sI| j        d         j         r7|j        d         j        t          j        k    r|j        d         j        dvrd}nd}| j                                         | j        r| j        d         j         |k    |j        s|j	        s|j        r|                     ||          S | j        rD| j        j        r8|j        s1|                    t*                    s|| j        j         k    r|pddfS | j        r| j        j        r|j        r|dfS | j        r| j        j        rd	S |dfS )
NrR   r   rr   r   r   r|   )withtryforwhileifmatchr~   r   r   )r*   r)   is_pyir-   rA   countminr  r  ro   r   "blank_line_after_nested_stub_classr]   rh   r;   r#   r?   r=   r   rW   #_maybe_empty_lines_for_class_or_defr   r   rz   r}   )rH   r  max_allowedrm   r   r*   s         rJ   r  z#EmptyLineTracker._maybe_empty_lines8  s   ""#y/6!!QK 	%,Q/J&,,T22F--F "JF"  &	%T%7%;%AU%J%Jy $)555 !4 9K9R  F^^FF>$)KK*2.7 L .r2@ L FF FFFF FF*2.4 %+B/4CC$+A.4MN N FFF""$$$M   &	%T%7%;%AU%J%JN $ 	R(; 	R|?T 	R;;L&QQQ 	$",	$ !*	$ !66)6TT		$
 +111Ka!## 	"+	 4	
 19 	$"4"@ 	4qyrK   r   c                    |j         s| j                            |           | j        dS | j        j         r| j        j        r	|j        rdS dS | j        j        |j        k     r| j        j        s| j        j	        rdS d }| j        j
        r]| j        j        |j        k    rH|dk    rB| j        }|7|j        0|j        j        j        s|j        j        j        s|j        dk    r|}ndS | j        j        r|j        s| j        j        rI| j        j        |j        k     rd}n| j        j        |j        k    rd}nt|j        r| j        j        rd}n^d}n[|j	        s|j         r'| j        j	        s|j        rt!          d|          }n)d}n&| j        j        |j        k    rd}nd}n|j        rdnd}|-|j        }|$t#          |j        |          |j        z
  |_        d}|dfS )Nr  )r   rR   r   rR   rr   )rW   r  rG   r  r)   r  rh   r*   r]   ro   rN   r  r   r   r}   r   r  maxr   )rH   r  r   comment_to_add_newlinesslcnewlinesr   s          rJ   r  z4EmptyLineTracker._maybe_empty_lines_for_class_or_def  sc    ( 	4%%l333%4* 	y L$> t4#l&888' 9+/+=+D 9 48<)	"(L,>>>!/C&2*8A 3*8D 3 J!OO*-''t9 	6$ (:(C %+l.@@@ HH'-0BBB HH!/ !D4F4T ! HH HH#'3'@(/  % !  #1f~~HH  !HH#)L,>>>(.5qqAH".4CN)/6AANDXX (. {rK   )r   r   r   r   r   r   r  r	   r(   r   r   r   r   r  r   r  r	  r   r   r  r  r   rK   rJ   r   r      s          JJJ$(M8D>(((+/NHZ(/// %d ; ; ;M4:;;;59hz2999&d &z & & & &PIt Ic3h I I I IVJ J*-J	sCxJ J J J J JrK   r   sequencer6   c              #   h   K   t          |           dz
  }t          |           D ]}||fV  |dz  }dS )z;Like `reversed(enumerate(sequence))` if that were possible.rR   N)rS   r   )r  r   elements      rJ   r   r     sS      MMAEH%%  g
 rK   Fnew_lineold_liner-   r4   c                     |D ]r}t          |j        |j                  }t          ||           |                     ||           |                    |          D ]}|                     |d           sdS )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).
    rM   TN)r$   r;   r=   r   rG   r   )r   r!  r-   r4   old_leafnew_leafcomment_leafs          rJ   append_leavesr&    s      = =x~66h)))|<<<$33H== 	= 	=LOOLtO<<<<	== =rK   r   )line_strliner)   r'  c                z   |st          |           }|j        rt          nt          }t          j        |vr- ||          |j        k    od|vo|                                  S |                                 rdS d|vr ||          |j        k    S |                    d          ^}}} ||          |j        k    s ||          |j        k    rdS g }d}g }	t          j
        }
t          | j                  D ]\  }}|
t          j
        k    rd}|j        dz   t          |          k    r|                    d           n/|j        dz   t          |          k     r|                                }|%|#|j        |j        dz   k    r|j        }
|dk    r dS |j        |
k    rV|j        t"          j        k    rAdg}||	z  }|j        |v r|t          | j                  dz
  k    s||j        xx         dz  cc<   |
t          j
        k    rt)          |
|j                  }
t+          |          r`t          |	          dk    r dS |}|}t-          |          |v r5|	                    |           |j        n|j        }t-          |          |v 5t          |	          dk    rdS t1          d |D                       S )a  For 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.
    r   FNrR   r   Tc              3   "   K   | ]
}|d k    V  dS r   r   )rc   vals     rJ   r   z'is_line_short_enough.<locals>.<genexpr>H  s&      **Csax******rK   )line_to_stringpreviewr"   rS   r   multiline_string_handlingline_lengthr   splitmathinfr   r-   r   rG   r   r;   r#   r   prev_siblingr  r   r   r   all)r(  r)   r'  widthr   rd   lastcommasmultiline_stringmultiline_string_contextsmax_level_to_updateir3   	had_commaignore_ctxsctxs                   rJ   is_line_short_enoughr?    s#     (!$''.II3E(44E(OOt// 8H$855777	
 ((** u8uX$"222nnT**OEAtuU||d&&&%%++8H*H*Hu F'+*,(T[)) '! '!4$(**'+I!A%F33a    #a'#f++55"JJLL	%$0$2d6H16LLL '+&8#q== 55!444ek9Q9Q 04fK44K%44c$+>N>NQR>R9R9Rt)***a/***$(**"%&94;M"N"Nt$$ 	!,--11uu#Cc((h&&)00555:%j	 c((h&& $%%**t**6******rK   c                    | j         }t          |          dk     rdS |d         j        t          j        k    r|d         j        t          j        k    rd}d}|d         }|ddd         D ]}|j        t          v r|j        t          vr dS |dz  }nh|j        t          j        k    r|dz  }nM|j        t          j        k    r'|j        t          j        k    s|j        t          v s dS n|j        t          vr dS |dk    r	|dk    r dS dS )	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).
    rr   Fr   rR   r|   r8   NT)	r-   rS   r;   r#   rx   rb   r   r   r\   )r(  r-   
call_count	dot_countr   r3   s         rJ   can_be_splitrC  K  s    [F
6{{Quay~%%&).EI*E*E
	bz26r6N 	 	Dy,,,9$444 55a

ei''Q		ej((	UY..$)?O2O2O 55"222uu1}}auu4rK   rhsr/  c                 J   | j         }|j        }|j        sdS |                                }|                    |          }|dk    rdS |dk    r,t
          j        |j        v r|t          k    r| j	        j
        rdS |t          k    rdS t          |j                  dk    s
J d            |j        d         }|j        d         }|j        t          v r"|j        t           vrt#          |||          rdS |j        d         }|j        d	         }	|	j        t$          j        k    sK|	j        t$          j        k    s6|	j        t$          j        k    rV|	j        rO|	j        j        t.          j        k    r5|j        t          v rdS t3          |          rdS t5          ||	|
          rdS dS )a  Does `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.
    TrR   Frr   zStranded delimiterr   )r   r/  r8   r|   )r6  r/  )r   r/   
delimitersmax_delimiter_prioritydelimiter_count_with_priorityr   (wrap_multiple_context_managers_in_parensr)   r   r   r   r   rS   r-   r;   r   r   _can_omit_opening_parenr#   rv   r   r   r   r    r   r   _can_omit_closing_paren)
rD  r/  r(  btmax_prioritydelimiter_countr   secondpenultimater6  s
             rJ   can_omit_invisible_parensrQ  o  s    8D		B= t,,..L66|DDOu!<	II..3 / 5
 |##tt{q   "6    KNE[^Fz%%%&+=M*M*M"4u+NNN 	4 +b/K;r?D 		UZ9$$ I## $ DL00 ///5u%% 	 4"4dLLL 	45rK   r   c                   d}d| j         z  }d}|                                 D ]>\  }}}|j        t          v r|j        |u rd}|r||z  }||k    r n.|j        t
          v rd}?t          | j                  |dz   k    rdS dS ) See `can_omit_invisible_parens`.Frq   r|   TrR   )r*   r   r;   r   r   r   rS   r-   )r(  r   r/  	remainderr   _indexr3   leaf_lengths           rJ   rJ  rJ    s    I^FF%)%?%?%A%A  !k9(((T-AU-J-JI 	"k!F##y,,,!	 t{vz))45rK   r6  c                    d| j         z  }d}|                                 D ]0\  }}}||z  }||j        u r|s||k    r dS  |j        t          v rd}1dS )rS  rq   FT)r*   r   r   r;   r   )r(  r6  r/  r   seen_other_bracketsrU  r3   rV  s           rJ   rK  rK    s    ^F%)%?%?%A%A ' '!k+4'''" f&;&;tt '< Y***"&5rK   c                 F    t          |                               d          S )zmReturns the string representation of @line.

    WARNING: This is known to be computationally expensive.
    r   )r   r>   )r(  s    rJ   r,  r,    s    
 t99??4   rK   r   )Ar   r1  r   dataclassesr   r   typingr   r   r   r   r	   r
   r   r   r   r   black.bracketsr   r   r   
black.moder   r   black.nodesr   r   r   r   r   r   r   r   r   r   r   r   r    r!   black.stringsr"   blib2to3.pgen2r#   blib2to3.pytreer$   r%   r&   r   r   r   LNr(   r   r   r   r   r<   r&  r   r?  rC  rQ  rJ  rK  r,  r   rK   rJ   <module>rc     s        



 ( ( ( ( ( ( ( (                        H G G G G G G G G G $ $ $ $ $ $ $ $                                 $ # # # # #             & & & & & & & & GCLL	
4: l2 l2 l2 l2 l2 l2 l2 l2^         
 
 
 
 
 
 
 
* L L L L L L L L^! %q/1J     NS= =="=,0J=FJ=	= = = =2 02X+ X+ X+
X+X+),X+	X+ X+ X+ X+v!t ! ! ! ! !HQ	QQ 
Q Q Q Qh$ $ S T    2$  C D    "! !# ! ! ! ! ! !rK   