
    c2                     j   d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
mZ  G d d          Z	 	 	 	 	 	 	 ddZ G d	 d
          Zd ZddZ G d d          Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          ZdS )a  Find occurrences of a name in a project.

This module consists of a `Finder` that finds all occurrences of a name
in a project. The `Finder.find_occurrences()` method is a generator that
yields `Occurrence` instances for each occurrence of the name. To create
a `Finder` object, use the `create_finder()` function:

    finder = occurrences.create_finder(project, 'foo', pyname)
    for occurrence in finder.find_occurrences():
        pass

It's possible to filter the occurrences. They can be specified when
calling the `create_finder()` function.

  * `only_calls`: If True, return only those instances where the name is
    a function that's being called.

  * `imports`: If False, don't return instances that are in import
    statements.

  * `unsure`: If a predicate function, return instances where we don't
    know what the name references. It also filters based on the
    predicate function.

  * `docs`: If True, it will search for occurrences in regions normally
    ignored. E.g., strings and comments.

  * `in_hierarchy`: If True, it will find occurrences if the name is in
    the class's hierarchy.

  * `instance`: Used only when you want implicit interfaces to be
    considered.

  * `keywords`: If False, don't return instances that are the names of keyword
    arguments
    N)astcodeanalyzeevaluate
exceptionspynames	pyobjectsutilsworderc                   "    e Zd ZdZddZddZdS )Findera  For finding occurrences of a name

    The constructor takes a `filters` argument.  It should be a list
    of functions that take a single argument.  For each possible
    occurrence, these functions are called in order with the an
    instance of `Occurrence`:

      * If it returns `None` other filters are tried.
      * If it returns `True`, the occurrence will be a match.
      * If it returns `False`, the occurrence will be skipped.
      * If all of the filters return `None`, it is skipped also.

    NFc                 v    |d g}|| _         || _        || _        || _        t	          ||          | _        d S )Nc                     dS NT )os    9lib/python3.11/site-packages/rope/refactor/occurrences.py<lambda>z!Finder.__init__.<locals>.<lambda>G   s         )docs)projectnamer   filters_TextualFinder_textual_finder)selfr   r   r   r   s        r   __init__zFinder.__init__E   sK     	'%~&G		-d>>>r   c              #      K   t          | j        ||| j                  }| j                            |j                  D ]0}t          ||          }| j        D ]} ||          }||r|V   1dS )zGenerate `Occurrence` instances)resourcepymoduler   N)_OccurrenceToolsCreatorr   r   r   find_offsetssource_code
Occurrencer   )r   r   r   toolsoffset
occurrencefilterresults           r   find_occurrenceszFinder.find_occurrencesN   s      'L8hTY
 
 
 *778IJJ 	 	F#E622J,  
++  %$$$$	 	r   NF)NN)__name__
__module____qualname____doc__r   r)   r   r   r   r   r   6   sF         ? ? ? ?     r   r   FTc
                    |h}
g }|r!|                     t                                 |s!|                     t                                 |	s!|                     t                                 t	          |t
          j                  rH|                                D ]3}	 |
                    ||                    # t          j
        $ r Y 0w xY w|
D ]H}|                     t          |                     |r"|                     t          |                     I|r"|                     t          |                     t          | |||          S )zA factory for `Finder`

    Based on the arguments it creates a list of filters.  `instance`
    argument is needed only when you want implicit interfaces to be
    considered.

    )r   r   )appendCallsFilterNoImportsFilterNoKeywordsFilter
isinstancer   ParameterNameget_objectsaddr   AttributeNotFoundErrorPyNameFilterInHierarchyFilterUnsureFilterr   )r   r   pyname
only_callsimportsunsurer   instancein_hierarchykeywordspynames_r   pyobjects                r   create_finderrE   ^   sf   & xHG &{}}%%% *(())) +'))***(G122  ,,.. 	 	HXd^,,,,4    6 6|F++,,, 	6NN,V44555 -|F++,,,'4t<<<<s    B<<CCc                      e Zd Zd Zej        d             Zej        d             Zej        d             Zej        d             Z	ej        d             Z
d Zd Zd	 Zd
 Zd Zd Zeej        d                         ZdS )r#   c                 :    || _         || _        |j        | _        d S N)r$   r%   r   )r   r$   r%   s      r   r   zOccurrence.__init__   s    
