B
    –ìZ‚  ã               @   sê   d 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mZmZmZmZ ddd„ZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZdS )z‡A module containing classes for move refactoring

`create_move()` is a factory for creating move refactoring objects
based on inputs.

é    )Ú	pyobjectsÚcodeanalyzeÚ
exceptionsÚpynamesÚ
taskhandleÚevaluateÚworderÚlibutils)Ú	ChangeSetÚChangeContentsÚMoveResource)ÚimportutilsÚrenameÚoccurrencesÚsourceutilsÚfunctionutilsNc             C   sÄ   |dkrt | |ƒS |  |¡}t ||¡}|dk	r¶| ¡ }t|tjƒsPt|tjƒr^t | | 	¡ ƒS t|tj
ƒr„t|jtjƒr„t| ||ƒS t|tjƒržt|jtjƒsªt|tjƒr¶t| ||ƒS t d¡‚dS )z®A factory for creating Move objects

    Based on `resource` and `offset`, return one of `MoveModule`,
    `MoveGlobal` or `MoveMethod` for performing move refactoring.

    NzKMove only works on global classes/functions/variables, modules and methods.)Ú
MoveModuleÚget_pymoduler   Úeval_locationÚ
get_objectÚ
isinstancer   ZPyModuleZ	PyPackageÚget_resourceZ
PyFunctionÚparentÚPyClassÚ
MoveMethodÚPyDefinedObjectr   ÚAssignedNameÚ
MoveGlobalr   ÚRefactoringError)ÚprojectÚresourceÚoffsetÚthis_pymoduleÚpynameÚpyobject© r%   ú1lib/python3.7/site-packages/rope/refactor/move.pyÚcreate_move   s$    

r'   c               @   sŽ   e Zd ZdZdd„ Zdde ¡ fdd„Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zd dd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )!r   zÔFor moving methods

    It makes a new method in the destination class and changes
    the body of the old method to call the new method.  You can
    inline the old method to change all of its occurrences.

    c             C   sR   || _ | j  |¡}t ||¡}t ||¡| _| ¡ | _| j 	¡ dkrNt
 d¡‚d S )NÚmethodz!Only normal methods can be moved.)r   r   r   r   r   Úget_name_atÚmethod_namer   Ú
pyfunctionZget_kindr   r   )Úselfr   r    r!   r"   r#   r%   r%   r&   Ú__init__3   s    
zMoveMethod.__init__Nc             C   s   t d| j ƒ}|dkr | j ¡ }|dkr0|  ¡ }|  ||¡\}}}}	t | ¡ ¡}
|
 	|||	¡ |  
