
    Dd7j                         d dl Z d dlm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 d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlm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! 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/  G d de'          Z0d Z1d Z2d Z3d Z4d Z5d  Z6 e/j7                    d!             Z8 G d" d#          Z9d$ Z: e j;        d%          Z<d& Z=d' Z>d( Z?	 	 d+d*Z@dS ),    N)dedent)	Parameter)PythonTokenTypes)tree)search_ancestorLeaf)split_lines)debug)settings)classes)helpers)keywords)complete_dict)complete_file_name)imports)ValueSet)infer_call_of_leafparse_dotted_names)get_global_filters)TreeInstance)DocstringModule)ParamNameWrapperSubModuleName)convert_valuesconvert_namescut_value_at_position)plugin_managerc                       e Zd Zd ZdS )ParamNameWithEqualsc                     | j         dz   S )N=)string_name)selfs    3lib/python3.11/site-packages/jedi/api/completion.pyget_public_namez#ParamNameWithEquals.get_public_name   s    #%%    N)__name__
__module____qualname__r&    r'   r%   r    r       s#        & & & & &r'   r    c              #      K   | D ]s}t          |j                  D ]\\  }}|j        }||k     r|t          j        k    r#|t          j        t          j        fv r|j        |vrt          |j                  V  ]td S N)		enumerateparamskindr   POSITIONAL_OR_KEYWORDKEYWORD_ONLYnamer    _name)
signaturespositional_countused_kwargscall_sigipr0   s          r%   _get_signature_param_namesr;   "   s       3 3ho.. 	3 	3DAq6D## 	0O(O 	79OPP 3k13)!'22222	33 3r'   c                     |rdS d}| D ]]}t          |j                  D ]B\  }}|j        }|t          j        u r  dS ||k    r|t          j        t          j        fv rd} nC|s n^|S )NTF)r.   r/   r0   r   VAR_POSITIONALr1   POSITIONAL_ONLY)r5   r6   r7   must_be_kwarg	signaturer9   r:   r0   s           r%   _must_be_kwargrA   .   s     tM  	i.// 
	 
	DAq6Dy//  uuu$$ )2Q2;2K2M *M  % 	E	r'   c           	   #     K   t                      }t          j        r|                                }|D ]}|j        }t          j        r|                                }t          j        |||          r}t          j        | ||t          |          ||          }	|	j
        |	j        f}
|
|vrD|                    |
           |j        }|"|                                }||j        dk    r|	V  d S )Nfuzzy)is_fuzzycached_namedel_stmt)setr   case_insensitive_completionlowerr#   r   matchr   
Completionlenr3   completeadd	tree_nameget_definitiontype)inference_statecompletion_namesstack	like_namerD   rF   comp_dctr3   stringnewkrP   
definitions                r%   filter_namesr\   D   s     uuH+ &OO%%	   !/ 	$\\^^F=%888 	$I'  C 3<(A  Q N	 !!*!9!9!;!;J! !*/Z2O ! 			+ r'   c                 8    d |D             fd| D             S )Nc                     h | ]	}|j         
S r+   r3   ).0ds     r%   	<setcomp>z%_remove_duplicates.<locals>.<setcomp>a   s    ///QV///r'   c                 &    g | ]}|j         v|S r+   r_   )r`   cnamess     r%   
<listcomp>z&_remove_duplicates.<locals>.<listcomp>b   s%    :::!afE&9:A:::r'   r+   )completionsother_completionsre   s     @r%   _remove_duplicatesri   `   s1    //.///E::::{::::r'   c                 d    | j                             |d          }|                     |          S )zK
    Returns the scope in which the user resides. This includes flows.
    Tinclude_prefixes)	tree_nodeget_leaf_for_positioncreate_context)module_contextpositionleafs      r%   get_user_contextrs   e   s4     #99(UY9ZZD((...r'   c                     |                      |d          }t          |t          j        t          j        f          s-|j        }t          |t          j        t          j        f          -|S )NTrk   )rn   