r   c                 J    | j         j                            | j                  S rH   )r$   word_finderget_word_ranger%   r   s    r   rK   zOccurrence.get_word_range   s    z%44T[AAAr   c                 J    | j         j                            | j                  S rH   )r$   rJ   get_primary_ranger%   rL   s    r   rN   zOccurrence.get_primary_range   s    z%77DDDr   c                     t          j        t          j                  5  | j        j                            | j                  cd d d            S # 1 swxY w Y   d S rH   )
contextlibsuppressr   BadIdentifierErrorr$   name_finderget_pyname_atr%   rL   s    r   
get_pynamezOccurrence.get_pyname   s     !>?? 	E 	E:)77DD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E   $AAAc                     t          j        t          j                  5  | j        j                            | j                  cd d d            S # 1 swxY w Y   d S rH   )rP   rQ   r   rR   r$   rS   get_primary_and_pyname_atr%   rL   s    r   get_primary_and_pynamez!Occurrence.get_primary_and_pyname   s     !>?? 	Q 	Q:)CCDKPP	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	QrV   c                     | j         j                            | j                  p#| j         j                            | j                  S rH   )r$   rJ   is_from_statementr%   is_import_statementrL   s    r   is_in_import_statementz!Occurrence.is_in_import_statement   sC    z%77K
 
 EZ#77DD	Er   c                 J    | j         j                            | j                  S rH   )r$   rJ   is_a_function_being_calledr%   rL   s    r   	is_calledzOccurrence.is_called   s    z%@@MMMr   c                 J    | j         j                            | j                  S rH   )r$   rJ   %is_a_class_or_function_name_in_headerr%   rL   s    r   
is_definedzOccurrence.is_defined   s    z%KKDKXXXr   c                     | j         j                            | j                  p#| j         j                            | j                  S rH   )r$   rJ   rb   r%   is_a_name_after_from_importrL   s    r   is_a_fixed_primaryzOccurrence.is_a_fixed_primary   sC    z%KKK
 
 MZ#??LL	Mr   c                 J    | j         j                            | j                  S rH   )r$   rJ   is_assigned_herer%   rL   s    r   
is_writtenzOccurrence.is_written   s    z%66t{CCCr   c                 D    t          |                                           S rH   )unsure_pynamerU   rL   s    r   	is_unsurezOccurrence.is_unsure   s    T__..///r   c                 J    | j         j                            | j                  S rH   )r$   rJ   is_function_keyword_parameterr%   rL   s    r   rn   z(Occurrence.is_function_keyword_parameter   s    z%CCDKPPPr   c                 ~    |                                  d         }| j        j        j                            |          S )Nr   )rK   r$   r   linesget_line_number)r   r%   s     r   linenozOccurrence.lineno   s6     $$&&q)z"(88@@@r   N)r+   r,   r-   r   r	   saveitrK   rN   rU   rY   r]   r`   rc   rf   ri   rl   rn   propertyrr   r   r   r   r#   r#      sX       ' ' '
 \B B \B \E E \E \E E \E \Q Q \Q \E E \E
