o
    ˷et"                     @   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   T/var/www/ideatree/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| j|}| j	d u rHd}n| j	|}t
j||t|t|d}d|dS )NTkeepends 
)fromfiletofile )r   r   get_codeget_new_coder
   projectpathr   relative_tor   difflibunified_diffstrjoinrstrip)r   	old_lines	new_linesproject_pathfrom_pto_pdiffr   r   r   get_diff   s&   



zChangedFile.get_diffc                 C   s   | j j| j| jS r	   )r
   grammarrefactorr   r   r   r   r   r   r!   ;   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   apply>   s   
"zChangedFile.applyc                 C   s   d| j j| jf S )Nz<%s: %s>)	__class____name__r   r3   r   r   r   __repr__G   s   zChangedFile.__repr__N)r;   
__module____qualname__r   r0   r!   r9   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   L   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)rC   r   r   calculate_to_pathR   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_)rJ   r   r   r   
<dictcomp>\   s    z1Refactoring.get_changed_files.<locals>.<dictcomp>)get_renamessortedrA   itemsr3   r   )rJ   rC   r   r   get_changed_filesQ   s
   	zRefactoring.get_changed_filesc                 C   s
   t | jS )z8
        Files can be renamed in a refactoring.
        )rR   r@   r3   r   r   r   rQ   f   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	   )r0   )rN   r8   r   r   r   	<genexpr>s   s    z'Refactoring.get_diff.<locals>.<genexpr>)r
   r"   r#   rQ   r$   r(   rT   values)r   textr,   rH   rI   r   r   r   r0   l   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)rT   rV   r9   rQ   rename)r   r8   oldnewr   r   r   r9   u   s
   
zRefactoring.applyN)r   )r;   r=   r>   r   r   r   r   rT   r   r   rQ   r0   r9   r   r   r   r   r?   K   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   
ra   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   addra   
setdefaultprefixr?   )	r   definitionsr_   file_renamesfile_tree_name_mapdrf   rG   fmapr   r   r   rX      s   rX   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 )(Nrb   c                 s   s    | ]}|j d v V  qdS ))rc   	namespaceN)api_typerN   nr   r   r   rU          zinline.<locals>.<genexpr>z,Cannot inline imports, modules or namespacesc                 s   s    | ]}|j d u V  qd S r	   )rf   rs   r   r   r   rU      ru   z!Cannot inline builtins/extensionsc                 S   s   g | ]	}|j  r|qS r   rf   is_definitionrs   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   rv   rs   r   r   r   rx      ry   testlist_star_exprtrailer().r   z 	r5   ;)r   anyrF   rf   get_definitionrg   dictgetget_defined_nameschildrenr    get_rhsget_root_context
py__file__r[   EXPRESSION_PARTSget_next_siblingrj   rk   index_remove_indent_of_prefixget_first_leafget_next_leafstripr?   )r   namesrl   rf   r{   type_first_childrhsreplace_code
referencesrB   r\   r#   sof_pathrt   rk   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   )rk   r   r   r   r      s   r   )r%   pathlibr   typingr   r   r   parsor   jedi.api.exceptionsr   splitr   r   r?   ra   rX   r   r   r   r   r   r   <module>   s    <5R