
    Æe                    \   d Z ddlmZ ddlZddlmZmZm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lmZ ej8                  rdd	lmZmZ dd
Z G d d      Z  G d d      Z! G d d      Z" G d d      Z# G d d      Z$d Z%d Z& G d d      Z' G d d      Z(y)zA module containing classes for move refactoring

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

    )annotationsN)OptionalListUnion)codeanalyzeevaluate
exceptionslibutilspynames	pyobjects
taskhandleworder)ChangeContents	ChangeSetMoveResource)functionutilsimportutilsoccurrencesrenamesourceutils)*get_first_decorator_or_function_start_line)project	resourcesc                   |t        | |      S  | j                  |      }t        j                  ||      }||j	                         }t        |t        j                        st        |t        j                        rt        | |j                               S t        |t        j                        r1t        |j                  t        j                        rt        | ||      S t        |t        j                        r$t        |j                  t        j                        st        |t        j                         rt#        | ||      S t%        j&                  d      )zA factory for creating Move objects

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

    zKMove only works on global classes/functions/variables, modules and methods.)
MoveModuleget_pymoduler   eval_location
get_object
isinstancer   PyModule	PyPackageget_resource
PyFunctionparentPyClass
MoveMethodPyDefinedObjectr   AssignedName
MoveGlobalr	   RefactoringError)r   resourceoffsetthis_pymodulepynamepyobjects         4/usr/lib/python3/dist-packages/rope/refactor/move.pycreate_mover1       s    ~'8,,(G((2M##M6:F$$&h	 2 23zi))8
 gx'<'<'>??h	 4 45*OOY..;
 gx88x!:!:;8??I,>,>?&'"6"67gx88

%
%U     c                      e Zd ZdZd Zddej                  fdZd Zd Z	d Z
d Zd	 Zd
 Zd ZddZd Zd Zd Zd Zy)r&   zFor 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                8   || _         | j                   j                  |      }t        j                  ||      }t	        j
                  ||      | _        |j                         | _        | j                  j                         dk7  rt        j                  d      y )Nmethodz!Only normal methods can be moved.)r   r   r   r   r   get_name_atmethod_namer   
