a
    ÷£Bem6  ã                   @   s
  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	m
Z
 d dlmZ d dlmZmZ dZe
d	 ¡  Zd
d„ Zdd„ Zdd„ Zd1dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Z d'd(„ Z!d)d*„ Z"d+d,„ Z#d-d.„ Z$d/d0„ Z%dS )2é    )Údedent)Úsplit_lines)Údebug)ÚRefactoringError)ÚRefactoringÚEXPRESSION_PARTS)Úindent_block)Úfunction_is_classmethodÚfunction_is_staticmethod©ZsuiteÚ
file_inputz_atom testlist_star_expr testlist test lambdef lambdef_nocond keyword name number string fstringc                 C   s\   t |||ƒ}t d|¡ t|ƒ\}}|s0t|ƒ‚|d t|ƒ }	|t|||	|ƒi}
t| |
ƒS )NzExtracting nodes: %sú = )Ú_find_nodesr   ZdbgÚ_is_expression_with_errorr   Ú_expression_nodes_to_stringÚ_replacer   )Úinference_stateÚpathÚmodule_nodeÚnameÚposÚ	until_posÚnodesÚis_expressionÚmessageZgenerated_codeÚfile_to_node_changes© r   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/api/refactoring/extract.pyÚextract_variable   s    r   c                 C   s:   t dd„ | D ƒƒrdS | d jtvr6dd| d j fS dS )z8
    Returns a tuple (is_expression, error_string).
    c                 s   s    | ]}|j d ko| ¡ V  qdS )r   N)ÚtypeÚis_definition)Ú.0Únoder   r   r   Ú	<genexpr>#   ó    z,_is_expression_with_error.<locals>.<genexpr>)Fz,Cannot extract a name that defines somethingr   FzCannot extract a "%s")TÚ )Úanyr   Ú_VARIABLE_EXCTRACTABLE©r   r   r   r   r      s
    r   c                 C   s*  | j |dd�}|du r||jdkr>| ¡ }|dur>|j|kr>|}t|ƒrL|j}|jjdkr`|jj}|jjtv rt|j}q`|g}n€|j|krš| ¡ }|durš|}t|ƒr¨|j}| j |dd�}|j|krØ| ¡ }|du rØt	dƒ‚|}|j|jk rð|j}qÜt
|||ƒ}t|ƒdk�r&|jdv �r&|d	 jd gS |S )
zd
    Looks up a module and tries to find the appropriate amount of nodes that
    are in there.
    T)Zinclude_prefixesNÚoperatorÚtrailerz!Cannot extract anything from thaté   )Úreturn_stmtZ
yield_exprr   )Zget_leaf_for_positionr   Úget_next_leafÚ	start_posÚ_is_not_extractable_syntaxÚparentr   Úend_posZget_previous_leafr   Ú!_remove_unwanted_expression_nodesÚlenÚchildren)r   r   r   Z
start_nodeZ	next_leafr   Zend_leafÚparent_noder   r   r   r   +   s>    


r   Nc                 C   sò   t | d ƒ}|d u r| ¡ }| d  ¡ }t|jdd�}||u rR|d urR||d d…< t||d ƒd g|dd…< d |¡}	i }
||u r–|	| |
| d < n>|d u r¦|j}n|t| d ƒ }|| |
| d < |	|j |
|< | dd … D ]}d|
|< qà|
S )Nr   T©ÚkeependséÿÿÿÿÚ
r%   r+   )Ú_get_parent_definitionÚget_first_leafr   Úprefixr   ÚjoinÚ_get_indentationÚvalue)r   Zexpression_replacementZ	extractedr   Úinsert_before_leafÚremaining_prefixZ
definitionZfirst_node_leafÚlinesZextracted_prefixÚreplacement_dctÚpr"   r   r   r   r   a   s*    

