B
    SA                 @   s   d dl Zd dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ ddde fddZde fdd	ZdddZG dd deZdd ZdS )    N)
taskhandle
exceptionsworder)	fixsyntax)occurrencesFc                sz   t ||}| |}tjj||\}	}
 fdd}tj| ||
|||	d}|dkr\| 	 }|j
dt|d}t|||S )aQ  Return a list of `Location`\s

    If `unsure` is `True`, possible matches are returned, too.  You
    can use `Location.unsure` to see which are unsure occurrences.
    `resources` can be a list of `rope.base.resource.File`\s that
    should be searched for occurrences; if `None` all python files
    in the project are searched.

    c                s    S )N )
occurrence)unsurer   2lib/python3.7/site-packages/rope/contrib/findit.pyis_match   s    z"find_occurrences.<locals>.is_match)r	   in_hierarchyinstanceNzFinding Occurrences)count)r   get_name_atget_pymoduleropebaseevaluateZeval_location2r   Zcreate_finderget_python_filescreate_jobsetlen_find_locations)projectresourceoffsetr	   	resourcesr   task_handlenamethis_pymoduleZprimarypynamer   finderjob_setr   )r	   r
   find_occurrences	   s    

r"   c                s   t ||}| |}tjj||  dk	r^  }t|tjj	j
rR| dkrhtdn
tddd } fdd}	||	t d	g}
tj| ||
d
}|dkr|  }|jdt|d}t|||S )zFind the places a given method is overridden.

    Finds the places a method is implemented.  Returns a list of
    `Location`\s.
    NmethodzNot a method!zCannot resolve the identifier!c             S   s   |   sdS d S )NF)
is_defined)r   r   r   r
   r$   8   s    z(find_implementations.<locals>.is_definedc                s   |      krdS d S )NF)Z
get_pyname
get_object)r   )r   r   r
   not_self<   s    z&find_implementations.<locals>.not_selfT)filterszFinding Implementations)r   )r   r   r   r   r   r   Zeval_locationr%   
isinstanceZ	pyobjectsZ
PyFunctionZget_kindr   ZBadIdentifierErrorr   ZInHierarchyFilterFinderr   r   r   r   )r   r   r   r   r   r   r   Zpyobjectr$   r&   r'   r    r!   r   )r   r
   find_implementations&   s&    

r*      c                s   t | |||}||}|dk	r| \}}tjj||}	|dk	r|j	
|  fdd}
t|}t| |	|
|g}x|j|dD ]
}t|S W dS )zReturn the definition location of the python name at `offset`

    A `Location` object is returned if the definition location can be
    determined, otherwise ``None`` is returned.
    Nc                s   | j  k rdS d S )NF)r   )r   )startr   r
   check_offsetW   s    
z%find_definition.<locals>.check_offset)Zpymodule)r   Z	FixSyntaxZ	pyname_atZget_definition_locationr   r   r   ZWorderZget_word_atlinesZget_line_startr   ZPyNameFilterr)   r"   Location)r   coder   r   ZmaxfixesZfixerr   modulelinenor   r-   Zpyname_filterr    r   r   )r,   r
   find_definitionI   s    


r3   c               @   s   e Zd Zdd ZdS )r/   c             C   s4   |j | _ | | _| jd | _| | _|j| _d S )Nr   )r   Zget_word_rangeZregionr   Z	is_unsurer	   r2   )selfr   r   r   r
   __init__c   s
    

zLocation.__init__N)__name__
__module____qualname__r5   r   r   r   r
   r/   a   s   r/   c             C   sL   g }xB|D ]:}| |j x | |D ]}|t| q&W |  q
W |S )N)Zstarted_jobpathr"   appendr/   Zfinished_job)r    r   r!   resultr   r   r   r   r
   r   k   s    
r   )Nr+   )Zrope.base.codeanalyzer   Zrope.base.evaluateZrope.base.pyobjectsZ	rope.baser   r   r   Zrope.contribr   Zrope.refactorr   ZNullTaskHandler"   r*   r3   objectr/   r   r   r   r   r
   <module>   s   "