pyfunctionget_kindr	   r*   )selfr   r+   r,   r-   r.   s         r0   __init__zMoveMethod.__init__I   s    11(;''v>!--h? ++-??##%1--.QRR 2r2   Nc                p   t        d| j                  z        }|| j                  j                         }|| j	                         }| j                  ||      \  }}}}	t        j                  |j                               }
|
j                  |||	       | j                  ||      \  }}}}||k(  r|
j                  |||       nt        j                  |j                               }|j                  |||       |j                         }t        j                  | j                        }| j                  |      }|r.t        j                   | j                  ||      }t#        |||      }||v r|j                  t%        ||             ||v r)|j                  t%        ||
j                                      |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` to
          apply this refactoring on.  If `None`, the restructuring
          will be applied to all python files.

        zMoving method <%s>)r   r7   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_namer   task_handlechanges	resource1start1end1new_content1
collector1	resource2start2end2new_content2
collector2resultimport_toolsnew_importsgoal_pymodules                       r0   get_changeszMoveMethod.get_changesR   s   & 043C3CCD557I++-H040S0Sx1
-	64 !001AB
fdL9040S0Sx1
-	64 	!!!&$=$44Y^^5EFJ!!&$=++-F&224<<@L00>K ( : :LL&)! 0 - I%"">)V#DE	!~i9O9O9QRSr2   c                    | j                   S N)r7   r:   s    r0   r>   zMoveMethod.get_method_name   s    r2   c                V    t        j                  | j                  | j                        S r]   )r   get_importsr   r8   )r:   rX   s     r0   rF   zMoveMethod._get_used_imports   s    &&t||T__EEr2   c                H   | j                   j                         }| j                  | j                         }dj                  ||| j	                               }t        j                  | j                         }|j                         |d   |d   t        j                  ||      fS )Nzreturn self.{}.{}({})
r      )	r8   
get_module_get_scope_indentsformat_get_passed_arguments_stringr   get_body_regionr"   fix_indentation)r:   rI   rJ   pymoduleindentsbodyregions          r0   r?   z)MoveMethod._get_changes_made_by_old_class   s    ??--/))$//:(//--/

 ,,T__=!!#1I1I''g6	
 	
r2   c                    |j                         }t        j                  |j                  |j	                         j                               t        j                  | j                        z   S r]   )rc   r   get_indentslines	get_scope	get_start
get_indentr   )r:   r/   ri   s      r0   rd   zMoveMethod._get_scope_indents   sU    &&(&&NNH..0::<
""4<<01 	1r2   c                b   | j                   j                  }||vrt        j                  d|z        ||   j	                         j                         }t        |t        j                        st        j                  d|z        |j                         }|j                         j                         }t        j                  |      \  }}d}	|j                  || j                         dk7  rd}	|}| j                  |      }
|	t        j                   | j#                  |      |
      z   }||||fS )Nz$Destination attribute <%s> not foundz%Unknown class type for attribute <%s>
pass

)r8   r$   r	   r*   r   get_typer   r   r%   rc   r"   r   rg   source_codestriprd   rh   get_new_method)r:   rI   rJ   old_pyclasspyclassri   r+   startend
pre_blanksrj   rk   s               r0   rC   z)MoveMethod._get_changes_made_by_new_class   s-   oo,,K'--6B  i(335>>@'9#4#45--7)C  %%'%%'446 009
s
c*002f<JE))'2K77)7
 
 T))r2   c           	         dj                  | j                  |      t        j                  | j	                         t        j
                  | j                                    S )Nz{}
{})re   _get_new_headerr   rh   	_get_bodyrr   r   )r:   names     r0   rz   zMoveMethod.get_new_method   sJ      &'' +"8"8"F
 	
r2   c                @    t        j                  | j                        S r]   )r   get_bodyr8   r^   s    r0   _get_unchanged_bodyzMoveMethod._get_unchanged_body   s    ##DOO44r2   c                <   | j                         }|dz   | j                         z   }t        j                  | j                  |      }t        j                  | j                  |||         }t        j                  |||      }||}||j                  d      dz   d  S )Nz = None
)ri   rt   rb   )
_get_self_namer   r
   rG   r   r   create_finderr   rename_in_moduleindex)r:   host	self_namerk   ri   finderrW   s          r0   r   zMoveMethod._get_body   s    '')	;&)A)A)CC--dllDA**4<<HYDWX((I>Ffll4(1,.//r2   c                <    | j                   j                         d   S Nr   )r8   get_param_namesr^   s    r0   r   zMoveMethod._get_self_name   s    ..033r2   c                    d|z  }| j                         r|dz  }t        j                  j                  | j                        }|j                  d      }|r|d|z   z  }|dz   S )Nzdef %s(selfz, hostrb   , z):_is_host_usedr   DefinitionInforA   r8   arguments_to_string)r:   r   headerdefinition_infootherss        r0   r   zMoveMethod._get_new_header   sg    %hF'66;;DOOL 44Q7dVm#F}r2   c                    d}| j                         rd}t        j                  j                  | j                        }|j                  d      }|r|r|dz  }||z  }|S )N r:   rb   r   r   )r:   rW   r   r   s       r0   rf   z'MoveMethod._get_passed_arguments_string   s`    F'66;;DOOL 44Q7$fFr2   c                F    | j                  d      | j                         k7  S )N
__old_self)r   r   r^   s    r0   r   zMoveMethod._is_host_used   s    ~~l+t/G/G/IIIr2   )r   )__name__
__module____qualname____doc__r;   r   DEFAULT_TASK_HANDLEr[   r>   rF   r?   rd   rC   rz   r   r   r   r   rf   r    r2   r0   r&   r&   @   sb    S 225n F
 1*0
504
Jr2   r&   c                      e Zd ZU dZded<   d Zd Zd Zd Zd Z	d	 Z
d
ej                  f	 	 	 ddZd Zd Zd Zd Zd Zd Zd Zd Zd Zy
)r)   z&For moving global function and classeszproject.Projectr   c                   || _         | j                   j                  |      }t        j                  ||      | _        | j                  t        j                  d      | j                  | j                        rt        j                  ||      | _
        |}nU| j                  j                         j                         | _
        | j                  j                         j                         }| j                          |j                         | _        t#        | j                   | j                   | j                  | j                        | _        | j$                  j&                  | _        y )NzBMove refactoring should be performed on a class/function/variable.)r   r   r   r   
