
    Æep                        d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZ d dlmZ d dlmZ d d	lmZmZmZmZmZ  G d
 d      Z G d de      Z G d de      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d dej>                        Z  G d d      Z! G d dej>                        Z"d  Z# G d! d"ej>                        Z$ G d# d$ej>                        Z% G d% d&e%      Z& G d' d(e%      Z' G d) d*e%      Z( G d+ d,ej>                        Z) G d- d.ej>                        Z*d/ Z+d0 Z,d1 Z-d2 Z.y)3    N)contextmanager)chain)Dict)astcodeanalyze)ChangeContents	ChangeSetRefactoringError)
OrderedSet)
patchedastsimilarfindersourceutilssuitesusefunctionc                   h    e Zd ZU i Zeeef   ed<   d
dZd Zd Z	ddZ
d Zed        Zed	        Zy)_ExtractRefactoringkind_prefixesc                     || _         || _        | j                  |j                         |      | _        | j                  |j                         |      | _        y N)projectresource
_fix_startreadstart_offset_fix_end
end_offset)selfr   r   r   r   variables         7/usr/lib/python3/dist-packages/rope/refactor/extract.py__init__z_ExtractRefactoring.__init__(   sA      OOHMMO\J--D    c                     |t        |      k  r:||   j                         r'|dz  }|t        |      k  r||   j                         r'|S N   )lenisspacer   sourceoffsets      r    r   z_ExtractRefactoring._fix_start.   sH    s6{"vf~'='='?aKF s6{"vf~'='='?r"   c                 ~    |dkD  r7||dz
     j                         r!|dz  }|dkD  r||dz
     j                         r!|S Nr   r%   )r'   r(   s      r    r   z_ExtractRefactoring._fix_end3   sH    qjVFQJ/779aKF qjVFQJ/779r"   Nc           
         | j                  ||      \  }}t        | j                  | j                  | j                  | j
                  || j                  |      dk(  ||      }| j                  |      |_        t        |      j                         }t        d|j                   d| d      }|j                  t        | j                  |             |S )a  Get the changes this refactoring makes

        :parameters:
            - `extracted_name`: target name, when starts with @ - set kind to
            classmethod, $ - staticmethod
            - `similar`: if `True`, similar expressions/statements are also
              replaced.
            - `global_`: if `True`, the extracted method/variable will
              be global.
            - `kind`: kind of target refactoring to (staticmethod, classmethod)

        r   )r   similarmake_globalzExtract z <>)_get_kind_from_name_ExtractInfor   r   r   r   	_get_kindkind_ExtractPerformerextractr	   
add_changer   )r   extracted_namer.   global_r4   infonew_contentschangess           r    get_changesz_ExtractRefactoring.get_changes8   s      $77MLLMMOO^^D)Z7	
 NN4(	(.668htyykN3C1EF>$--FGr"   c                     | j                   j                         D ]1  \  }}|j                  |      s| j                  ||       |dd  |fc S  ||fS r$   )r   items
startswith_validate_kind_prefix)r   r8   r4   signselected_kinds        r    r1   z'_ExtractRefactoring._get_kind_from_nameW   sb    #'#5#5#;#;#= 	9D-((.**4?%ab)=88	9 t##r"   c                 *    | r| |k7  rt        d      y y )Nz"Kind and shortcut in name mismatchr
   )r4   rC   s     r    rA   z)_ExtractRefactoring._validate_kind_prefix^   s    DM)"#GHH *4r"   c                     t        d|        )NzYou have to subclass )NotImplementedErrorclsr4   s     r    r3   z_ExtractRefactoring._get_kindc   s    !$9#"?@@r"   F)FFN)__name__
__module____qualname__r   r   str__annotations__r!   r   r   r=   r1   staticmethodrA   classmethodr3    r"   r    r   r   %   sZ    $&M4S>&E

>$ I I A Ar"   r   c                   .    e Zd ZdZdZdddZed        Zy)ExtractMethodmethod)functionrT   rO   rP   rP   rO   )@$c                 :    || j                   v r|S | j                  S r   )allowed_kindsr4   rG   s     r    r3   zExtractMethod._get_kindm   s    s000t>chh>r"   N)rJ   rK   rL   r4   rY   r   rP   r3   rQ   r"   r    rS   rS   h   s(    DIM'n=M? ?r"   rS   c                   (     e Zd Z fdZdZd Z xZS )ExtractVariablec                 D    t        |      }d|d<   t        |   |i | y )NTr   )dictsuperr!   )r   argskwds	__class__s      r    r!   zExtractVariable.__init__s   s(    DzZ$'$'r"   r   c                     | j                   S r   )r4   rG   s     r    r3   zExtractVariable._get_kindz   s    xxr"   )rJ   rK   rL   r!   r4   r3   __classcell__ra   s   @r    r[   r[   r   s    (
 Dr"   r[   c                       e Zd ZdZd Zd Zed        Zd Zd Z	ddZ
ed        Zed	        Zed
        Zed        Zed        Zed        ZdZed        ZdZed        ZdZed        ZdZed        Zy)r2   z3Holds information about the extract to be performedc	                    || _         || _        |j                  |      | _        | j                  j	                         | _        | j                  j                  | _        | j                  j                  | _        || _	        || _
        || _        | j                  ||       d | _        | j                          || _        y r   )r   r   get_pymodulepymodule	get_scopeglobal_scopesource_coder)   linesnew_namer   r.   _init_partsr4   _init_scoper/   )	r   r   r   startendrm   r   r.   r/   s	            r    r!   z_ExtractInfo.__init__   s      ,,X6 MM335mm//]]((
  $	&r"   c                 "   | j                  |      | j                  |d      f| _        | j                  j                  | j                  j                  | j                  d               d   }| j                  j                  | j                  j                  | j                  d               d   }||f| _        | j                  j                  | j                  d         | j                  j                  | j                  d         f| _	        y )NT)rq   r   r%   )
_choose_closest_line_endregionlogical_lineslogical_line_inrl   get_line_numberregion_linesget_line_startget_line_endlines_region)r   rp   rq   s      r    rn   z_ExtractInfo._init_parts   s    ))%0))#4)8

 ""22JJ&&t{{1~6

   00JJ&&t{{1~6

 #CL JJ%%d&7&7&:;JJ##D$5$5a$89
r"   c                 .    | j                   j                  S r   )rh   ru   r   s    r    ru   z_ExtractInfo.logical_lines   s    }}***r"   c                 
   | j                   d   }| j                  j                  |      }|j                         dk7  r|j	                         |k(  r|j
                  }|| _        | j                  | j                        | _        y )Nr   Module)	rx   rj   get_inner_scope_for_lineget_kind	get_startparentscope_get_scope_regionscope_region)r   
