o
    ï›hk%  ã                   @   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   úSc:\Users\shubh\Videos\agent\nova\Lib\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   ÚdifflibZunified_diffÚstrÚjoinÚrstrip)r   Z	old_linesÚ	new_linesÚproject_pathZfrom_pZto_pÚdiffr   r   r   Úget_diff   s6   



ÿ

ÿýzChangedFile.get_diffc                 C   s   | j j | j| j¡S r   )r   ZgrammarZ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   r0   r   r   r   Ú__repr__N   s   zChangedFile.__repr__N)r8   Ú
__module__Ú__qualname__r   r/   r#   r6   r9   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)r@   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ÚiterZget_root_node)Ú.0r%   Zmap_)rG   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Úsortedr>   Úitemsr0   r   )rG   r@   r   r   Úget_changed_filesX   s   		þ÷zRefactoring.get_changed_filesc                 C   s
   t | jƒS )z8
        Files can be renamed in a refactoring.
        )rP   r=   r0   r   r   r   rO   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   )r/   )rJ   r5   r   r   r   Ú	<genexpr>   s   € z'Refactoring.get_diff.<locals>.<genexpr>)r   r$   r%   rO   Ú_try_relative_tor*   rR   Úvalues)r   Útextr-   rE   rF   r   r   r   r/   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)rR   rU   r6   rO   Úrename)r   r5   ÚoldÚnewr   r   r   r6   �   s
   
ÿzRefactoring.applyN)r   )r8   r:   r;   r   r   r   r
   rR   r   r   rO   r/   r6   r   r   r   r   r<   R   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_nameZdir_r   r   r   Ú_calculate_renameŒ   s   
r_   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   Úaddr_   Ú
isinstancer	   Ú_valueZ
py__path__Ú
setdefaultÚprefixr<   )	r   Údefinitionsr^   Zfile_renamesZfile_tree_name_mapÚdrd   rD   Zfmapr   r   r   rW   “   s$   
ÿ€rW   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r‰t|j	ƒdkr‰|j	d }|dkr |jdkr–t 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ràd!| 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 )(Nr`   c                 s   s   � | ]}|j d v V  qdS ))ra   Ú	namespaceN)Zapi_type©rJ   Únr   r   r   rS   ®   ó   € zinline.<locals>.<genexpr>z,Cannot inline imports, modules or namespacesc                 s   s   � | ]}|j d u V  qd S r   )rd   ro   r   r   r   rS   °   rq   z!Cannot inline builtins/extensionsc                 S   s   g | ]	}|j  ¡ r|‘qS r   ©rd   Zis_definitionro   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)ZfuncdefZclassdefzCannot inline a %sT)Zinclude_setitemz3Cannot inline a statement with multiple definitionsZ	annassigné   é   ú=z:Cannot inline a statement that is defined by an annotationz#Cannot inline a statement with "%s"F)Zinclude_prefixc                 S   s   g | ]	}|j  ¡ s|‘qS r   rr   ro   r   r   r   rs   Ø   rt   Ztestlist_star_exprÚtrailerú(ú)Ú.r   z 	r2   ú;)r   ÚanyrC   rd   Zget_definitionre   ÚdictÚgetZget_defined_namesÚchildrenr"   Zget_rhsZget_root_contextZ
py__file__rZ   ÚEXPRESSION_PARTSZget_next_siblingrj   rk   ÚindexÚ_remove_indent_of_prefixZget_first_leafZget_next_leafÚstripr<   )r   Únamesrl   rd   rv   Útype_Zfirst_childÚrhsZreplace_codeZ
referencesr?   r[   r%   ÚsZof_pathrp   rk   ÚparZ	some_nodeÚchangesZ	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%   ÚbaserA   c                 C   s$   z|   |¡W S  ty   |  Y S w r   )r&   r'   )r%   r�   r   r   r   rT     s
   ÿrT   )r(   Úpathlibr   Útypingr   r   r   Zparsor   Zjedi.api.exceptionsr   Zjedi.inference.value.namespacer	   Úsplitr…   r
   r<   r_   rW   r�   r‡   rT   r   r   r   r   Ú<module>   s     ýB:R