
    ?d                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dZ
dZd	Z eh d
      Zd Z G d dej                         Zd Zd Zd Zy)a;  Calculate the number of blank lines between top-level entities.

Calculates how many blank lines we need between classes, functions, and other
entities at the same level.

  CalculateBlankLines(): the main function exported by this module.

Annotations:
  newlines: The number of newlines required before the node.
    )token)pytree_utils)pytree_visitor)	py3compat)style         >   if_stmtdel_stmtfor_stmttry_stmt	exec_stmt	expr_stmt	pass_stmt	with_stmt
async_stmt
break_stmt
print_stmt
raise_stmt
small_stmt
while_stmt
yield_stmtassert_stmtglobal_stmtimport_stmtreturn_stmtsimple_stmtcontinue_stmtnonlocal_stmtc                 :    t               }|j                  |        y)zRun the blank line calculator visitor over the tree.

  This modifies the tree in place.

  Arguments:
    tree: the top-level pytree node to annotate with subtypes.
  N)_BlankLineCalculatorVisit)treeblank_line_calculators     C/usr/lib/python3/dist-packages/yapf/pytree/blank_line_calculator.pyCalculateBlankLinesr'   -   s     /0d#    c                   R     e Zd ZdZd Zd Zd Zd Zd Z fdZ	d Z
d	 Zd
 Z xZS )r"   zB_BlankLineCalculator - see file-level docstring for a description.c                 J    d| _         d| _        d| _        d| _        d| _        y )Nr   F)class_levelfunction_levellast_comment_linenolast_was_decoratorlast_was_class_or_function)selfs    r&   __init__z_BlankLineCalculator.__init__<   s*    DD D#D&+D#r(   c                     | j                  |       |j                  d   j                  t        j                  k(  r|j                  d   j
                  | _        y y Nr   )DefaultNodeVisitchildrentypegrammar_tokenCOMMENTlinenor-   r0   nodes     r&   Visit_simple_stmtz&_BlankLineCalculator.Visit_simple_stmtC   sI    $}}Q 5 55!%q!1!8!8d 6r(   c                 L   | j                   rG| j                   |j                  d   j                  dz
  k(  rt        |j                  d   t               n(t        |j                  d   | j                  |             |j                  D ]  }| j                  |        d| _        y )Nr   r   T)r-   r5   r9   _SetNumNewlines_NO_BLANK_LINES_GetNumNewlinesr#   r.   )r0   r;   childs      r&   Visit_decoratorz$_BlankLineCalculator.Visit_decoratorH   s        DMM!$4$;$;a$??dmmA&8dmmA&(<(<T(BC 
jj"Dr(   c                     d| _         | j                  |      }d| _        | xj                  dz  c_        |j                  |d  D ]  }| j                  |        | xj                  dz  c_        d| _         y )NFr   T)r/   (_SetBlankLinesBetweenCommentAndClassFuncr.   r+   r5   r#   r0   r;   indexrA   s       r&   Visit_classdefz#_BlankLineCalculator.Visit_classdefR   sr    &+D#99$?E#Duv& 
jj&*D#r(   c                    d| _         | j                  |      }t        |      r?| j                  |j                  j                        }t        |j                  d   d        n| j                  |      }d| _        | xj                  dz  c_        |j                  |d  D ]  }| j                  |        | xj                  dz  c_        d| _         y )NFr   r   T)
r/   rD   _AsyncFunctionprev_siblingparentr>   r5   r.   r,   r#   rE   s       r&   Visit_funcdefz"_BlankLineCalculator.Visit_funcdef\   s    &+D#99$?Ed;;



"
"$edmmA&-;;DAe#D1uv& 
jj1&*D#r(   c                     | j                   rKt        j                  |      t        v r0t        j                  |      }t        || j                  |             d| _         t        t        | '  |       y)zOverride the default visitor for Node.

    This will set the blank lines required if the last entity was a class or
    function.

    Arguments:
      node: (pytree.Node) The node to visit.
    FN)
r/   r   NodeName_PYTHON_STATEMENTSFirstLeafNoder>   r@   superr"   r4   )r0   r;   leaf	__class__s      r&   r4   z%_BlankLineCalculator.DefaultNodeVisitl   s_     &&			t	$(:	:))$/d22489&+D#	
6t<r(   c                    d}t        j                  |j                  |         r| j                  |j                  |   j                  d          | j                  s*t        |j                  |   j                  d   t               |dz  }t        j                  |j                  |         r|re|j                  |   j                  dz
  |j                  |dz
     j                  d   j                  k(  rt        |j                  |   t               |S | j                  dz   |j                  |   j                  k(  rt        }n| j                  |      }t        |j                  |   |       |S )aL  Set the number of blanks between a comment and class or func definition.

    Class and function definitions have leading comments as children of the
    classdef and functdef nodes.

    Arguments:
      node: (pytree.Node) The classdef or funcdef node.

    Returns:
      The index of the first child past the comment nodes.
    r   r   )r   IsCommentStatementr5   r#   r.   r>   _ONE_BLANK_LINEr9   r?   r-   r@   )r0   r;   rF   num_newliness       r&   rD   z=_BlankLineCalculator._SetBlankLinesBetweenCommentAndClassFunc|   s7    E

)
)$--*>
? jju%..q12$$e,55a8/Jqje 
)
)$--*>
? 	$--&--1==#,,Q/667dmmE*O< L 
	!	!A	%u)=)D)D	D&++D1dmmE*L9Lr(   c                     | j                   rt        S | j                  |      rdt        j                  d      z   S t
        S )Nr   'BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION)r.   r?   _IsTopLevelr   GetrV   r:   s     r&   r@   z$_BlankLineCalculator._GetNumNewlines   s:    			$	DEEEr(   c                 R    | j                   xs | j                   xr t        |      S N)r+   r,   _StartsInZerothColumnr:   s     r&   rZ   z _BlankLineCalculator._IsTopLevel   s+    !!8T%8%89 (!$')r(   )__name__
__module____qualname____doc__r1   r<   rB   rG   rL   r4   rD   r@   rZ   __classcell__)rS   s   @r&   r"   r"   9   s3    J,9
#++ = >)r(   r"   c                 b    t        j                  | t         j                  j                  |       y r]   )r   SetNodeAnnotation
AnnotationNEWLINES)r;   rW   s     r&   r>   r>      s#      |'>'>'G'G!-/r(   c                     t        j                  |       j                  dk(  xs& t        |       xr | j                  j                  dk(  S r3   )r   rP   columnrI   rJ   r;   s    r&   r^   r^      sE    

$
$T
*
1
1Q
6 C$AD$5$5$<$<$ADr(   c                     t         j                  xr5 | j                  xr' | j                  j                  t        j
                  k(  S r]   )r   PY3rJ   r6   r7   ASYNCrj   s    r&   rI   rI      s:    
-- 8D-- 8



 
 M$7$7
79r(   N)rb   lib2to3.pgen2r   r7   yapf.pytreer   r   yapf.yapflibr   r   r?   rV   _TWO_BLANK_LINES	frozensetrO   r'   PyTreeVisitorr"   r>   r^   rI    r(   r&   <module>ru      se   	 1 $ & "      	$k)>77 k)\/
D
9r(   