N N NY Y YM M M
D D D0 0 0Q Q Q 
\A A \ XA A Ar   r#   c                 b   | |dS | |k    rdS t          | t          j        t          j        f          s(t          |t          j        t          j        f          sdS |                                 |                                k    o)|                                 |                                k    S )z2Check whether `expected` and `pyname` are the sameNFT)r4   r   ImportedModuleImportedNameget_definition_location
get_object)expectedr<   s     r   same_pynamer{      s     6 u6 t		!56   		!56  u((**f.L.L.N.NN 	9!!V%6%6%8%88r   c                     | dS |rt          | t          j                  sdS |                                 t	          j                    k    rdS dS )z8Return `True` if we don't know what this name referencesNTF)r4   r   UnboundNamery   r   get_unknown)r<   unbounds     r   rk   rk      sa     t z&'*=>> ui3555 t r   c                       e Zd ZdZd Zd ZdS )r9   z"For finding occurrences of a name.c                     || _         d S rH   )r<   )r   r<   s     r   r   zPyNameFilter.__init__       r   c                 X    t          | j        |                                          rdS d S r   )r{   r<   rU   r   r&   s     r   __call__zPyNameFilter.__call__   s1    t{J$9$9$;$;<< 	4	 	r   Nr+   r,   r-   r.   r   r   r   r   r   r9   r9      s8        ,,      r   r9   c                   ,    e Zd ZdZddZd Zd Zd ZdS )	r:   z=Finds the occurrence if the name is in the class's hierarchy.Fc                    || _         || _        |                     |          | _        | j        R|                                                                | _        |                     | j        | j                  | _        d S d | _        d S rH   )	r<   	impl_only_get_containing_classpyclassry   get_namer   _get_root_classesroots)r   r<   implementations_onlys      r   r   zInHierarchyFilter.__init__   sx    -11&99< 	))++4466DI//diHHDJJJDJJJr   c                     | j         d S |                     |                                          }|7|                     || j                  }| j                             |          rdS d S d S r   )r   r   rU   r   r   intersection)r   r&   r   r   s       r   r   zInHierarchyFilter.__call__   s    : 	F,,Z-B-B-D-DEE 	**7DI>>Ez&&u-- t	 	 r   c                     t          |t          j                  rN|                                                                }|j        }|!|                                dk    r|j        S d S d S d S )NClass)r4   r   DefinedNamery   	get_scopeparentget_kindrD   )r   r<   scoper   s       r   r   z'InHierarchyFilter._get_containing_class   s    fg122 	'%%''1133E\F 'foo&7&77&B '&		' 	'' ' ' 'r   c                     | j         r|| j        k    r|hS t                      }|                                D ]/}||v r)|                    |                     ||                     0|s|hS |S rH   )r   r   setget_superclassesupdater   )r   r   r   r(   
superclasss        r   r   z#InHierarchyFilter._get_root_classes  s    > 	g5 	9!2244 	H 	HJz! Hd44ZFFGGG 	9r   NF)r+   r,   r-   r.   r   r   r   r   r   r   r   r:   r:      s[        GG     ' ' '	 	 	 	 	r   r:   c                       e Zd ZdZd Zd ZdS )r;   z:Occurrences where we don't knoow what the name references.c                     || _         d S rH   )r?   )r   r?   s     r   r   zUnsureFilter.__init__  r   r   c                 `    |                                 r|                     |          rdS d S d S r   )rl   r?   r   s     r   r   zUnsureFilter.__call__  sB    !! 	dkk*&=&= 	4	 	 	 	r   Nr   r   r   r   r;   r;     s8        DD      r   r;   c                       e Zd ZdZd ZdS )r2   z/Don't include import statements as occurrences.c                 2    |                                 rdS d S r*   )r]   r   s     r   r   zNoImportsFilter.__call__!  s%    ,,.. 	5	 	r   Nr+   r,   r-   r.   r   r   r   r   r2   r2     s)        99    r   r2   c                       e Zd ZdZd ZdS )r1   z Filter out non-call occurrences.c                 2    |                                 sdS d S r*   )r`   r   s     r   r   zCallsFilter.__call__)  s%    ##%% 	5	 	r   Nr   r   r   r   r1   r1   &  s)        **    r   r1   c                       e Zd ZdZd ZdS )r3   zFilter out keyword parameters.c                 2    |                                 rdS d S r*   )rn   r   s     r   r   zNoKeywordsFilter.__call__1  s%    3355 	5	 	r   Nr   r   r   r   r3   r3   .  s)        ((    r   r3   c                   \    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zed             ZdS )r   Fc                 j   || _         || _        t                              ddg          | _        t                              dt          j                    g          | _        t                              dt          j                    g          | _	        | 
                    | j                   | _        d S )Ncommentz#[^\n]*stringfstring)r   r   r   anycomment_patternr   get_string_patternstring_patternget_formatted_string_patternf_string_pattern_get_occurrence_patternpattern)r   r   r   s      r   r   z_TextualFinder.__init__7  s    		-11)j\JJ,00{5778
 
 !/ 2 2@BBC!
 !
 33DI>>r   c              #      K   |                      |          sd S | j        r| j        }n| j        } ||          E d {V  d S rH   )_fast_file_queryr   _normal_search
