
    f7j                        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/jn                         d!        Z8 G d" d#      Z9d$ Z: e jv                  d%      Z<d& Z=d' Z>d( Z?	 	 d*d)Z@y)+    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y)ParamNameWithEqualsc                      | j                   dz   S )N=)string_name)selfs    N/var/www/cvtools/html/venv/lib/python3.12/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              #   0  K   | D ]  }t        |j                        D ]s  \  }}|j                  }||k  r|t        j                  k(  r+|t        j                  t        j
                  fv sN|j                  |vs]t        |j                         u  y w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hoo. 	3DAq66D##	0O0O(O	779O9OPPk1)!''22	33s   A(B+B:Bc                     |ryd}| D ]q  }t        |j                        D ]R  \  }}|j                  }|t        j                  u r  y||k\  s-|t        j
                  t        j                  fv sPd} n |rp |S  |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    M 	i../ 
	DAq66Dy/// $$)2Q2Q2;2K2K2M *M %
	  r'   c           	   #     K   t               }t        j                  r|j                         }|D ]  }|j                  }t        j                  r|j                         }t        j                  |||      sHt        j                  | ||t        |      ||      }	|	j                  |	j                  f}
|
|vs|j                  |
       |j                  }|"|j                         }||j                  dk(  r|	  y w)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     uH++OO%	  !!//\\^F==%8$$I'C 3<<(A Q NN	(!*!9!9!;J!-*//Z2O 	+s   A4D 7A D 8AD c                     |D ch c]  }|j                    }}| D cg c]  }|j                   |vs| c}S c c}w c c}w r-   )r3   )completionsother_completionsdnamescs        r%   _remove_duplicatesrc   `   s=    ./QVV/E/":!affE&9A:: 0:s   9>>c                 ^    | j                   j                  |d      }| j                  |      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_contextrm   e   s1     ##99(UY9ZD((..r'   c                     | j                  |d      }t        |t        j                  t        j                  f      s7|j
                  }t        |t        j                  t        j                  f      s7|S )NTre   )rh   
isinstancer   ScopeFlowparent)module_noderk   nodes      r%   get_flow_scope_noderu   m   sW    ,,X,MD

DII67{{ 

DII67 Kr'   c                     g S r-   r+   )contextfunction_namedecorator_nodess      r%   complete_param_namesrz   u   s	     Ir'   c                   b    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y)rL   c                     || _         || _        |j                  | _        || _        t        j                  | j                  ||      | _        || _        || _	        || _
        y r-   )_inference_state_module_contextrg   _module_node_code_linesr   get_on_completion_name
_like_name_original_position_signatures_callback_fuzzy)r$   rS   rj   
code_linesrk   signatures_callbackrD   s          r%   __init__zCompletion.__init__}   s`     /-*44% "889J9JJX`a #+$7!r'   c                    | j                   j                  | j                  d      }t        || j                        \  }}}t	        | j
                  | j                  |xs || j                  |d n||z   | j                        }|e|sct        t        | j                  | j
                  |||| j                  | j                  | j                  | j                  | j                  
            }||sd|v r| j                  ||      }|S | j                  |      \  }}t        t        | j                  || j                   | j                  | j                  |            }t#        ||      t%        |d       z   S )NTre   rC   
)rF   c                     | j                   j                  d      | j                   j                  d      | j                   j                         fS )N___)r3   
startswithrJ   )xs    r%   <lambda>z%Completion.complete.<locals>.<lambda>   s:    1B1B41H121B1B31G121A r'   )key)r   rh   r   _extract_string_while_in_stringr   r~   r   r   listr   r}   r   r   _complete_in_string_complete_pythonr\   rU   rc   sorted)	r$   rl   rX   
start_leafquoteprefixed_completionsrF   rT   r^   s	            r%   rN   zCompletion.complete   s     66##! 7 
 %DD$JaJa$b!
E,  $##ND++ 
 &:#'(:%%t';';ZPV!:!:  $"9"9	) $  'DFN'+'?'?
F'S$''(,(=(=d(C%%<(=(=?O(,

DOO(,N O 3[A[ 'A BB	
r'   c                 
	   | j                   j                  }dx| _        }| j                  d   | j                  d   t	        | j
                        z
  f| _        d}	 t        j                  || j                  || j                        x| _        }t        |j                               dv r| j                   j#                  | j                  d      }|j%                         }| 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(  r0|