start_liner   s      r    ro   z_ExtractInfo._init_scope   sl    &&q)
!!:::F>>x'EOO,=,KLLE
 224::>r"   c                     | j                   j                  |j                               | j                   j                  |j	                               dz   fS r$   )rl   ry   r   rz   get_end)r   r   s     r    r   z_ExtractInfo._get_scope_region   sB    JJ%%eoo&78JJ##EMMO4q8
 	
r"   c                 x   | j                   j                  |      }| j                   j                  |      }| j                   j                  |      }| j                  || j                         dk(  r	|r|dz
  S |S | j                  || j                         dk(  rt        |t        | j                              S |S )N r%   )rl   rw   ry   rz   r)   stripminr&   )r   r*   rq   lineno
line_startline_ends         r    rs   z%_ExtractInfo._choose_closest_line_end   s    ++F3ZZ..v6
::**62;;z&)//1R7!A~%!![[)//1R7xT[[!122r"   c                     | j                   | j                  k7  xrQ | j                  j                  | j                  d         | j                  j                  | j                  d         k(  S r,   )rt   r{   ru   rv   rx   r}   s    r    one_linez_ExtractInfo.one_line   sb    {{d/// 
..t/@/@/CD!!11$2C2CA2FGH	
r"   c                 2    | j                   j                  d u S r   )r   r   r}   s    r    r9   z_ExtractInfo.global_   s    zz  D((r"   c                     | j                   j                  d uxr' | j                   j                  j                         dk(  S )NClass)r   r   r   r}   s    r    rT   z_ExtractInfo.method   s5    zz  ,X1B1B1K1K1MQX1XXr"   c                 p    t        j                  | j                  j                  | j                  d         S Nr   )r   get_indentsrh   rl   rx   r}   s    r    indentsz_ExtractInfo.indents   s*    &&t}}':':D<M<Ma<PQQr"   c                     | j                   ryt        j                  | j                  j                  | j
                  j                               S r   )r9   r   r   rh   rl   r   r   r}   s    r    scope_indentsz_ExtractInfo.scope_indents   s6    <<&&t}}':':DJJ<P<P<RSSr"   c                 T    | j                   | j                  d   | j                  d    S r,   )r)   rt   r}   s    r    	extractedz_ExtractInfo.extracted   s#    {{4;;q>DKKN;;r"   Nc                 f    | j                   t        | j                        | _         | j                   S r   )_cached_parsed_extracted_parse_textr   r}   s    r    _parsed_extractedz_ExtractInfo._parsed_extracted   s+    ((0,7,GD),,,r"   c                 z    | j                   $t        j                  | j                        | _         | j                   S )z1Does the extracted piece contain return statement)	_returnedr   _returns_lastr   r}   s    r    returnedz_ExtractInfo.returned   s0     >>!(66t7M7MNDN~~r"   c                 z    | j                   $t        j                  | j                        | _         | j                   S )z?Does the extracted piece contains named expression/:= operator))_returning_named_exprr   _namedexpr_lastr   r}   s    r    returning_named_exprz!_ExtractInfo.returning_named_expr   s:     %%-)4)D)D&&*D& )))r"   c                 d   | j                   t        | j                  t        j                        xrn t        | j                  j
                  d   t        j                        xr; t        | j                  j
                  d   j                  t        j                        | _         | j                   S )z8Does the extracted piece contains a generator expressionr   )	_returning_generator
isinstancer   r   r   bodyExprvalueGeneratorExpr}   s    r    returning_generator_expz$_ExtractInfo.returning_generator_exp   s     $$,4113::> Wt55::1=sxxHWt55::1=CCSEUEUV % (((r"   rI   )rJ   rK   rL   __doc__r!   rn   propertyru   ro   r   rs   r   r9   rT   r   r   r   r   r   r   r   r   r   r   r   rQ   r"   r    r2   r2   ~   s   ='"
& + +?
 
 
 ) ) Y Y R R T T
 < <  $- -
 I  !* *  	) 	)r"   r2   c                       e Zd ZdZd Zy)_ExtractCollectorz6Collects information needed for performing the extractc                 f    d | _         d | _        i | _        d | _        d | _        d | _        d | _        y r   )
definitionbody_patternchecksreplacement_patternmatchesreplacementsdefinition_locationr   r:   s     r    r!   z_ExtractCollector.__init__  s7     #'  #' r"   N)rJ   rK   rL   r   r!   rQ   r"   r    r   r     s
    @(r"   r   c                   L    e Zd Zd Zd Zd Zd Zd Zed        Z	d Z
d Zd	 Zy
)r5   c                 F    || _          t               | j                          y r   )r:   _ExceptionalConditionCheckerr   s     r    r!   z_ExtractPerformer.__init__  s    	&$&tyy1r"   c                    | j                         }t        j                  | j                  j                        }|j
                  }|j                  \  }}| j                  j                  j                  |      }t        j                  ||      }|j                  |||       | j                  ||       |j                         S r   )_collect_infor   ChangeCollectorr:   r)   r   r   rl   ry   r   fix_indentationr7   _replace_occurrencesget_changed)r   extract_infocontentr   r   r   r*   indenteds           r    r6   z_ExtractPerformer.extract  s    ))+--dii.>.>?!,,
&:://7..z7C6684!!'<8""$$r"   c                    |j                   D ]  }t        j                  |j                        }i }|j	                         D ]^  }|j                  |      }|rDt        j                  |j                  |            \  }}	| j                  j                  ||	 ||<   Z|||<   ` |j                         }
|j                  |
d   |
d   |j                  |              y r,   )r   r   CodeTemplater   	get_namesget_astr   node_regionr:   r)   
get_regionr7   
substitute)r   r   r   matchreplacementmappingnamenoderp   rq   rt   s              r    r   z&_ExtractPerformer._replace_occurrences)  s    !)) 	VE'44\5U5UVKG#--/ )}}T*!+!7!7d8K!LJE3$(II$4$4U3$?GDM$(GDM) %%'Fvay&)[5K5KG5TU	Vr"   c                     t        | j                        }| j                  |       | j                  |       | j	                  |       |S r   )r   r:   _find_definition_find_matches_find_definition_location)r   extract_collectors     r    r   z_ExtractPerformer._collect_info7  sD    -dii8/0,-&&'89  r"   c                    | j                         }t        j                  | j                  j                        }g }|D ]  \  }}|j                  |j                  |j                  ||      }d}|D ]V  }	| j                  j                  r| j                  |	      r+|	j                         \  }}||k  sD|j                  |	       |}X  ||_        y )N)_where_to_searchr   SimilarFinderr:   rh   get_matchesr   r   r   _is_assignmentr   appendr   )
r   	collectorregionsfinderr   rp   rq   region_matcheslast_match_endregion_matchs
             r    r   z_ExtractPerformer._find_matches>  s    ''),,TYY-?-?@! 	)JE3#//&&	(8(8%N  N . )99%%$*=*=l*K)446
