
    c$                         d dl Z d dlmZmZmZmZmZmZmZm	Z	 d dl
mZmZmZ d dlmZ  G d d          Z G d d          Z	 	 	 	 	 	 ddZd ZdS )    N)codeanalyzeevaluate
exceptionslibutilspynames	pyobjects
taskhandleworder)ChangeContents	ChangeSetMoveResource)occurrencesc                   V    e Zd ZdZddZd Zdddddej        fdZd Z	d Z
d	 Zd
 ZdS )RenamezA class for performing rename refactoring

    It can rename everything: classes, functions, modules, packages,
    methods, variables and keyword arguments.

    Nc                 P   || _         || _        |}t          j        | j        |          | _        | j                             | j                  }t          j        ||          \  | _        | _	        | j	        t          j        d          dS |                                s|j        dk    r|j        }t          j        | j         d          }d| _        t#          j        ||          | _	        |                                r|j        | _        dS |j        dd         | _        dS )z:If `offset` is None, the `resource` itself will be renamedNzHRename refactoring should be performed on resolvable python identifiers.__init__.py resource)projectr   r
   get_name_atold_nameget_pymoduler   eval_location2old_instance
old_pynamer   RefactoringError	is_foldernameparentr   get_string_moduler   ImportedModule)selfr   r   offsetthis_pymoduledummy_pymodules         4lib/python3.11/site-packages/rope/refactor/rename.py__init__zRename.__init__   s+     	3".t}fEEDM L55dmDDM191Hv2 2.Dt   19    %%'' +HM],J +#?%7bIIN $D%4^hWWWDO!!## 3 ( (crc 2    c                     | j         S N)r   r$   s    r(   get_old_namezRename.get_old_name3   s
    }r*   Fc           
         |dv r#t          j        dt          d           |fd}|}|&t          j        dt          d           |r| j        g}t	          | j                  r| j        g}|| j                                        }t          d| j	         d	| d
          }	t          j        | j        | j	        | j        ||| j        |o|                                           }
|                    dt          |                    }|D ]g}|                    |j                   t%          |
||          }|#|	                    t)          ||                     |                                 h|                                 rX| j                                                                        }|                     ||          r|                     |||	           |	S )a  Get the changes needed for this refactoring

        Parameters:

        - `in_hierarchy`: when renaming a method this keyword forces
          to rename all matching methods in the hierarchy
        - `docs`: when `True` rename refactoring will rename
          occurrences in comments and strings where the name is
          visible.  Setting it will make renames faster, too.
        - `unsure`: decides what to do about unsure occurrences.
          If `None`, they are ignored.  Otherwise `unsure` is
          called with an instance of `occurrence.Occurrence` as
          parameter.  If it returns `True`, the occurrence is
          considered to be a match.
        - `resources` can be a list of `rope.base.resources.File` to
          apply this refactoring on.  If `None`, the restructuring
          will be applied to all python files.
        - `in_file`: this argument has been deprecated; use
          `resources` instead.

        )TFz@unsure parameter should be a function that returns True or False   )