r   c                 C   s   d  dd„ t| ƒD ƒ¡S )Nr%   c                 s   s"   | ]\}}|j |d kd�V  qdS )r   ©Zinclude_prefixN©Úget_code)r!   ÚiÚnr   r   r   r#   ƒ   r$   z._expression_nodes_to_string.<locals>.<genexpr>)r=   Ú	enumerater(   r   r   r   r   ‚   s    r   c                 C   sV   | d }t | ¡ |d d ƒ\}}||jdd� d dd„ | dd … D ƒ¡ }||fS )Nr   r+   FrE   r%   c                 s   s   | ]}|  ¡ V  qd S )NrF   )r!   rI   r   r   r   r#   Š   r$   z)_suite_nodes_to_string.<locals>.<genexpr>)Ú_split_prefix_atr;   rG   r=   )r   r   rI   r<   Zpart_of_codeÚcoder   r   r   Ú_suite_nodes_to_string†   s    ÿrM   c                 C   sD   | j d | }t| jdd�}d |d| … ¡d || d… ¡fS )zU
    Returns a tuple of the leaf's prefix, split at the until_line
    position.
    r   Tr6   r%   N)r.   r   r<   r=   )ÚleafZ
until_lineZsecond_line_countrB   r   r   r   rK   Ž   s    rK   c                 C   s   t |  ¡ jƒd S )Nr8   )r   r;   r<   ©r"   r   r   r   r>   ™   s    r>   c                 C   s,   | dur | j jtv r| S | j } q tdƒ‚dS )z8
    Returns the statement where a node is defined.
    NzWe should never even get here)r0   r   Ú_DEFINITION_SCOPESÚNotImplementedErrorrO   r   r   r   r:   �   s
    r:   c                 C   s  | j }|dv }|tv s|�r| j}t|ƒD ],\}}|j|kr*|}|j dkrR|d8 } qXq*ttt|ƒƒƒD ]T\}}|j|k rh|}	|j dkr�|	d7 }	||d… D ]}
t|
ƒr²|	d7 }	qœ q¸qœ q¾qh|||	d … }|�st	|d ||ƒ|dd…< t	|d ||ƒ|dd…< |S | gS )z~
    This function makes it so for `1 * 2 + 3` you can extract `2 + 3`, even
    though it is not part of the expression.
    r   r)   r+   Nr   r8   )
r   r   r4   rJ   r1   ÚreversedÚlistr.   r/   r2   )r5   r   r   ÚtypZis_suite_partr   rH   rI   Ústart_indexZ	end_indexZn2r   r   r   r2   ¨   s4    




r2   c                 C   s   | j dkp| j dko| jdvS )Nr)   Úkeyword)ÚNoneÚTrueÚFalse)r   r?   rO   r   r   r   r/   Ì   s    
ÿr/   c                    sj  t |j||ƒ}t|ƒsJ ‚t|ƒ\}}| |d ¡}	|	 ¡ }
tt||	|ƒƒ\}}|	 ¡ r^d }nt	|	j|
ƒ}| 
¡ }|r�dt|ƒ d }d }d}nœt|d ƒ}|sÐ|rÌtt|	|d j|d j|ƒƒpÎ|d gng }t||ƒ\}}|d  ¡ }t||d ƒ\}}||7 }t|ƒ}|�s,d |¡}|d| d 7 }t|�r@|d d… n|ƒ d}d ‰ |
�r¤t|	jƒ�s’|	 ¡  ¡ }t|ƒ�r’|d j‰ ‡ fdd	„|D ƒ}t|	jƒ�r¬d
}n|d7 }d||d ˆ d u �rÄ|nˆ g| ¡t|ƒf }dˆ d u �rêdnˆ d | d |¡f }|�r|}n$|�r"d| d }n|d | d }t||||||ƒ}|�sX||j ||< ||i}t| |ƒS )Nr   zreturn r9   Fr8   z, r%   c                    s   g | ]}|ˆ kr|‘qS r   r   )r!   rD   ©Z
self_paramr   r   Ú
<listcomp>  r$   z$extract_function.<locals>.<listcomp>z@classmethod
z%sdef %s(%s):
%sz%s(%s)Ú.r   )r   Z	tree_noder3   r   Zcreate_contextÚis_bound_methodrS   Ú_find_inputs_and_outputsÚ	is_moduleÚ_get_code_insertion_noder;   r   Ú_is_node_ending_return_stmtÚ_find_needed_output_variablesr0   r1   rM   r-   rK   r   r=   Ú_check_for_non_extractablesr
   Ú	get_valueZget_param_namesZstring_namer	   r   r   r?   r   )r   r   Úmodule_contextr   r   r   r   r   Ú_Úcontextr]   ÚparamsÚreturn_variablesr@   r"   Z