s!E)NN<0%(N)	) $	r"   c                     t        | j                  t        j                  t        j                  f      xr. t        | j                  j                  t        j
                        S r   )r   r   	Attribute	SubscriptctxStore)r   s    r    r   z _ExtractPerformer._is_assignmentQ  sF    s}}cmm<
 :))--syy9	:r"   c                    | j                   j                  r| j                   j                  s| j                   j                  r,dt	        | j                   j
                  j                        fgS | j                   j                  r| j                   j                  s| j                   j                  j                  }g }t        | j                   j                        }|j                         D ]  }|dk(  rt        |      dk7  r| j                   j                  j                  |j                               }| j                   j                  j!                  |j#                               }|j%                  ||f        |S | j                   j                  r| j                   j&                  gS | j                   j)                  | j                   j                  j                        gS | j                   j*                  gS )Nr   rT   )r:   r.   r/   r9   r&   rh   rk   rT   r   r   r   _get_function_kind
get_scopesrl   ry   r   rz   r   r   r   r   rt   )r   class_scoper   method_kindr   rp   rq   s          r    r   z"_ExtractPerformer._where_to_searchW  sk   99yy$$		(9(9C		 2 2 > >?@AAyy		(:(:"iioo440A(335 1E#x/.u5A  IIOO::5??;LME))//66u}}GCNNE3<01 99%% II2233 II77		8N8NOPPII$$%%r"   c                 ~   g }|j                   D ]q  }| j                  j                  j                  |j	                         d         }| j                  j
                  j                  |      d   }|j                  |       s t        | j                  |      }|j                         |j                         f|_        y r   )r   r:   rl   rw   r   ru   rv   r   _DefinitionLocationFinderfind_linenofind_indentsr   )r   r   matched_linesr   rp   r   location_finders          r    r   z+_ExtractPerformer._find_definition_locationq  s    && 	-EIIOO33E4D4D4Fq4IJE00@@GJJ  ,	- 4DII}M'')((*)
	%r"   c                 .   | j                   j                  rt        | j                         }nt        | j                         }|j	                         |_        |j                         |_        |j                         |_	        |j                         |_        y r   )r:   r   _ExtractVariableParts_ExtractMethodPartsget_definitionr   get_body_patternr   get_replacement_patternr   
get_checksr   )r   r   partss      r    r   z"_ExtractPerformer._find_definition}  so    99)$))4E'		2E$335	!&!7!7!9	(-(E(E(G	% ++-	r"   N)rJ   rK   rL   r!   r6   r   r   r   rO   r   r   r   r   rQ   r"   r    r5   r5     s?    2	%V!$& : :
&4

.r"   r5   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)r   c                     || _         || _        |s3| j                  j                  | j                   j                  d          y y r   )r:   r   r   rx   )r   r:   r   s      r    r!   z"_DefinitionLocationFinder.__init__  s<    	*%%dii&<&<Q&?@ r"   c                 t   | j                   j                  r&| j                   j                  s| j                         S | j                   j                  r| j                  | j                   j                        }| j                   j                  j                         }t        | j                  |j                         dz   gz         }t        j                  ||      S | j                   j                  r8| j                  | j                   j                        }|j                         dz   S | j                         S r$   )r:   r   r/   _get_before_liner9   _find_toplevelr   rh   r   sortedr   r   r   find_visible_get_after_scope)r   toplevelr   newliness       r    r   z%_DefinitionLocationFinder.find_lineno  s    99dii&;&;((**99**499??;H))$$,,.Cd00H4D4D4F4J3KKLH&&sH5599  **499??;H##%))$$&&r"   c                     |}|j                   9|j                   j                   #|j                   }|j                   j                   #|S r   )r   )r   r   r	  s      r    r  z(_DefinitionLocationFinder._find_toplevel  sA    ??&//((4#?? //((4r"   c                 P   | j                   j                  rN| j                   j                  s8t        j                  | j                   j
                  | j                               S | j                   j                  s| j                   j                  ry| j                   j                  S r   )	r:   r   r/   r   r   rl   r  r9   r   r}   s    r    r   z&_DefinitionLocationFinder.find_indents  sk    99dii&;&;**499??D<Q<Q<STTyy  DII$9$9yy&&&r"   c                     | j                   j                  j                  j                         }t	        j
                  || j                        S r   )r:   r   pyobjectr   r   r  r   )r   r   s     r    r  z*_DefinitionLocationFinder._get_before_line  s7    iioo&&..0""3(:(:;;r"   c                 P    | j                   j                  j                         dz   S r$   )r:   r   r   r}   s    r    r  z*_DefinitionLocationFinder._get_after_scope  s    yy&&(1,,r"   N)	rJ   rK   rL   r!   r   r  r   r  r  rQ   r"   r    r   r     s!    A''<-r"   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)r   c                     | j                  |       |j                  r| j                  |       y | j                  |       y r   )base_conditionsr   one_line_conditionsmulti_line_conditionsr   s     r    __call__z%_ExceptionalConditionChecker.__call__  s3    T"==$$T*&&t,r"   c                    |j                   d   |j                  d   kD  rt        d      |j                  d   }|j                  j                  |      }||j                  k7  r|j                         |k7  rt        d      	 |j                  }|j                  rd|z  }t        j                  |      rt        d      y # t        $ r t        d      w xY w)Nr%   z&Bad region selected for extract method(%s)z:A break/continue without having a matching for/while loop.3Extracted piece should contain complete statements.)rt   r   r   rx   rj   r   r   r   r   r   _UnmatchedBreakOrContinueFinder
has_errorsSyntaxError)r   r:   end_line	end_scoper   s        r    r  z,_ExceptionalConditionChecker.base_conditions  s    ;;q>D--a00"#KLL$$Q'%%>>xH	

"y'8'8':h'F"#KLL	I}}"Y.	.99)D&P  E  	"E 	s   =C Cc                    | j                  |      rt        d      |j                  r|j                  st        d      t	        j
                  |j                        t	        j                  |j                        z
  rt        d      y )Nz#Should extract complete statements.z0Extract variable should not span multiple lines.z>Extracted piece cannot contain named expression (:= operator).)_is_region_on_a_wordr   r   r   r   _named_expr_countr   r   r   s     r    r  z0_ExceptionalConditionChecker.one_line_conditions  s~    $$T*"#HII=="#UVV((""
