o
    ˷e7j                     @   s  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 de'Z0dd Z1dd Z2dd  Z3d!d" Z4d#d$ Z5d%d& Z6e/7 d'd( Z8G d)d* d*Z9d+d, Z:e ;d-Z<d.d/ Z=d0d1 Z>d2d3 Z?	4	4d7d5d6Z@dS )8    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                   @   s   e Zd Zdd ZdS )ParamNameWithEqualsc                 C   s
   | j d S )N=)string_name)self r"   J/var/www/ideatree/venv/lib/python3.10/site-packages/jedi/api/completion.pyget_public_name   s   
z#ParamNameWithEquals.get_public_nameN)__name__
__module____qualname__r$   r"   r"   r"   r#   r      s    r   c                 c   sd    | D ],}t |jD ]$\}}|j}||k r|tjkrq
|tjtjfv r.|j|vr.t|jV  q
qd S N)		enumerateparamskindr   POSITIONAL_OR_KEYWORDKEYWORD_ONLYnamer   _name)
signaturespositional_countused_kwargscall_sigipr+   r"   r"   r#   _get_signature_param_names"   s   
r6   c                 C   sp   |rdS d}| D ]-}t |jD ] \}}|j}|tju r  dS ||kr/|tjtjfv r/d} nq|s5 |S q|S )NTF)r)   r*   r+   r   VAR_POSITIONALr,   POSITIONAL_ONLY)r0   r1   r2   must_be_kwarg	signaturer4   r5   r+   r"   r"   r#   _must_be_kwarg.   s&   
r;   c              	   c   s    t  }tjr| }|D ]H}|j}tjr| }tj|||drUtj| ||t	|||d}	|	j
|	jf}
|
|vrU||
 |j}|d urR| }|d urR|jdkrRq|	V  qd S )Nfuzzy)is_fuzzycached_namedel_stmt)setr
   case_insensitive_completionlowerr    r   matchr   
Completionlenr.   completeadd	tree_nameget_definitiontype)inference_statecompletion_namesstack	like_namer=   r?   comp_dctr.   stringnewkrI   
definitionr"   r"   r#   filter_namesD   s8   
rU   c                    s    dd |D   fdd| D S )Nc                 S   s   h | ]}|j qS r"   r.   ).0dr"   r"   r#   	<setcomp>a       z%_remove_duplicates.<locals>.<setcomp>c                    s   g | ]	}|j  vr|qS r"   rV   rW   cnamesr"   r#   
<listcomp>b   s    z&_remove_duplicates.<locals>.<listcomp>r"   )completionsother_completionsr"   r]   r#   _remove_duplicates`   s   rb   c                 C   s   | j j|d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leafr"   r"   r#   get_user_contexte   s   
rk   c                 C   s<   | j |dd}t|tjtjfs|j}t|tjtjfr|S )NTrc   )rf   
isinstancer   ScopeFlowparent)module_noderi   noder"   r"   r#   get_flow_scope_nodem   s
   rr   c                 C      g S r(   r"   )contextfunction_namedecorator_nodesr"   r"   r#   complete_param_namesu   s   rw   c                   @   s|   e Zd Z	dddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zd ddZd!ddZdd Zdd ZdS )"rE   Fc                 C   sB   || _ || _|j| _|| _t| j||| _|| _|| _	|| _
d S r(   )_inference_state_module_contextre   _module_node_code_linesr   get_on_completion_name
_like_name_original_position_signatures_callback_fuzzy)r!   rL   rh   
code_linesri   signatures_callbackr=   r"   r"   r#   __init__}   s   
zCompletion.__init__c           	      C   s   | j j| jdd}t|| j\}}}t| j| j|p|| j|d u r"d n|| | jd}|d urF|sFtt	| j
| j|||| j| j| j| j| j
}|d urX|sVd|v rV| ||}|S | |\}}tt| j
|| j| j| j|d}t||t|dd d S )	NTrc   r<   
)r?   c                 S   s    | j d| j d| j  fS )N___)r.   
startswithrC   )xr"   r"   r#   <lambda>   s   
 