||¡\}}}}||krŒ|
 	|||¡ nrt | ¡ ¡}| 	|||¡ | ¡ }t | j¡}|  |¡}|ræt | j||¡}t|||ƒ}||krþ| 	t||ƒ¡ ||kr| 	t||
 ¡ ƒ¡ |S )aœ  Return the changes needed for this refactoring

        Parameters:

        - `dest_attr`: the name of the destination attribute
        - `new_name`: the name of the new method; if `None` uses
          the old name
        - `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.

        zMoving method <%s>N)r
   r*   r   Úget_python_filesÚget_method_nameÚ_get_changes_made_by_old_classr   ÚChangeCollectorÚreadÚ
add_changeÚ_get_changes_made_by_new_classÚget_changedr   ÚImportToolsÚ_get_used_importsr	   Úget_string_moduleÚ_add_imports_to_moduler   )r,   Ú	dest_attrÚnew_nameÚ	resourcesÚtask_handleÚchangesZ	resource1Zstart1Zend1Znew_content1Z
collector1Z	resource2Zstart2Zend2Znew_content2Z
collector2ÚresultÚimport_toolsÚnew_importsZgoal_pymoduler%   r%   r&   Úget_changes=   s6    



zMoveMethod.get_changesc             C   s   | j S )N)r*   )r,   r%   r%   r&   r/   l   s    zMoveMethod.get_method_namec             C   s   t  | j| j¡S )N)r   Zget_importsr   r+   )r,   r@   r%   r%   r&   r7   o   s    zMoveMethod._get_used_importsc             C   sT   | j  ¡ }|  | j ¡}d|||  ¡ f }t | j ¡}| ¡ |d |d t ||¡fS )Nzreturn self.%s.%s(%s)
r   é   )r+   Ú
get_moduleÚ_get_scope_indentsÚ_get_passed_arguments_stringr   Úget_body_regionr   Úfix_indentation)r,   r:   r;   ÚpymoduleÚindentsÚbodyZregionr%   r%   r&   r0   r   s    
z)MoveMethod._get_changes_made_by_old_classc             C   s*   |  ¡ }t |j| ¡  ¡ ¡t | j¡ S )N)rD   r   Zget_indentsÚlinesÚ	get_scopeÚ	get_startÚ
get_indentr   )r,   r$   rI   r%   r%   r&   rE   {   s    zMoveMethod._get_scope_indentsc             C   s¸   | j j}||krt d| ¡‚||  ¡  ¡ }t|tjƒsHt d| ¡‚| 	¡ }| 	¡  
¡ }t |¡\}}d}	|j||…  ¡ dkrŒd}	|}|  |¡}
|	t |  |¡|
¡ }||||fS )Nz$Destination attribute <%s> not foundz%Unknown class type for attribute <%s>Ú
Úpassz

)r+   r   r   r   r   Zget_typer   r   r   rD   r   r   rG   Úsource_codeÚstriprE   rH   Úget_new_method)r,   r:   r;   Zold_pyclassZpyclassrI   r    ÚstartÚendZ
pre_blanksrJ   rK   r%   r%   r&   r4      s&    


z)MoveMethod._get_changes_made_by_new_classc             C   s&   d|   |¡t |  ¡ t | j¡¡f S )Nz%s
%s)Ú_get_new_headerr   rH   Ú	_get_bodyrO   r   )r,   Únamer%   r%   r&   rT   –   s    
zMoveMethod.get_new_methodc             C   s   t  | j¡S )N)r   Zget_bodyr+   )r,   r%   r%   r&   Ú_get_unchanged_bodyœ   s    zMoveMethod._get_unchanged_bodyÚhostc             C   sl   |   ¡ }|d |  ¡  }t | j|¡}t | j||| ¡}tj|||d}|d krV|}|| 	d¡d d … S )Nz = None
)rI   rP   rC   )
Ú_get_self_namerZ   r	   r8   r   r   Úcreate_finderr   Úrename_in_moduleÚindex)r,   r[   Z	self_namerK   rI   Úfinderr?   r%   r%   r&   rX   Ÿ   s    zMoveMethod._get_bodyc             C   s   | j  ¡ d S )Nr   )r+   Zget_param_names)r,   r%   r%   r&   r\   ª   s    zMoveMethod._get_self_namec             C   sH   d| }|   ¡ r|d7 }tj | j¡}| d¡}|r@|d| 7 }|d S )Nzdef %s(selfz, hostrC   z, z):)Ú_is_host_usedr   ÚDefinitionInfor2   r+   Úarguments_to_string)r,   rY   ÚheaderÚdefinition_infoÚothersr%   r%   r&   rW   ­   s    
zMoveMethod._get_new_headerc             C   sD   d}|   ¡ rd}tj | j¡}| d¡}|r@|r8|d7 }||7 }|S )NÚ r,   rC   z, )ra   r   rb   r2   r+   rc   )r,   r?   re   rf   r%   r%   r&   rF   ·   s    
z'MoveMethod._get_passed_arguments_stringc             C   s   |   d¡|  ¡ kS )NZ
__old_self)rX   rZ   )r,   r%   r%   r&   ra   Ã   s    zMoveMethod._is_host_used)r[   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r   ÚNullTaskHandlerB   r/   r7   r0   rE   r4   rT   rZ   rX   r\   rW   rF   ra   r%   r%   r%   r&   r   *   s    
.	

r   c               @   sš   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	de
 ¡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ ZdS )#r   z&For moving global function and classesc             C   sª   || _ | j  |¡}t ||¡| _| jd kr4t d¡‚|  | j¡rTt 	||¡| _
|}n| j ¡  ¡ | _
| j ¡  ¡ }|  ¡  | ¡ | _t| j | j| j| j
ƒ| _| jj| _d S )NzBMove refactoring should be performed on a class/function/variable.)r   r   r   r   Ú
old_pynamer   r   Ú_is_variabler   r)   Úold_namer   Úget_namerD   Ú_check_exceptional_conditionsr   ÚsourceÚ
_MoveToolsÚtoolsr@   )r,   r   r    r!   r"   rI   r%   r%   r&   r-   Ê   s     


zMoveGlobal.__init__c                s”   t  | j¡‰t|jtjƒr4t‡fdd„|jjD ƒƒS t|jtj	ƒrdˆkr„ˆ 
dd¡\}‰ |jj|kr„t‡ fdd„|jjD ƒƒr„dS |jjˆkS dS )Nc             3   s   | ]\}}ˆ |kV  qd S )Nr%   )Ú.0rY   Úalias)Úmodule_namer%   r&   ú	<genexpr>ã   s   z,MoveGlobal._import_filter.<locals>.<genexpr>Ú.rC   c             3   s   | ]\}}ˆ |kV  qd S )Nr%   )ru   rY   rv   )Úbasenamer%   r&   rx   ê   s   TF)r	   Úmodnamerr   r   Úimport_infor   ÚNormalImportÚanyÚnames_and_aliasesÚ
FromImportÚrsplitrw   )r,   ÚstmtZpackage_namer%   )rz   rw   r&   Ú_import_filterÞ   s    zMoveGlobal._import_filterc             C   s~   |   | j¡rP| j ¡ d }y| ¡  | j¡ W qz tjk
rL   |  ¡  Y qzX n*t	| j 
¡ tjƒrr|  | j 
¡ ¡sz|  ¡  d S )Nr   )rn   rm   Úget_definition_locationrM   rp   ro   r   ZNameNotFoundErrorÚ_raise_refactoring_errorr   r   r   r   Ú
_is_global)r,   rI   r%   r%   r&   rq   ð   s    
z(MoveGlobal._check_exceptional_conditionsc             C   s   t  d¡‚d S )NzMMove refactoring should be performed on a global class, function or variable.)r   r   )r,   r%   r%   r&   r…   ü   s    z#MoveGlobal._raise_refactoring_errorc             C   s   |  ¡ j| ¡   ¡ kS )N)rM   r   rD   )r,   r$   r%   r%   r&   r†     s    zMoveGlobal._is_globalc             C   s   t |tjƒS )N)r   r   r   )r,   r#   r%   r%   r&   rn     s    zMoveGlobal._is_variableNc             C   s|   |d kr| j  ¡ }|d ks"| ¡ s,t d¡‚| ¡ rH| d¡rH| d¡}| ¡ rZt d¡‚| j|krnt d¡‚|  	|||¡S )Nz Move destination does not exist.z__init__.pyz7Move destination for non-modules should not be folders.z*Moving global elements to the same module.)
r   r.   Úexistsr   r   Ú	is_folderÚ	has_childZ	get_childrr   Ú_calculate_changes)r,   Údestr<   r=   r%   r%   r&   rB     s    


zMoveGlobal.get_changesc             C   sH  t d| j ƒ}| dt|ƒ¡}x"|D ]}| |j¡ || jkrT| |  |¡¡ nä||krn| |  	|¡¡ nÊ| j
j|dr8| j |¡}d| j }| j
j||d}	|	d k	}
| j
 ||	¡}| jj|d| jd}	|
r| j
 ||	¡}t | j||  |¡| j¡\}	}|	 ||¡}	| j
 ||	¡}	|	| ¡ kr8| t||	ƒ¡ | ¡  q&W |S )NzMoving global <%s>zCollecting Changes)r    z__rope_renaming_%s_F)ÚsortÚimport_filter)r
   ro   Úcreate_jobsetÚlenÚstarted_jobÚpathrr   r3   Ú_source_module_changesÚ_dest_module_changesrt   Úoccurs_in_moduler   r   r^   Únew_pymoduler@   Úorganize_importsrƒ   r   Ú
add_importÚ_new_modnameÚreplaceÚ
new_sourcer2   r   Úfinished_job)r,   r‹   r<   r=   r>   Újob_setZfile_rI   Úplaceholderrr   Úshould_importÚimportedr%   r%   r&   rŠ     s:    


zMoveGlobal._calculate_changesc             C   s¼   d| j  }t|ƒ}t | j| j | j¡}|  ¡ \}}t|| j|||ƒ}| 	¡ }t
 | j|| j¡}	| jj|	dd}|jr°t
 | j|| j¡}	t | j|	|  |¡| j ¡\}}
| ||
¡}t| j|ƒS )Nz__rope_moving_%s_F)rŒ   )ro   Ú_ChangeMoveOccurrencesHandler   r]   r   rm   Ú_get_moving_regionÚModuleSkipRenamerrr   Úget_changed_moduler	   r8   r@   r–   Úoccurredr   r—   r˜   r™   r   )r,   r‹   r   ÚhandleÚoccurrence_finderrU   rV   Zrenamerrr   rI   rŸ   r%   r%   r&   r’   :  s"    

z!MoveGlobal._source_module_changesc             C   s
   t  |¡S )N)r	   r{   )r,   r‹   r%   r%   r&   r˜   N  s    zMoveGlobal._new_modnamec             C   sD  | j  |¡}| j | j|¡}| j ||¡}|  ¡ \}}|  ||¡\}}| j 	|¡}|j
}d}|jrt|jd jd }n6x4||j ¡ k r¨|j |d ¡ ¡  d¡r¨|d7 }qvW |dkrä|j |¡d }	|d |	… d | ||	d …  }
n|| }
|
}t | j ||¡}| jj|ddd}t | j ||¡}| jj|ddd| jd	}t||ƒS )
Nr   éÿÿÿÿrC   ú#z

F)rŒ   ÚunusedT)rŒ   Úselfsr©   r   )r   r   rt   r^   ro   r•   Ú _get_moving_element_with_importsÚ_add_imports2r@   Úmodule_importsrR   ÚimportsÚend_linerL   ÚlengthÚget_lineÚlstripÚ
startswithÚget_line_endr	   r8   r–   rƒ   r   )r,   r‹   rI   rr   Úmovingr®   Zhas_changedÚmodule_with_importsÚlinenoZcutr?   r%   r%   r&   r“   Q  s6    "

zMoveGlobal._dest_module_changesc             C   s   t | j| j|  ¡ ƒS )N)Úmoving_code_with_importsr   rr   Ú_get_moving_element)r,   r%   r%   r&   r«   v  s    z+MoveGlobal._get_moving_element_with_importsc             C   s   t  | j||¡}| j |¡S )N)r	   r8   r   r@   r­   )r,   rR   r    rI   r%   r%   r&   Ú_get_module_with_importsz  s    z#MoveGlobal._get_module_with_importsc             C   s*   |   ¡ \}}| j ¡ ||… }| ¡ d S )NrP   )r¡   rr   r2   Úrstrip)r,   rU   rV   rµ   r%   r%   r&   r¹     s    zMoveGlobal._get_moving_elementc       
      C   s  | j  | j¡}|j}|  | j¡rX|j}| | j ¡ d ¡d }| 	|¡}| |¡d }n$| j 
¡  ¡ }| 	| ¡ ¡}| ¡ }| |¡}x(|dkr®| |d ¡ d¡r®|d8 }qˆW | 	|¡}x.|| ¡ k rè| |d ¡ ¡ dkrè|d7 }q¼W t| |¡d t|jƒƒ}	||	fS )NrC   r   r¨   rg   )r   r   rr   rL   rn   rm   Úlogical_linesZlogical_line_inr„   Úget_line_startr   rM   rN   Zget_endZget_line_numberr±   r³   r°   rS   Úminr´   r   rR   )
r,   rI   rL   r¼   r·   rU   r¯   ZscopeZ
start_linerV   r%   r%   r&   r¡   „  s(    


zMoveGlobal._get_moving_regionc             C   sB   | j  ||¡}|d kr|dfS | ¡ }t | j||¡}|dfS d S )NFT)rt   Úadd_importsr   r	   r8   r   )r,   rI   rA   rr   r    r%   r%   r&   r¬   ž  s    zMoveGlobal._add_imports2)rh   ri   rj   rk   r-   rƒ   rq   r…   r†   rn   r   rl   rB   rŠ   r’   r˜   r“   r«   rº   r¹   r¡   r¬   r%   r%   r%   r&   r   Ç   s$   "%r   c               @   sd   e Zd ZdZdd„ Zde ¡ fdd„Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zddd„Zdd„ Zdd„ ZdS )r   zFor moving modules and packagesc             C   s¶   || _ | ¡ s|jdkr|j}| ¡ r:| d¡s:t d¡‚t | j d¡}t	j
||d| _| j ¡  ¡ | _| j ¡ r~| jj| _n| jjd d… | _t| j | j| j| jƒ| _| jj| _d S )Nz__init__.pyzCannot move non-package folder.rg   )r    éýÿÿÿ)r   rˆ   rY   r   r‰   r   r   r	   r8   r   ZImportedModulerm   r   r   rr   ro   rs   rt   r@   )r,   r   r    Zdummy_pymoduler%   r%   r&   r-   ¬  s     


zMoveModule.__init__Nc             C   s:   |d kr| j  ¡ }|d ks"| ¡ s,t d¡‚|  |||¡S )Nz0Move destination for modules should be packages.)r   r.   rˆ   r   r   rŠ   )r,   r‹   r<   r=   r%   r%   r&   rB   ¿  s    
zMoveModule.get_changesc             C   s¤   t d| j ƒ}| dt|ƒ¡}x^|D ]V}| |j¡ || jkrL|  ||¡ n&| j||d}|d k	rr| 	t
||ƒ¡ | ¡  q$W | j| jjkr | 	t| j|jƒ¡ |S )NzMoving module <%s>zCollecting changes)r    )r
   ro   rŽ   r   r   r‘   rr   Ú_change_moving_moduleÚ_change_occurrences_in_moduler3   r   r›   r   r   )r,   r‹   r<   r=   r>   rœ   Úmodulerr   r%   r%   r&   rŠ   È  s    


zMoveModule._calculate_changesc             C   s"   t  |¡}|r|d | j S | jS )Nry   )r	   r{   ro   )r,   r‹   Zdestnamer%   r%   r&   r˜   Ú  s    
zMoveModule._new_modnamec             C   s   t  |  |¡d fg¡S )N)r   r}   r˜   )r,   r‹   r%   r%   r&   Ú_new_importà  s    zMoveModule._new_importc             C   sp   | j  ¡ sl| j | j ¡}| j |¡}| j ||¡}|  ||¡}| j 	||¡}|| j  
¡ krl| t| j |ƒ¡ d S )N)rr   rˆ   r   r   r@   Úrelatives_to_absolutesrt   r•   rÂ   rš   r2   r3   r   )r,   r>   r‹   rI   rr   r%   r%   r&   rÁ   ã  s    
z MoveModule._change_moving_modulec       
      C   s,  | j j||dsd S |d kr(| j |¡}|  |¡}t | j|¡}d}d }t |¡rˆ|  	|||¡}|rˆ| 
¡ }| j  ||¡}| j  ||¡}|  |¡}| j j|d||s¦|nd d}| j j||dd}	| j  ||¡}| j  |¡}|	rü| j  ||¡}| j  ||g¡}| j  ||¡}|d k	r(||j ¡ kr(|S d S )N)rI   r    FT)r®   rI   r    )rI   r    r®   )rt   r”   r   r   r˜   r   Zget_module_importsr	   r{   Ú_change_import_statementsÚget_changed_sourcerš   r•   rÄ   r^   Úremove_old_importsr¿   r    r2   )
r,   r‹   rI   r    r;   r­   Úchangedrr   Ú
new_importrž   r%   r%   r&   rÂ   í  s>    


z(MoveModule._change_occurrences_in_modulec       	         sÄ   ˆ j }|j}d}x®|jD ]¤}t‡ fdd„|jjD ƒƒsRt‡ fdd„|jjD ƒƒsRqt|jtjƒrbqˆ  	||||¡pt|}tj
 ˆ jd ¡}|j ¡ s|j |¡|krt ||jj|jj¡|_d}qW |S )NFc             3   s   | ]}|d  ˆ j kV  qdS )r   N)ro   )ru   Úname_and_alias)r,   r%   r&   rx     s   z7MoveModule._change_import_statements.<locals>.<genexpr>c             3   s"   | ]}|d  t  ˆ j¡kV  qdS )r   N)r	   r{   rr   )ru   rË   )r,   r%   r&   rx     s   T)rr   r   r®   r~   r|   r   r   r   r}   Ú"_handle_moving_in_from_import_stmtÚ
importinfoÚImportContextr   Zis_emptyÚget_imported_resourcer€   Úlevel)	r,   r‹   r;   r­   Zmoving_moduleÚparent_modulerÉ   Úimport_stmtÚcontextr%   )r,   r&   rÆ     s,    

z$MoveModule._change_import_statementsc             C   s¼   d}t j | jd ¡}|j |¡|kr¸|jj}g }xT|D ]L\}	}
|	| jkrtd}t  t	 
|¡d| j|
fg¡}| |¡ q6| |	|
f¡ q6W ||kr¸d}|r°t  |jj|jj|¡|_n| ¡  |S )NFTr   )r   rÍ   rÎ   r   r|   rÏ   r   ro   r€   r	   r{   r—   Úappendrw   rÐ   Zempty_import)r,   r‹   rÒ   r­   rÑ   rÉ   rÓ   r®   rA   rY   rv   rÊ   r%   r%   r&   rÌ   2  s.    



z-MoveModule._handle_moving_in_from_import_stmt)NN)rh   ri   rj   rk   r-   r   rl   rB   rŠ   r˜   rÄ   rÁ   rÂ   rÆ   rÌ   r%   r%   r%   r&   r   ©  s   
 
""r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )r    c             C   s   || _ d| _d S )NF)r;   r¤   )r,   r;   r%   r%   r&   r-   T  s    z%_ChangeMoveOccurrencesHandle.__init__c             C   s   d S )Nr%   )r,   Úchange_collectorÚ
occurrencer%   r%   r&   Úoccurred_inside_skipX  s    z1_ChangeMoveOccurrencesHandle.occurred_inside_skipc             C   s&   |  ¡ \}}| ||| j¡ d| _d S )NT)Úget_primary_ranger3   r;   r¤   )r,   rÕ   rÖ   rU   rV   r%   r%   r&   Úoccurred_outside_skip[  s    z2_ChangeMoveOccurrencesHandle.occurred_outside_skipN)rh   ri   rj   r-   r×   rÙ   r%   r%   r%   r&   r    R  s   r    c               @   sP   e Zd Zdd„ Zdd„ Zddd„Zdd
d„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dS )rs   c             C   s*   || _ || _|| _|| _t | j ¡| _d S )N)r   rr   rm   ro   r   r6   r@   )r,   r   rr   r#   ro   r%   r%   r&   r-   c  s
    z_MoveTools.__init__c                sP   ˆ j }ˆj ˆ ¡}G ‡ ‡fdd„dtƒ}|ƒ }| |¡ | ¡ }||krL|S d S )Nc                   s(   e Zd ZdZ”jZ”jZ‡ fdd„ZdS )z0_MoveTools.remove_old_imports.<locals>.CanSelectFc                sJ   y.|| j kr,ˆ |  ¡ | j ¡ kr,d| _dS W n tjk
rD   Y nX dS )NTF)ro   r   rm   rÉ   r   ZAttributeNotFoundError)r,   rY   )rI   r%   r&   Ú__call__s  s    

z9_MoveTools.remove_old_imports.<locals>.CanSelect.__call__N)rh   ri   rj   rÉ   ro   rm   rÚ   r%   )rI   r,   r%   r&   Ú	CanSelectn  s   rÛ   )rR   r@   r­   ÚobjectZfilter_namesrÇ   )r,   rI   Z
old_sourcer¶   rÛ   Z
can_selectrš   r%   )rI   r,   r&   rÈ   j  s    
z_MoveTools.remove_old_importsNFc             C   s"   |   |¡}tj||d||d}|S )NT)Zreplace_primaryrI   r    )Ú_create_finderr   r^   )r,   r;   rI   r®   r    r¦   rr   r%   r%   r&   r^   ƒ  s
    

z_MoveTools.rename_in_moduleTc             C   s(   |   |¡}x|j||dD ]}dS W dS )N)rI   r    TF)rÝ   Úfind_occurrences)r,   rI   r    r®   r`   rÖ   r%   r%   r&   r”   ‹  s
    
