B
    Khh                 @   s  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 dZ3eG dd dZ4eG dd dZ5eG dd dZ6ee. e	ee0e.f  dddZ7d-e3e3e
e, e8ddddZ9dde3ee:e8dddZ;e3e8dd d!Z<e4e/e8d"d#d$Z=e3e,e/e8d%d&d'Z>e3e,e/e8d(d)d*Z?e3e:dd+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               @   sv  e Zd ZU dZeed< dZeed< ee	dZ
ee ed< eedZeeee f ed< eedZeed< d	Zeed
< d	Zeed< dZee ed< dLeeeddddZdMeeddddZeedddZeedddZeedddZeedddZeedddZeeddd Z eedd!d"Z!eedd#d$Z"eedd%d&Z#eedd'd(Z$dd)ee%egef  ed*d+d,Z&e'j(feed-d.d/Z)edd0d1Z*edd2d3Z+edd4d5Z,dNeeed6d7d8Z-eed9d:d;Z.eee d<d=d>Z/ddd?d@Z0eed<dAdBZ1dOee2e3e4eef  dCdDdEZ5d ddFdGZ6e7ddHdIZ8eddJdKZ9dS )P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             C   s   |j tkpt|j }|s dS tj|j kr>| jr>| jdd= | jrd|sd| j	t
|| |d7  _	| jsr|rr|r| j| | jjr| |r|| _n| j|ddr|   | |s| j| 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)Zcomplex_subscriptT)ensure_removable)typer   boolvaluestripr"   COLONis_class_paren_emptyr*   prefixr    is_complex_subscriptr-   r,   markr'   r/   has_magic_trailing_commaremove_trailing_commaappend_commentappend)selfr0   r1   r2   Z	has_value rD   ./tmp/pip-install-vv81h98y/black/black/lines.pyrB   =   s"    