''(>(>?@ #P @r"   c                    t        |j                  |j                  d   |j                  d          }t        j                  |      }|j
                  }|dkD  rt        d      t        j                  |      rt        d      t        t        d      s t        j                  |      rt        d      |dk(  r t        j                  |      st        d      |j                  |j                  k7  rt        d      t        |j                  d   |j                  d         }|j!                  |j"                  j$                         |j&                  rt        d	      y )
Nr   r%   z3Extracted piece can have only one return statement.z-Extracted piece cannot have yield statements.PyCF_ALLOW_TOP_LEVEL_AWAITz_Extracted piece can only have async/await statements if Rope is running on Python 3.8 or higherz$Return should be the last statement.r  zDExtracted piece cannot contain the start of a block without the end.)r   r)   rt   r   _return_countr   r   _yield_counthasattrr   _AsyncStatementFinderr  r   r{   _UnbalancedRegionFinderrx   visitrh   ast_nodeerror)r   r:   r   countr   unbalanced_region_finders         r    r  z2_ExceptionalConditionChecker.multi_line_conditions  sE   4;;t{{1~AGH))$/NN	19"E  ##D)"#RSS-
#..y9"  
 A:k77="#IJJ;;$+++"E  $;a $"3"3A"6$
  	!&&t}}'='=>#))"V  *r"   c                     |j                   d   dkD  r"| j                  ||j                   d   dz
        s"| j                  ||j                   d   dz
        ryy )Nr   r%   T)rt   _is_on_a_wordr   s     r    r  z1_ExceptionalConditionChecker._is_region_on_a_word  sT    KKNQ""4Q!);<!!$A(:; <r"   c                     |j                   |   }|j                         s|dk(  r|dz   t        |j                         k(  ry|j                   |dz      }|j                         xs |dk(  S )N_r%   F)r)   isalnumr&   )r   r:   r*   prevnexts        r    r.  z*_ExceptionalConditionChecker._is_on_a_word  s]    {{6"$#+&1*DKK@P2P{{6A:&||~,,r"   N)	rJ   rK   rL   r  r  r  r  r  r.  rQ   r"   r    r   r     s!    -,
B-r"   r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!e"d         Z#e"d!        Z$y")#r   c                     || _         | j                         | _        | j                         | j                   _        | j                          y r   )r:   _create_info_collectorinfo_collector_get_kind_by_scoper4   _check_constraintsr   s     r    r!   z_ExtractMethodParts.__init__  s:    	"99;002		!r"   c                 r    | j                         ry| j                         ry| j                  j                  S )NrO   rP   )_extacting_from_staticmethod_extracting_from_classmethodr:   r4   r}   s    r    r8  z&_ExtractMethodParts._get_kind_by_scope  s.    ,,.!..0 yy~~r"   c                     | j                         s| j                         r"| j                  j                  st	        d      y y )Nz8Cannot extract to staticmethod/classmethod outside class)_extracting_staticmethod_extracting_classmethodr:   rT   r   r}   s    r    r9  z&_ExtractMethodParts._check_constraints  sA    ((*d.J.J.L99##&N  $ /Mr"   c                 v    | j                   j                  xr" t        | j                   j                        dk(  S NrO   r:   rT   r   r   r}   s    r    r;  z0_ExtractMethodParts._extacting_from_staticmethod$  s,    IIV!3DIIOO!D!V	
r"   c                 v    | j                   j                  xr" t        | j                   j                        dk(  S NrP   rB  r}   s    r    r<  z0_ExtractMethodParts._extracting_from_classmethod)  s*    yyX$6tyy$G=$XXr"   c                 z    | j                   j                  rd| j                         z  S d| j                         z  S )Nz
%s
z
%s)r:   r9   _get_function_definitionr}   s    r    r   z"_ExtractMethodParts.get_definition,  s7    99d;;===D99;;;r"   c                     g }|j                  | j                                |j                  | j                                t        j                  | j                         |      S r   )extend_find_function_arguments_find_function_returnsr   make_pattern	_get_callr   	variabless     r    r   z+_ExtractMethodParts.get_replacement_pattern2  sO    	66894467))$..*:IFFr"   c                    g }|j                  | j                                |j                  | j                                |j                  | j                                t	        j
                  | j                         |      S r   )rH  rI  rJ  _find_tempsr   rK  	_get_bodyrM  s     r    r   z$_ExtractMethodParts.get_body_pattern8  sf    	66894467))+,))$..*:IFFr"   c                     t        j                  | j                  j                  d      }| j                  j                  rd|z  }|S )Nr   r  )r   r   r:   r   r   r   results     r    rQ  z_ExtractMethodParts._get_body?  s9    ,,TYY-@-@!D99f_Fr"   c                 r    t        j                  | j                  j                  | j	                               S r   )r   
find_tempsr:   r   rQ  r}   s    r    rP  z_ExtractMethodParts._find_tempsE  s&    %%dii&7&79IJJr"   c                 F   | j                   j                  r| j                   j                  stt        | j                   j                        dk(  rRt        j                  | j                   j                  j                  j                        }| j                         d|z   iS i S )NrT   ztype=)
r:   rT   r/   r   r   r   _pydefined_to_strr   r  _get_self_name)r   
class_names     r    r   z_ExtractMethodParts.get_checksH  sw    99DII$9$9!$))//2h>*<<IIOO**33
 ++-w/CDD	r"   c                    | j                   j                  j                         dz
  }| j                   j                  d   |z
  }| j                   j                  d   |z
  }t	        ||| j                   j
                        }| j                   j                  | j                   j                  d   | j                   j                  d    }t        |      }|j                  |       |S )Nr%   r   )
r:   r   r   rx   _FunctionInformationCollectorr9   r)   r   r   r(  )r   zeror   r  r7  r   r   s          r    r6  z*_ExtractMethodParts._create_info_collectorQ  s    yy((*Q.YY++A.5
99))!,t36$))"3"3
 yy		 6 6q 9DII<R<RST<UV4 T"r"   c                    | j                         }| j                         }g }| j                  |       |j                  d| j	                  |      z         | j                  |      }t        j                  | j                  j                        }t        j                  ||      }|j                  |       dj                  |      }|dz   S )Nzdef %s:
r   
)rI  rJ  _append_decoratorsr   _get_function_signature_get_unindented_function_bodyr   
get_indentr:   r   indent_linesjoin)r   r_   returnsrT  unindented_bodyr   function_bodyr   s           r    rF  z,_ExtractMethodParts._get_function_definition]  s    ,,.--/'kD$@$@$FFG<<WE(():):;#00'Jm$WWV_
D  r"   c                     | j                         r|j                  d       y | j                         r|j                  d       y y )Nz@staticmethod
z@classmethod
)r>  r   r?  rS  s     r    r`  z&_ExtractMethodParts._append_decoratorsl  s9    ((*MM+,))+MM*+ ,r"   c                 4    | j                   j                  dk(  S rD  r:   r4   r}   s    r    r?  z+_ExtractMethodParts._extracting_classmethodr  s    yy~~..r"   c                 4    | j                   j                  dk(  S rA  rk  r}   s    r    r>  z,_ExtractMethodParts._extracting_staticmethodu  s    yy~~//r"   c                 @   t        |      }d}| j                         s| j                         rD| j                         }|t	        d      ||v r|j                  |       |j                  d|       || j                  j                  z   d| j                  |      z  z   S )Nr   z:Extracting a method from a function with no self argument.r   r  )
list_extracting_methodr?  rY  r   removeinsertr:   rm   _get_comma_formr   r_   prefix	self_names       r    ra  z+_ExtractMethodParts._get_function_signaturex  s    Dz""$(D(D(F++-I &P  D I&KK9%		***Vd6J6J46P-PPPr"   c                     | j                          xrS | j                  j                  xr; | j                  j                   xr" t	        | j                  j
                        dk(  S )NrT   )r>  r:   rT   r/   r   r   r}   s    r    ro  z&_ExtractMethodParts._extracting_method  sY    0022 
II @II)))@"499??3x?	
r"   c                 D    | j                         ry| j                         S )NrH   )r?  _get_scope_self_namer}   s    r    rY  z"_ExtractMethodParts._get_self_name  s     '')((**r"   c                     | j                   j                  j                  j                         dk(  ry | j                   j                  j                  j	                         }|r|d   S y )NrO   r   )r:   r   r  r   get_param_names)r   param_namess     r    rx  z(_ExtractMethodParts._get_scope_self_name  sR    99??##,,..@iioo..>>@q>! r"   c                     dj                  | j                  |      | j                  j                  | j	                  |            S )Nz{prefix}{name}({args}))rt  r   r_   )format_get_function_call_prefixr:   rm   rr  )r   r_   s     r    _get_function_callz&_ExtractMethodParts._get_function_call  sC    '..11$7##%%d+ / 
 	
r"   c                 p   d}| j                   j                  r| j                   j                  s| j                         s| j	                         r=| j                   j
                  j                  j                  j                         dz   }|S | j                         }||v r|j                  |       |dz   }|S )Nr   .)r:   rT   r/   r>  r?  r   r   r  get_namerY  rp  rs  s       r    r~  z-_ExtractMethodParts._get_function_call_prefix  s    99DII$9$9,,.$2N2N2P//88AACcI 	 !//1	$KK	*"Sr"   c                 $    dj                  |      S )N, )re  )r   namess     r    rr  z#_ExtractMethodParts._get_comma_form  s    yyr"   c                 V   | j                         }| j                         }d}|rZ| j                  j                  r| j                  j                  r.| j                  j                  rdnd}| j                  |      |z   }| j                  j                  rd}|| j                  |      z   S )Nr   z :=  = return )rI  rJ  r:   r   r   rr  r   r  )r   r_   rf  call_prefixassignment_operators        r    rL  z_ExtractMethodParts._get_call  s    ,,.--/DII..$))2P2P,0II,>,>&E..w7:MMK99#KT44T:::r"   c                    | j                   j                  rc| j                   j                  sMt        | j                  j
                  | j                  j                  z  | j                  j                  z        S | j                   j                  s| j                  j                  | j                  j
                  z  }|| j                  j                  | j                  j                  z  | j                  j                  | j                  j                  z
  z  z  }t        |      S | j                   j                  d   }|| j                   j                  d   k(  r;|t        j                  d| j                   j                        j!                         z   }| j                   j"                  || j                   j                  d    }t$        j'                  |      }t        | j                  j                  j)                  |            S )Nr   z\Sr%   )r:   r9   r/   rn  r7  r   postreadwrittenr   
prewrittenmaybe_writtenrt   r{   researchr   rp   r)   _VariableReadsAndWritesFinderfind_reads_for_one_linersintersection)r   rT  rp   function_definitionr   s        r    rI  z,_ExtractMethodParts._find_function_arguments  s    99TYY%:%:##((%%../%%--. 
 yy!!((33d6I6I6N6NNF##..%%../&&44t7J7J7R7RRTF
 <		  #DII**1--BIIeTYY-@-@AGGIIE"ii..utyy7G7G7JK,FF
 D''22??EFFr"   c                    | j                   j                  rO| j                  j                  | j                  j                  z  }t        || j                  j                  z        S | j                   j                  rg S | j                  j                  | j                  j                  z  }t        || j                  j                  z        S r   )r:   r   r7  r  r  rn  r  r   )r   r  s     r    rJ  z*_ExtractMethodParts._find_function_returns  s    99))11D4G4G4U4UUG$"5"5">">>??99I%%--0C0C0Q0QQGd11:::;;r"   c                 p    | j                   j                  r| j                         S | j                  |      S r   )r:   r   $_get_single_expression_function_body_get_multiline_function_body)r   rf  s     r    rb  z1_ExtractMethodParts._get_unindented_function_body  s/    99<<>>0099r"   c                     t        j                  | j                  j                  d      }| j	                  |      }| j                  |      }|r|d| j                  |      z  z  }|S )Nr   z

return %s)r   r   r:   r   _insert_globals_insert_nonlocalsrr  )r   rf  rg  s      r    r  z0_ExtractMethodParts._get_multiline_function_body  sa    %55dii6I6I1M..?00A}t/C/CG/LLLOr"   c                     t        | j                  j                  | j                        }d|z   }| j                  |      }| j	                  |      }|S )Nr:   r  )_get_single_expression_bodyr:   r   r  r  )r   r   r   s      r    r  z8_ExtractMethodParts._get_single_expression_function_body  sK    /		0C0C$))T	9$##D)%%d+r"   c                     | j                  |      }| j                  j                  | j                  j                  | j                  j                  z  z  }||z
  }|r!dj                  dj                  |      |      }|S )Nzglobal {}
{}r  )_get_globals_in_bodyr7  globals_r  r  r}  re  )r   rg  globals_in_bodyr  s       r    r  z#_ExtractMethodParts._insert_globals  s}    33OD&&//''$*=*=*K*KK
 o--44		(#_O r"   c                     | j                  |      }| j                  j                  | j                  j                  | j                  j                  z  z  }||z
  }|r!dj                  dj                  |      |      }|S )Nznonlocal {}
{}r  )_get_nonlocals_in_bodyr7  
nonlocals_r  r  r}  re  )r   rg  nonlocals_in_bodyr  s       r    r  z%_ExtractMethodParts._insert_nonlocals  s     77H((33''$*=*=*K*KK

  "33
/66		*%O r"   c                 f    t        |       }t               }|j                  |       |j                  S r   )r   _GlobalFinderr(  r  rg  r   visitors      r    r  z(_ExtractMethodParts._get_globals_in_body  s+    ?+/dr"   c                 f    t        |       }t               }|j                  |       |j                  S r   )r   _NonlocalFinderr(  r  r  s      r    r  z*_ExtractMethodParts._get_nonlocals_in_body  s,    ?+!#d!!!r"   N)%rJ   rK   rL   r!   r8  r9  r;  r<  r   r   r   rQ  rP  r   r6  rF  r`  r?  r>  ra  ro  rY  rx  r  r~  rr  rL  rI  rJ  rb  r  r  r  r  rO   r  r  rQ   r"   r    r   r     s    "

