a
    ]9a                     @   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u r\| 	 }|j
dt|d}t|||S )aM  Return a list of `Location`

    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` that
    should be searched for occurrences; if `None` all python files
    in the project are searched.

    c                    s    S N 
occurrenceunsurer   2lib/python3.9/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u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u 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`.
    NmethodzNot a method!zCannot resolve the identifier!c                 S   s   |   sdS d S NF)
is_definedr	   r   r   r   r)   E   s    z(find_implementations.<locals>.is_definedc                    s   |      krdS d S r(   )Z
get_pyname
get_objectr	   r#   r   r   not_selfI   s    z&find_implementations.<locals>.not_selfT)filterszFinding Implementationsr   )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_implementations0   s&    


r0      c                    s   t | |||}||}|dur| \}}tjj||}	|dur|j	
|  fdd}
t|}t| |	|
|g}|j|dD ]}t|  S 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 r(   )r   r	   startr   r   check_offsetc   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!   r4   Zpyname_filterr$   r
   r   r2   r   find_definitionU   s    

r:   c                   @   s   e Zd Zdd ZdS )r6   c                 C   s4   |j | _ | | _| jd | _| | _|j| _d S )Nr   )r   Zget_word_rangeZregionr   Z	is_unsurer   r9   )selfr
   r   r   r   __init__n   s
    

zLocation.__init__N)__name__
__module____qualname__r<   r   r   r   r   r6   m   s   r6   c                 C   sD   g }|D ]6}| |j | |D ]}|t| q"|  q|S r   )Zstarted_jobpathr&   appendr6   Zfinished_job)r$   r   r%   resultr   r
   r   r   r   r   v   s    
r   )Nr1   )Zrope.base.codeanalyzer   Zrope.base.evaluateZrope.base.pyobjectsZ	rope.baser   r   r   Zrope.contribr   Zrope.refactorr   ZNullTaskHandler&   r0   r:   objectr6   r   r   r   r   r   <module>   s   
(
%
	