
    fm6                         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	j#                         z   Zd
 Zd Zd Z	 ddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%y)    )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                     t        |||      }t        j                  d|       t        |      \  }}|st	        |      |dz   t        |      z   }	|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_changess              W/var/www/cvtools/html/venv/lib/python3.12/site-packages/jedi/api/refactoring/extract.pyextract_variabler"      st    S)4E	II$e,6u=M7w''E\$?$FFN (5$"LM(<==    c                 |    t        d | D              ry| d   j                  t        vrdd| d   j                  z  fS y)z8
    Returns a tuple (is_expression, error_string).
    c              3   ^   K   | ]%  }|j                   d k(  xr |j                          ' yw)r   N)typeis_definition).0nodes     r!   	<genexpr>z,_is_expression_with_error.<locals>.<genexpr>#   s*     
JD49974#5#5#77
Js   +-)Fz,Cannot extract a name that defines somethingr   FzCannot extract a "%s")T )anyr&   _VARIABLE_EXCTRACTABLEr   s    r!   r   r      sB     
JE
JJDQx}}22-a===r#   c                    | j                  |d      }||j                  dk(  r#|j                         }||j                  |k(  r|}t	        |      r|j
                  }|j
                  j                  dk(  r|j
                  j
                  }|j
                  j                  t        v r)|j
                  }|j
                  j                  t        v r)|g}n|j                  |k(  r|j                         }||}t	        |      r|j
                  }| j                  |d      }|j                  |kD  r|j                         }|t        d      |}|j                  |j                  k  r&|j
                  }|j                  |j                  k  r&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)include_prefixes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_nodes           r!   r   r   +   s   
 2232NJ??j("002I$)<)<)C&
%j1#**J!!Y.#**11J$$(88#**J $$(88  $"002I$&
 &j1#**J 44YQU4V	)113H&'JKK !!H$4$44%,,K !!H$4$44 2+sIN
 5zQ:??.KKa!!!$%%Lr#   Nc                    t        | d         }||j                         }| d   j                         }t        |j                  d      }||u r|||d d t	        ||d         dz   g|dd dj                  |      }	i }
||u r|	|z   |
| d   <   n=||j                  }n|t        | d         z   }||z   |
| d   <   |	|j                  z   |
|<   | dd  D ]  }d|
|<   	 |
S )Nr   Tkeepends
r+   r3   )_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)   s                r!   r   r   a   s)    (a1J!'668Ah--/O*11DAE,,')E#2J E"I6=>E"RLwwu~O,,$47M$Ma!#&&A #3E!H#==A$%(>$>a!.>ASAYAY.Y*+ab	 # "#r#   c                 D    dj                  d t        |       D              S )Nr+   c              3   L   K   | ]  \  }}|j                  |d k7          yw)r   include_prefixNget_code)r(   ins      r!   r*   z._expression_nodes_to_string.<locals>.<genexpr>   s#     RA1::Q!V:4Rs   "$)rL   	enumerater.   s    r!   r   r      s    77R5AQRRRr#   c                     | d   }t        |j                         |d   dz
        \  }}||j                  d      z   dj                  d | dd  D              z   }||fS )Nr   r3   Fr[   r+   c              3   <   K   | ]  }|j                           y w)Nr]   )r(   r`   s     r!   r*   z)_suite_nodes_to_string.<locals>.<genexpr>   s     21!**,2s   )_split_prefix_atrJ   r^   rL   )r   r   r`   rK   part_of_codecodes         r!   _suite_nodes_to_stringrg      si    aA+A,<,<,>A
KFL!**E*::
''2ab	2
23D4<r#   c                     | j                   d   |z
  }t        | j                  d      }dj                  |d|        dj                  || d       fS )zU
    Returns a tuple of the leaf's prefix, split at the until_line
    position.
    r   TrE   r+   N)r8   r   rK   rL   )leaf
until_linesecond_line_countrU   s       r!   rd   rd      s]     q)J6d3E775,,,-.?P>P>Q8R0SSSr#   c                 N    t        | j                         j                        d   S )NrG   )r   rJ   rK   r)   s    r!   rM   rM      s!    t**,334R88r#   c                 v    | -| j                   j                  t        v r| S | j                   } | -t        d      )z8
    Returns the statement where a node is defined.
    zWe should never even get here)r:   r&   _DEFINITION_SCOPESNotImplementedErrorrm   s    r!   rI   rI      sA     
;;11K{{ 
 =
>>r#   c                    | j                   }|dv }|t        v s|r| j                  }t        |      D ],  \  }}|j                  |kD  s|}|j                   dk(  r|dz  } n t        t        t        |                  D ]H  \  }}|j                  |k  s|}	|j                   dk(  r|	dz  }	||d D ]  }
t        |
      r|	dz  }	 n  n |	dz    }|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   r1   r3   Nr   rG   )