z_MoveTools.occurs_in_modulec             C   s   t j| j| j| j|ddS )NF)r®   Úkeywords)r   r]   r   ro   rm   )r,   r®   r%   r%   r&   rÝ   ’  s    z_MoveTools._create_finderc             C   s    |d k	rt  | j|| ¡ ¡S |S )N)r	   r8   r   r   )r,   rI   rr   r%   r%   r&   r•   —  s    z_MoveTools.new_pymodulec             C   s   |d kr|j S |S )N)rR   )r,   rI   rr   r%   r%   r&   rš     s    z_MoveTools.new_sourcec             C   s   t | j||ƒS )N)r9   r@   )r,   rI   rA   r%   r%   r&   r¿   ¢  s    z_MoveTools.add_imports)NFN)NNT)rh   ri   rj   r-   rÈ   r^   r”   rÝ   r•   rš   r¿   r%   r%   r%   r&   rs   a  s    

rs   c             C   s*   |   |¡}x|D ]}| |¡ qW | ¡ S )N)r­   r—   rÇ   )r@   rI   rA   r¶   rÊ   r%   r%   r&   r9   ¦  s    

r9   c             C   sº  t  | ¡}t | ||¡}t |¡}d}x(|| ¡ k rN| |¡ d¡rN|d7 }q(W |d | 	|¡… }t | || 	|¡d … |¡}|  
|¡}g }	x | |¡jD ]}
|	 |
j¡ qšW g }x|D ]}||kr¸| |¡ q¸W |	 | ||¡¡ t|||	ƒ}t | ||¡}| |¡}t | ||¡}|j|dd}t | ||¡}| |¡}dd„ |jD ƒ}	d}|jrd|jd j}t |¡}x,|| ¡ k rš| |¡ ¡ sš|d7 }qpW ||| 	|¡d …  }||	fS )NrC   r¨   F)rª   c             S   s   g | ]
}|j ‘qS r%   )r|   )ru   rÒ   r%   r%   r&   ú
<listcomp>Ñ  s   z,moving_code_with_imports.<locals>.<listcomp>r§   )r   r6   r	   r8   r   ZSourceLinesAdapterr°   r±   r³   r½   r   r­   r®   rÔ   r|   Zget_from_importr9   rÅ   r–   r¯   rS   )r   r    rr   r@   rI   rL   rU   Zmoving_prefixÚoriginr®   r‚   Z
back_namesrY   r­   rµ   r%   r%   r&   r¸   ­  sF    







 r¸   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚModuleSkipRenamerHandlec             C   s   d S )Nr%   )r,   rÕ   rÖ   r%   r%   r&   rÙ   á  s    z-ModuleSkipRenamerHandle.occurred_outside_skipc             C   s   d S )Nr%   )r,   rÕ   rÖ   r%   r%   r&   r×   ä  s    z,ModuleSkipRenamerHandle.occurred_inside_skipN)rh   ri   rj   rÙ   r×   r%   r%   r%   r&   râ   ß  s   râ   c               @   s"   e Zd ZdZd	dd„Zdd„ ZdS )