isinstancer   ScopeFlowparent)module_noderq   nodes      r%   get_flow_scope_noder{   m   s`    ,,X,MMD
DI677 { 
DI677  Kr'   c                     g S r-   r+   )contextfunction_namedecorator_nodess      r%   complete_param_namesr   u   s	     Ir'   c                   d    e Zd Z	 ddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 ZddZddZd Zd ZdS )rL   Fc                     || _         || _        |j        | _        || _        t          j        | j        ||          | _        || _        || _	        || _
        d S r-   )_inference_state_module_contextrm   _module_node_code_linesr   get_on_completion_name
_like_name_original_position_signatures_callback_fuzzy)r$   rS   rp   
code_linesrq   signatures_callbackrD   s          r%   __init__zCompletion.__init__}   s^     /-*4% "89JJX`aa #+$7!r'   c                    | j                             | j        d          }t          || j                  \  }}}t	          | j        | j        |p|| j        |d n||z   | j                  }|J|sHt          t          | j
        | j        |||| j        | j        | j        | j        | j        
  
                  }||sd|v r|                     ||          }|S |                     |          \  }}t          t          | j
        || j        | j        | j        |                    }t#          ||          t%          |d           z   S )NTrk   rC   
)rF   c                     | j                             d          | j                             d          | j                                         fS )N___)r3   
startswithrJ   )xs    r%   <lambda>z%Completion.complete.<locals>.<lambda>   s<    1B1B41H1H121B1B31G1G121A r'   )key)r   rn   r   _extract_string_while_in_stringr   r   r   r   listr   r   r   r   _complete_in_string_complete_pythonr\   rU   ri   sorted)	r$   rr   rX   
start_leafquoteprefixed_completionsrF   rT   rg   s	            r%   rN   zCompletion.complete   s    66#! 7 
 
 %DD$Ja$b$b!
E, $#6DD+ 
  
  
  	&: 	#'(:%t';ZPV!: $"9	) ) $ $   	(' TDFN T'+'?'?
F'S'S$''(,(=(=d(C(C%%<(=?O(,
DO(,N N N O O 3[AA[ 'A 'A B B BB	
r'   c                 	   | j         j        }dx| _        }| j        d         | j        d         t	          | j                  z
  f| _        d}	 t          j        || j	        || j                  x| _        }nM# t          j
        $ r;}|j        j        }|dk    r	|g fcY d}~S ||                                 fcY d}~S d}~ww xY wt          |                                          dv r| j                            | j        d          }|                                }| j        d         }|j        | j        cxk    r|j        k    sn |j        d         }||}		 t+          |	dd	d