old_pynamer	   r*   _is_variabler   r6   old_namer   get_namerc   _check_exceptional_conditionsr"   source
_MoveToolstoolsrX   )r:   r   r+   r,   r-   ri   s         r0   r;   zMoveGlobal.__init__   s   11(;"00G??"--T  T__-"..x@DM$H OO668AACDM113>>@H**,++-LL$++t

 !JJ33r2   c                   t        j                  | j                        t        |j                  t
        j                        r(t        fd|j                  j                  D              S t        |j                  t
        j                        rtdv rWj                  dd      \  }|j                  j                  |k(  r)t        fd|j                  j                  D              ry|j                  j                  k(  S y)Nc              3  .   K   | ]  \  }}|k(    y wr]   r   ).0r   aliasmodule_names      r0   	<genexpr>z,MoveGlobal._import_filter.<locals>.<genexpr>  s"      D% t#   .rb   c              3  .   K   | ]  \  }}|k(    y wr]   r   )r   r   r   basenames      r0   r   z,MoveGlobal._import_filter.<locals>.<genexpr>  s$      H#e $Hr   TF)r
   modnamer   r   import_infor   NormalImportanynames_and_aliases
FromImportrsplitr   )r:   stmtpackage_namer   r   s      @@r0   _import_filterzMoveGlobal._import_filter	  s    &&t{{3d&&(@(@A #'#3#3#E#E   ((+*@*@Ak!)4););C)C&h##//<?C H'+'7'7'I'IH E  ##//;>>r2   c                   | j                  | j                        rH| j                  j                         d   }	 |j                         j	                  | j
                         y t        | j                  j                         t        j                        r)| j                  | j                  j                               s| j                          y y # t        j                  $ r | j                          Y y w xY wr   )r   r   get_definition_locationrp   r   r   r	   NameNotFoundError_raise_refactoring_errorr   r   r   r'   
_is_global)r:   ri   s     r0   r   z(MoveGlobal._check_exceptional_conditions  s    T__->>@CH0""$--dmm< t113Y5N5NO : : <=))+ >	 // 0--/0s   )C #C76C7c                ,    t        j                  d      )NzMMove refactoring should be performed on a global class, function or variable.)r	   r*   r^   s    r0   r   z#MoveGlobal._raise_refactoring_error+  s    ))
 	
r2   c                t    |j                         j                  |j                         j                         k(  S r]   )rp   r$   rc   )r:   r/   s     r0   r   zMoveGlobal._is_global1  s.    !!#**h.A.A.C.M.M.OOOr2   c                6    t        |t        j                        S r]   )r   r   r(   )r:   r.   s     r0   r   zMoveGlobal._is_variable4  s    &'"6"677r2   Nc                   t        |t              r| j                  j                  |      }|| j                  j	                         }||j                         st        j                  d      |j                         r"|j                  d      r|j                  d      }|j                         rt        j                  d      | j                  |k(  rt        j                  d      | j                  |||      S )aS  Return the changes needed for this refactoring

        Parameters:

        - `dest`: the Resource or dotted moddule name of the move destination
        - `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.

        z Move destination does not exist.__init__.pyz7Move destination for non-modules should not be folders.z*Moving global elements to the same module.)r   strr   find_moduler=   existsr	   r*   	is_folder	has_child	get_childr   _calculate_changesr:   destr   rK   s       r0   r[   zMoveGlobal.get_changes7  s    $ dC <<++D1D557I <t{{}--.PQQ>>} =>>-0D >>--I  ;;$--<  &&tYDDr2   c                L   t        d| j                  z        }|j                  dt        |            }|D ]  }|j	                  |j
                         || j                  k(  r"|j                  | j                  |             n||k(  r"|j                  | j                  |             nd| j                  j                  |      rG| j                  j                  |      }d| j                  z  }| j                  j                  ||      }	|	d u}
| j                  j                  ||	      }| j                   j#                  |d| j$                        }	|
rl| j                  j                  ||	      }t'        j(                  | j                  || j+                  |      | j                        \  }	}|	j-                  ||      }	| j                  j/                  ||	      }	|	|j1                         k7  r|j                  t3        ||	             |j5                           |S )NzMoving global <%s>zCollecting Changesr+   z__rope_renaming_%s_F)sortimport_filter)r   r   create_jobsetlenstarted_jobpathr   rB   _source_module_changes_dest_module_changesr   occurs_in_moduler   r   r   new_pymodulerX   organize_importsr   r   
add_import_new_modnamereplace
new_sourcerA   r   finished_job)r:   r   r   rK   rL   job_setfile_ri   placeholderr   should_importimporteds               r0   r   zMoveGlobal._calculate_changes_  s   04==@A++,@#i.Q 	#E

+#""4#>#>t#DE$""4#<#<T#BC,,e,<<<44U;3dmmC44[54Q &d 2::228VD**;;58K8K <  !#zz66xHH'2'='=h0A0A$0G($FH $^^KBF..x@UZZ\)&&~eV'DE  "7	#8 r2   c                   d| j                   z  }t        |      }t        j                  | j                  | j                   | j
                        }| j                         \  }}t        || j                  |||      }|j                         }t        j                  | j                  || j                        }	| j                  j                  |	d      }|j                  r{t        j                  | j                  || j                        }	t        j                   | j                  |	| j#                  |      | j                         \  }}
|j%                  ||
      }t'        | j                  |      S )Nz__rope_moving_%s_F)r   )r   _ChangeMoveOccurrencesHandler   r   r   r   _get_moving_regionModuleSkipRenamerr   get_changed_moduler
   rG   rX   r   occurredr   r   r   r   r   )r:   r   r   handleoccurrence_finderr}   r~   renamerr   ri   r   s              r0   r   z!MoveGlobal._source_module_changes  s&   )DMM9-k:'55LL$--
 ,,.
s#$5t{{FESVW++---dllFDKKP""33H53I??11$,,TH*55h(9(9$(? FH ^^K:Fdkk622r2   c                ,    t        j                  |      S r]   )r
   r   r:   r   s     r0   r   zMoveGlobal._new_modname  s    %%r2   c                   | j                   j                  |      }| j                  j                  | j                  |      }| j                  j                  ||      }| j                         \  }}| j                  ||      \  }}| j                  j                  |      }|j                  }d}|j                  r|j                  d   j                  dz
  }n||j                  j                         k  r|j                  j                  |dz         j!                         j#                  d      r^|dz  }||j                  j                         k  r<|j                  j                  |dz         j!                         j#                  d      r^|dkD  r0|j                  j%                  |      dz   }	|d |	 dz   |z   ||	d  z   }
n||z   }
|
}t'        j(                  | j                   ||      }| j                  j+                  |dd      }t'        j(                  | j                   ||      }| j                  j+                  |ddd| j,                  	      }t/        ||      S )
Nr   rb   #rv   F)r   unusedT)r   selfsr   r   )r   r   r   r   r   r    _get_moving_element_with_imports_add_imports2rX   module_importsrx   importsend_linero   lengthget_linelstrip
startswithget_line_endr
   rG   r   r   r   )r:   r   ri   r   movingr   has_changedmodule_with_importslinenocutrW   s              r0   r   zMoveGlobal._dest_module_changes  s(   <<,,T2,,T]]HE::**8V<??A $ 2 28W E+"//>>xH%%&&(004==AF8>>0022x~~7N7N
8fhzz#8' ! 8>>0022x~~7N7N
8fhzz#8' A:..--f59CDS\F*V3fSTlBFf_F --dllFDI""33H5QV3W--dllFDI""33-- 4 
 dF++r2   c                `    t        | j                  | j                  | j                               S r]   )moving_code_with_importsr   r   _get_moving_elementr^   s    r0   r   z+MoveGlobal._get_moving_element_with_imports  s'    'LL$++t'?'?'A
 	