dk(  rddgz  n#|
dk(  rg dz  n|
d	k(  rj5                  d       g }d}t7        fdt8        j:                  t8        j<                  fD              r|D cg c]  }|j>                   }}tA        |      }|r$|d   dv r|t        | jC                  d            fS d|v r5tE        |d|v       \  }}d|v xr d|v  }|| jG                  |||      z  }nH|d   dv rt|d   dk(  rl| j                   j#                  | j                        }|j,                  dk(  r|j%                         }| jI                  |j%                               \  }}||z  }n| jK                         r|| jM                  |      z  }n|d   dv rw|d   dv rp | jN                  | j                   }|rU|d   jP                  }t        |jS                               }|jU                         }|tW        |||      z  }tY        |||      }|s(|| j                         z  }|| jC                  d      z  }|sZ| j                  | j                  d   dz
     d| j                  d    }|| j[                  | xs |d   dv xr |dd d k7   !      z  }||fS # t        j                  $ r@}|j                  j                  }|dk(  r	|g fcY d}~S || j                         fcY d}~S d}~ww xY wc c}w )"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      .ifTre   if_stmtfor_stmt
while_stmttry_stmt
error_node_stmtelifelse)exceptfinallyr   Fc              3   &   K   | ]  }|v  
 y wr-   r+   ).0tallowed_transitionss     r%   	<genexpr>z.Completion._complete_python.<locals>.<genexpr>  s      LAq'' Ls   )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   rh   get_previous_leaf	start_posend_posr   rR   childrenro   r   appendanyr   NAMEINDENTnonterminal_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$   rl   r   rU   rF   er   previous_leafindentstmttype_firstrT   kwargs_only
stack_nodenonterminalsnodeslevelra   r   dotnr5   call_detailsr7   r6   current_liner   s                              @r%   r   zCompletion._complete_python   s    ''//!!
U##A&##A&T__)==
 	>!(!>!>))4" DJ @@BC 	 &&$$::4>>\`:aD 224M^^A&FNNdnnDD*($*i\:$D | IIE, $a 0%eT2$)KK'$9E~~a(F2 I-/FF3CC/"j0/3PP/"j0/66v>) ,  L2B2G2G2B2I2I2K L L FKKzJ22KLK!%(Er&<< #D)A)Ad)A)S$TTT,.1%,9VWu$1\$A$WhRWFWX D$<$<!- %= %  
 b!%??E"IQTDT''==dnnM88{* 002C!%!7!78M8M8O!PQ A% ..0 D$9$9$$??  9
*(,0SS!:!:!:DNN!KJ!'1!}'B'B&*<+S+S+U&V+7+R+R+T((,F&,'- ( '5ZAQS^&_"$(C(C(EE$$(@(@U(@(SS$++DNN1,=,ABCUDNNSTDUVL 7 7#%1!1 "@\"5E5P 6@%1"#%6%%?A !8 !  ,,,g "" 	>LL&&E| #B&  ; ; ===	>h Ls0   3P* R *Q==Q8Q=!Q82Q=8Q=c                     | j                   d   }|j                  dk(  rt        |j                        dk(  ry|j                  dv ry|j                  dv xr |j                  d   dk(  S )Nr   lambdefr   T
parameters)typedargslistvarargslistr   )rU   r   rM   r   )r$   toss     r%   r   z#Completion._is_parameter_completionG  sd    jjn??i'C		Na,? ??l* "BB[syyQS}X[G[[r'   c                    | j                   d   }|j                  dk(  r| j                   d   }|j                  dk(  rt        | j                  | j                        }t        |dd      }||j                  dk(  rA|j                  d   }|j                  dk(  r|j                  }n&|j                  dk(  r|g}ng }n|j                         }|j                  d	   }t        ||j                  |      S g S )
Nr   r   funcdefr   r   
decoratorsr   r   )rU   r   rm   r~   r   r   rR   r   get_decoratorsr   rz   r   )r$   rl   r   rw   rt   r   r   rx   s           r%   r   zCompletion._complete_paramsT  s    ZZ^
!!\1BJ!!Y.&t';';T^^LG"4yAD99,a(Avv-%&ZZ
;.&'S
%'
!%!4!4!6J * 0 0 3+G]5H5H*UU	r'   c              #      K   |D ]M  }t        |t              s|j                         s%|r|dv s,t        j                  | j
                  |       O y w)N)TrueFalseNone)ro   strisalphar   KeywordNamer}   )r$   r   r   rZ   s       r%   r   zCompletion._complete_keywordsk  sM     $ 	IA!S!aiik"a+D&D"..t/D/DaHH	Is   AAA%Ac                    t        | j                  | j                        }t        j                  d|       t        | j                  | j                        }t        || j                  |      }g }|D ]  }||j                         z  } |S )Nzglobal completion scope: %s)	rm   r~   r   r
   dbgru   r   r   values)r$   rw   flow_scope_nodefiltersrT   filters         r%   r   z!Completion._complete_global_scopeq  s    "4#7#7H		/9-d.?.?P$NN

  	0F/	0r'   c                 H   | j                   j                  |      }t        ||      }t        j                  d|d       d }t        |      dk(  rA|\  }|j                         r-t        |j                        dk(  r|j                  d   }|dv r|}|| j                  |      fS )Nztrailer completion values: %sMAGENTA)colorr   r   )numpy
