o
    ˷e                     @   s   d 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Zdd Zejedd Zeddedd Zdd Zdd Zdd Z dS )a  
One of the really important features of |jedi| is to have an option to
understand code like this::

    def foo(bar):
        bar. # completion here
    foo(1)

There's no doubt wheter bar is an ``int`` or not, but if there's also a call
like ``foo('str')``, what would happen? Well, we'll just show both. Because
that's what a human would expect.

It works as follows:

- |Jedi| sees a param
- search for function calls named ``foo``
- execute these calls and check the input.
    )settings)debug)get_parent_scope)inference_state_method_cache)TreeArguments)get_executed_param_names)is_stdlib_path)to_list)instance)ValueSet	NO_VALUES)#get_module_contexts_containing_name)	recursion   c                    s    fdd}|S )Nc              
      s   | j }t|| j0}|r1| jd7  _z | |W | jd8  _W  d    S | jd8  _w tW  d    S 1 s=w   Y  d S )N   )inference_stater   execution_allowed	tree_nodedynamic_params_depthr   )function_valueparam_indexinfallowedfunc T/var/www/ideatree/venv/lib/python3.10/site-packages/jedi/inference/dynamic_params.pywrapper&   s   
	$z"_avoid_recursions.<locals>.wrapperr   )r   r   r   r   r   _avoid_recursions%   s   r   c                    s    j }tjstS    }|durt|rtS |jdkr(t|}|du r'tS n|j	j
}tjd|dd   }t|||}t fdd|D }tjddd |S )	a:  
    A dynamic search for param values. If you try to complete a type:

    >>> def func(foo):
    ...     foo
    >>> func(1)
    >>> func("")

    It is not known what the type ``foo`` without analysing the whole code. You
    have to look for all calls to ``func`` to find out what ``foo`` possibly
    is.
    NlambdefzDynamic param search in %s.MAGENTA)colorc                 3   s"    | ]}t  |  V  qd S )N)r   infer).0	argumentsr   r   r   r   	<genexpr>\   s    
z'dynamic_param_lookup.<locals>.<genexpr>zDynamic param result finished)r   r   dynamic_paramsr   get_root_context
py__file__r   type_get_lambda_namenamevaluer   dbg_search_function_argumentsr   	from_sets)r   r   funcdefpathstring_namemodule_contextarguments_listvaluesr   r%   r   dynamic_param_lookup6   s(   
r7   N)defaultc                 c   s    |}|dkrt |}|jdkr|jj}|}d}d}| j}tjr*t|| g|dd}n| g}|D ]5}	t|	|D ](\}
}|d7 }||j	 t
krI  dS |	|
}t||||
|D ]}d	}|V  qVq6|rd dS q/dS )
z(
    Returns a list of param names.
    __init__classdefFr      )limit_reductionr   NT)r   r*   r,   r-   r   r    dynamic_params_for_other_modulesr   _get_potential_nodesr   MAX_PARAM_SEARCHEScreate_context_check_name_for_execution)r4   r1   r3   compare_nodeclsfound_argumentsir   module_contextsfor_mod_contextr,   trailerrandom_contextr$   r   r   r   r/   f   s@   


r/   c                 C   sD   | j }|jdkr t| d }|dkr |jd }|jdkr |jS d S )N	expr_stmt=r   r,   )parentr*   nextyield_operatorschildrenr-   )nodestmtfirst_operatorfirstr   r   r   r+      s   


r+   c                 c   sb    z	| j  | }W n
 ty   Y d S w |D ]}| }|j}|jdkr.|dkr.||fV  qd S )NrH   ()r   get_used_namesKeyErrorget_next_leafrL   r*   )module_valuefunc_string_namenamesr,   bracketrH   r   r   r   r>      s   
r>   c                 #   s   ddl m}  fdd} |D ]o}|j}||kr$||V  qt|j|r|jdkr|j }	t|	dkr;q|	d  }
dd |
D |gkr 	 }|
||}t||	d j}|D ]#\}|j|j  k rq|jk rn q`||}t|||E d H  q`qd S )	Nr   )BaseFunctionExecutionContextc                    sp   j d }|dkrd }t |}ddlm} | jjdkr,t| j| |}|||S | 	 r6|| j|}|S )Nr   )r   )InstanceArgumentsr:   )
rO   r   jedi.inference.value.instancer^   r   r*   r
   TreeInstanceparent_contextis_bound_method)r-   arglistargsr^   created_instancecontextr   rH   r   r   create_args   s    

z._check_name_for_execution.<locals>.create_argsr1   r   c                 S   s   g | ]}|j qS r   )r   )r#   vr   r   r   
<listcomp>   s    z-_check_name_for_execution.<locals>.<listcomp>)jedi.inference.value.functionr\   r"   r   
isinstancera   r*   get_param_nameslenr(   
as_contextr>   r3   	start_posend_posr@   rA   )r   rg   rB   r,   rH   r\   rh   r-   
value_nodeparam_namesr6   r4   execution_contextpotential_nodesrI   r   rf   r   rA      s:   



rA   )!__doc__jedir   r   jedi.parser_utilsr   jedi.inference.cacher   jedi.inference.argumentsr   jedi.inference.paramr   jedi.inference.helpersr   jedi.inference.utilsr	   jedi.inference.valuer
   jedi.inference.base_valuer   r   jedi.inference.referencesr   jedi.inferencer   r?   r   increase_indentr7   r/   r+   r>   rA   r   r   r   r   <module>   s0    ..