stacklevelc                     | S r,    )values    r(   unsure_funcz'Rename.get_changes.<locals>.unsure_func\   s    r*   NzA`in_file` argument has been deprecated; use `resources` instead. z
Renaming <z> to <>)unsuredocsinstancein_hierarchyzCollecting Changesr   )warningswarnDeprecationWarningr   	_is_localr   r   get_python_filesr   r   r   create_finderr   	is_methodcreate_jobsetlenstarted_jobpathrename_in_module
add_changer   finished_job_is_renaming_a_module
get_objectget_resource_is_allowed_to_move_rename_module)r$   new_namein_filer:   r7   r8   	resourcestask_handler5   changesfinderjob_setfile_new_contentr   s                  r(   get_changeszRename.get_changes6   s*   > ]" 
	!MU"    #)     !F 	,MV"   
  ,!]O	T_%% 	(I 	85577IIIIhIIIJJ*LMO&%:$..*:*:
 
 
 ++,@#i..QQ 	# 	#E
+++*68eLLLK G"">%#E#EFFF  """"%%'' 	A1133@@BBH''	8<< A##Hh@@@r*   c                     |                                 r.	 |                    d          |v S # t          j        $ r Y dS w xY w||v S )Nr   F)r   	get_childr   ResourceNotFoundError)r$   rP   r   s      r(   rL   zRename._is_allowed_to_move   sf     	)))-88IEE3   uu y((s   - A A c                 d    t          | j                                        t          j                  S r,   )
isinstancer   rJ   r   AbstractModuler-   s    r(   rI   zRename._is_renaming_a_module   s#    $/4466	8PQQQr*   c                     | j         }t          |t          j                  o\t          |                                t
          j                  o0t          |                                j        t
          j                  S r,   )	r   r\   r   DefinedNamerJ   r   
PyFunctionr!   PyClass)r$   pynames     r(   rA   zRename.is_method   se    vw233 J6,,..	0DEEJ6,,..5y7HII	
r*   c                     |                                 s|dz   }|j        j        }|dk    r|}n|dz   |z   }|                    t	          ||                     d S )Nz.pyr   /)r   r!   rE   rG   r   )r$   r   rN   rR   parent_pathnew_locations         r(   rM   zRename._rename_module   sq    !!## 	(%'Ho*" 	8#LL&,x7L<,??@@@@@r*   r,   )__name__
__module____qualname____doc__r)   r.   r	   DEFAULT_TASK_HANDLErW   rL   rI   rA   rM   r3   r*   r(   r   r      s         3 3 3 34   2K K K KZ) ) )R R R
 
 
A A A A Ar*   r   c                   ,    e Zd ZdZd Zd Zd Zd	dZdS )
ChangeOccurrencesa  A class for changing the occurrences of a name in a scope

    This class replaces the occurrences of a name.  Note that it only
    changes the scope containing the offset passed to the constructor.
    What's more it does not have any side-effects.  That is for
    example changing occurrences of a module does not rename the
    module; it merely replaces the occurrences of that module in a
    scope with the given expression.  This class is useful for
    performing many custom refactorings.

    c                     || _         || _        || _        t          j        ||          | _        |                    | j                  | _        t          j	        | j        |          | _
        d S r,   )r   r   r%   r
   r   r   r   pymoduler   eval_locationr   )r$   r   r   r%   s       r(   r)   zChangeOccurrences.__init__   s\     *8V<<,,T];;"0GGr*   c                     t          j        | j                                                  }|                    | j                  S r,   )r
   Worderr   readget_primary_atr%   )r$   word_finders     r(   r.   zChangeOccurrences.get_old_name   s5    mDM$6$6$8$899))$+666r*   c                     | j                                                             | j                  }|                                S r,   )ro   	get_scopeget_inner_scope_for_offsetr%   
get_region)r$   scopes     r(   _get_scope_offsetz#ChangeOccurrences._get_scope_offset   s7    ''))DDT[QQ!!!r*   FTc           	      H   t          d| j         d| d          }|                                 \  }}t          j        | j        | j        | j        d|          }t          ||| j        d||f||          }	|	(|	                    t          | j        |	                     |S )Nz
Changing <z> occurrences to <r6   F)imports
only_callsT)ro   replace_primaryregionreadswrites)r   r   r{   r   r@   r   r   rF   ro   rG   r   r   )
r$   rN   r~   r   r   rR   scope_start	scope_endrS   new_contentss
             r(   rW   zChangeOccurrences.get_changes   s    UUU(UUUVV!%!7!7!9!9Y*LMO!
 
 
 (] +
 
 
  	L~dm\JJKKKr*   N)FTT)rg   rh   ri   rj   r)   r.   r{   rW   r3   r*   r(   rm   rm      sb        
 
H H H7 7 7" " "     r*   rm   FTc                    ||                                 }n|j        }t          j        |          }	|                     ||          D ]}
|r|
                                r|r|
                                \  }}n|
                                \  }}|s|
                                r|s|
                                rw||d         |cxk    r|d         k     rn |		                    |||           |	
                                S )z;Returns the changed source or `None` if there is no changesNr      )rs   source_coder   ChangeCollectorfind_occurrencesis_a_fixed_primaryget_primary_rangeget_word_range
is_writtenrG   get_changed)occurrences_finderrN   r   ro   r   r   r   r   r   change_collector
occurrencestartends                r(   rF   rF      sO     +mmoo*"2;??(99(HMM > >
 	z<<>> 	 	5#5577JE33#2244JE3 	j3355 		%0022	  	>VAY% 	> 	> 	> 	>&) 	> 	> 	> 	> 	>''sH==='')))r*   c                    |                                  \  }}|dS |                                                    |          }t          | t          j                  r|                                dv r|j        }|                                dk    oA| |                                	                                v ot          | t          j
                  S )NF)FunctionClassr   )get_definition_locationrw   get_inner_scope_for_liner\   r   r_   get_kindr!   	get_namesvaluesAssignedName)rb   modulelinenorz   s       r(   r>   r>      s    3355NFF u77??E&'-.. 5>>3C3C H 4  J& 	5eoo''..000	5vw344r*   )NNFNTT)r;   	rope.baser   r   r   r   r   r   r	   r
   rope.base.changer   r   r   rope.refactorr   r   rm   rF   r>   r3   r*   r(   <module>r      sI   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 E D D D D D D D D D % % % % % %NA NA NA NA NA NA NA NAb2 2 2 2 2 2 2 2p 
* * * *@    r*   