Y<GGK
!,/0Q
+
"

 	;G4<:
     " "r"   r   c                   *    e Zd Zd Zd Zd Zd Zd Zy)r   c                     || _         y r   r  r   s     r    r!   z_ExtractVariableParts.__init__  s	    	r"   c                     t        | j                  j                  | j                        }| j                  j                  dz   |z   dz   S )Nr  r  r_  )r  r:   r   rm   )r   r   s     r    r   z$_ExtractVariableParts.get_definition  s;    /		0C0C$))T	yy!!E)I5<<r"   c                 P    d| j                   j                  j                         z  S )Nr  )r:   r   r   r}   s    r    r   z&_ExtractVariableParts.get_body_pattern#  s     		++11333r"   c                 .    | j                   j                  S r   )r:   rm   r}   s    r    r   z-_ExtractVariableParts.get_replacement_pattern&  s    yy!!!r"   c                     i S r   rQ   r}   s    r    r   z _ExtractVariableParts.get_checks)  s    	r"   N)rJ   rK   rL   r!   r   r   r   r   rQ   r"   r    r   r     s    =4"r"   r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zed        Zed        Zy)r\  c                 V   || _         || _        || _        t               | _        t               | _        t               | _        t               | _        t               | _        t               | _	        d| _
        d| _        t               | _        t               | _        d| _        d| _        y )NTFr   )rp   rq   	is_globalr   r  r  r  r   r  postwrittenhost_functionconditionalr  r  surrounded_by_loop