zLine.append)r0   r1   r3   c             C   sD   | j jdkr2| jrtd| jr2|jtkr2td| j||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)r1   N)r,   r(   
is_comment
ValueErrorr*   r6   r   rB   )rC   r0   r1   rD   rD   rE   append_safe_   s    zLine.append_safe)r3   c             C   s   t | jdko| jd jtkS )z"Is this line a standalone comment?   r   )lenr*   r6   r   )rC   rD   rD   rE   rF   p   s    zLine.is_commentc             C   s   t | o| jd jtjkS )zIs this line a decorator?r   )r7   r*   r6   r"   AT)rC   rD   rD   rE   is_decoratoru   s    zLine.is_decoratorc             C   s   t | ot| jd S )zIs this an import line?r   )r7   r   r*   )rC   rD   rD   rE   r   z   s    zLine.is_importc             C   s   t | ot| jd S )zIs this a with_stmt line?r   )r7   r   r*   )rC   rD   rD   rE   r      s    zLine.is_with_or_async_with_stmtc             C   s*   t | o(| jd jtjko(| jd jdkS )z Is this line a class definition?r   class)r7   r*   r6   r"   NAMEr8   )rC   rD   rD   rE   is_class   s    zLine.is_classc             C   s&   | j o$| jdd dd tdD kS )zEIs this line a class definition with a body consisting only of "..."?Nc             S   s   g | ]}t tjd qS ).)r#   r"   DOT).0_rD   rD   rE   
<listcomp>   s    z&Line.is_stub_class.<locals>.<listcomp>   )rO   r*   range)rC   rD   rD   rE   is_stub_class   s    zLine.is_stub_classc             C   s   y| j d }W n tk
r"   dS X y| j d }W n tk
rJ   d}Y nX |jtjkrb|jdkp|jtjko|dk	o|jtjko|jdkS )zBIs this a function definition? (Also returns True for async defs.)r   FrI   Ndef)r*   
IndexErrorr6   r"   rN   r8   ZASYNC)rC   
first_leafZsecond_leafrD   rD   rE   is_def   s    
zLine.is_defc             C   s`   t | o^t| jdko^| jo^| jd jtjko^| jd jdko^| 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.
              (rV   ))	r7   rJ   r*   rO   r6   r"   LPARr8   RPAR)rC   rD   rD   rE   r;      s    zLine.is_class_paren_emptyc             C   s,   t | o*| jd jtjko*| jd jdS )z#Is the line a triple quoted string?r   )z"""z''')r7   r*   r6   r"   STRINGr8   
startswith)rC   rD   rD   rE   is_triple_quoted_string   s    zLine.is_triple_quoted_stringc             C   s$   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)rJ   r*   r6   r"   r:   )rC   rD   rD   rE   opens_block   s    zLine.opens_block)first_leaf_matches)rh   r3   c            C   sF   t | jdkrdS | jd }|jtks0|jdkr4dS |dkpD||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.
        rI   Fr   N)rJ   r*   r6   r   Zfmt_pass_converted_first_leaf)rC   rh   r0   rD   rD   rE   is_fmt_pass_converted   s    



zLine.is_fmt_pass_converted)depth_limitr3   c             C   s,   x&| j D ]}|jtkr|j|krdS qW dS )z)If so, needs to be split before emitting.TF)r*   r6   r   bracket_depth)rC   rj   r0   rD   rD   rE   contains_standalone_comments   s    z!Line.contains_standalone_commentsc             C   s   t  }yR| jd }|t| |jtjks>|jtjkrV|jsV| jd }|t| W n t	k
rl   dS X d}xJ| j
 D ]<\}}x2|D ]*}t|r|st|ds||krdS d}qW q~W dS )Nrf   r4   Fz ignoreT)setr*   addidr6   r"   COMMArb   r8   rZ   r+   itemsr   )rC   Zignored_ids	last_leafZcomment_seenZleaf_idr+   commentrD   rD   rE   $contains_uncollapsable_type_comments   s(    



z)Line.contains_uncollapsable_type_commentsc             C   s   | j s
dS tdd | j D d}tdd t| j D d}||krx@| j dd  D ].}x(| jt|g D ]}t|drjdS qjW qRW dS )	NFc             s   s   | ]}|j d kr|j V  qdS )r   N)lineno)rS   r0   rD   rD   rE   	<genexpr>  s    z9Line.contains_unsplittable_type_ignore.<locals>.<genexpr>r   c             s   s   | ]}|j d kr|j V  qdS )r   N)ru   )rS   r0   rD   rD   rE   rv     s    r4   z ignoreT)r*   nextreversedr+   getro   r   )rC   
first_line	last_linenoders   rD   rD   rE   !contains_unsplittable_type_ignore  s    
z&Line.contains_unsplittable_type_ignorec             C   s   t dd | jD S )Nc             s   s   | ]}t |V  qd S )N)r   )rS   r0   rD   rD   rE   rv   )  s    z2Line.contains_multiline_strings.<locals>.<genexpr>)anyr*   )rC   rD   rD   rE   contains_multiline_strings(  s    zLine.contains_multiline_strings)closingr5   r3   c             C   s   |j tkr"| jr"| jd j tjks&dS |j tjkr6dS |j tjkr|jr||jj tj	kr||j
r|t|j
|| jtjtjfdr|dS |sdS | jd }|jdkrdS |jj tjkp|j
dkpt|j
|| jtjtjfd S | jrdS |j
dk	rt|j
|| jsd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)
        rf   FT)ZbracketsN)r6   r   r*   r"   rp   RBRACERSQBparentr   traileropening_bracketr   LSQBsubscriptlistr   )rC   r   r5   commarD   rD   rE   r?   +  sD    



zLine.has_magic_trailing_comma)rs   r3   c             C   s   |j tkr| j rd|_dS |j tjkr.dS | jsDt|_ d|_dS | jd }|j tjkr|j	s|j
rtt|j
 dkrt|st| jdk rt|_ d|_dS | jd }| jt|g | dS )z0Add an inline or standalone comment to the line. Frf   rV   r^   r4   T)r6   r   r,   Zany_open_bracketsr<   r"   COMMENTr*   rb   r8   r   rJ   listr   r+   
setdefaultro   rB   )rC   rs   rr   rD   rD   rE   rA   i  s.    



zLine.append_comment)r0   r3   c             C   s   | j t|g S )z;Generate comments that should appear directly after `leaf`.)r+   ry   ro   )rC   r0   rD   rD   rE   comments_after  s    zLine.comments_afterc             C   s>   | j  }| jt|g }| jt| j d g | dS )z@Remove the trailing comma and moves the comments attached to it.rf   N)r*   popr+   ro   r   extend)rC   Ztrailing_commaZtrailing_comma_commentsrD   rD   rE   r@     s    
zLine.remove_trailing_commac             C   sj   | j  }|dkrdS |j}t|trL|jtjkr6dS |jtjkrLt	||}|dk	oht
dd | D S )zAReturn True iff `leaf` is part of a slice with non-trivial exprs.NFc             s   s   | ]}|j tkV  qd S )N)r6   r   )rS   nrD   rD   rE   rv     s    z,Line.is_complex_subscript.<locals>.<genexpr>)r,   Zget_open_lsqbZnext_sibling
isinstancer$   r6   r   Z	listmakerr   r   r~   Z	pre_order)rC   r0   Z	open_lsqbZsubscript_startrD   rD   rE   r=     s    



zLine.is_complex_subscript)rx   r3   c             c   s   t ttt gttttf  f |r&tnt}xf|| j	D ]X\}}t
|jt
|j }d|jkrbdS x | |D ]}|t
|j7 }qnW |||fV  q8W d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*   rJ   r<   r8   r   )rC   rx   opindexr0   lengthrs   rD   rD   rE   enumerate_with_length  s    
zLine.enumerate_with_lengthc             C   s   t | j| j| j| j| jdS )N)r'   r(   r-   r.   r/   )r&   r'   r(   r-   r.   r/   )rC   rD   rD   rE   clone  s    z
Line.clonec             C   s   | sdS d| j  }t| j}t|}|j | |j }x|D ]}|t|7 }q>W x&tj	| j
 D ]}|t|7 }qfW |d S )zRender the line.r   z    )r(   iterr*   rw   r<   r8   str	itertoolschainfrom_iterabler+   values)rC   indentr*   firstresr0   rs   rD   rD   rE   __str__  s    


zLine.__str__c             C   s   t | jp| jS )z/Return True if the line has leaves or comments.)r7   r*   r+   )rC   rD   rD   rE   __bool__  s    zLine.__bool__)FF)F)F)F):__name__
__module____qualname____doc__r   __annotations__r(   intr   r   r*   r   r#   dictr+   r   LeafIDr   r,   r-   r7   r.   r/   r   rB   rH   propertyrF   rL   r   r   rO   rX   r\   r;   re   rg   r   ri   sysmaxsizerl   rt   r}   r   r?   rA   r   r@   r=   r   r
   r   r   r   r   r   r   rD   rD   rD   rE   r&   /   sb   
 $!<%	r&   c               @   s:   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   Zclosing_bracketN)r   r   r   r   r&   r   r#   rD   rD   rD   rE   r     s   
r   c               @   sn   e Zd ZU dZeed< ed  ed< eed< dZe	ed< e
edZee ed< dZe	ed	< ee d
d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   before)r)   content_linesafter)r3   c             C   s.   t t| jd}|| j g| j || j g S )N)r'   )r   r&   r'   r   r   r   )rC   Z
empty_linerD   rD   rE   	all_lines  s    zLinesBlock.all_linesN)r   r   r   r   r   r   r   r&   r   r   r   r   r   r   r   r   r   rD   rD   rD   rE   r     s   
r   c               @   s   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< eedZee ed< dZee
 ed< ee
d	d
dZeeeef d	ddZeeeeef dd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_liner3   c             C   s   |  |\}}| jr| jjnd}| jdkr.dn|| }t| j| j|||d}|jr| jdks~| jjs| jjrp|r| jdks~|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).
        r   N)r'   r   r   r   r   )	_maybe_empty_linesr   r   r   r   r'   rF   rL   r   )rC   r   r   r   Zprevious_afterblockrD   rD   rE   maybe_empty_lines  s*    

z"EmptyLineTracker.maybe_empty_linesc             C   s  d}|j dkr| jjrdnd}|jrL|jd }|jd}t||}d|_nd}|j }x| jr:| jd j |kr:| jjr| jd k	st	|r|j
s| jj
rtd|}n8tj| jkr| jd jr| jd jsd}n|rd}nd}nN|rd}nD|s*| jd j r*|jd jtjkr*|jd jdkr*d}nd}| j  qXW |jsT|j
sT|jr`| ||S | jr| jjr|js|jtds|| jj kr|pddfS | jr| jjr|jr|dfS | jr| jjrd	S |dfS )
NrI   r   r^   r   r   rf   )withtryforwhileifmatch)rh   )r   r   )r(   r'   is_pyir*   r<   countminr   r   AssertionErrorr\   r   Z"blank_line_after_nested_stub_classrO   rX   r6   r"   r:   r8   r   rL   #_maybe_empty_lines_for_class_or_defr   ri   re   rg   )rC   r   Zmax_allowedr[   r   r(   rD   rD   rE   r   8  s^    






z#EmptyLineTracker._maybe_empty_lines)r   r   r3   c             C   s  |j s| j| | jd kr 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r|dkr| j}|d k	r|jd k	r|jjjs|jjjs|jdkr|}ndS | jjrt|js| jjr$| jj|jk rd}n2| jj|jkrd}n|jr| jjrd}nd}nN|j	s4|j rX| jj	sX|jrRtd|}nd}n| jj|jkrnd}nd}n|jrdnd}|d k	r|j}|d k	rt|j||j |_d}|dfS )N)r   r   )r   rI   r   rI   r^   )rL   r   rB   r   r'   r   rX   r(   rO   r\   rF   r   r   r   rg   r   r   maxr   )rC   r   r   Zcomment_to_add_newlinesZslcnewlinesr   rD   rD   rE   r     s^    








z4EmptyLineTracker._maybe_empty_lines_for_class_or_def)r   r   r   r   r   r   r   r   r&   r   r   r   r   r   r   r   r   r
   r   r   r   rD   rD   rD   rE   r      s   