r2   c                z    t        j                  | j                  ||      }| j                  j	                  |      S r]   )r
   rG   r   rX   r   )r:   rx   r+   ri   s       r0   _get_module_with_importsz#MoveGlobal._get_module_with_imports  s1    --dllKR  //99r2   c                    | j                         \  }}| j                  j                         || }|j                         dz   S )Nrt   )r   r   rA   rstrip)r:   r}   r~   r  s       r0   r  zMoveGlobal._get_moving_element  s>    ,,.
s!!#E#.}}%%r2   c                   | j                   j                  | j                        }|j                  }| j	                  | j
                        ra|j                  }|j                  | j
                  j                         d         d   }|j                  |      }|j                  |      d   }nt| j
                  j                         j                  }|j                  t        |            }| j
                  j                         j                         j                         }|j                  |      }|dkD  rQ|j!                  |dz
        j#                  d      r.|dz  }|dkD  r$|j!                  |dz
        j#                  d      r.|j                  |      }||j%                         k  rc|j!                  |dz         j'                         dk(  r>|dz  }||j%                         k  r&|j!                  |dz         j'                         dk(  r>t)        |j+                  |      dz   t-        |j.                              }	||	fS )Nrb   r   r   r   )r   r   r   ro   r   r   logical_lineslogical_line_inr   get_line_startr   ast_noder   rp   get_endget_line_numberr  r  r  ry   minr  r   rx   )
