
    fk%                         d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ dj                         Z G d d	      Z G d
 d      Zd Zd Zd Zd ZdededefdZy)    NPath)DictIterableTuple)split_lines)RefactoringError)ImplicitNSNamezmor_test and_test not_test comparison expr xor_expr and_expr shift_expr arith_expr term factor power atom_exprc                   *    e Zd Zd Zd Zd Zd Zd Zy)ChangedFilec                 J    || _         || _        || _        || _        || _        y N)_inference_state
_from_path_to_path_module_node_node_to_str_map)selfinference_state	from_pathto_pathmodule_nodenode_to_str_maps         X/var/www/cvtools/html/venv/lib/python3.12/site-packages/jedi/api/refactoring/__init__.py__init__zChangedFile.__init__   s(     /#' /    c                    t        | j                  j                         d      }t        | j                         d      }|d   dk7  r|dxx   dz  cc<   |d   dk7  r|dxx   dz  cc<   | j                  j
                  j                  }| j                  d}n	 | j                  j                  |      }| j                  d}n	 | j                  j                  |      }t        j                  ||t        |      t        |            }dj                  |      j                  d      S # t        $ r | j                  }Y w xY w# t        $ r | j                  }Y }w xY w)NTkeepends 
)fromfiletofile )r   r   get_codeget_new_coder   projectpathr   relative_to
ValueErrorr   difflibunified_diffstrjoinrstrip)r   	old_lines	new_linesproject_pathfrom_pto_pdiffs          r   get_diffzChangedFile.get_diff   sD    1 1 : : <tL	 1 1 3dC	 R=BbMT!MR=BbMT!M,,4499??"F)44\B == D%}}00> ##y[t9
 wwt}##C((!  ))  %}}%s$   D, E ,EEEEc                 v    | j                   j                  j                  | j                  | j                        S r   )r   grammarrefactorr   r   r   s    r   r'   zChangedFile.get_new_codeB   s-    $$,,55d6G6GI^I^__r   c                     | j                   t        d      t        | j                   dd      5 }|j                  | j	                                d d d        y # 1 sw Y   y xY w)Nz5Cannot apply a refactoring on a Script with path=Nonewr!   )newline)r   r	   openwriter'   )r   fs     r   applyzChangedFile.applyE   sZ    ??""G  $//33 	)qGGD%%'(	) 	) 	)s    AA"c                 P    d| j                   j                  d| j                  dS )N<z: >)	__class____name__r   r;   s    r   __repr__zChangedFile.__repr__N   s    !^^44dooFFr   N)rG   
__module____qualname__r   r7   r'   rB   rH    r   r   r   r      s    0')R`)Gr   r   c                   R    e Zd ZddZdeeef   fdZdee	eef      fdZ
d Zd Zy)	Refactoringc                 .    || _         || _        || _        y r   )r   _renames_file_to_node_changes)r   r   file_to_node_changesrenamess       r   r   zRefactoring.__init__S   s     /%9"r   returnc                 *   fd}| j                         t        | j                  j                         d       D ci c]F  \  }}|t	        | j
                  | ||      t        t        |            j                         |      H c}}S c c}}w )Nc                     | | S t        |       } D ]C  \  }}| j                  t        |            s!t        |      | t        t        |            d  z   } E t        |       S r   )r.   
startswithlenr   )pfrom_torR   s      r   calculate_to_pathz8Refactoring.get_changed_files.<locals>.calculate_to_pathY   sb    yAA$ 6	r<<E
+B!CE
O$4"55A6 7Nr   c                 &    | d   xs t        d      S )Nr   r!   r   )xs    r   <lambda>z/Refactoring.get_changed_files.<locals>.<lambda>n   s    ad.d2h r   )key)r   r   r   r   )	get_renamessortedrP   itemsr   r   nextiterget_root_node)r   r[   r)   map_rR   s       @r   get_changed_fileszRefactoring.get_changed_filesX   s    	 ""$ %**002.
 d +%%)$/ d,::< $ 
 	
 
s    ABc                 ,    t        | j                        S )z8
        Files can be renamed in a refactoring.
        )ra   rO   r;   s    r   r`   zRefactoring.get_renamesr   s     dmm$$r   c           	      $   d}| j                   j                  j                  }| j                         D ]%  \  }}|dt	        ||      dt	        ||      dz  }' |dj                  d | j                         j                         D              z   S )Nr!   zrename from z
rename to r"   c              3   <   K   | ]  }|j                           y wr   )r7   ).0rA   s     r   	<genexpr>z'Refactoring.get_diff.<locals>.<genexpr>   s     VqajjlVs   )r   r(   r)   r`   _try_relative_tor/   rg   values)r   textr3   rY   rZ   s        r   r7   zRefactoring.get_diffx   s    ,,4499))+ 	^IE2#E<8:J2|:\^ ^D	^ bggVD4J4J4L4S4S4UVVVVr   c                     | j                         j                         D ]  }|j                           | j                         D ]  \  }}|j	                  |        y)zU
        Applies the whole refactoring to the files, which includes renames.
        N)rg   rn   rB   r`   rename)r   rA   oldnews       r   rB   zRefactoring.apply   sW     '')002 	AGGI	 ((* 	HCJJsO	r   N)rK   )rG   rI   rJ   r   r   r   r   rg   r   r   r`   r7   rB   rK   r   r   rM   rM   R   sB    :