(Lr   )sequencer3   c             c   s4   t | d }x"t| D ]}||fV  |d8 }qW dS )z;Like `reversed(enumerate(sequence))` if that were possible.rI   N)rJ   rx   )r   r   elementrD   rD   rE   r     s    
r   F)new_lineold_liner*   r1   r3   c             C   sZ   xT|D ]L}t |j|j}t|| | j||d x ||D ]}| j|dd q<W qW d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).
    )r1   TN)r#   r6   r8   r   rB   r   )r   r   r*   r1   Zold_leafZnew_leafZcomment_leafrD   rD   rE   append_leaves  s    

r   r   )line_str)liner'   r   r3   c            C   s6  |st | }|jrtnt}tj|krD|||jkoBd|koB|   S |  rPdS d|krf|||jkS |d^}}}|||jks|||jkrdS g }d}g }	t	j
}
xdt| jD ]T\}}|
t	j
krFd}|jd t|kr|d n|jd t|k r| }|dk	rF|dk	rF|j|jd krF|j}
|dkrFdS |j|
kr|jtjkrdg}||	7 }|j|kr|t| jd ks||j  d7  < |
t	j
krt|
|j}
t|rt|	dkrdS |}|}x2t||kr|	| |jdkrP |j}qW qW t|	dkr$dS td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   FNrI   r   Tc             s   s   | ]}|d kV  qdS )r   NrD   )rS   valrD   rD   rE   rv   H  s    z'is_line_short_enough.<locals>.<genexpr>)line_to_stringpreviewr!   rJ   r   Zmultiline_string_handlingline_lengthrl   splitmathinfr   r*   rk   rB   r   r6   r"   rp   Zprev_siblingr   r   r   r   all)r   r'   r   widthr   rT   lastZcommasZmultiline_stringZmultiline_string_contextsZmax_level_to_updateir0   Z	had_commaZignore_ctxsctxrD   rD   rE   is_line_short_enough  sf    




 
r   )r   r3   c             C   s   | j }t|dk rdS |d jtjkr|d jtjkrd}d}|d }x|ddd D ]}|jtkr||jtkrrdS |d7 }nL|jtjkr|d7 }n6|jtjkr|jtjks|jtksdS n|jtkrdS |dkrV|dkrVdS qVW 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).
    r^   Fr   rI   rf   r4   NT)	r*   rJ   r6   r"   rc   rR   r   r   rN   )r   r*   Z
