ó
†Å•Zc           @   sd   d  d l  Z d  d l m Z m Z m Z m Z d  d l m Z m	 Z	 m
 Z
 d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t
   exceptionst   evaluatet   wordert   codeanalyze(   t   functionutilst   sourceutilst   occurrencest   IntroduceParameterc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s­  Introduce parameter refactoring

    This refactoring adds a new parameter to a function and replaces
    references to an expression in it with the new parameter.

    The parameter finding part is different from finding similar
    pieces in extract refactorings.  In this refactoring parameters
    are found based on the object they reference to.  For instance
    in::

      class A(object):
          var = None

      class B(object):
          a = A()

      b = B()
      a = b.a

      def f(a):
          x = b.a.var + a.var

    using this refactoring on ``a.var`` with ``p`` as the new
    parameter name, will result in::

      def f(p=a.var):
          x = p + p

    c         C   s¿   | |  _  | |  _ | |  _ |  j  j |  j ƒ |  _ |  j j ƒ  j | ƒ } | j ƒ  d k ro t j	 d ƒ ‚ n  | j
 |  _ |  j ƒ  \ |  _ |  _ |  j d  k r» t j	 d |  j ƒ ‚ n  d  S(   Nt   Functions8   Introduce parameter should be performed inside functionss"   Cannot find the definition of <%s>(   t   projectt   resourcet   offsett   get_pymodulet   pymodulet	   get_scopet   get_inner_scope_for_offsett   get_kindR    t   RefactoringErrort   pyobjectt
   pyfunctiont   _get_name_and_pynamet   namet   pynamet   None(   t   selfR	   R
   R   t   scope(    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyt   __init__%   s    			c         C   s(   t  j |  j j ƒ  ƒ } | j |  j ƒ S(   N(   R   t   WorderR
   t   readt   get_primary_atR   (   R   t   word_finder(    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyt   _get_primary4   s    c         C   s.   t  j |  j |  j ƒ t j |  j |  j ƒ f S(   N(   R   t   get_name_atR
   R   R   t   eval_locationR   (   R   (    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyR   8   s    c   
      C   sí   t  j j |  j ƒ } | j j | |  j ƒ  f ƒ t j |  j	 j ƒ  ƒ } |  j
 ƒ  \ } } t j |  j ƒ \ } } | j | | | j ƒ  ƒ |  j | | | | ƒ t j j j d | ƒ } t j j j |  j	 | j ƒ  ƒ }	 | j |	 ƒ | S(   Ns   Introduce parameter <%s>(   R   t   DefinitionInfoR   R   t   args_with_defaultst   appendR   R   t   ChangeCollectorR
   t   _get_header_offsetsR   t   get_body_regiont
   add_changet	   to_stringt   _change_function_occurancest   ropet   baset   changet	   ChangeSett   ChangeContentst   get_changed(
   R   t   new_parametert   definition_infot	   collectort   header_startt
   header_endt
   body_startt   body_endt   changesR-   (    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyt   get_changes<   s     

c         C   s™   |  j  j } |  j j ƒ  j ƒ  } |  j  j j | ƒ d } | j | ƒ } | j | ƒ } |  j  j	 j
 d | ƒ d } |  j  j	 j d | | ƒ } | | f S(   Ni   t   defi   t   :(   R   t   linesR   R   t	   get_startt   logical_linest   logical_line_int   get_line_startt   get_line_endt   source_codet   findt   rfind(   R   R<   t
   start_linet   end_linet   startt   end(    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyR&   N   s    c   	      C   s†   t  j |  j |  j |  j ƒ } xa | j d |  j ƒ D]J } | j ƒ  \ } } | | k oc | k  n r4 | j | | | ƒ q4 q4 Wd  S(   NR
   (	   R   t   create_finderR	   R   R   t   find_occurrencesR
   t   get_primary_rangeR(   (	   R   R3   t   function_startt   function_endt   new_namet   findert
   occurrenceRG   RH   (    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyR*   Y   s    (	   t   __name__t
   __module__t   __doc__R   R   R   R9   R&   R*   (    (    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyR      s   					(   t   rope.base.changeR+   t	   rope.baseR    R   R   R   t   rope.refactorR   R   R   t   objectR   (    (    (    s@   lib/python2.7/site-packages/rope/refactor/introduce_parameter.pyt   <module>   s   "