Format: application/x-python-code
⬇ Download File (9.3 KB)
   1  o
   2  
   3  �hm6�@sddlmZddlmZddlmZddlmZddlm	Z	m
   4  Z
   5  ddlmZddl
   6  mZmZdZe
   7  d	��Zd
   8  d�Zdd
   9  �Zdd�Z	d1dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Z d'd(�Z!d)d*�Z"d+d,�Z#d-d.�Z$d/d0�Z%dS)2�)�dedent)�split_lines)�debug)�RefactoringError)�Refactoring�EXPRESSION_PARTS)�indent_block)�function_is_classmethod�function_is_staticmethod�Zsuite�
  10  file_inputz_atom testlist_star_expr testlist test lambdef lambdef_nocond keyword name number string fstringcCs\t|||�}t�d|�t|�\}}|st|��|dt|�}	|t|||	|�i}
  11  t||
  12  �S)NzExtracting nodes: %s� = )�_find_nodesrZdbg�_is_expression_with_errorr�_expression_nodes_to_string�_replacer)�inference_state�path�module_node�name�pos�	until_pos�nodes�
  13  is_expression�messageZgenerated_code�file_to_node_changes�r�Rc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/api/refactoring/extract.py�extract_variables
  14  rcCs:tdd�|D��rdS|djtvrdd|djfSdS)z8
  15      Returns a tuple (is_expression, error_string).
  16      css"�|]}|jdko|��VqdS)rN)�type�
  17  is_definition)�.0�noderrr�	<genexpr>#s� z,_is_expression_with_error.<locals>.<genexpr>)Fz,Cannot extract a name that defines somethingrFzCannot extract a "%s")T�)�anyr�_VARIABLE_EXCTRACTABLE�rrrrrs
  18  rcCs:|j|dd�}|durC|jdkr|��}|dur|j|kr|}t|�r&|j}|jjdkr0|jj}|jjtvr?|j}|jjtvs6|g}nE|j|krR|��}|durR|}t|�rY|j}|j|dd�}|j|krq|��}|durqt	d��|}|j|jkr�|j}|j|jksyt
  19  |||�}t|�dkr�|jdvr�|d	jdgS|S)
  20  zd
  21      Looks up a module and tries to find the appropriate amount of nodes that
  22      are in there.
  23      T)Zinclude_prefixesN�operator�trailerz!Cannot extract anything from that�)�return_stmtZ
  24  yield_exprr)
  25  Zget_leaf_for_positionr�
  26  get_next_leaf�	start_pos�_is_not_extractable_syntax�parentr�end_posZget_previous_leafr�!_remove_unwanted_expression_nodes�len�children)rrrZ
  27  start_nodeZ	next_leafrZend_leaf�parent_noderrrr+sB
  28  �
  29  
  30  �rNc
  31  Cs�t|d�}|dur|��}|d��}t|jdd�}||ur)|dur)||dd�<t||d�dg|dd�<d�|�}	i}
  32  ||urK|	||
  33  |d<n|durS|j}n|t|d�}|||
  34  |d<|	|j|
  35  |<|dd�D]}d|
  36  |<qp|
  37  S)NrT��keepends������
  38  r$r*)�_get_parent_definition�get_first_leafr�prefixr�join�_get_indentation�value)
  39  rZexpression_replacementZ	extractedr�insert_before_leaf�remaining_prefixZ
  40  definitionZfirst_node_leaf�linesZextracted_prefix�replacement_dct�pr"rrrras*
  41  
  42  rcCsd�dd�t|�D��S)Nr$css$�|]
  43  \}}|j|dkd�VqdS)r�Zinclude_prefixN��get_code)r!�i�nrrrr#�s�"z._expression_nodes_to_string.<locals>.<genexpr>)r<�	enumerater'rrrr�srcCsV|d}t|��|dd�\}}||jdd�d�dd�|dd�D��}||fS)Nrr*FrDr$css�|]}|��VqdS)NrE)r!rHrrrr#�s�z)_suite_nodes_to_string.<locals>.<genexpr>)�_split_prefix_atr:rFr<)rrrHr;Zpart_of_code�coderrr�_suite_nodes_to_string�s�rLcCsD|jd|}t|jdd�}d�|d|��d�||d��fS)zU
  44      Returns a tuple of the leaf's prefix, split at the until_line
  45      position.
  46      rTr5r$N)r-rr;r<)�leafZ
  47  until_lineZsecond_line_countrArrrrJ�s(rJcCst|��j�dS)Nr7)rr:r;�r"rrrr=�sr=cCs.|dur|jjtvr|S|j}|dustd��)z8
  48      Returns the statement where a node is defined.
  49      NzWe should never even get here)r/r�_DEFINITION_SCOPES�NotImplementedErrorrNrrrr9�s�r9cCs|j}|dv}|tvs
  50  |r�|j}t|�D]\}}|j|kr*|}|jdkr(|d8}nqttt|���D](\}}|j|kr[|}	|jdkrG|	d7}	||d�D]}
  51  t|
  52  �rX|	d7}	qMnq3|||	d�}|s~t	|d||�|dd�<t	|d||�|dd�<|S|gS)z~
  53      This function makes it so for `1 * 2 + 3` you can extract `2 + 3`, even
  54      though it is not part of the expression.
  55      rr(r*Nrr7)
  56  rrr3rIr0�reversed�listr-r.r1)r4rr�typZ
  57  is_suite_partrrGrHZstart_indexZ	end_indexZn2rrrr1�s8
  58  
  59  �
  60  
  61  
  62  �r1cCs|jdkp|jdko|jdvS)Nr(�keyword)�None�True�False)rr>rNrrrr.�s
  63  �r.csXt|j||�}t|�s
  64  J�t|�\}}|�|d�}	|	��}
  65  tt||	|��\}}|	��r/d}
  66  n
  67  t	|	j|
  68  �}|�
  69  �}
  70  |rHdt|�d}d}d}nMt|d�}|sh|rftt
  71  |	|dj|dj|��pe|dgng}t||�\}}|d��}t||d�\}}||7}t|�}|s�d�|�}|d|d7}t|r�|dd�n|�d}d�|
  72  r�t|	j�s�|	����}t|�r�|dj��fdd	�|D�}t|	j�r�d
  73  }n|d7}d||d��dur�|n�g|�t|�f}d�dur�dn�d
  74  |d�|�f}|r�|}n|�rd|d}n|d|d}t|||||
  75  |�}|�s#||j||<||i}t||�S)Nrzreturn r8Fr7z, r$csg|]}|�kr|�qSrr)r!rC�Z
  76  self_paramrr�
  77  <listcomp>sz$extract_function.<locals>.<listcomp>z
  78  @classmethod
  79  z%sdef %s(%s):
  80  %sz%s(%s)�.r
  81  )rZ	tree_noder2rZcreate_context�is_bound_methodrR�_find_inputs_and_outputs�	is_module�_get_code_insertion_noder:r�_is_node_ending_return_stmt�_find_needed_output_variablesr/r0rLr,rJrr<�_check_for_non_extractablesr
  82  �	get_valueZget_param_namesZstring_namer	rrr>r)rr�module_contextrrrrr�_�contextr[�params�return_variablesr?r"Z
  83  code_blockr@Zhas_ending_return_stmtZ
  84  after_leaf�first�secondZoutput_var_str�	decoratorZfunction_param_namesZ
  85  function_codeZ
  86  function_call�replacementrBrrrXr�extract_function�s�	����
  87  
  88  
  89  
  90  ���
  91  �
  92  rlc	CsX|D]'}z|j}Wnty$|jdkrtd��|jdkr"td��Yqwt|�qdS)N�returnz:Can only extract return statements if they are at the end.�yieldz Cannot extract yield statements.)r3�AttributeErrorr>rra)rrHr3rrrra(s
  93  
  94  �
  95  ��
  96  �racCs`|D]+}|jdks|j��s-|��|urdS|jdus*||jkr)|ks-dSdSqdS)N�paramTF)Zapi_typeZparent_contextr]Zget_root_contextr-)rc�namesrh�lastrrrr�_is_name_input6s��rsc	Cs�|dj}|dj}g}g}t|�D],}|��r#||vr"|�|j�q|j|vr>|�||j�}|r8t||||�r>|�|j�q||fS)Nrr7)r-r0�_find_non_global_namesr �appendr>Zgotors)	rcrerrhrrZinputsZoutputsrZname_definitionsrrrr\@s"
  97  
  98  �
  99  ��r\c	csf�|D]-}z|j}Wnty|jdkr|VYqw|jdkr)|jddkr)qt|�EdHqdS)Nrr)rrZ)r3rorrt)rr"r3rrrrtUs�
 100  
 101  ���rtcCsL|rt|�r|jjdkr|j}|jjdks|jjdvr$|j}|jjdvs|S)Nr)Z
 102  async_funcdefZ	decoratedZ
 103  async_stmt)r
 104  r/r)r"r[rrrr^ds��r^ccs\�|jD]'}|j|krqt|�}t|g�D]}|��s*|j|vr*|�|j�|jVqqdS)z�
 105      Searches everything after at_least_pos in a node and checks if any of the
 106      return_variables are used in there and returns those.
 107      N)r3r-�setrtr r>�remove)reZsearch_nodeZat_least_posrgr"rrrrr`ns�
 108  
 109  ���r`cCs$|j}|dkrt|jd�S|dkS)NZsimple_stmtrr+)rr_r3)r"�trrrr_~sr_)NN)&�textwraprZparsorZjedirZjedi.api.exceptionsrZjedi.api.refactoringrrZjedi.commonrZjedi.parser_utilsr	r
 110  rO�splitr&rrrrrrLrJr=r9r1r.rlrarsr\rtr^r`r_rrrr�<module>s@�
 111  7
 112  �!$W
 113  
 114