call_count	dot_countrw   r0   rD   rD   rE   can_be_splitK  s,     




r   )rhsr   r3   c       
      C   sD  | j }|j}|jsdS | }||}|dkr4dS |dkr\tj|jkr\|tkr\| j	j
r\dS |tkrhdS t|jdks~td|jd }|jd }|jtkr|jtkrt|||drdS |jd }|jd	 }	|	jtjks|	jtjks|	jtjkr@|	jr@|	jjtjkr@|jtkrdS t|r,dS t||	|d
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.
    TrI   Fr^   zStranded delimiterr   )r   r   r4   rf   )r   r   )r   r,   
delimitersZmax_delimiter_priorityZdelimiter_count_with_priorityr   Z(wrap_multiple_context_managers_in_parensr'   r   r   r   r   rJ   r*   r   r6   r   r   _can_omit_opening_parenr"   rb   r   r   r   r   r   r   _can_omit_closing_paren)
r   r   r   ZbtZmax_priorityZdelimiter_countr   secondZpenultimater   rD   rD   rE   can_omit_invisible_parenso  sF    






r   )r   r   r   r3   c            C   s   d}d| j  }d}xh|  D ]F\}}}|jtkr>|j|kr>d}|r||7 }||krTP |jtkrd}qW t| j|d kr|dS dS )z See `can_omit_invisible_parens`.Fr]   rf   TrI   )r(   r   r6   r   r   r   rJ   r*   )r   r   r   	remainderr   _indexr0   leaf_lengthrD   rD   rE   r     s    

r   )r   r   r   r3   c            C   s\   d| j  }d}xH|  D ]<\}}}||7 }||jkrF|s@||krTdS q|jtkrd}qW dS )z See `can_omit_invisible_parens`.r]   FT)r(   r   r   r6   r   )r   r   r   r   Zseen_other_bracketsr   r0   r   rD   rD   rE   r     s    


r   c             C   s   t | dS )zmReturns the string representation of @line.

    WARNING: This is known to be computationally expensive.
    r   )r   r9   )r   rD   rD   rE   r     s    r   )F)Ar   r   r   Zdataclassesr   r   typingr   r   r   r   r   r	   r
   r   r   r   Zblack.bracketsr   r   r   Z
black.moder   r   Zblack.nodesr   r   r   r   r   r   r   r   r   r   r   r   r   r    Zblack.stringsr!   Zblib2to3.pgen2r"   Zblib2to3.pytreer#   r$   r%   r   r   r   LNr&   r   r   r   r   r7   r   r   r   r   r   r   r   r   rD   rD   rD   rE   <module>   sD   0@   3 Q 	Y%Q