code_blockrA   Zhas_ending_return_stmtZ
after_leafÚfirstÚsecondZoutput_var_strÚ	decoratorZfunction_param_namesZfunction_codeZfunction_callÚreplacementrC   r   r   rZ   r   Úextract_functionÑ   sŠ    	ûüûû


üþ
ÿrn   c              	   C   sX   | D ]N}z
|j }W n6 tyH   |jdkr2tdƒ‚|jdkrDtdƒ‚Y q0 t|ƒ qd S )NÚreturnz:Can only extract return statements if they are at the end.Úyieldz Cannot extract yield statements.)r4   ÚAttributeErrorr?   r   rc   )r   rI   r4   r   r   r   rc   (  s    

ÿ
rc   c                 C   s\   |D ]R}|j dks|j ¡ s| ¡ | ur. dS |jd u sP||j  krN|k sn  dS qdS )NÚparamTF)Zapi_typeZparent_contextr_   Zget_root_contextr.   )re   Únamesrj   Úlastr   r   r   r   Ú_is_name_input6  s    "ru   c           	      C   s†   |d j }|d j}g }g }t|ƒD ]X}| ¡ rF||vr|| |j¡ q$|j|vr$| ||j ¡}|rpt| |||ƒr$| |j¡ q$||fS )Nr   r8   )r.   r1   Ú_find_non_global_namesr    Úappendr?   Zgotoru   )	re   rg   r   rj   rt   ÚinputsÚoutputsr   Zname_definitionsr   r   r   r^   @  s    


ÿr^   c              	   c   sd   | D ]Z}z
|j }W n" ty4   |jdkr0|V  Y q0 |jdkrP|j d dkrPqt|ƒE d H  qd S )Nr   r*   r   r\   )r4   rq   r   rv   )r   r"   r4   r   r   r   rv   U  s    

rv   c                 C   s8   |rt | ƒr | jjdkr | j} q| jjdv r4| j} q | S )Nr   )Zasync_funcdefZ	decoratedZ
async_stmt)r
   r0   r   )r"   r]   r   r   r   r`   d  s    r`   c                 c   sZ   |j D ]N}|j|k rqt|ƒ}t|gƒD ]*}| ¡ s(|j|v r(| |j¡ |jV  q(qdS )z�
    Searches everything after at_least_pos in a node and checks if any of the
    return_variables are used in there and returns those.
    N)r4   r.   Úsetrv   r    r?   Úremove)rg   Zsearch_nodeZat_least_posri   r"   r   r   r   r   rb   n  s    

rb   c                 C   s$   | j }|dkrt| jd ƒS |dkS )NZsimple_stmtr   r,   )r   ra   r4   )r"   Útr   r   r   ra   ~  s    ra   )NN)&Útextwrapr   Zparsor   Zjedir   Zjedi.api.exceptionsr   Zjedi.api.refactoringr   r   Zjedi.commonr   Zjedi.parser_utilsr	   r
   rP   Úsplitr'   r   r   r   r   r   rM   rK   r>   r:   r2   r/   rn   rc   ru   r^   rv   r`   rb   ra   r   r   r   r   Ú<module>   s<   ÿ7 ÿ
!$W

