o
    ˷em6                     @   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suite
file_inputz_atom testlist_star_expr testlist test lambdef lambdef_nocond keyword name number string fstringc                 C   s\   t |||}td| t|\}}|st||d t| }	|t|||	|i}
t| |
S )NzExtracting nodes: %s = )_find_nodesr   dbg_is_expression_with_errorr   _expression_nodes_to_string_replacer   )inference_statepathmodule_nodenamepos	until_posnodesis_expressionmessagegenerated_codefile_to_node_changes r   S/var/www/ideatree/venv/lib/python3.10/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rd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>#   s     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 rC|jdkr| }|dur|j|kr|}t|r&|j}|jjdkr0|jj}|jjtv r?|j}|jjtv s6|g}nE|j|krR| }|durR|}t|rY|j}| j |dd}|j|krq| }|du rqt	d|}|j|jk r|j}|j|jk syt
|||}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)include_prefixesNoperatortrailerz!Cannot extract anything from that   )return_stmt
yield_exprr   )get_leaf_for_positionr"   get_next_leaf	start_pos_is_not_extractable_syntaxparentr   end_posget_previous_leafr   !_remove_unwanted_expression_nodeslenchildren)r   r   r   
start_node	next_leafr   end_leafparent_noder   r   r    r   +   sB   


r   Nc                 C   s   t | d }|d u r| }| d  }t|jdd}||u r)|d ur)||d d< t||d d g|dd< d|}	i }
||u rK|	| |
| d < n|d u rS|j}n|t| d  }|| |
| d < |	|j |
|< | dd  D ]}d|
|< qp|
S )Nr   Tkeepends
r'   r.   )_get_parent_definitionget_first_leafr   prefixr   join_get_indentationvalue)r   expression_replacement	extractedr   insert_before_leafremaining_prefix
definitionfirst_node_leaflines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   include_prefixNget_code)r$   inr   r   r    r&      s   " z._expression_nodes_to_string.<locals>.<genexpr>)rF   	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.   FrS   r'   c                 s   s    | ]}|  V  qd S )NrU   )r$   rX   r   r   r    r&      s    z)_suite_nodes_to_string.<locals>.<genexpr>)_split_prefix_atrD   rV   rF   )r   r   rX   rE   part_of_codecoder   r   r    _suite_nodes_to_string   s   r]   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   Tr?   r'   N)r3   r   rE   rF   )leaf
until_linesecond_line_countrO   r   r   r    rZ      s   (rZ   c                 C   s   t |  jd S )NrA   )r   rD   rE   r%   r   r   r    rG      s   rG   c                 C   s.   | dur| j jtv r| S | j } | dustd)z8
    Returns the statement where a node is defined.
    NzWe should never even get here)r5   r"   _DEFINITION_SCOPESNotImplementedErrorra   r   r   r    rC      s   rC   c                 C   s  | j }|dv }|tv s|r| j}t|D ]\}}|j|kr*|}|j dkr(|d8 } nqttt|D ](\}}|j|k r[|}	|j dkrG|	d7 }	||d D ]}
t|
rX|	d7 }	qM  nq3|||	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   rA   )
r"   r   r:   rY   r6   reversedlistr3   r4   r8   )r>   r   r   typis_suite_partr   rW   rX   start_index	end_indexn2r   r   r    r8      s8   




r8   c                 C   s   | j dkp| j dko| jdvS )Nr,   keyword)NoneTrueFalse)r"   rH   ra   r   r   r    r4      s   
r4   c                    sX  t |j||}t|sJ t|\}}||d }	|	 }
tt||	|\}}|	 r/d }n
t	|	j|
}|
 }|rHdt| d }d }d}nMt|d }|sh|rftt|	|d j|d j|pe|d gng }t||\}}|d  }t||d \}}||7 }t|}|sd|}|d| d 7 }t|r|d d n| d}d  |
rt|	js|	  }t|r|d j  fdd	|D }t|	jrd
}n|d7 }d||d d u r|n g| t|f }d d u rdn d | d|f }|r|}n|rd| d }n|d | d }t||||||}|s#||j ||< ||i}t| |S )Nr   zreturn rB   FrA   z, r'   c                    s   g | ]}| kr|qS r   r   )r$   rR   
self_paramr   r    
<listcomp>  s    z$extract_function.<locals>.<listcomp>z@classmethod
z%sdef %s(%s):
%sz%s(%s).r   )r   	tree_noder9   r   create_contextis_bound_methodre   _find_inputs_and_outputs	is_module_get_code_insertion_noderD   r   _is_node_ending_return_stmt_find_needed_output_variablesr5   r6   r]   r2   rZ   r   rF   _check_for_non_extractablesr
   	get_valueget_param_namesstring_namer	   r   r   rH   r   )r   r   module_contextr   r   r   r   r   _contextru   paramsreturn_variablesrK   r%   
code_blockrL   has_ending_return_stmt
after_leaffirstsecondoutput_var_str	decoratorfunction_param_namesfunction_codefunction_callreplacementrQ   r   r   ro   r    extract_function   s   	 





r   c              	   C   sX   | D ]'}z|j }W n ty$   |jdkrtd|jdkr"tdY qw t| qd S )Nreturnz:Can only extract return statements if they are at the end.yieldz Cannot extract yield statements.)r:   AttributeErrorrH   r   r{   )r   rX   r:   r   r   r    r{   (  s   



r{   c                 C   s`   |D ]+}|j dks|j s-| | ur dS |jd u s*||j  kr)|k s- dS   dS qdS )NparamTF)api_typeparent_contextrw   get_root_contextr3   )r   namesr   lastr   r   r   r    _is_name_input6  s   r   c           	      C   s   |d j }|d j}g }g }t|D ],}| r#||vr"||j q|j|vr>|||j }|r8t| |||r>||j q||fS )Nr   rA   )r3   r6   _find_non_global_namesr#   appendrH   gotor   )	r   r   r   r   r   inputsoutputsr   name_definitionsr   r   r    rv   @  s"   


rv   c              	   c   sf    | D ]-}z|j }W n ty   |jdkr|V  Y qw |jdkr)|j d dkr)qt|E d H  qd S )Nr   r-   r   rr   )r:   r   r"   r   )r   r%   r:   r   r   r    r   U  s   

r   c                 C   sL   |rt | r| jjdkr| j} | jjdks| jjdv r$| j} | jjdv s| S )Nr   )async_funcdef	decorated
async_stmt)r
   r5   r"   )r%   ru   r   r   r    rx   d  s   rx   c                 c   s\    |j D ]'}|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)r:   r3   setr   r#   rH   remove)r   search_nodeat_least_posr   r%   r   r   r   r    rz   n  s   

rz   c                 C   s$   | j }|dkrt| jd S |dkS )Nsimple_stmtr   r/   )r"   ry   r:   )r%   tr   r   r    ry   ~  s   ry   )NN)&textwrapr   parsor   jedir   jedi.api.exceptionsr   jedi.api.refactoringr   r   jedi.commonr   jedi.parser_utilsr	   r
   rb   splitr)   r!   r   r   r   r   r]   rZ   rG   rC   r8   r4   r   r{   r   rv   r   rx   rz   ry   r   r   r   r    <module>   s@    7
!$W