tensorflow
matplotlibpandas)	r~   ri   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#$4mD		16K v;!BA{{}q~~&!+"#.."3K"&UU&1D==fEEEr'   c                 Z    t        | j                  | j                        }t        ||      S r-   )rm   r~   r   complete_trailer)r$   r   user_contexts      r%   r  z'Completion._complete_trailer_for_values  s%    '(<(<dnnMf55r'   c                     |D cg c]  }|j                    }}t        j                  | j                  || j                  |      }|j                  | j                  |      S c c}w )Nr   )r   r   Importerr}   r~   rT   )r$   ra   r   r   r   r9   s         r%   r   zCompletion._get_importer_names  s]    "'(Q((T22E4;O;OQVW!!$"7"7l!SS )s   A#c              #     K   | j                   j                  | j                  d      }t        j                  |d      }|y| j
                  j                  |      }|j                  d   |j                  d   k\  ry|j                  d      }t        |       |D ].  }|j                         D ]  }|j                  dk(  |k(  s|  0 yw)zP
        Autocomplete inherited methods when overriding in child class.
        Tre   classdefNr   )is_instancefunction)r   rh   r   r   r   r~   create_valuer   get_filtersnextr   api_type)r$   r   rl   clsclass_valuer   r  r3   s           r%   r   zCompletion._complete_inherited  s        66t~~X\6]""44; **77<==t~~a00))d);W 	F MMZ/K?J	s   CC	Cc                 8   fd}t        |      }t        |d      t         |            }|d   $|D cg c]  }|dn|
 }}| j                  |      S t	        j
                  dd         }|r!| j                  |j                  d      g      S g S c c}w )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 ]c  }|s"|j                  d      s|j                  d      rt        j                  dd|       nd  t        t        j                  d|            }e y w)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  sd      % ?$U(;q||C?P&&!12q99J$('1)=$>!?s   A,A/T)keependsr   r   z
`([^`\s]+)r   )r   r	   r   _complete_code_linesr"  searchgroup)r$   r   rX   r(  relevant_code_linesrb   rK   r   s          @r%   r   zCompletion._complete_in_string  s    	?  $7
"#6z#BCr".EX"Y194!#;"Y"Y,,-@AA		-B8,,ekk!n-=>>	 #Zs   Bc                 H   | j                   j                  j                  dj                  |            }t	        | j
                  | j                   ||      }t        | j                   |j                         ||j                  d | j                        j                         S )Nr!  )in_module_contextrS   rs   r   c                      g S r-   r+   )argskwargss     r%   r   z1Completion._complete_code_lines.<locals>.<lambda>  s     r'   )r   rk   r   rD   )r}   r   parsejoinr   r~   rL   
as_contextr   r   rN   )r$   r   rs   module_values       r%   r*  zCompletion._complete_code_lines  s    ++3399"''*:MN&"22 11#!	
 !!##%! (( :++
 (*	r'   N)F)r   T)T)r(   r)   r*   r   rN   r   r   r   r   r   r   r  r   r   r   r*  r+   r'   r%   rL   rL   |   sN    ,1 )
VM-^\.I F$6
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     sD    E &
>>##|3EZ%%%E	&
 Lr'   z^\w*(\'{3}|"{3}|\'|")c                    fd}| j                   k  ry| j                  dk(  r ||       S g }| | j                  dk(  rd| j                  v sd| j                  v rt        | j                        dkD  r ||       S d }| j                  sM| j                         }|9|j                  dk7  s*t        d	 |j                  j                         D              sd }d