dd          }	|	n|	j        }
|
dk    r,|	j        d         }t1          |t2                    r
|j        dz   }
|	j        d         |k    r7|
dk    rddgz  n)|
dk    rg dz  n|
d	k    r                    d           g }d}t7          fdt8          j        t8          j        fD                       rd |D             }t?          |          }|r/|d         dv r%|t          |                      d                    fS d|v r;tC          |d|v           \  }}d|v od|v  }|| "                    |||          z  }no|d         dv rz|d         dk    rn| j                            | j                  }|j        dk    r|                                }| #                    |                                          \  }}||z  }n| $                                r|| %                    |          z  }n|d         dv r|d         dv rx | j&        | j         }|rg|d         j'        }t          |(                                          }|)                                }|tU          |||          z  }tW          |||          }|s0||                                 z  }||                      d          z  }|sb| j	        | j        d         dz
           d| j        d                  }|| ,                    | p|d         dv o|d d         d!k     "          z  }||fS )#a  
        Analyzes the current context of a completion and decides what to
        return.

        Technically this works by generating a parser stack and analysing the
        current stack for possible grammar nodes.

        Possible enhancements:
        - global/nonlocal search global
        - yield from / raise from <- could be only exceptions/generators
        - In args: */**: no completion
        - In params (also lambda): no completion before =
        Nr      .ifTrk   if_stmtfor_stmt
while_stmttry_stmt
error_node_stmtelifelse)exceptfinallyr   Fc              3       K   | ]}|v V  	d S r-   r+   )r`   tallowed_transitionss     r%   	<genexpr>z.Completion._complete_python.<locals>.<genexpr>  sA       L LAq'' L L L L L Lr'   c                     g | ]	}|j         
S r+   )nonterminal)r`   
stack_nodes     r%   rf   z/Completion._complete_python.<locals>.<listcomp>  s    KKKzJ2KKKr'   )asdefclass)is_functionimport_stmtimport_fromimportonly_modules)trailerdotted_name	endmarker)(,)r   arglist	decoratorz 	.;z...)only_values)-r   grammarrU   r   rM   r   	_positionr   get_stack_at_positionr   OnErrorLeaf
error_leafvalue_complete_global_scoper   )_allowed_transition_names_and_token_typesr   rn   get_previous_leaf	start_posend_posr   rR   childrenru   r   appendanyr   NAMEINDENT_gather_nodes_complete_inheritedr   _get_importer_names_complete_trailer_is_parameter_completion_complete_paramsr   _call_detailsiter_used_keyword_argumentscount_positional_argumentsr;   rA   _complete_keywords)r$   rr   r   rU   rF   er   previous_leafindentstmttype_firstrT   kwargs_onlynonterminalsnodeslevelre   r   dotnr5   call_detailsr7   r6   current_liner   s                             @r%   r   zCompletion._complete_python   s    '/!!
U#A&#A&T_)=)==
 	>!(!>)4" " DJ " 	> 	> 	>L&E| ' #B&&&&&&  ; ; = ========	> @@BBCC 	 && 	?$::4>\`:aaD 2244M^A&FNdn + + + + + + + +* ?$?*i\:$ D   IE, : $a 0%eT22 :$)K'$9E~a(F2 ? I- ?/FF3CC//"j0 ?/3P3P3PP//"j0 ?/66v>>>)?,  L L L L2B2G2B2I2K L L L L L 9	T LKUKKKL!%((E 2Tr&<< 2T #D)A)Ad)A)S)S$T$TTT,. .T1%,9VWWu$1\$A$WhRWFWX D$<$<!- %= % %   
 b!%?? %TE"IQTDT %T'==dnMM8{* 3 0022C!%!7!78M8M8O8O!P!PQ A%  ..00 T D$9$9$$?$??   9
* `(,0SS`!:!:DN!KJ! `'1!}'B&*<+S+S+U+U&V&V+7+R+R+T+T((,F&,'- - ( '5ZAQS^&_&_" T$(C(C(E(EE$$(@(@U(@(S(SS$ 	+DN1,=,ABCUDNSTDUCUVL 7 7#%1!1 "@\"5E5P 6@%1"##%6%%?A !8 ! !  ,,,s*   (A7 7CB<C!B<6C<Cc                     | j         d         }|j        dk    rt          |j                  dk    rdS |j        dv rdS |j        dv o|j        d         dk    S )Nr   lambdefr   T
parameters)typedargslistvarargslistr   )rU   r   rM   r   )r$   toss     r%   r   z#Completion._is_parameter_completionG  sm    jn?i' 	C	NNa,? 	 4?l* 	 4"BB[syQS}X[G[[r'   c                    | j         d         }|j        dk    r| j         d         }|j        dk    rt          | j        | j                  }t          |dd          }|t|j        dk    r2|j        d         }|j        dk    r|j        }n&|j        dk    r|g}ng }n|                                }|j	        d	         }t          ||j        |          S g S )
Nr   r   funcdefr   r   
decoratorsr   r   )rU   r   rs   r   r   r   rR   r   get_decoratorsr   r   r   )r$   rr   r   r}   rz   r   r   r~   s           r%   r   zCompletion._complete_paramsT  s    Z^
!\1 	(BJ!Y. 	V&t';T^LLG"4yAAD V9, 	7a(Av- (%&Z

;. (&'S

%'

!%!4!4!6!6J * 0 3+G]5H*UUU	r'   c              #      K   |D ]M}t          |t                    r6|                                r"|r|dv rt          j        | j        |          V  Nd S )N)TrueFalseNone)ru   strisalphar   KeywordNamer   )r$   r   r   rZ   s       r%   r   zCompletion._complete_keywordsk  sz      $ 	I 	IA!S!! Iaiikk I" Ia+D&D I".t/DaHHHHH	I 	Ir'   c                     t          | j        | j                  }t          j        d|           t          | j        | j                  }t          || j        |          }g }|D ]}||                                z  }|S )Nzglobal completion scope: %s)	rs   r   r   r
   dbgr{   r   r   values)r$   r}   flow_scope_nodefiltersrT   filters         r%   r   z!Completion._complete_global_scopeq  s    "4#7HH	/999-d.?PP$N
 

  	0 	0F/r'   c                 b   | j                             |          }t          ||          }t          j        d|d           d }t          |          dk    rC|\  }|                                r+t          |j                  dk    r|j        d         }|dv r|}||                     |          fS )Nztrailer completion values: %sMAGENTA)colorr   r   )numpy
tensorflow
matplotlibpandas)	r   ro   r   r
   r  rM   	is_modulestring_names_complete_trailer_for_values)r$   r   inferred_contextr  rF   vmodule_names          r%   r   zCompletion._complete_trailer  s    />>}MM#$4mDD	16KKKK v;;! 	2BA{{}} 2q~&&!+ 2"#."3K"&UU 2&1D==fEEEEr'   c                 V    t          | j        | j                  }t          ||          S r-   )rs   r   r   complete_trailer)r$   r  user_contexts      r%   r  z'Completion._complete_trailer_for_values  s&    '(<dnMMf555r'   r   Tc                     d |D             }t          j        | j        || j        |          }|                    | j        |          S )Nc                     g | ]	}|j         
S r+   )r   r`   r   s     r%   rf   z2Completion._get_importer_names.<locals>.<listcomp>  s    (((Q(((r'   r   )r   Importerr   r   rT   )r$   re   r   r   r9   s        r%   r   zCompletion._get_importer_names  sN    ((%(((T2E4;OQVWW!!$"7l!SSSr'   c              #     K   | j                             | j        d          }t          j        |d          }|dS | j                            |          }|j        d         |j        d         k    rdS |                    d          }t          |           |D ],}|
                                D ]}|j        dk    |k    r|V  -dS )zP
        Autocomplete inherited methods when overriding in child class.
        Trk   classdefNr   )is_instancefunction)r   rn   r   r   r   r   create_valuer   get_filtersnextr  api_type)r$   r   rr   clsclass_valuer  r  r3   s           r%   r   zCompletion._complete_inherited  s        66t~X\6]]"444 	F *77<<=t~a00 	F))d);;W 	 	F  MZ/K? JJJ	 	r'   c                 `   fd}t          |          }t          |d          t           |                    }|d         !d |D             }|                     |          S t	          j        dd                   }|r)|                     |                    d          g          S g S )	aU  
        To make it possible for people to have completions in doctests or
        generally in "Python" code in docstrings, we use the following
        heuristic:

        - Having an indented block of code
        - Having some doctest code that starts with `>>>`
        - Having backticks that doesn't have whitespace inside it
        c              3      K   d}D ]m}|s*|                     d          s|                     d          rt          j        dd|          V  nd V  t          t          j        d|                    }nd S )NFz>>> z^( *>>> ?| +) z *>>>)r   resubboolrK   )linesinclude_next_linelr   s      r%   iter_relevant_linesz;Completion._complete_in_string.<locals>.iter_relevant_lines  s       % ? ?$ U(;(; q||C?P?P &!12q999999JJJ$('1)=)=$>$>!!? ?r'   T)keependsr   Nc                     g | ]}|dn|	S )Nr   r+   r`   rd   s     r%   rf   z2Completion._complete_in_string.<locals>.<listcomp>  s!    "Y"Y"Y1#;44!"Y"Y"Yr'   z
`([^`\s]+)r   )r   r	   r   _complete_code_linesr*  searchgroup)r$   r   rX   r0  relevant_code_linesrK   r   s         @r%   r   zCompletion._complete_in_string  s    	? 	? 	? 	? 	?  $777
"#6#6z#B#BCCr" 	B"Y"YEX"Y"Y"Y,,-@AAA	-B88 	?,,ekk!nn-=>>>	r'   c                 2   | j         j                            d                    |                    }t	          | j        | j         ||          }t          | j         |                                ||j        d | j	                  
                                S )Nr)  )in_module_contextrS   ry   r   c                      g S r-   r+   )argskwargss     r%   r   z1Completion._complete_code_lines.<locals>.<lambda>  s     r'   )r   rq   r   rD   )r   r   parsejoinr   r   rL   
as_contextr   r   rN   )r$   r   ry   module_values       r%   r4  zCompletion._complete_code_lines  s    +399"''*:M:MNN&"2 1#!	
 
 
 !##%%! ( : :+
 
 
 (**	r'   N)F)r   T)T)r(   r)   r*   r   rN   r   r   r   r   r   r   r  r   r   r   r4  r+   r'   r%   rL   rL   |   s        ,1    )
 )
 )
VM- M- M-^\ \ \  .I I I     F F F$6 6 6
T T T T
   0  @    r'   rL   c                 N    g }| D ]}|j         j        dk    rg }||j        z  } |S )N
small_stmt)dfa	from_ruler   )rU   r   r   s      r%   r   r     sC    E & &
>#|3 	&EEZ%%EELr'   z^\w*(\'{3}|"{3}|\'|")c                    fd}| j         k     rdS | j        dk    r ||           S g }| | j        dk    rd| j        v s	d| j        v rt          | j                  dk    r ||           S d }| j        sS|                                 }|;|j        dk    s0t          d	 |j                                        D                       sd }d
                    fd|D                       |p| |d
n|j        t          |           z   fS | j
        d         k    rn-|                    d|            |                                 } | dS )Nc                 |   i }| j         d         k    rd         | j        z
  |d<   t          j        | j        fi |}|sdS |                    d          }| j         d         k    r(d         | j        |                                z   k     rdS t          |           |                                d          | |fS )Nr   r   endposNNN)linecolumn_string_startrK   r   r6  endr   )rr   r<  rK   startrq   s       r%   return_part_of_leafz<_extract_string_while_in_string.<locals>.return_part_of_leaf  s    9# 	9'{T[8F8#DJ99&99 	$##A9# 	$dkEIIKK6O(O 	$##$T844UYY[[\\BD%OOr'   rH  rX   r   "'r   r3   c              3      K   | ]}|d v V  	dS )rubfNr+   r3  s     r%   r   z2_extract_string_while_in_string.<locals>.<genexpr>  s&      "R"R11;"R"R"R"R"R"Rr'   r)  c              3   8   K   | ]}t          |          V  d S r-   r   )r`   r/  rq   s     r%   r   z2_extract_string_while_in_string.<locals>.<genexpr>  s.      KKq-a::KKKKKKr'   r   )r   rR   r   rM   prefixr   allrJ   r>  r   rI  insert)rr   rq   rN  leavesprefix_leafs    `   r%   r   r     s   
P 
P 
P 
P 
P $.   yH )""4(((F
 (9$ 	#*; 	sdj?P 	4:" 1**4000K; '"4466 '+*:f*D '""R"R8I8O8O8Q8Q"R"R"RRR'"&K KKKKFKKKKK#t"A0A'h778  9# 	
 a%%''1  (2 r'   c                    g }|D ]s}|                     | j                  D ]}||                                z  }|                                s(t	          |t
                    r|t          | |          z  }tt          |          }|D ];}||vr5|                     | j                  D ]}||                                z  }<|S )N)origin_scope)r!  rm   r  is_stubru   r   _complete_getattrr   )r  r  rT   r   r  python_valuesrd   s          r%   r  r  "  s     G G''\5K'LL 	0 	0F/}} 	G:e\#B#B 	G 1, F FF"6**M 4 4F? 	4--\5K-LL 4 4 FMMOO3  r'   c                 T   |                     d          p|                     d          }t          j        d |D                       }|D ][}|j        }||j        dk    r|                                D ]-}|j        dk    r|j        d         }|j        dk    r(|j        d	         }|j        d         }	t          |j                  d
k    s|j        dk    s|j        dk    rq|	j        d         }
|
j        dk    st          |
j                  dk     r|	                                }|
j        d	         }|
j        d
         }|
                    ||j                  }t          d |D                       s|                    |          }t          | |          c c S ]g S )aV  
    A heuristic to make completion for proxy objects work. This is not
    intended to work in all cases. It works exactly in this case:

        def __getattr__(self, name):
            ...
            return getattr(any_object, name)

    It is important that the return contains getattr directly, otherwise it
    won't work anymore. It's really just a stupid heuristic. It will not
    work if you write e.g. `return (getatr(o, name))`, because of the
    additional parentheses. It will also not work if you move the getattr
    to some other place that is not the return statement itself.

    It is intentional that it doesn't work in all cases. Generally it's
    really hard to do even this case (as you can see below). Most people
    will write it like this anyway and the other ones, well they are just
    out of luck I guess :) ~dave.
    __getattr____getattribute__c              3   >   K   | ]}|                                 V  d S r-   )infer)r`   r3   s     r%   r   z$_complete_getattr.<locals>.<genexpr>I  s>       # # 	

# # # # # #r'   Nr   return_stmtr   	atom_exprr      r3   getattrr      c              3   ,   K   | ]}|j         d k    V  dS )paramN)r#  r  s     r%   r   z$_complete_getattr.<locals>.<genexpr>h  s)      @@qzW,@@@@@@r'   )get_function_slot_namesr   	from_setsrm   rR   iter_return_stmtsr   rM   r   r?  gotor   r   
infer_noder  )r  instancere   	functionsfuncrm   rc  rd  atomr   r   r}   object_node	name_node	name_listobjectss                   r%   r\  r\  3  s   ( --m<< E001CDD 
" # ## # #  I  "; ";N	 		) ; 	$6688 	; 	;K =0 #,Q/I~, %a(D(+G9%&&!+ tyF/B zY.&q)G|y( C0@,A,AA,E oo''G!*1-K  (+IY	0CDDI@@i@@@@@ 
 ((55G#L':::::::;	;< Ir'   Fc	           
   #     K   |d d         D ]l}	g }
|D ]E}|	|j         k    r8|j        |j        dv r|r |
t          ||                                          z  }
Ft          j        d|
|d d                    |
}m|d                                         }|D ]}|j                                         }|rt          j	        |||          s|s||k    rt          |t                    rd |                                D             }n|g}|rt          |          }|D ]Q}|r't          j        | |d t          |          |          }nt          j        | |          }|r||j        k    r|V  Rd S )Nr   )module	namespacezdot lookup on search %s from %s
   rC   c                     g | ]	}|j         
S r+   r_   )r`   r  s     r%   rf   z$search_in_module.<locals>.<listcomp>  s    333A333r'   )rU   like_name_lengthrE   )r#   rP   r#  r  rb  r
   r  rJ   r   rK   ru   r   r   r   rL   rM   NamerR   )rS   rp   re   wanted_nameswanted_typerN   rD   ignore_importsconverts	new_namesr   	last_namerX   n2def_s                   r%   search_in_moduler  s  s      #2#  	 	 	AAM! ; qz=T/T *-"GGII  	 		3Ycrc
KKKR &&((I  $$&& 	fiuEEE 		$*i$7	!]++ 33333 -%e,,   ="-'"),Y!&	  DD #<<<D" kTY&> JJJ+ r'   )FFFF)Ar*  textwrapr   inspectr   parso.python.tokenr   parso.pythonr   
parso.treer   r   parsor	   jedir
   r   jedi.apir   r   r   jedi.api.stringsr   jedi.api.file_namer   jedi.inferencer   jedi.inference.base_valuer   jedi.inference.helpersr   r   jedi.inference.contextr   jedi.inference.valuer   jedi.inference.docstring_utilsr   jedi.inference.namesr   r   !jedi.inference.gradual.conversionr   r   jedi.parser_utilsr   jedi.pluginsr   r    r;   rA   r\   ri   rs   r{   decorater   rL   r   compilerK  r   r  r\  r  r+   r'   r%   <module>r     s   				             / / / / / /       , , , , , , , ,                                     * * * * * * 1 1 1 1 1 1 " " " " " " . . . . . . I I I I I I I I 5 5 5 5 5 5 - - - - - - : : : : : : @ @ @ @ @ @ @ @ K K K K K K K K 3 3 3 3 3 3 ' ' ' ' ' '& & & & &* & & &
	3 	3 	3  ,  8; ; ;
/ / /     f f f f f f f fR   
344- - -`  "= = =B 9>38' ' ' ' ' 'r'   