o
    i[#                     @   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 ZG dd dZG d	d
 d
Zdd Zdd Zdd Zdd ZdS )    N)Path)DictIterableTuple)split_lines)RefactoringErrorzmor_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   X/var/www/edux/Edux_v2/venv/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!   A   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   applyD   s   
"zChangedFile.applyc                 C   s   d| j j| jf S )Nz<%s: %s>)	__class____name__r   r4   r   r   r   __repr__M   s   zChangedFile.__repr__N)r<   
__module____qualname__r   r1   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   R   s   
zRefactoring.__init__returnc                    s2   fdd    fddtj 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)rD   r   r   calculate_to_pathX   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_)rK   r   r   r   
<dictcomp>b   s    z1Refactoring.get_changed_files.<locals>.<dictcomp>)get_renamessortedrB   itemsr4   r   )rK   rD   r   r   get_changed_filesW   s
   	zRefactoring.get_changed_filesc                 C   s
   t | jS )z8
        Files can be renamed in a refactoring.
        )rS   rA   r4   r   r   r   rR   l   s   
zRefactoring.get_renamesc                 C   s\   d}| j jj}|  D ]\}}|d||||f 7 }q|ddd |   D  S )Nr   zrename from %s
rename to %s
c                 s   s    | ]}|  V  qd S r	   )r1   )rO   r9   r   r   r   	<genexpr>y   s    z'Refactoring.get_diff.<locals>.<genexpr>)r
   r"   r#   rR   r$   r)   rU   values)r   textr-   rI   rJ   r   r   r   r1   r   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)rU   rW   r:   rR   rename)r   r9   oldnewr   r   r   r:   {   s
   
zRefactoring.applyN)r   )r<   r>   r?   r   r   r   r   rU   r   r   rR   r1   r:   r   r   r   r   r@   Q   s    
	r@   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   
rb   c           	      C   s   t  }i }|std|D ]6}|jj}|jdkr1|d u r1|jd u r#d nt|j}|t|| q|d urC|	|ji }|j
| ||< qt| ||S )N!There is no name under the cursormodule)setr   _name	tree_nametypemodule_pathr   addrb   
setdefaultprefixr@   )	r   definitionsr`   file_renamesfile_tree_name_mapdrg   rH   fmapr   r   r   rY      s   rY   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 )(Nrc   c                 s   s    | ]}|j d v V  qdS ))rd   	namespaceN)api_typerO   nr   r   r   rV          zinline.<locals>.<genexpr>z,Cannot inline imports, modules or namespacesc                 s   s    | ]}|j d u V  qd S r	   )rg   rt   r   r   r   rV      rv   z!Cannot inline builtins/extensionsc                 S   s   g | ]	}|j  r|qS r   rg   is_definitionrt   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   rw   rt   r   r   r   ry      rz   testlist_star_exprtrailer().r   z 	r6   ;)r   anyrG   rg   get_definitionrh   dictgetget_defined_nameschildrenr    get_rhsget_root_context
py__file__r\   EXPRESSION_PARTSget_next_siblingrk   rl   index_remove_indent_of_prefixget_first_leafget_next_leafstripr@   )r   namesrm   rg   r|   type_first_childrhsreplace_code
referencesrC   r]   r#   sof_pathru   rl   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   )rl   r   r   r   r      s   r   )r&   pathlibr   typingr   r   r   parsor   jedi.api.exceptionsr   splitr   r   r@   rb   rY   r   r   r   r   r   r   <module>   s    B5R