r&   r   r?   ra   r;   reversedlistr8   r9   r=   )rC   r   r   typis_suite_partr   r_   r`   start_index	end_indexn2s              r!   r=   r=      s=   
 

C22M
-$$e$ 	DAqyy366Z'1$K	 T)E"234 	DAq{{Y&	66Z'NI  ) B1"5!Q		
 	 k)a-0:58S)TE!AJ:59c9UE"#J=r#   c                 b    | j                   dk(  xs | j                   dk(  xr | j                  dvS )Nr1   keyword)NoneTrueFalse)r&   rN   rm   s    r!   r9   r9      s5    99
" R99	!Qdjj8Q&QRr#   c           	      R   t        |j                  ||      }t        |      sJ t        |      \  }}|j	                  |d         }	|	j                         }
t        t        ||	|            \  }}|	j                         rd }n&t        |	j                  |
      }|j                         }|rdt        |      z   dz   }d }d}nt        |d         }|s=|r9t        t        |	|d   j                  |d   j                  |            xs |d   gng }t!        ||      \  }}|d   j#                         }t%        ||d         \  }}||z  }t'        |      }|sdj)                  |      }|d|z   dz   z  }t+        |r|d d n|       d}d }|
rzt-        |	j                        sM|	j/                         j1                         }t        |      r$|d   j2                  }|D cg c]
  }||k7  s	| }}t5        |	j                        rd}n|dz  }|d	|d
dj)                  ||n|g|z         dt7        |      }|dn|dz   |z   d
dj)                  |      d}|r|}n|r	d|z   dz   }ndz   |z   dz   }t9        ||||||      }|sj:                  z   ||<   ||i}t=        | |      S c c}w )Nr   zreturn rH   FrG   z, r+   z@classmethod
zdef (z):
.)r   )r   	tree_noder>   r   create_contextis_bound_methodrs   _find_inputs_and_outputs	is_module_get_code_insertion_noderJ   r   _is_node_ending_return_stmt_find_needed_output_variablesr:   r;   rg   r7   rd   r   rL   _check_for_non_extractablesr   	get_valueget_param_namesstring_namer
   r	   r   rN   r   )r   r   module_contextr   r   r   r   r   _contextr   paramsreturn_variablesrQ   r)   
code_blockrR   has_ending_return_stmt
after_leaffirstsecondoutput_var_str	decorator
self_paramfunction_param_namesrX   function_codefunction_callreplacementrW   r    s                                  r!   extract_functionr      s   00#yAEu::07M1++E!H5G--/O#$<^WV[$\]F !'(9(9?K!002!<U!CCdJ
!&!<U2Y!G% -=  $$Aab	!! 	%    )
 $B'("  (>eS'I$*2Y,,.
(Yq\Bve
J'
%!YY'78N)n4t;;J  .Dcr
%PIJ'(9(9:#*#4#4#6#F#F#H '(1!4@@
%+?qJ!??"7#4#45(Id
 			J.&ZL64IJZ 	M !zC'74?		&M #!#m3d:K(50=@4GKuk=#13CEO&,z/?/?&?
# /2(<==? @s   
J$$J$c                     | D ]  }	 |j                   }t        |        y # t        $ r7 |j                  dk(  rt	        d      |j                  dk(  rt	        d      Y ]w xY w)Nreturnz:Can only extract return statements if they are at the end.yieldz Cannot extract yield statements.)r?   r   AttributeErrorrN   r   )r   r`   r?   s      r!   r   r   (  sw     
2		2zzH (1
2  	Kww("&PR Rww'!&'IJJ "		Ks   !=A! A!c                     |D ]f  }|j                   dk(  s|j                  j                         r-|j                         | ur y|j                  ||j                  cxk  r|k  rc y  y y)NparamTF)api_typeparent_contextr   get_root_contextr8   )r   namesr   lastr   s        r!   _is_name_inputr   6  sm     ==G#4+>+>+H+H+J$$&n<~~%et~~.L.L /M r#   c                 ~   |d   j                   }|d   j                  }g }g }t        |      D ]  }|j                         r!||vs|j	                  |j
                         4|j
                  |vsC|j                  ||j                         }|rt        | |||      sp|j	                  |j
                          ||fS )Nr   rG   )r8   r;   _find_non_global_namesr'   appendrN   gotor   )	r   r   r   r   r   inputsoutputsr   name_definitionss	            r!   r   r   @  s    !HE9DFG&u- 	.7"tzz*zz'#*<<dnn#E ').:JESWXMM$**-	. 7?r#   c              #      K   | D ]D  }	 |j                   }|j                  dk(  r|j                   d   dk(  r2t        |      E d {    F y 7 # t        $ r |j                  dk(  r| Y hw xY ww)Nr2   r   r   r   )r?   r&   r   r   )r   r)   r?   s      r!   r   r   U  sy      8
	8}}H yyI%$--*:c*A-h7778 8  	yyF"
	s3   A1A0A1AA1A.+A1-A..A1c                    |rt        |       r?| j                  j                  dk7  r&| j                  } | j                  j                  dk7  r&| j                  j                  dv r%| j                  } | j                  j                  dv r%| S )Nr   )async_funcdef	decorated
async_stmt)r   r:   r&   )r)   r   s     r!   r   r   d  st    6t<kk,.;;D kk,. ++

J
J{{ ++

J
JKr#   c              #     K   |j                   D ]w  }|j                  |k  rt        |      }t        |g      D ]K  }|j	                         r|j
                  |v s#|j                  |j
                         |j
                   M y yw)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?   r8   setr   r'   rN   remove)r   search_nodeat_least_posr   r)   r   s         r!   r   r   n  s     
 $$ !>>L(/0*D62 	!D%%'DJJ:J,J ''

3jj 	!!s   A
B
B
.B
c                 ^    | j                   }|dk(  rt        | j                  d         S |dk(  S )Nsimple_stmtr   r4   )r&   r   r?   )r)   ts     r!   r   r   ~  s2    		AM*4==+;<<r#   )NN)&textwrapr   parsor   jedir   jedi.api.exceptionsr   jedi.api.refactoringr   r   jedi.commonr	   jedi.parser_utilsr
   r   ro   splitr-   r"   r   r   r   r   rg   rd   rM   rI   r=   r9   r   r   r   r   r   r   r   r    r#   r!   <module>r      s       0 > $ O - )*+0573 

>	3n 8<BST9?!HR
T>n2*8! r#   