r:   ri   ro   r  r
  r}   r  node
start_liner~   s
             r0   r   zMoveGlobal._get_moving_region  s   <<,,T[[9T__-$22M"22779!<F ((0E$44V<Q?H??--/88D((:4@E 113==?GGIH **51
1n
Q!?!J!J3!O!OJ 1n
Q!?!J!J3!O$$Z0'ENN8a<,H,N,N,PTV,VMH 'ENN8a<,H,N,N,PTV,V%$$X.2C8L8L4MNczr2   c                    | j                   j                  ||      }||dfS |j                         }t        j                  | j
                  ||      }|dfS )NFT)r   add_importsr"   r
   rG   r   )r:   ri   rY   r   r+   s        r0   r   zMoveGlobal._add_imports2  sW    ''+>>U?",,.H11$,,QHT>!r2   )r   z(Optional[Union[str, resources.Resource]]r   zOptional[List[resources.File]])r   r   r   r   __annotations__r;   r   r   r   r   r   r   r   r[   r   r   r   r   r   r  r  r   r   r   r2   r0   r)   r)      s    04**,
P8 5922	&E6&E 2&EPB3(&&,P

:&
8"r2   r)   c                  b    e Zd ZdZd Zdej                  fdZd Zd Z	d Z
d Zdd	Zd
 Zd Zy)r   zFor moving modules and packagesc                   || _         |j                         s|j                  dk(  r|j                  }|j                         r&|j	                  d      st        j                  d      t        j                  | j                   d      }t        j                  ||      | _        | j                  j                         j                         | _        | j                  j                         r| j                  j                  | _        n| j                  j                  d d | _        t!        | j                   | j                  | j                  | j                        | _        | j"                  j$                  | _        y )Nr   zCannot move non-package folder.r   r   )r   r   r   r$   r   r	   r*   r
   rG   r   ImportedModuler   r   r"   r   r   r   r   rX   )r:   r   r+   dummy_pymodules       r0   r;   zMoveModule.__init__  s   !!#(FH(:(:=(I--.OPP!33DLL"E!00(Soo002??A;;  " KK,,DM KK,,Sb1DMLL$++t

 !JJ33r2   Nc                    || j                   j                         }||j                         st        j                  d      | j                  |||      S )Nz0Move destination for modules should be packages.)r   r=   r   r	   r*   r   r   s       r0   r[   zMoveModule.get_changes  sW     557I<t~~/--B  &&tYDDr2   c                   t        d| j                  z        }|j                  dt        |            }|D ]  }|j	                  |j
                         || j                  k(  r| j                  ||       n0| j                  ||      }||j                  t        ||             |j                           | j                  | j                  j                  k(  r/|j                  t        | j                  |j
                               |S )NzMoving module <%s>zCollecting changesr   )r   r   r   r   r   r   r   _change_moving_module_change_occurrences_in_modulerB   r   r   r   r   )r:   r   r   rK   rL   r   moduler   s           r0   r   zMoveModule._calculate_changes  s    04==@A++,@#i.Q 	#F,$**7D9;;D6;R%&&~ff'EF  "	# <<4;;...|DKKCDr2   c                l    t        j                  |      }|r|dz   | j                  z   S | j                  S )Nr   )r
   r   r   )r:   r   destnames      r0   r   zMoveModule._new_modname#  s2    ##D)c>DMM11}}r2   c                P    t        j                  | j                  |      d fg      S r]   )r   r   r   r   s     r0   _new_importzMoveModule._new_import)  s&    ''$*;*;D*A4)H(IJJr2   c                   | j                   j                         s| j                  j                  | j                         }| j                  j                  |      }| j                  j                  ||      }| j                  ||      }| j                  j                  ||      }|| j                   j                         k7  r&|j                  t        | j                   |             y y y r]   )r   r   r   r   rX   relatives_to_absolutesr   r   r(  r   rA   rB   r   )r:   rL   r   ri   r   s        r0   r'  z MoveModule._change_moving_module,  s    {{$$&||00=H&&==hGFzz..x@H77hGFZZ**8V<F))++"">$++v#FG , 'r2   c                   | j                   j                  ||      sy || j                  j                  |      }| j	                  |      }t        j                  | j                  |      }d}d }t        j                  |      r]| j                  |||      }|rH|j                         }| j                   j                  ||      }| j                   j                  ||      }| j                  |      }| j                   j                  |d||s|nd       }| j                   j                  ||d      }	| j                   j                  ||      }| j                   j                  |      }|	r9| j                   j                  ||      }| j                   j!                  ||g      }| j                   j                  ||      }|||j"                  j%                         k7  r|S y )Nri   r+   FT)r   ri   r+   )ri   r+   r   )r   r   r   r   r   r   get_module_importsr
   r   _change_import_statementsget_changed_sourcer   r   r-  r   remove_old_importsr  r+   rA   )