r¢   z™Rename occurrences in a module

    This class can be used when you want to treat a region in a file
    separately from other parts when renaming.

    Nr   rg   c             C   s:   || _ || _|| _|| _|| _|| _| jdkr6tƒ | _dS )zˆConstructor

        if replacement is `None` the region is not changed.  Otherwise
        it is replaced with `replacement`.

        N)r¦   r    Ú
skip_startÚskip_endÚreplacementr¥   râ   )r,   r¦   r    r¥   rã   rä   rå   r%   r%   r&   r-   ð  s    
zModuleSkipRenamer.__init__c             C   s°   | j  ¡ }t |¡}| jd k	r2| | j| j| j¡ x\| j 	| j ¡D ]J}| 
¡ \}}| j|  krj| jk r~n n| j ||¡ qB| j ||¡ qBW | ¡ }|d k	r¬||kr¬|S d S )N)r    r2   r   r1   rå   r3   rã   rä   r¦   rÞ   rØ   r¥   r×   rÙ   r5   )r,   rr   rÕ   rÖ   rU   rV   r?   r%   r%   r&   r£     s    


z$ModuleSkipRenamer.get_changed_module)Nr   r   rg   )rh   ri   rj   rk   r-   r£   r%   r%   r%   r&   r¢   è  s    
r¢   )N)rk   Z	rope.baser   r   r   r   r   r   r   r	   Zrope.base.changer
   r   r   Zrope.refactorr   r   r   r   r   r'   rÜ   r   r   r   r    rs   r9   r¸   râ   r¢   r%   r%   r%   r&   Ú<module>   s    (
  c *E2	