loop_depth)r   rp   rq   r  s       r    r!   z&_FunctionInformationCollector.__init__.  s    
"$,'\!|L	"%<! "$,"#r"   c                 B   | j                   |cxk  r| j                  k  rFn nC|| j                  vr5| j                  r|| j                  vr| j
                  j                  |       | j                  |k  r+|| j                  vr| j                  j                  |       y y y r   )	rp   rq   r  r  r  r   addr  r  r   r   r   s      r    _read_variablez,_FunctionInformationCollector._read_variable?  s    ::+488+4<<'''4t7I7I+IIIMM$'88f4+++!!$' , r"   c                    | j                   |cxk  r| j                  k  r~n n{| j                  r| j                  j	                  |       n| j
                  j	                  |       | j                  dkD  r)|| j                  v r| j                  j	                  |       | j                   |kD  r| j                  j	                  |       | j                  |k  r| j                  j	                  |       y y r   )rp   rq   r  r  r  r  r  r   r  r  r  r  s      r    _written_variablez/_FunctionInformationCollector._written_variableH  s    ::+488+""&&t,  &"ttyy'8!!$'::OO%88f  & r"   c                    | j                   sl| j                  r`d| _        t        |j                        D ]  }| j	                  ||j
                           |j                  D ]  }| j                  |        y | j	                  |j                  |j
                         t               }|j                  D ]  }|j                  |        |j                  |j                  z
  D ]  }| j                  ||j
                           y NF)r  r  _get_argnamesr_   r  r   r   r(  r   r  r   r  r  )r   r   r   childr  s        r    _FunctionDefz*_FunctionInformationCollector._FunctionDefU  s    ~~$"4"4!&D%dii0 :&&tT[[9: "

5!" ""499dkk:35G %e$%w6 7##D$++67r"   c                 J     | j                   j                  |j                    y r   r  r  r  r   r   s     r    _Globalz%_FunctionInformationCollector._Globald      4::&r"   c                 J     | j                   j                  |j                    y r   r  r  r  r  s     r    	_Nonlocalz'_FunctionInformationCollector._Nonlocalg      TZZ(r"   c                 &    | j                  |       y r   )r  r  s     r    _AsyncFunctionDefz/_FunctionInformationCollector._AsyncFunctionDefj  s    $r"   c                 N   t        |j                  t        j                  t        j                  f      r&| j                  |j                  |j                         t        |j                  t        j                        s'| j                  |j                  |j                         y y r   )	r   r   r   r   AugStorer  idr   r  r  s     r    _Namez#_FunctionInformationCollector._Namem  sd    dhhCLL 9:""477DKK8$((CII.5 /r"   c                     | j                  |j                  |j                         |j                  r| j	                  |j                         y y r   )r  r   r   patternr(  r  s     r    _MatchAsz&_FunctionInformationCollector._MatchAss  s6    tyy$++6<<JJt||$ r"   c                 ~    | j                  |j                         |j                  D ]  }| j                  |        y r   )r(  r   targetsr   r   r  s      r    _Assignz%_FunctionInformationCollector._Assignx  s1    

4::\\ 	EJJu	r"   c                 ~   | j                  |j                         t        |j                  t        j
                        rc|j                  j                  }| j                  ||j                  j                         | j                  ||j                  j                         y | j                  |j                         y r   )
r(  r   r   targetr   Namer  r  r   r  )r   r   	target_ids      r    
_AugAssignz(_FunctionInformationCollector._AugAssign}  su    

4::dkk388,I	4;;+=+=>""9dkk.@.@AJJt{{#r"   c                 P    | j                  |j                  |j                         y r   )r  r   r   r  s     r    	_ClassDefz'_FunctionInformationCollector._ClassDef  s    tyy$++6r"   c                 &    | j                  |       y r   	_comp_expr  s     r    	_ListCompz'_FunctionInformationCollector._ListComp      tr"   c                 &    | j                  |       y r   r  r  s     r    _GeneratorExpz+_FunctionInformationCollector._GeneratorExp  r  r"   c                 &    | j                  |       y r   r  r  s     r    _SetCompz&_FunctionInformationCollector._SetComp  r  r"   c                 &    | j                  |       y r   r  r  s     r    	_DictCompz'_FunctionInformationCollector._DictComp  r  r"   c                     t         j                        }t         j                        }t         j                        }t	        j
                  |      D ]  } j                  |        t        t        j                   fd|j                  D                    } j                  |z
  |z   _         j                  |z
  |z   _         j                  |z
  |z   _        y )Nc              3   T   K   | ]  }j                  |j                         ! y wr   )_flatten_nested_tuple_of_namesr  ).0	generatorr   s     r    	<genexpr>z:_FunctionInformationCollector._comp_exp.<locals>.<genexpr>  s*        33I4D4DE s   %()r   r   r  r  r   iter_child_nodesr(  rn  r   from_iterable
generators)r   r   r   r  r  r  
comp_namess   `      r    r  z'_FunctionInformationCollector._comp_exp  s    $))$T\\*"4#5#56))$/ 	EJJu	   !%  

 II
*T1	||j07:!//*<}Lr"   c              #      K   t        |t        j                        r%|j                  D ]  }| j	                  |        y t        |t        j
                        r|j                   y J d|       w)Nz3Unexpected node type in list comprehension target: )r   r   Tupleeltsr  r  r  )r   r   elts      r    r  z<_FunctionInformationCollector._flatten_nested_tuple_of_names  sb     dCII&yy ?99#>>?chh'''MXOPTxXX5s   A3A5c                 &    | j                  |       y r   )_handle_conditional_noder  s     r    _Ifz!_FunctionInformationCollector._If  s    %%d+r"   c                 r    | j                  |      5  | j                  |       d d d        y # 1 sw Y   y xY wr   )_handle_loop_contextr  r  s     r    _Whilez$_FunctionInformationCollector._While  s3    &&t, 	0))$/	0 	0 	0s   -6c                    | j                  |      5  | j                  |      5  | j                  |j                         | j                  |j                         |j
                  D ]  }| j                  |        |j                  D ]  }| j                  |        	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr   )r  _handle_conditional_contextr(  iterr  r   orelser  s      r    _Forz"_FunctionInformationCollector._For  s    &&t, 	"d.N.Nt.T 	"JJtyy!JJt{{# "

5!" "

5!"	" 	" 	" 	" 	" 	"s#   B=A;B1 B=1B:	6B==Cc                     | j                  |      5  t        j                  |      D ]  }| j                  |        	 d d d        y # 1 sw Y   y xY wr   )r  r   r  r(  r  s      r    r  z6_FunctionInformationCollector._handle_conditional_node  sK    --d3 	"--d3 "

5!"	" 	" 	"s   ,AAc              #      K   | j                   |j                  cxk  r| j                  k  r
n nd| _        	 d  d| _        y # d| _        w xY ww)NTF)rp   r   rq   r  r  s     r    r  z9_FunctionInformationCollector._handle_conditional_context  s?     ::00#D	%$DuDs   4AA A	AAc              #      K   |j                   | j                  k  r| xj                  dz  c_        	 d  | xj                  dz  c_        y # | xj                  dz  c_        w xY wwr$   )r   rp   r  r  s     r    r  z2_FunctionInformationCollector._handle_loop_context  sH     ;;#OOq O	!OOq ODOOq Os   /A&A A&A##A&N)rJ   rK   rL   r!   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  rQ   r"   r    r\  r\  -  s    "('7') 6%

$7M$Y,0	""
 % % ! !r"   r\  c                    g }|j                  d t        | dg       D               |j                  d | j                  D               | j                  r%|j	                  | j                  j
                         | j                  r%|j	                  | j                  j
                         |j                  d | j                  D               |S )Nc              3   4   K   | ]  }|j                     y wr   argr  r   s     r    r  z _get_argnames.<locals>.<genexpr>  s     Mt$((M   posonlyargsc              3   4   K   | ]  }|j                     y wr   r  r  s     r    r  z _get_argnames.<locals>.<genexpr>  s     6t$((6r  c              3   4   K   | ]  }|j                     y wr   r  r  s     r    r  z _get_argnames.<locals>.<genexpr>  s     <t$((<r  )rH  getattrr_   varargr   r  kwarg
kwonlyargs)	argumentsrT  s     r    r  r    s    F
MMMwy-'LMM
MM6y~~66i&&**+ioo))*
MM<y';';<<Mr"   c                   D    e Zd Zd Zd Zd Zd Zed        Zed        Z	y)r  c                 @    t               | _        t               | _        y r   )setr  r   r}   s    r    r!   z&_VariableReadsAndWritesFinder.__init__  s    uE	r"   c                 6   t        |j                  t        j                  t        j                  f      r%| j
                  j                  |j                         t        |t        j                        s&| j                  j                  |j                         y y r   )	r   r   r   r   r  r  r  r  r   r  s     r    r  z#_VariableReadsAndWritesFinder._Name  sZ    dhhCLL 9:LLTWW%$		*IIMM$''" +r"   c                    | j                   j                  |j                         t               }t	        j
                  |      D ]  }|j                  |        | j                  j                  |j                  |j                   z
         y r   )	r  r  r   r  r   r  r(  r   update)r   r   r  r  s       r    r  z*_VariableReadsAndWritesFinder._FunctionDef  se    #/1))$/ 	!EMM% 	!		78r"   c                 N    | j                   j                  |j                         y r   )r  r  r   r  s     r    _Classz$_VariableReadsAndWritesFinder._Class  s    #r"   c                     | j                         dk(  rt               t               fS t        |       }t               }|j	                  |       |j
                  |j                  fS Nr   )r   r  r   r  r(  r   r  coder   r  s      r    find_reads_and_writesz3_VariableReadsAndWritesFinder.find_reads_and_writes  sO    ::<25#%<4 /1d||W__,,r"   c                     | j                         dk(  rt               t               fS t        |       }t               }|j	                  |       |j
                  S r"  )r   r  r   r  r(  r   r#  s      r    r  z7_VariableReadsAndWritesFinder.find_reads_for_one_liners  sF    ::<25#%<4 /1d||r"   N)