r:   r   ri   r+   rJ   r   changedr   
new_importr   s
             r0   r(  z(MoveModule._change_occurrences_in_module6  s   zz**Hx*P||00:H$$T*$77hOD!44T8^TG'::<..x@::228VD%%d+
,,%,X$	 - 
 

33% 4 
 ::**8V<..x8zz..x@HZZ++HzlCF&&x8&H,=,=,B,B,D"DMr2   c                     j                   }|j                  }d}|j                  D ];  }t         fd|j                  j
                  D              s)t         fd|j                  j
                  D              sUt        |j                  t        j                        rz j                  ||||      xs |}t        j                  j                   j                  d       }|j                  j                         r|j                  j                  |      |k(  st        j                  ||j                  j                   |j                  j
                        |_        d}> |S )NFc              3  B   K   | ]  }|d    j                   k(    ywr   N)r   r   name_and_aliasr:   s     r0   r   z7MoveModule._change_import_statements.<locals>.<genexpr>`  s'      " q!T]]2s   c              3  h   K   | ])  }|d    t        j                  j                        k(   + ywr:  )r
   r   r   r;  s     r0   r   z7MoveModule._change_import_statements.<locals>.<genexpr>c  s2      " q!X%5%5dkk%BBs   /2T)r   r$   r   r   r   r   r   r   r   "_handle_moving_in_from_import_stmt
importinfoImportContextr   is_emptyget_imported_resourcer   level)	r:   r   rJ   r   moving_moduleparent_moduler6  import_stmtcontexts	   `        r0   r3  z$MoveModule._change_import_statementsZ  sO   %,,)11 "	K &1&=&=&O&O   &1&=&=&O&O   +11;3K3KL 77+~}  	  ",,::4<<NG++446++AA'J ! +6*@*@++11++==+'
 E"	H r2   c                v   d}t         j                  j                  | j                  d       }|j                  j                  |      |k(  r|j                  j                  }g }|D ]r  \  }	}
