B
    ¢èåS¨$  ã               @   s†   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„ deƒZG dd„ deƒZddd„Zdd„ ZdS )é    N)Ú
exceptionsÚ	pyobjectsÚpynamesÚ
taskhandleÚevaluateÚworderÚcodeanalyzeÚlibutils)Ú	ChangeSetÚChangeContentsÚMoveResource)Úoccurrencesc               @   s\   e Zd ZdZddd„Zdd„ Zddddde ¡ fdd	„Zd
d„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚRenamez£A class for performing rename refactoring

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

    Nc             C   s¾   || _ || _|dk	r\t | j|¡| _| j  | j¡}t ||¡\| _| _	| j	dkrºt
 d¡‚n^| ¡ st|jdkrt|j}t | j d¡}d| _tj||d| _	| ¡ rª|j| _n|jdd… | _dS )z:If `offset` is None, the `resource` itself will be renamedNzHRename refactoring should be performed on resolvable python identifiers.z__init__.pyÚ )Úresourceéýÿÿÿ)Úprojectr   r   Úget_name_atÚold_nameÚget_pymoduler   Zeval_location2Úold_instanceÚ
old_pynamer   ZRefactoringErrorÚ	is_folderÚnameÚparentr	   Zget_string_moduler   ZImportedModule)Úselfr   r   ÚoffsetZthis_pymoduleZdummy_pymodule© r   ú3lib/python3.7/site-packages/rope/refactor/rename.pyÚ__init__   s$    


zRename.__init__c             C   s   | j S )N)r   )r   r   r   r   Úget_old_name*   s    zRename.get_old_nameFc          	   C   s:  |dkr(t jdtdd |fdd„}|}|dk	rLt jdtdd |rL| jg}t| jƒr^| jg}|dkrp| j ¡ }td	| j	|f ƒ}	t
j| j| j	| j||| j|o¤|  ¡ d
}
| dt|ƒ¡}xF|D ]>}| |j¡ t|
||d}|dk	rö|	 t||ƒ¡ | ¡  qÀW |  ¡ r6| j ¡  ¡ }|  ||¡r6|  |||	¡ |	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`\s 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   s   | S )Nr   )Úvaluer   r   r   Úunsure_funcJ   s    z'Rename.get_changes.<locals>.unsure_funcNzA`in_file` argument has been deprecated; use `resources` instead. zRenaming <%s> to <%s>)ÚunsureÚdocsÚinstanceÚin_hierarchyzCollecting Changes)r   )ÚwarningsÚwarnÚDeprecationWarningr   Ú	_is_localr   r   Zget_python_filesr
   r   r   Úcreate_finderr   Ú	is_methodZcreate_jobsetÚlenZstarted_jobÚpathÚrename_in_moduleÚ
add_changer   Zfinished_jobÚ_is_renaming_a_moduleÚ
get_objectZget_resourceÚ_is_allowed_to_moveÚ_rename_module)r   Únew_nameZin_filer(   r%   r&   Ú	resourcesZtask_handler$   ÚchangesÚfinderZjob_setZfile_Znew_contentr   r   r   r   Úget_changes-   sF    






zRename.get_changesc             C   s<   |  ¡ r0y| d¡|kS  tjk
r,   dS X n||kS d S )Nz__init__.pyF)r   Z	get_childr   ZResourceNotFoundError)r   r8   r   r   r   r   r5   k   s    zRename._is_allowed_to_movec             C   s   t | j ¡ tjƒrdS dS )NTF)Ú
isinstancer   r4   r   ZAbstractModule)r   r   r   r   r3   t   s    zRename._is_renaming_a_modulec             C   s4   | j }t|tjƒo2t| ¡ tjƒo2t| ¡ jtjƒS )N)	r   r<   r   ÚDefinedNamer4   r   Z
PyFunctionr   ZPyClass)r   Úpynamer   r   r   r.   y   s    zRename.is_methodc             C   sF   |  ¡ s|d }|jj}|dkr&|}n|d | }| t||ƒ¡ d S )Nz.pyr   ú/)r   r   r0   r2   r   )r   r   r7   r9   Úparent_pathZnew_locationr   r   r   r6      s    zRename._rename_module)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r   ZNullTaskHandler;   r5   r3   r.   r6   r   r   r   r   r   	   s   
<	r   c               @   s2   e Zd ZdZdd„ Zdd„ Zdd„ Zd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             C   sB   || _ || _|| _t ||¡| _| | j¡| _t 	| j|¡| _
d S )N)r   r   r   r   r   r   r   Úpymoduler   Zeval_locationr   )r   r   r   r   r   r   r   r   —   s    zChangeOccurrences.__init__c             C   s   t  | j ¡ ¡}| | j¡S )N)r   ZWorderr   ÚreadZget_primary_atr   )r   Zword_finderr   r   r   r    Ÿ   s    zChangeOccurrences.get_old_namec             C   sD   | j j}| j  ¡  | | j¡¡}| | ¡ ¡}| | 	¡ ¡}||fS )N)
rF   ÚlinesÚ	get_scopeÚget_inner_scope_for_lineZget_line_numberr   Zget_line_startZ	get_startZget_line_endZget_end)r   rH   ÚscopeÚstartÚendr   r   r   Ú_get_scope_offset£   s    
z#ChangeOccurrences._get_scope_offsetFTc       
   	   C   sr   t d| j|f ƒ}|  ¡ \}}tj| j| j| jd|d}t||| jd||f||d}	|	d k	rn| 	t
| j|	ƒ¡ |S )Nz!Changing <%s> occurrences to <%s>F)ZimportsÚ
only_callsT)rF   Úreplace_primaryÚregionÚreadsÚwrites)r
   r   rN   r   r-   r   r   r1   rF   r2   r   r   )
r   r7   rO   rR   rS   r9   Zscope_startZ	scope_endr:   Znew_contentsr   r   r   r;   «   s    

zChangeOccurrences.get_changesN)FTT)rA   rB   rC   rD   r   r    rN   r;   r   r   r   r   rE   Š   s
   rE   FTc             C   s¼   |dk	r|  ¡ }n|j}t |¡}	x|  ||¡D ]€}
|rB|
 ¡ rBq0|rT|
 ¡ \}}n|
 ¡ \}}|sl|
 ¡ r0|sz|
 ¡ rzq0|dks¢|d |  krž|d k r0n q0|	 	|||¡ q0W |	 
¡ S )z;Returns the changed source or `None` if there is no changesNr   é   )rG   Úsource_coder   ZChangeCollectorZfind_occurrencesZis_a_fixed_primaryZget_primary_rangeZget_word_rangeZ
is_writtenr2   Zget_changed)Zoccurrences_finderr7   r   rF   rP   rQ   rR   rS   rU   Zchange_collectorZ
occurrencerL   rM   r   r   r   r1   º   s     

(r1   c             C   sl   |   ¡ \}}|d krdS | ¡  |¡}t| tjƒrD| ¡ dkrD|j}| ¡ dkoj| | ¡  	¡ kojt| tj
ƒS )NF)ÚFunctionZClassrV   )Zget_definition_locationrI   rJ   r<   r   r=   Zget_kindr   Z	get_namesÚvaluesZAssignedName)r>   ÚmoduleÚlinenorK   r   r   r   r,   Ò   s    r,   )NNFNTT)r)   Z	rope.baser   r   r   r   r   r   r   r	   Zrope.base.changer
   r   r   Zrope.refactorr   Úobjectr   rE   r1   r,   r   r   r   r   Ú<module>   s   ( 0  
