Format: application/x-python-code
⬇ Download File (8.1 KB)
   1  o
   2  
   3  �hk%�@s�ddlZddlmZddlmZmZmZddlmZddl	m
   4  Z
   5  ddlmZd�
   6  �ZGdd	�d	�ZGd
   7  d�d�Zdd
   8  �Zdd�Zdd�Zdd�Zdededefdd�ZdS)�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@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
   9  �ZdS)�ChangedFilecCs"||_||_||_||_||_dS�N)�_inference_state�
  10  _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
  11  
  12  zChangedFile.__init__cCst|j��dd�}t|��dd�}|ddkr|dd7<|ddkr-|dd7<|jjj}|jdur:d}nz|j�|�}Wnt	yN|j}Ynw|j
  13  durWd}nz|j
  14  �|�}Wnt	yk|j
  15  }Ynwtj||t
  16  |�t
  17  |�d�}d�|��d�S)NT��keepends�������
  18  )�fromfile�tofile� )rr�get_code�get_new_coder�project�pathr
  19  �relative_to�
  20  ValueErrorr�difflibZunified_diff�str�join�rstrip)rZ	old_lines�	new_lines�project_pathZfrom_pZto_p�diffrrr�get_diffs6
  21  
  22  
  23  
  24  �
  25  
  26  ��zChangedFile.get_diffcCs|jj�|j|j�Sr)rZgrammarZrefactorrr�rrrrr#BszChangedFile.get_new_codecCsV|jdur	td��t|jddd��}|�|���Wd�dS1s$wYdS)Nz5Cannot apply a refactoring on a Script with path=None�wr)�newline)r
  27  r�open�writer#)r�frrr�applyEs
  28  �"�zChangedFile.applycCsd|jj|jfS)Nz<%s: %s>)�	__class__�__name__r
  29  r0rrr�__repr__NszChangedFile.__repr__N)r8�
  30  __module__�__qualname__rr/r#r6r9rrrrr
  31  s)	r
  32  c@sVeZdZddd�Zdeeeffdd�Zdee	eeffdd�Z
  33  d	d
  34  �Zdd�Zd
  35  S)�RefactoringrcCs||_||_||_dSr)r�_renames�_file_to_node_changes)rr�file_to_node_changes�renamesrrrrSs
  36  zRefactoring.__init__�returncs:�fdd��������fdd�t�j��dd�d�D�S)NcsT|dur|St|�}�D]\}}|�t|��r%t|�|tt|��d�}qt|�Sr)r)�
  37  startswith�lenr)�p�from_�to)r@rr�calculate_to_pathYs�z8Refactoring.get_changed_files.<locals>.calculate_to_pathc
  38  s6i|]\}}|t�j|�|�tt|����|d��qS))rrrr)r
  39  r�next�iterZ
  40  get_root_node)�.0r%Zmap_)rGrrr�
  41  <dictcomp>cs	���z1Refactoring.get_changed_files.<locals>.<dictcomp>cSs|dptd�S)Nrrr)�xrrr�<lambda>nsz/Refactoring.get_changed_files.<locals>.<lambda>)�key)�get_renames�sortedr>�itemsr0r)rGr@rr�get_changed_filesXs		��zRefactoring.get_changed_filescCs
  42  t|j�S)z8
  43          Files can be renamed in a refactoring.
  44          )rPr=r0rrrrOrs
  45  zRefactoring.get_renamescCs\d}|jjj}|��D]\}}|dt||�t||�f7}q|d�dd�|����D��S)Nrzrename from %s
  46  rename to %s
  47  css�|]}|��VqdSr)r/)rJr5rrr�	<genexpr>s�z'Refactoring.get_diff.<locals>.<genexpr>)rr$r%rO�_try_relative_tor*rR�values)r�textr-rErFrrrr/xs
  48  � zRefactoring.get_diffcCs:|����D]}|��q|��D]	\}}|�|�qdS)zU
  49          Applies the whole refactoring to the files, which includes renames.
  50          N)rRrUr6rO�rename)rr5�old�newrrrr6�s
  51  
  52  �zRefactoring.applyN)r)
  53  r8r:r;rrrr
  54  rRrrrOr/r6rrrrr<Rs
  55  	r<cCs4|j}|jdvr||j�|�fS||�||j�fS)N)z__init__.pyz__init__.pyi)�parent�name�joinpath�suffix)r%�new_nameZdir_rrr�_calculate_rename�s
  56  r_c	Cs�t�}i}|std��|D]N}|jj}|jdkr/|dur/|jdur/t|j�}|�t||��q
  57  t	|jt
  58  �rI|jj��D]}|�tt|�|��q;q
  59  |dur[|�
  60  |ji�}|j|||<q
  61  t|||�S)N�!There is no name under the cursor�module)�setr�_name�	tree_name�type�module_pathr�addr_�
  62  isinstancer	�_valueZ
  63  py__path__�
  64  setdefault�prefixr<)	r�definitionsr^Zfile_renamesZfile_tree_name_map�drdrDZfmaprrrrW�s$
  65  ��rWcCs�|std��tdd�|D��rtd��tdd�|D��r td��dd�|D�}t|�d	kr1td
  66  ��t|�dkr;td��t|�dkrEtd
  67  ��|d	j}|��}|jdkretddd��|j|j�}td|��t|jdd��dkrstd��|j	d}|jdkr�t|j	�dkr�|j	d}|dkr�|jdkr�td��td|j
  68  dd���|��}|j
  69  dd�}dd�|D�}	i}
  70  |	D]k}|j}|���
  71  �}|}
  72  |jdks�|jjtvs�|jjd kr�|j��dur�d!|d"}
  73  |
  74  �|i�}|}|j}|j}|jd k�r|j	d	d#k�r|jj	d	j}|}|jj	d|jj	�|��D]}d$||<�q||
  75  ||<q�|d	���
  76  �}|
  77  �|i�}t|��j�||<|��}|j�d%�d$k�rT|jd&k�sP|d'k�rTd$||<t||
  78  �S)(Nr`css�|]}|jdvVqdS))ra�	namespaceN)Zapi_type�rJ�nrrrrS���zinline.<locals>.<genexpr>z,Cannot inline imports, modules or namespacescss�|]}|jduVqdSr)rdrorrrrS�rqz!Cannot inline builtins/extensionscSsg|]	}|j��r|�qSr�rdZ
  79  is_definitionrorrr�
  80  <listcomp>��zinline.<locals>.<listcomp>rzNo definition found to inline�z.Cannot inline a name with multiple definitionsz$There are no references to this name�	expr_stmt�function�class)ZfuncdefZclassdefzCannot inline a %sT)Zinclude_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)Zinclude_prefixcSsg|]	}|j��s|�qSrrrrorrrrs�rtZtestlist_star_expr�trailer�(�)�.rz 	r2�;)r�anyrCrdZget_definitionre�dict�getZget_defined_names�childrenr"Zget_rhsZget_root_contextZ
  81  py__file__rZ�EXPRESSION_PARTSZget_next_siblingrjrk�index�_remove_indent_of_prefixZget_first_leafZ
  82  get_next_leaf�stripr<)r�namesrlrdrv�type_Zfirst_child�rhsZreplace_codeZ
  83  referencesr?r[r%�sZof_pathrprk�parZ	some_node�changesZ	next_leafrrr�inline�s�
  84  
  85  ��
  86  
  87  
  88  �
  89  ��
  90  
  91  r�cCsd�t|dd�dd��S)zT
  92      Removes the last indentation of a prefix, e.g. " \n \n " becomes " \n \n".
  93      rTrNr)r*r)rkrrrr��sr�r%�baserAcCs$z|�|�WSty|YSwr)r&r')r%r�rrrrTs
  94  �rT)r(�pathlibr�typingrrrZparsorZjedi.api.exceptionsrZjedi.inference.value.namespacer	�splitr�r
  95  r<r_rWr�r�rTrrrr�<module>s �B:R