4k(9#: 
4%XeD$J&78 %Wr   rM   c                     | j                   }| j                  dv r||j                   j                  |      fS | |j                  || j                  z         fS )N)z__init__.pyz__init__.pyi)parentnamejoinpathsuffix)r)   new_namedir_s      r   _calculate_renamer{      sO    ;;Dyy33T[[))(333x$++5666r   c           	      Z   t               }i }|st        d      |D ]   }|j                  j                  }|j                  dk(  r?|=|j
                  1t        |j
                        }|j                  t        ||             ht        |j                  t              rN|j                  j                  j                         D ]&  }|j                  t        t        |      |             ( ||j                  |j
                  i       }|j                  |z   ||<    t        | ||      S )N!There is no name under the cursormodule)setr	   _name	tree_nametypemodule_pathr   addr{   
isinstancer
   _value
py__path__
setdefaultprefixrM   )	r   definitionsry   file_renamesfile_tree_name_mapdr   rX   fmaps	            r   rq   rq      s
   5LBCC > GG%%	66X)"38QQ]]#A.q(;<0WW^^..0 G  !247H!EFG $)44Q]]BG"+"2"2X"=Y> (:LIIr   c                 h   |st        d      t        d |D              rt        d      t        d |D              rt        d      |D cg c]  }|j                  j                         s|! }}t	        |      dk(  rt        d      t	        |      dkD  rt        d	      t	        |      dk(  rt        d
      |d   j                  }|j                         }|j                  dk7  r?t        dd      j                  |j                  |j                        }t        d|z        t	        |j                  d            dkD  rt        d      |j                  d   }|j                  dk(  r't	        |j                        dk(  r|j                  d   }|dk7  r8|j                  dk(  rt        d      t        d|j                  d      z        |j                         }|j                  d      }	|D cg c]  }|j                  j                         r|! }
}i }|
D ]U  }|j                  }|j                         j                         }|	}|j                  dk(  sO|j                  j                  t         v s3|j                  j                  dk(  r"|j                  j#                         d|	z   dz   }|j%                  |i       }|}|j&                  }|j                  }|j                  dk(  r}|j                  d   dk(  rk|j                  j                  d   j&                  }|}|j                  j                  d |j                  j                  j)                  |       D ]  }d ||<   	 ||z   ||<   X |d   j                         j                         }|j%                  |i       }t+        |j-                         j&                        ||<   |j/                         }|j&                  j1                  d!      d k(  r|j                  d"k(  s|d#k(  rd ||<   t3        | |      S c c}w c c}w )$Nr}   c              3   8   K   | ]  }|j                   d v   yw))r~   	namespaceN)api_typerk   ns     r   rl   zinline.<locals>.<genexpr>   s     
@Q1::00
@   z,Cannot inline imports, modules or namespacesc              3   8   K   | ]  }|j                   d u   y wr   )r   r   s     r   rl   zinline.<locals>.<genexpr>   s     
.11;;$
.r   z!Cannot inline builtins/extensionsr   zNo definition found to inline   z.Cannot inline a name with multiple definitionsz$There are no references to this name	expr_stmtfunctionclass)funcdefclassdefzCannot inline a %sT)include_setitemz3Cannot inline a statement with multiple definitions	annassign      =z:Cannot inline a statement that is defined by an annotationz#Cannot inline a statement with "%s"F)include_prefixtestlist_star_exprtrailer().r!   z 	r>   ;)r	   anyr   is_definitionrW   get_definitionr   dictgetget_defined_nameschildrenr&   get_rhsget_root_context
py__file__ru   EXPRESSION_PARTSget_next_siblingr   r   index_remove_indent_of_prefixget_first_leafget_next_leafstriprM   )r   namesr   r   r   r   type_first_childrhsreplace_code
referencesrQ   rv   r)   sof_pathr   par	some_nodechanges	next_leafs                        r   inliner      s   BCC

@%
@@MNN

.
..BCC#Cq{{'@'@'B1CKC
;1>??
;!OPP
5zQEFFA((I((*I~~$
 #inninn
- 	 3e;<<
9&&t&<=ATUU$$Q'K;&3{/C/C+D+I!**1-c{*"L  #5&&e&<= 
 


C<<u<5L"F!++*C*C*E!FJF  NN	$$&11388++##((,<<##((I5$$557Cl"S(A&11$;hh88y S\\!_%;ZZ((+22FA ZZ001P#**2E2E2K2KC2PQ (	%'	"(aZ
) , q>**,779D"--dB7G1)2J2J2L2S2STGI'')I e$*9,	S0@	(<==O DJ Gs   P*,P*;P/P/c                 @    dj                  t        | d      dd       S )zT
    Removes the last indentation of a prefix, e.g. " \n \n " becomes " \n \n".
    r!   Tr   Nr    )r/   r   )r   s    r   r   r      s!     77;v5cr:;;r   r)   baserS   c                 H    	 | j                  |      S # t        $ r | cY S w xY wr   )r*   r+   )r)   r   s     r   rm   rm     s,    %% s    !!)r,   pathlibr   typingr   r   r   parsor   jedi.api.exceptionsr	   jedi.inference.value.namespacer
   splitr   r   rM   r{   rq   r   r   rm   rK   r   r   <module>r      su      ( (  0 9O%' ?G ?GD7 7t7J0O>d<4 t  r   