o
    qoik%                     @   s   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 ZG dd	 d	ZG d
d dZdd Zdd Zdd Zdd ZdededefddZdS )    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                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )ChangedFilec                 C   s"   || _ || _|| _|| _|| _d S 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_map r   Q/home/ubuntu/.local/lib/python3.10/site-packages/jedi/api/refactoring/__init__.py__init__   s
   
zChangedFile.__init__c                 C   s  t | j dd}t |  dd}|d dkr|d  d7  < |d dkr-|d  d7  < | jjj}| jd u r:d}nz| j|}W n t	yN   | j}Y nw | j
d u rWd}nz| j
|}W n t	yk   | j
}Y nw tj||t|t|d}d|dS )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diffr   r   r   get_diff   s6   





zChangedFile.get_diffc                 C   s   | j j| j| jS r   )r   grammarrefactorr   r   r   r   r   r   r#   B   s   zChangedFile.get_new_codec                 C   sV   | j d u r	tdt| j ddd}||   W d    d S 1 s$w   Y  d S )Nz5Cannot apply a refactoring on a Script with path=Nonewr   )newline)r   r   openwriter#   )r   fr   r   r   applyE   s   
"zChangedFile.applyc                 C   s   d| j j| jf S )Nz<%s: %s>)	__class____name__r   r6   r   r   r   __repr__N   s   zChangedFile.__repr__N)r>   
__module____qualname__r   r3   r#   r<   r?   r   r   r   r   r
      s    )	r
   c                   @   sV   e Zd ZdddZdeeef fddZdee	eef  fddZ
d	d
 Zdd ZdS )Refactoringr   c                 C   s   || _ || _|| _d S r   )r   _renames_file_to_node_changes)r   r   file_to_node_changesrenamesr   r   r   r   S   s   
zRefactoring.__init__returnc                    s:   fdd    fddtj dd dD S )Nc                    sT   | d u r| S t | }  D ]\}}| t |r%t || tt |d   } qt| S r   )r*   
startswithlenr   )pfrom_to)rF   r   r   calculate_to_pathY   s   z8Refactoring.get_changed_files.<locals>.calculate_to_pathc              
      s6   i | ]\}}|t j| |tt| |d qS ))r   r   r   r   )r
   r   nextiterget_root_node).0r%   map_)rM   r   r   r   
<dictcomp>c   s    	z1Refactoring.get_changed_files.<locals>.<dictcomp>c                 S   s   | d pt dS )Nr   r   r   )xr   r   r   <lambda>n   s    z/Refactoring.get_changed_files.<locals>.<lambda>)key)get_renamessortedrD   itemsr6   r   )rM   rF   r   r   get_changed_filesX   s   		zRefactoring.get_changed_filesc                 C   s
   t | jS )z8
        Files can be renamed in a refactoring.
        )rX   rC   r6   r   r   r   rW   r   s   
zRefactoring.get_renamesc                 C   s\   d}| j jj}|  D ]\}}|dt||t||f 7 }q|ddd |   D  S )Nr   zrename from %s
rename to %s
c                 s   s    | ]}|  V  qd S r   )r3   )rQ   r;   r   r   r   	<genexpr>   s    z'Refactoring.get_diff.<locals>.<genexpr>)r   r$   r%   rW   _try_relative_tor+   rZ   values)r   textr/   rK   rL   r   r   r   r3   x   s   
 zRefactoring.get_diffc                 C   s:   |    D ]}|  q|  D ]	\}}|| qdS )zU
        Applies the whole refactoring to the files, which includes renames.
        N)rZ   r]   r<   rW   rename)r   r;   oldnewr   r   r   r<      s
   
zRefactoring.applyN)r   )r>   r@   rA   r   r   r   r
   rZ   r   r   rW   r3   r<   r   r   r   r   rB   R   s    
	rB   c                 C   s4   | j }| jdv r||j |fS | ||| j fS )N)z__init__.pyz__init__.pyi)parentnamejoinpathsuffix)r%   new_namedir_r   r   r   _calculate_rename   s   
rh   c           	      C   s   t  }i }|std|D ]N}|jj}|jdkr/|d u r/|jd ur/t|j}|t|| qt	|jt
rI|jj D ]}|tt|| q;q|d ur[||ji }|j| ||< qt| ||S )N!There is no name under the cursormodule)setr   _name	tree_nametypemodule_pathr   addrh   
isinstancer	   _value
py__path__
setdefaultprefixrB   )	r   definitionsrf   file_renamesfile_tree_name_mapdrm   rJ   fmapr   r   r   r_      s$   
r_   c                 C   s  |st dtdd |D rt dtdd |D r t ddd |D }t|d	kr1t d
t|dkr;t dt|dkrEt d|d	 j}| }|jdkretddd|j|j}t d| t|jdddkrst d|j	d }|jdkrt|j	dkr|j	d }|dkr|jdkrt dt d|j
dd | }|j
dd}dd |D }	i }
|	D ]k}|j}|  }|}|jdks|jjtv s|jjd kr|j d urd!| d" }|
|i }|}|j}|j}|jd kr|j	d	 d#kr|jj	d	 j}|}|jj	d |jj	| D ]}d$||< q|| ||< q|d	   }|
|i }t| j||< | }|jd%d$krT|jd&ksP|d'krTd$||< t| |
S )(Nri   c                 s   s    | ]}|j d v V  qdS ))rj   	namespaceN)api_typerQ   nr   r   r   r[          zinline.<locals>.<genexpr>z,Cannot inline imports, modules or namespacesc                 s   s    | ]}|j d u V  qd S r   )rm   r}   r   r   r   r[      r   z!Cannot inline builtins/extensionsc                 S   s   g | ]	}|j  r|qS r   rm   is_definitionr}   r   r   r   
<listcomp>       zinline.<locals>.<listcomp>r   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_prefixc                 S   s   g | ]	}|j  s|qS r   r   r}   r   r   r   r      r   testlist_star_exprtrailer().r   z 	r8   ;)r   anyrI   rm   get_definitionrn   dictgetget_defined_nameschildrenr"   get_rhsget_root_context
py__file__rb   EXPRESSION_PARTSget_next_siblingrt   ru   index_remove_indent_of_prefixget_first_leafget_next_leafstriprB   )r   namesrv   rm   r   type_first_childrhsreplace_code
referencesrE   rc   r%   sof_pathr~   ru   par	some_nodechanges	next_leafr   r   r   inline   s   







r   c                 C   s   d t| dddd S )zT
    Removes the last indentation of a prefix, e.g. " \n \n " becomes " \n \n".
    r   Tr   Nr   )r+   r   )ru   r   r   r   r      s   r   r%   baserG   c                 C   s$   z|  |W S  ty   |  Y S w r   )r&   r'   )r%   r   r   r   r   r\     s
   r\   )r(   pathlibr   typingr   r   r   parsor   jedi.api.exceptionsr   jedi.inference.value.namespacer	   splitr   r
   rB   rh   r_   r   r   r\   r   r   r   r   <module>   s     B:R