z%Completion.complete.<locals>.<lambda>)key)rz   rf   r~   _extract_string_while_in_stringr   ry   r{   r   listr   rx   r}   r   _complete_in_string_complete_pythonrU   rN   rb   sorted)	r!   rj   rQ   
start_leafquoteprefixed_completionsr?   rM   r`   r"   r"   r#   rG      sB   	
zCompletion.completec              
      s  | j j}d | _}| jd | jd t| j f| _d}zt|| j	|| j | _}W n+ tj
yU } z|jj}|dkrE|g fW  Y d}~S ||  fW  Y d}~S d}~ww t|  d v r| jj| jdd}| }| jd }|j| j  kr|jksn |jd }|dur|}		 t|	dd	d
dd}	|	du rn=|	j}
|
dkr|	jd }t|tr|jd }
|	jd |kr|
dkr ddg7  n|
dkr͈ g d7  n	|
d	krֈ d qg }d}t fddtjtjfD rdd |D }t|}|r
|d dv r
|t| j ddfS d|v r-t!|d|v \}}d|v o d|v  }|| j"|||d7 }n|d dv rZ|d dkrZ| j| j}|jdkrL| }| #| \}}||7 }nR| $ rg|| %|7 }nE|d dv r|d d v r| j&| j }|r|d j'}t|( }|) }|t*|||7 }t+|||}|s||  7 }|| j dd7 }|s| j	| jd d  d| jd  }|| j, | p|d d!v o|d"d d#k d$7 }||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      .ifTrc   if_stmtfor_stmt
while_stmttry_stmt
error_node_stmtelifelse)exceptfinallyr   Fc                 3   s    | ]}| v V  qd S r(   r"   )rW   tallowed_transitionsr"   r#   	<genexpr>      z.Completion._complete_python.<locals>.<genexpr>c                 S      g | ]}|j qS r"   )nonterminal)rW   
stack_noder"   r"   r#   r_     rZ   z/Completion._complete_python.<locals>.<listcomp>)asdefclass)is_functionimport_stmtimport_fromimportonly_modules)trailerdotted_name	endmarker)(,)r   arglist	decoratorz 	.;z...)only_values)-rx   grammarrN   r~   rF   r}   	_positionr   get_stack_at_positionr{   OnErrorLeaf
error_leafvalue_complete_global_scoper   )_allowed_transition_names_and_token_typesrz   rf   get_previous_leaf	start_posend_posr   rK   childrenrl   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_argumentsr6   r;   _complete_keywords)r!   rj   r   rN   r?   er   previous_leafindentstmttype_firstrM   kwargs_onlynonterminalsnodeslevelr^   r   dotnr0   call_detailsr2   r1   current_liner"   r   r#   r      s   














"zCompletion._complete_pythonc                 C   sL   | 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   )rN   r   rF   r   )r!   tosr"   r"   r#   r   G  s   

z#Completion._is_parameter_completionc                 C   s   | j d }|jdkr| j d }|jdkrTt| j| j}t|dd}|d urT|jdkrD|jd }|jdkr8|j}n|jdkrA|g}ng }n| }|j	d	 }t
||j|S g S )
Nr   r   funcdefr   r   
decoratorsr   r   )rN   r   rk   ry   r   r   rK   r   get_decoratorsr   rw   r   )r!   rj   r   rt   rq   r   r   ru   r"   r"   r#   r   T  s$   








zCompletion._complete_paramsc                 c   s>    |D ]}t |tr| r|r|dv rt| j|V  qd S )N)TrueFalseNone)rl   strisalphar   KeywordNamerx   )r!   r   r   rS   r"   r"   r#   r   k  s   zCompletion._complete_keywordsc                 C   sT   t | j| j}td| t| j| j}t|| j|}g }|D ]}|| 7 }q|S )Nzglobal completion scope: %s)	rk   ry   r   r	   dbgrr   rz   r   values)r!   rt   flow_scope_nodefiltersrM   filterr"   r"   r#   r   q  s   z!Completion._complete_global_scopec                 C   sv   | j |}t||}tjd|dd d }t|dkr4|\}| r4t|jdkr4|jd }|dv r4|}|| |fS )Nztrailer completion values: %sMAGENTA)colorr   r   )numpy
tensorflow
matplotlibpandas)	ry   rg   r   r	   r   rF   	is_modulestring_names_complete_trailer_for_values)r!   r   inferred_contextr   r?   vmodule_namer"   r"   r#   r     s   

zCompletion._complete_trailerc                 C   s   t | j| j}t||S r(   )rk   ry   r   complete_trailer)r!   r   user_contextr"   r"   r#   r     s   
z'Completion._complete_trailer_for_valuesr   Tc                 C   s2   dd |D }t | j|| j|}|j| j|dS )Nc                 S   r   r"   )r   rW   r   r"   r"   r#   r_     rZ   z2Completion._get_importer_names.<locals>.<listcomp>r   )r   Importerrx   ry   rM   )r!   r^   r   r   r4   r"   r"   r#   r     s   zCompletion._get_importer_namesc                 c   s    | j j| jdd}t|d}|du rdS | j|}|jd |jd kr(dS |jdd}t	| |D ]}|
 D ]}|jdk|krF|V  q:q4dS )zP
        Autocomplete inherited methods when overriding in child class.
        Trc   classdefNr   )is_instancefunction)rz   rf   r   r   r   ry   create_valuer   get_filtersnextr   api_type)r!   r   rj   clsclass_valuer   r   r.   r"   r"   r#   r     s"   zCompletion._complete_inheritedc                    sz    fdd}t |}t|dd t| }|d dur(dd |D }| |S t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   sT    d} D ]"}|s| ds| drtdd|V  nd V  ttd|}qd S )NFz>>> z^( *>>> ?| +) z *>>>)r   resubboolrD   )linesinclude_next_linelr   r"   r#   iter_relevant_lines  s   z;Completion._complete_in_string.<locals>.iter_relevant_linesT)keependsr   Nc                 S   s   g | ]
}|d u r
dn|qS )Nr   r"   r[   r"   r"   r#   r_     s    z2Completion._complete_in_string.<locals>.<listcomp>z
`([^`\s]+)r   )r   r   r   _complete_code_linesr  searchgroup)r!   r   rQ   r  relevant_code_linesrD   r"   r  r#   r     s   


zCompletion._complete_in_stringc                 C   sN   | j jd|}t| j| j ||d}t| j | ||jdd | j	d
 S )Nr  )in_module_contextrL   rp   r   c                  _   rs   r(   r"   )argskwargsr"   r"   r#   r     s    z1Completion._complete_code_lines.<locals>.<lambda>)r   ri   r   r=   )rx   r   parsejoinr   ry   rE   
as_contextr   r   rG   )r!   r   rp   module_valuer"   r"   r#   r    s"   zCompletion._complete_code_linesN)F)r   T)T)r%   r&   r'   r   rG   r   r   r   r   r   r   r   r   r   r   r  r"   r"   r"   r#   rE   |   s     
+ 

 rE   c                 C   s.   g }| D ]}|j jdkrg }q||j7 }q|S )N
small_stmt)dfa	from_ruler   )rN   r   r   r"   r"   r#   r     s   r   z^\w*(\'{3}|"{3}|\'|")c                    s   fdd} | j k rdS | jdkr|| S g }| d ur| jdkrsd| jv s+d| jv rst| jdkr6|| S d }| jsV|  }|d u sT|jd	ksTtd
d |j D sVd }d fdd|D |pd| |d u rjdn|jt	|   fS | j
 d kr}	 dS |d|  |  } | d usdS )Nc                    s   i }| j  d kr d | j |d< tj| jfi |}|s dS |d}| j  d kr9 d | j|  k r9dS t|  | d  | |fS )Nr   r   endposNNN)linecolumn_string_startrD   r   r  endr   )rj   r!  rD   startri   r"   r#   return_part_of_leaf  s   
$z<_extract_string_while_in_string.<locals>.return_part_of_leafr*  rQ   r   "'r   r.   c                 s   s    | ]}|d v V  qdS )rubfNr"   r[   r"   r"   r#   r     r   z2_extract_string_while_in_string.<locals>.<genexpr>r  c                 3   s    | ]}t | V  qd S r(   r   )rW   r  r0  r"   r#   r         r   )r   rK   r   rF   prefixr   allrC   r#  r   r+  insert)rj   ri   r1  leavesprefix_leafr"   r0  r#   r     s<   

r   c                 C   s   g }|D ]"}|j | jdD ]}|| 7 }q| s&t|tr&|t| |7 }qt|}|D ]}||vrC|j | jdD ]}|| 7 }q:q-|S )N)origin_scope)r  re   r   is_stubrl   r   _complete_getattrr   )r  r   rM   r   r   python_valuesr\   r"   r"   r#   r  "  s   r  c                 C   s4  | dp	| d}tdd |D }|D ]}|j}|du s$|jdkr%q| D ]m}|jdkr1q)|jd }|jd	kr<q)|jd
 }|jd }	t|jdksW|jdksW|jdkrXq)|	jd }
|
jdksit|
jdk rjq)|	 }|
jd
 }|
jd }|
||j}tdd |D sq)||}t| |    S q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                 s   s    | ]}|  V  qd S r(   )infer)rW   r.   r"   r"   r#   r   I  s
    
z$_complete_getattr.<locals>.<genexpr>Nr   return_stmtr   	atom_exprr      r.   getattrr      c                 s   s    | ]}|j d kV  qdS )paramN)r  r  r"   r"   r#   r   h  r5  )get_function_slot_namesr   	from_setsre   rK   iter_return_stmtsr   rF   r   r$  gotor   r   
infer_noder  )r  instancer^   	functionsfuncre   rB  rC  atomr   r   rt   object_node	name_node	name_listobjectsr"   r"   r#   r=  3  sD   











r=  Fc	              	   c   s.   |d d D ]1}	g }
|D ]}|	|j kr*|jd ur!|jdv r!|r!q|
t|| 7 }
qtd|
|d d  |
}q|d  }|D ]S}|j  }|rRtj	|||dsX|s||krt
|trgdd | D }n|g}|rpt|}|D ]!}|rtj| |d t||d}nt| |}|r||jkr|V  qrqAd S )	Nr   )module	namespacezdot lookup on search %s from %s
   r<   c                 S   r   r"   rV   )rW   r  r"   r"   r#   r_     rZ   z$search_in_module.<locals>.<listcomp>)rN   like_name_lengthr>   )r    rI   r  r  rA  r	   r   rC   r   rD   rl   r   r   r   rE   rF   NamerK   )rL   rh   r^   wanted_nameswanted_typerG   r=   ignore_importsconverts	new_namesr   	last_namerQ   n2def_r"   r"   r#   search_in_modules  sR   


rc  )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   r6   r;   rU   rb   rk   rr   decoraterw   rE   r   compiler-  r   r  r=  rc  r"   r"   r"   r#   <module>   sX    
  k

0A