rJ   rK   rL   r!   r  r  r   rO   r%  r  rQ   r"   r    r  r    s>    #9$ - -  r"   r  c                       e Zd Zed        Zy)_BaseErrorFinderc                     |j                         dk(  ryt        |      } |        }|j                  |       |j                  S )Nr   F)r   r   r(  r*  )rH   r$  r   r  s       r    r  z_BaseErrorFinder.has_errors  s:    ::<24 %d}}r"   N)rJ   rK   rL   rP   r  rQ   r"   r    r(  r(    s     r"   r(  c                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)r  c                      d| _         d| _        y )NFr   )r*  
loop_countr}   s    r    r!   z(_UnmatchedBreakOrContinueFinder.__init__  s    
r"   c                 &    | j                  |       y r   loop_encounteredr  s     r    r  z$_UnmatchedBreakOrContinueFinder._For      d#r"   c                 &    | j                  |       y r   r.  r  s     r    r  z&_UnmatchedBreakOrContinueFinder._While!  r0  r"   c                 r   | xj                   dz  c_         |j                  D ]  }| j                  |        | xj                   dz  c_         |j                  r_t	        |j                  t
        t        f      r#|j                  D ]  }| j                  |        y | j                  |j                         y y r$   )r,  r   r(  r  r   rn  tuple)r   r   r  node_s       r    r/  z0_UnmatchedBreakOrContinueFinder.loop_encountered$  s    1YY 	EJJu	1;;$++e}5![[ &EJJu%& 

4;;' r"   c                 $    | j                          y r   
check_loopr  s     r    _Breakz&_UnmatchedBreakOrContinueFinder._Break0      r"   c                 $    | j                          y r   r6  r  s     r    	_Continuez)_UnmatchedBreakOrContinueFinder._Continue3  r9  r"   c                 2    | j                   dk  rd| _        y y )Nr%   T)r,  r*  r}   s    r    r7  z*_UnmatchedBreakOrContinueFinder.check_loop6  s    ??QDJ r"   c                      y r   rQ   r  s     r    r  z,_UnmatchedBreakOrContinueFinder._FunctionDef:      r"   c                      y r   rQ   r  s     r    r  z)_UnmatchedBreakOrContinueFinder._ClassDef=  r>  r"   N)rJ   rK   rL   r!   r  r  r/  r8  r;  r7  r  r  rQ   r"   r    r  r    s/    $$
(r"   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)r&  c                     d| _         y r  r*  r}   s    r    r!   z_AsyncStatementFinder.__init__B  s	    
r"   c                     d| _         y NTrB  r  s     r    	_AsyncForz_AsyncStatementFinder._AsyncForE  	    
r"   c                     d| _         y rD  rB  r  s     r    
_AsyncWithz _AsyncStatementFinder._AsyncWithH  rF  r"   c                      y r   rQ   r  s     r    r  z"_AsyncStatementFinder._FunctionDefK  r>  r"   c                      y r   rQ   r  s     r    r  z_AsyncStatementFinder._ClassDefN  r>  r"   N)rJ   rK   rL   r!   rE  rH  r  r  rQ   r"   r    r&  r&  A  s    r"   r&  c                   L     e Zd ZdZdedefdZdej                  f fdZ xZ	S )r'  z
    Flag an error if we are including the start of a block without the end.
    We detect this by ensuring there is no AST node that starts inside the
    selected range but ends outside of it.
    r   r   c                 .    d| _         || _        || _        y r  )r*  r   r   )r   r   r   s      r    r!   z _UnbalancedRegionFinder.__init__Y  s    
$ r"   r   c                 F   t        |d      st        | 	  |       y |j                  | j                  k  }|j
                  | j                  kD  }|s|ry |j
                  | j                  k\  }|j                  | j                  kD  }|r
|rd| _        y t        | 	  |       y )N
end_linenoT)r%  r^   generic_visitrN  r   r   r   r*  )r   r   ends_before_range_startsstarts_after_range_endsstarts_on_or_after_range_startends_after_range_endsra   s         r    rO  z%_UnbalancedRegionFinder.generic_visit^  s    t\*G!$'#'??T__#D "&++"=#'>)-)G& $$-- ?).CDJd#r"   )
rJ   rK   rL   r   intr!   r   ASTrO  rc   rd   s   @r    r'  r'  R  s0    !3 !# !
$#'' $ $r"   r'  c                       e Zd Zd Zd Zy)r  c                 "    t               | _        y r   )r   r  r}   s    r    r!   z_GlobalFinder.__init__o  s    "r"   c                 J     | j                   j                  |j                    y r   r  r  s     r    r  z_GlobalFinder._Globalr  r  r"   N)rJ   rK   rL   r!   r  rQ   r"   r    r  r  n  s    %'r"   r  c                       e Zd Zd Zd Zy)r  c                 "    t               | _        y r   )r   r  r}   s    r    r!   z_NonlocalFinder.__init__w  s    $,r"   c                 J     | j                   j                  |j                    y r   r  r  s     r    r  z_NonlocalFinder._Nonlocalz  r  r"   N)rJ   rK   rL   r!   r  rQ   r"   r    r  r  v  s    ')r"   r  c                 6    | j                   j                         S r   )r  r   )r   s    r    r   r   ~  s    >>""$$r"   c           	      l   t        j                  | d      } 	 t        j                  |       }|S # t        $ r{ 	 t        j                  d| z   dz         }Y |S # t        $ rO t        j                  dt        j                  | d      z         }|j
                  d   j
                  |_        Y Y |S w xY ww xY w)Nr   ()z"async def __rope_placeholder__():
   )r   r   r   parser  r   )r   r   s     r    r   r     s    &&tQ/D*yy K  	*	*99S4Z#-.D K  	*995--dA67D 		!))DIK	*		*s'   / 	B3AAB/*B3.B//B3c                     g }| j                         D ]U  }|j                  d      r#|j                  |d d j                                7|j                  |j                                W dj	                  |      S )N\r    )
splitlinesendswithr   r   re  )r$  rl   lines      r    _join_linesrh    sd    E! '==LLcr*+LL&	'
 88E?r"   c                 $   t        j                  | d      } | j                         d   dv xr | j                         d   dv }| j	                  d      dk\  xr |}|st        |       } d| v }|j                  s|j                  s|r
|sd| z   dz   } | S )	Nr   z({[r   z)}]r_     r^  r_  )r   r   lstriprstripr+  rh  r   r   )r   r:   already_parenthesizedlarge_multilinemultiline_expressions        r    r  r    s    ++Iq9I1&J9+;+;+=b+AU+J   ood+q0J5JO	*	9,!!'' )Oc)	r"   )/r  
contextlibr   	itertoolsr   typingr   	rope.baser   r   rope.base.changer   r	   rope.base.exceptionsr   rope.base.utils.datastructuresr   rope.refactorr   r   r   r   r   r   rS   r[   r2   r   r5   r   r   RopeNodeVisitorr   r   r\  r  r  r(  r  r&  r'  r  r  r   r   rh  r  rQ   r"   r    <module>ry     sF   	 %   & 6 1 5 U U6@A @AF?' ?	) 	K) K)\
( 
(l. l.^)- )-XX- X-vI"#-- I"X $j!C$7$7 j!Z	%C$7$7 %Ps** %&6 %P, "$. $8'C'' ')c)) )%"r"   