|	| j                  k(  rKd}t        j                  t        j                  |      d| j                  |
fg      }|j                  |       `|j                  |	|
f       t ||k7  rZd}|rFt        j                  |j                  j                  |j                  j                  |      |_        |S |j                          |S )NFTr   )r   r?  r@  r   r   rB  r   r   r   r
   r   r   appendr   rC  empty_import)r:   r   rF  r   rE  r6  rG  r   rY   r   r   r7  s               r0   r>  z-MoveModule._handle_moving_in_from_import_stmt  s(    ((66t||TJ""88A]R!--??GK& 	6e4==("G!,!7!7 ((.T]]E4J3K"J #--j9&&e}5	6 g%.9.D.D#//;;#//55#/K+   ,,.r2   )NN)r   r   r   r   r;   r   r   r[   r   r   r-  r'  r(  r3  r>  r   r2   r0   r   r     sD    )4& #
0N0N	E KH"H)Vr2   r   c                      e Zd Zd Zd Zd Zy)r   c                     || _         d| _        y )NF)rJ   r   )r:   rJ   s     r0   r;   z%_ChangeMoveOccurrencesHandle.__init__  s     r2   c                     y r]   r   r:   change_collector
occurrences      r0   occurred_inside_skipz1_ChangeMoveOccurrencesHandle.occurred_inside_skip      r2   c                r    |j                         \  }}|j                  ||| j                         d| _        y )NT)get_primary_rangerB   rJ   r   )r:   rO  rP  r}   r~   s        r0   occurred_outside_skipz2_ChangeMoveOccurrencesHandle.occurred_outside_skip  s1    113
s##E3>r2   N)r   r   r   r;   rQ  rU  r   r2   r0   r   r     s    r2   r   c                  @    e Zd Zd Zd Zd
dZddZd Zd Zd Z	d	 Z
y)r   c                    || _         || _        || _        || _        t	        j
                  | j                         | _        y r]   )r   r   r   r   r   rE   rX   )r:   r   r   r.   r   s        r0   r;   z_MoveTools.__init__  s5      '33DLLAr2   c                     j                   } j                  j                        } G  fdd      } |       }|j                  |       |j	                         }||k7  r|S y )Nc                  P    e Zd ZdZW j                  ZW j
                  Z fdZy)0_MoveTools.remove_old_imports.<locals>.CanSelectFc                    	 || j                   k(  r6|   j                         | j                  j                         k(  rd| _        yy# t        j
                  $ r Y yw xY w)NTF)r   r   r   r6  r	   AttributeNotFoundError)r:   r   ri   s     r0   __call__z9_MoveTools.remove_old_imports.<locals>.CanSelect.__call__  sb    -$TN5574??;U;U;WW'+$  "88 s   AA	 	AAN)r   r   r   r6  r   r   r]  )ri   r:   s   r0   	CanSelectrZ    s    G}}HJ
r2   r^  )rx   rX   r   filter_namesr4  )r:   ri   
old_sourcer	  r^  
can_selectr   s   ``     r0   r5  z_MoveTools.remove_old_imports  sg    ))
"//>>xH	 	" [
((4(;;=
# $r2   Nc                \    | j                  |      }t        j                  ||d||      }|S )NT)replace_primaryri   r+   )_create_finderr   r   )r:   rJ   ri   r   r+   r   r   s          r0   r   z_MoveTools.rename_in_module  s9     //8(( 
 r2   c                z    | j                  |      }|j                  ||      }t               }t        ||      |uS )Nr1  )rd  find_occurrencesobjectnext)r:   ri   r+   r   r   r   sentinels          r0   r   z_MoveTools.occurs_in_module  s@    $$W---x(-S8K*(::r2   c                r    t        j                  | j                  | j                  | j                  |d      S )NF)r   keywords)r   r   r   r   r   )r:   r   s     r0   rd  z_MoveTools._create_finder  s0    ((LLMMOO
 	
r2   c                h    |/t        j                  | j                  ||j                               S |S r]   )r
   rG   r   r"   r:   ri   r   s      r0   r   z_MoveTools.new_pymodule  s6    --fh&;&;&=  r2   c                "    ||j                   S |S r]   )rx   rm  s      r0   r   z_MoveTools.new_source  s    >'''r2   c                0    t        | j                  ||      S r]   )rH   rX   )r:   ri   rY   s      r0   r  z_MoveTools.add_imports  s    %d&7&7;OOr2   )NFN)NNT)r   r   r   r;   r5  r   r   rd  r   r   r  r   r2   r0   r   r     s,    B6	;

Pr2   r   c                t    | j                  |      }|D ]  }|j                  |        |j                         S r]   )r   r   r4  )rX   ri   rY   r	  r7  s        r0   rH   rH     s?    &55h?! 3
&&z231133r2   c                   t        j                  |       }t        j                  | ||      }t	        j
                  |      }d}||j                         k  rY|j                  |      j                  d      r9|dz  }||j                         k  r!|j                  |      j                  d      r9|d |j                  |       }t        j                  | ||j                  |      d  |      } | j                  |      }|j                  |      j                  D 	cg c]  }	|	j                   }
}	|D cg c]	  }||vs| }}|
j                  |j                  ||             t!        |||
      }t        j                  | ||      }|j#                  |      }t        j                  | ||      }|j%                  |d      }t        j                  | ||      }|j                  |      }|j                  D cg c]  }|j                   }
}d}|j                  r|j                  d   j&                  }t	        j
                  |      }||j                         k  rW|j                  |      j)                         s8|dz  }||j                         k  r |j                  |      j)                         s8|||j                  |      d  z   }||
fS c c}	w c c}w c c}w )Nrb   r   F)r   r   )r   rE   r
   rG   r   SourceLinesAdapterr  r  r  r  r   r   r   r   rI  get_from_importrH   r/  r   r  ry   )r   r+   r   rX   ri   ro   r}   moving_prefixoriginr   r   r   
back_namesr   rF  r  s                   r0   r  r    s   **73L))'68DH **62EE
%,,.
 U^^E%:%E%Ec%J
 %,,.
 U^^E%:%E%Ec%J8U11%89M)),,U356H "W!!(+F,8,G,G,O,W,WXDtXGX#)B4T-A$BJBNN<//*EF#L(GDF))'68DH00:F))'68DH**85*AF))'68DH "00:N:H:P:PQ;{&&QGQE&&r*33**62E
%,,.
 )>)D)D)F
 %,,.
 )>)D)D)F VE$8$8$?$ABBF7?3 YB Rs   K5	K?K;Kc                      e Zd Zd Zd Zy)ModuleSkipRenamerHandlec                     y r]   r   rN  s      r0   rU  z-ModuleSkipRenamerHandle.occurred_outside_skip6  rR  r2   c                     y r]   r   rN  s      r0   rQ  z,ModuleSkipRenamerHandle.occurred_inside_skip9  rR  r2   N)r   r   r   rU  rQ  r   r2   r0   rx  rx  5  s    r2   rx  c                  &    e Zd ZdZ	 	 	 	 ddZd Zy)r   zRename 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.

    Nc                    || _         || _        || _        || _        || _        || _        | j
                  t               | _        yy)zConstructor

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

        N)r   r+   
skip_startskip_endreplacementr   rx  )r:   r   r+   r   r}  r~  r  s          r0   r;   zModuleSkipRenamer.__init__E  sI     "3 $ &;;13DK r2   c                @   | j                   j                         }t        j                  |      }| j                  1|j                  | j                  | j                  | j                         | j                  j                  | j                         D ]p  }|j                         \  }}| j                  |cxk  r| j                  k  r n n| j                  j                  ||       U| j                  j                  ||       r |j                         }|||k7  r|S y y r]   )r+   rA   r   r@   r  rB   r}  r~  r   rf  rT  r   rQ  rU  rD   )r:   r   rO  rP  r}   r~   rW   s          r0   r   z$ModuleSkipRenamer.get_changed_module]  s    ##%&66v>'''0@0@ 00AA$--P 	PJ#557JE3%7$--7001A:N112BJO	P "--/&F"2M #3r2   )Nr   r   r   )r   r   r   r   r;   r   r   r2   r0   r   r   =  s     40r2   r   r]   ))r   
__future__r   typingr   r   r   	rope.baser   r   r	   r
   r   r   r   r   rope.base.changer   r   r   rope.refactorr   r   r   r   r   (rope.refactor.importutils.module_importsr   TYPE_CHECKINGr   r   r1   r&   r)   r   r   r   rH   r  rx  r   r   r2   r0   <module>r     s    #  ( (	 	 	 E D V V 
,@lJ lJ^A" A"Hp pf JP JPZ4*Z / /r2   