j                  fd|D              |xs | |d
n|j                  t        |       z   fS | j                  d   k7  r	 y|j                  d|        | j                         } | y)Nc                 j   i }| j                   d   k(  rd   | j                  z
  |d<   t        j                  | j                  fi |}|sy|j                  d      }| j                   d   k(  r$d   | j                  |j                         z   k  ryt        |       |j                         d  | |fS )Nr   r   endposNNN)linecolumn_string_startrK   r   r,  endr   )rl   r2  rK   startrk   s       r%   return_part_of_leafz<_extract_string_while_in_string.<locals>.return_part_of_leaf  s    99#'{T[[8F8##DJJ9&9#A99#dkkEIIK6O(O#$T84UYY[\BD%OOr'   r>  rX   r   "'r   r3   c              3   $   K   | ]  }|d v  
 yw)rubfNr+   )r   rb   s     r%   r   z2_extract_string_while_in_string.<locals>.<genexpr>  s     "R11;"Rs   r!  c              3   6   K   | ]  }t        |        y wr-   r   )r   r'  rk   s     r%   r   z2_extract_string_while_in_string.<locals>.<genexpr>  s     Kq-a:Ks   r   )r   rR   r   rM   prefixr   allrJ   r4  r   r?  insert)rl   rk   rD  leavesprefix_leafs    `   r%   r   r     sT   
P $.. yyH"4((F

99$#*;sdjj?P4::"*400K;;"446&+*:*:f*D""R8I8I8O8O8Q"RR"&K KFKK#t"*0A0A'h78  99#
   	a%%'1 
2 r'   c                 x   g }|D ]g  }|j                  | j                        D ]  }||j                         z  } |j                         rHt	        |t
              sY|t        | |      z  }i t        |      }|D ];  }||vs|j                  | j                        D ]  }||j                         z  } = |S )N)origin_scope)r  rg   r   is_stubro   r   _complete_getattrr   )r  r   rT   r   r  python_valuesrb   s          r%   r  r  "  s     G''\5K5K'L 	0F/	0 }}:e\#B 1, FFG #6*M 4F?--\5K5K-L 4 FMMO3 44 r'   c                 r   |j                  d      xs |j                  d      }t        j                  d |D              }|D ]p  }|j                  }||j                  dk7  r"|j                         D ];  }|j                  dk7  r|j                  d   }|j                  dk7  r3|j                  d   }|j                  d   }	t        |j                        d	k7  s|j                  d
k7  s|j                  dk7  r|	j                  d   }
|
j                  dk7  st        |
j                        dk  r|j                         }|
j                  d   }|
j                  d	   }|j                  ||j                        }t        d |D              s|j                  |      }t        | |      c c S  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   | ]  }|j                           y wr-   )infer)r   r3   s     r%   r   z$_complete_getattr.<locals>.<genexpr>I  s      # 	

#s   r   return_stmtr   	atom_exprr      r3   getattrr      c              3   :   K   | ]  }|j                   d k(    yw)paramN)r  )r   r   s     r%   r   z$_complete_getattr.<locals>.<genexpr>h  s     @qzzW,@s   )get_function_slot_namesr   	from_setsrg   rR   iter_return_stmtsr   rM   r   r5  gotor   r   
infer_noder  )r  instancera   	functionsfuncrg   rY  rZ  atomr   r   rw   object_node	name_node	name_listobjectss                   r%   rR  rR  3  s   ( --m< E001CD 
"" ## I  ";NN		) ;$668 	;K =0#,,Q/I~~,%%a(D((+G9%%&!+tyyF/BzzY.&&q)G||y(C0@0@,AA,Eoo'G!**1-K  ((+IY	0C0CDI@i@@
 ((5G#L'::;	;";F Ir'   c	           
   #     K   |d d D ]q  }	g }
|D ]L  }|	|j                   k(  s|j                  |j                  dv r|r0|
t        ||j	                               z  }
N t        j                  d|
|d d        |
}s |d   j                         }|D ]  }|j                   j                         }|rt        j                  |||      s	|r:||k(  s@t        |t              r(|j	                         D cg c]  }|j                   }}n|g}|rt        |      }|D ]T  }|r$t        j                  | |d t!        |      |      }nt        j"                  | |      }|r||j$                  k(  sQ| V  y c c}w w)Nr   )module	namespacezdot lookup on search %s from %s
   rC   )rU   like_name_lengthrE   )r#   rP   r  r  rX  r
   r   rJ   r   rK   ro   r   r3   r   r   rL   rM   NamerR   )rS   rj   ra   wanted_nameswanted_typerN   rD   ignore_importsconverts	new_namesr   	last_namerX   r  n2def_s                    r%   search_in_moduler|  s  s     #2 	 	AAMM!;;*qzz=T/T*-"GGI 		 			3Ycr
K R &&(I $$&fiuEFi$7!]+)*3A33%e, "--'"),Y!&	D #<<<D"kTYY&>J
 4s*   E:B(E:E:"E:3E5A%E:,E:)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\   rc   rm   ru   decoraterz   rL   r   compilerA  r   r  rR  r|  r+   r'   r%   <module>r     s    	   /  ,       * 1 " . I 5 - : @ K 3 '&* &
	3,8;
/  f fR 

34-`"=B 9>38'r'   