_re_search)r   sourcesearchers      r   r!   z_TextualFinder.find_offsetsC  sd      $$V,, 	F9 	'*HHH8F###########r   c              #   ~  K   | j                             |          D ]}|                                d         r|                    d          V  4|                                d         rQ|                                d         }|                     |          D ]!}|                    d          |j        z   V  "d S )Nr&   r   )r   finditer	groupdictstart_search_in_f_string
col_offset)r   r   matchf_stringoccurrence_nodes        r   r   z_TextualFinder._re_searchL  s      \**622 	N 	NE  . Nkk,//////""9- N ??,,Y7'+'?'?'I'I N NO++i00?3MMMMMM	N 	Nr   c              #      K   t          j        |          }t          j        |          D ]0}t          |t           j                  r|j        | j        k    r|V  1d S rH   )r   parsewalkr4   Nameidr   )r   r   treenodes       r   r   z"_TextualFinder._search_in_f_stringU  sd      y""HTNN 	 	D$)) dg.B 


	 	r   c              #   D  K   d}	 	 |                     | j        |          }|t          | j                  z   }|dk    s|                     ||dz
                     s2|t          |          k    s|                     ||                   s|V  n# t          $ r Y d S w xY w)Nr   T   )indexr   len_is_id_char
ValueError)r   r   currentfounds       r   r   z_TextualFinder._normal_search[  s      		TY88#di..0QJ  d&6&6veai7H&I&I  s6{{* 262B2B6'?2S2S   KKK   		s   BB 
BBc                 6    |                                 p|dk    S )N_)isalnum)r   cs     r   r   z_TextualFinder._is_id_charh  s    yy{{&a3h&r   c                     | j         |v S rH   )r   )r   r   s     r   r   z_TextualFinder._fast_file_queryk  s    yF""r   c                 <    ||                                 S |j        S rH   )readr"   )r   r   r   s      r   _get_sourcez_TextualFinder._get_sourcen  s!     	(==??"''r   c                     t                               dd|z   dz   g          }t          j        |dz   | j        z   dz   | j        z   dz   | j        z             }|S )Nr&   z\b|)r   r   recompiler   r   r   )r   r   occurrence_patternr   s       r   r   z&_TextualFinder._get_occurrence_patternt  s    +//ut|e?S>TUU*"#  !	"
  #$
 
 r   c                 >    d| z  d                     |          z   dz   S )Nz(?P<%s>r   ))join)r   list_s     r   r   z_TextualFinder.any  s!    4#((5//1C77r   Nr   )r+   r,   r-   r   r!   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   6  s        
? 
? 
? 
?$ $ $N N N    ' ' '# # #( ( (   8 8 \8 8 8r   r   c                      e Zd Zd	dZeej        d                         Zeej        d                         Zeej        d                         Z	eej        d                         Z
eej        d                         ZdS )
r    NFc                 >    || _         || _        || _        || _        d S rH   )r   !_OccurrenceToolsCreator__resource!_OccurrenceToolsCreator__pymoduler   )r   r   r   r   r   s        r   r   z _OccurrenceToolsCreator.__init__  s"    ""			r   c                 4    t          j        | j                  S rH   )r   ScopeNameFinderr   rL   s    r   rS   z#_OccurrenceToolsCreator.name_finder  s     '666r   c                     | j         j        S rH   )r   r"   rL   s    r   r"   z#_OccurrenceToolsCreator.source_code  s     }((r   c                 @    t          j        | j        | j                  S rH   )r
   Worderr"   r   rL   s    r   rJ   z#_OccurrenceToolsCreator.word_finder  s     }T-ty999r   c                 H    | j         | j         S | j        | j        j        S d S rH   )r   r   r   rL   s    r   r   z _OccurrenceToolsCreator.resource  s5     ? 	#?"? 	,?++	, 	,r   c                 \    | j         | j         S | j                            | j                  S rH   )r   r   get_pymoduler   rL   s    r   r   z _OccurrenceToolsCreator.pymodule  s.     ? 	#?"|((777r   )NNF)r+   r,   r-   r   rt   r	   rs   rS   r"   rJ   r   r   r   r   r   r    r      s            
\7 7 \ X7 
\) ) \ X) 
\: : \ X: 
\, , \ X, 
\8 8 \ X8 8 8r   r    )FTNFNFT)T)r.   rP   r   	rope.baser   r   r   r   r   r   r	   r
   r   rE   r#   r{   rk   r9   r:   r;   r2   r1   r3   r   r    r   r   r   <module>r      s  # #L     					 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	% % % % % % % %X 	'= '= '= '=T6A 6A 6A 6A 6A 6A 6A 6Ar  (          & & & & & & & &R                            M8 M8 M8 M8 M8 M8 M8 M8`#8 #8 #8 #8 #8 #8 #8 #8 #8 #8r   