B
    œúðb7j  ã               @   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?d7d5d6„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!   ú2lib/python3.7/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   sj   xd| D ]\}xVt |jƒD ]H\}}|j}||k r8|tjkr8q|tjtjfkr|j|krt|jƒV  qW qW d S )N)	Ú	enumerateÚparamsÚkindr   ÚPOSITIONAL_OR_KEYWORDZKEYWORD_ONLYÚnamer   Ú_name)Ú
signaturesÚpositional_countÚused_kwargsZcall_sigÚiÚpr)   r!   r!   r"   Ú_get_signature_param_names"   s    

r2   c             C   sn   |rdS d}x\| D ]T}xHt |jƒD ]:\}}|j}|tjkr>dS ||kr"|tjtjfkr"d}P q"W |sP qW |S )NTF)r'   r(   r)   r   ZVAR_POSITIONALr*   ZPOSITIONAL_ONLY)r-   r.   r/   Zmust_be_kwargZ	signaturer0   r1   r)   r!   r!   r"   Ú_must_be_kwarg.   s    


r3   c          	   c   s²   t ƒ }tjr| ¡ }x˜|D ]}|j}tjr2| ¡ }tj|||drtj| ||t	|ƒ||d}	|	j
|	jf}
|
|kr| |
¡ |j}|d k	r¤| ¡ }|d k	r¤|jdkr¤q|	V  qW d S )N)Úfuzzy)Úis_fuzzyÚcached_nameZdel_stmt)Úsetr
   Zcase_insensitive_completionÚlowerr   r   Úmatchr   Ú
CompletionÚlenr+   ÚcompleteÚaddÚ	tree_nameZget_definitionÚtype)Úinference_stateÚcompletion_namesÚstackZ	like_namer4   r6   Zcomp_dctr+   ÚstringÚnewÚkr>   Z
definitionr!   r!   r"   Úfilter_namesD   s0    

rF   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   s    z%_remove_duplicates.<locals>.<setcomp>c                s   g | ]}|j ˆ kr|‘qS r!   )r+   )rG   Úc)Únamesr!   r"   ú
<listcomp>b   s    z&_remove_duplicates.<locals>.<listcomp>r!   )ÚcompletionsZother_completionsr!   )rK   r"   Ú_remove_duplicates`   s    rN   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    rV   c             C   s0   | j |dd}xt|tjtjfƒs*|j}qW |S )NT)rO   )rQ   Ú
isinstancer   ZScopeZFlowÚparent)Úmodule_noderT   Únoder!   r!   r"   Úget_flow_scope_nodem   s    
r[   c             C   s   g S )Nr!   )ÚcontextÚfunction_nameZdecorator_nodesr!   r!   r"   Úcomplete_param_namesu   s    r^   c               @   sz   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 )"r:   Fc             C   sB   || _ || _|j| _|| _t | j||¡| _|| _|| _	|| _
d S )N)Ú_inference_stateÚ_module_contextrP   Ú_module_nodeÚ_code_linesr   Zget_on_completion_nameÚ
_like_nameÚ_original_positionÚ_signatures_callbackÚ_fuzzy)r    r@   rS   Ú
code_linesrT   Úsignatures_callbackr4   r!   r!   r"   Ú__init__}   s    zCompletion.__init__c       	      C   sø   | j j| jdd}t|| jƒ\}}}t| j| j|p4|| j|d krDd n|| | jd}|d k	rŒ|sŒtt	| j
| j|||| j| j| j| j| jƒ
ƒ}|d k	r°|s¬d|kr¬|  ||¡}|S |  |¡\}}tt| j
|| j| j| j|dƒ}t||ƒt|dd„ d S )	NT)rO   )r4   Ú
)r6   c             S   s    | j  d¡| j  d¡| j  ¡ fS )NÚ__Ú_)r+   Ú
startswithr8   )Úxr!   r!   r"   Ú<lambda>³   s   

z%Completion.complete.<locals>.<lambda>)Úkey)ra   rQ   rd   Ú_extract_string_while_in_stringr   r`   rb   rf   Úlistr   r_   rc   re   Ú_complete_in_stringÚ_complete_pythonrF   rB   rN   Úsorted)	r    rU   rC   Ú
start_leafZquoteZprefixed_completionsr6   rA   rM   r!   r!   r"   r<      s6    


zCompletion.completec          
      sÄ  | j j}d | _}| jd | jd t| jƒ f| _d}yt || j	|| j¡ | _}W nD tj
k
r˜ } z$|jj}|dkr€|g fS ||  ¡ fS d}~X Y nX t| ¡ ƒ‰ dˆ kr´| jj| jdd}| ¡ }| jd }|j| j  krî|jksún |jd }|dk	r´|}	xªt|	dd	d
ddƒ}	|	dkr(P |	j}
|
dkrX|	jd }t|tƒrX|jd }
|	jd |kr
|
dkr€ˆ ddg7 ‰ n.|
dkršˆ dddg7 ‰ n|
d	kr
ˆ  d¡ q
W g }d}t‡ fdd„tjtjfD ƒƒr^dd„ |D ƒ}t|ƒ}|r|d dkr|t| j ddƒfS d|kr`t!|d|kƒ\}}d|koFd|k }|| j"|||d7 }nþ|d dkrº|d dkrº| j | j¡}|jdkrž| ¡ }|  #| ¡ ¡\}}||7 }n¤|  $¡ rÔ||  %|¡7 }nŠ|d d kr<|d d!k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"k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   é   Ú.ÚifT)rO   Zif_stmtZfor_stmtZ
while_stmtZtry_stmtÚ
error_nodeZ_stmtÚelifÚelseÚexceptÚfinallyFc             3   s   | ]}|ˆ kV  qd S )Nr!   )rG   Út)Úallowed_transitionsr!   r"   ú	<genexpr>  s    z.Completion._complete_python.<locals>.<genexpr>c             S   s   g | ]
}|j ‘qS r!   )Únonterminal)rG   Ú
stack_noder!   r!   r"   rL     s    z/Completion._complete_python.<locals>.<listcomp>éÿÿÿÿ)ÚasÚdefÚclass)Úis_functionZimport_stmtZimport_fromÚimport)Úonly_modules)ÚtrailerZdotted_nameZ	endmarker)ú(ú,)r‹   ÚarglistÚ	decoratorz 	.;éýÿÿÿz...)Úonly_values)-r_   ÚgrammarrB   rd   r;   rc   Ú	_positionr   Zget_stack_at_positionrb   ZOnErrorLeafÚ
error_leafÚvalueÚ_complete_global_scoperr   Z)_allowed_transition_names_and_token_typesra   rQ   Úget_previous_leafÚ	start_posÚend_posr   r?   ÚchildrenrW   r   ÚappendÚanyr   ÚNAMEÚINDENTÚ_gather_nodesÚ_complete_inheritedr   Ú_get_importer_namesÚ_complete_trailerÚ_is_parameter_completionÚ_complete_paramsre   Z_call_detailsZiter_used_keyword_argumentsZcount_positional_argumentsr2   r3   Ú_complete_keywords)r    rU   r’   rB   r6   Úer•   Úprevious_leafÚindentZstmtZtype_ÚfirstrA   Zkwargs_onlyZnonterminalsÚnodesÚlevelrK   rŠ   ÚdotÚnr-   Zcall_detailsr/   r.   Zcurrent_liner!   )r€   r"   rt   ¸   sª    

















"zCompletion._complete_pythonc             C   sL   | j d }|jdkr&t|jƒdkr&dS |jdkr4dS |jdkoJ|jd dkS )Nr„   Zlambdefrw   TÚ
parameters)ZtypedargslistZvarargslistr   )rB   r‚   r;   rª   )r    Ztosr!   r!   r"   r£   G  s    

z#Completion._is_parameter_completionc             C   s¬   | j d }|jdkr| j d }|jdkr¨t| j| jƒ}t|ddƒ}|d k	r¨|jdkrˆ|jd }|jdkrp|j}q|jdkr‚|g}qg }n| ¡ }|j	d	 }t
||j|ƒS g S )
Néþÿÿÿr®   r   Úfuncdefrz   r   Ú
decoratorsr   rw   )rB   r‚   rV   r`   r“   r   r?   rš   Zget_decoratorsrª   r^   r•   )r    rU   rƒ   r\   rZ   r­   r±   r]   r!   r!   r"   r¤   T  s$    








zCompletion._complete_paramsc             c   s@   x:|D ]2}t |tƒr| ¡ r|r(|dkrt | j|¡V  qW d S )N)ÚTrueÚFalseÚNone)rW   ÚstrÚisalphar   ZKeywordNamer_   )r    r€   r‘   rE   r!   r!   r"   r¥   k  s    
zCompletion._complete_keywordsc             C   sX   t | j| jƒ}t d|¡ t| j| jƒ}t|| j|ƒ}g }x|D ]}|| ¡ 7 }q@W |S )Nzglobal completion scope: %s)	rV   r`   r“   r	   Údbgr[   ra   r   Úvalues)r    r\   Zflow_scope_nodeÚfiltersrA   Úfilterr!   r!   r"   r–   q  s    
z!Completion._complete_global_scopec             C   sv   | j  |¡}t||ƒ}tjd|dd d }t|ƒdkrh|\}| ¡ rht|jƒdkrh|jd }|dkrh|}||  |¡fS )Nztrailer completion values: %sZMAGENTA)Zcolorrw   r   )ZnumpyZ
tensorflowZ
matplotlibZpandas)	r`   rR   r   r	   r·   r;   Z	is_moduleZstring_namesÚ_complete_trailer_for_values)r    r§   Zinferred_contextr¸   r6   ÚvZmodule_namer!   r!   r"   r¢     s    

zCompletion._complete_trailerc             C   s   t | j| jƒ}t||ƒS )N)rV   r`   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   s   g | ]
}|j ‘qS r!   )r•   )rG   r­   r!   r!   r"   rL   —  s    z2Completion._get_importer_names.<locals>.<listcomp>)rŠ   )r   ZImporterr_   r`   rA   )r    rK   r«   rŠ   r0   r!   r!   r"   r¡   –  s    zCompletion._get_importer_namesc             c   sš   | j j| jdd}t |d¡}|dkr*dS | j |¡}|jd |jd krNdS |jdd}t	|ƒ x2|D ]*}x$| 
¡ D ]}|jdk|krv|V  qvW qhW dS )zP
        Autocomplete inherited methods when overriding in child class.
        T)rO   ZclassdefNrw   )Zis_instanceZfunction)ra   rQ   r“   r   r   r`   Zcreate_valuer˜   Úget_filtersÚnextr¸   Úapi_type)r    rˆ   rU   ÚclsZclass_valuer¹   rº   r+   r!   r!   r"   r    ›  s    
zCompletion._complete_inheritedc                sz   ‡ fdd„}t |ƒ}t|dd‰ t|ˆ ƒƒ}|d dk	rPdd„ |D ƒ}|  |¡S t d	ˆ d ¡}|rv|  | 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   sV   d}xLˆ D ]D}|s&|  d¡s&|  d¡r8t dd|¡V  nd V  tt d|¡ƒ}q
W d S )NFz>>>ú z^( *>>> ?| +)Ú z *>>>)rm   ÚreÚsubÚboolr9   )ÚlinesZinclude_next_lineÚl)rg   r!   r"   Úiter_relevant_lines½  s    
z;Completion._complete_in_string.<locals>.iter_relevant_linesT)Úkeependsr„   Nc             S   s   g | ]}|d krdn|‘qS )Nrj   r!   )rG   rJ   r!   r!   r"   rL   Ì  s    z2Completion._complete_in_string.<locals>.<listcomp>z
`([^`\s]+)rw   )r   r   rr   Ú_complete_code_linesrÅ   ÚsearchÚgroup)r    rv   rC   rÊ   Zrelevant_code_linesr9   r!   )rg   r"   rs   ³  s    


zCompletion._complete_in_stringc             C   sN   | j j d |¡¡}t| j| j ||d}t| j | ¡ ||jdd„ | j	d 
¡ S )NrÄ   )Zin_module_contextr@   rY   rg   c              _   s   g S )Nr!   )ÚargsÚkwargsr!   r!   r"   ro   à  ó    z1Completion._complete_code_lines.<locals>.<lambda>)rg   rT   rh   r4   )r_   r’   ÚparseÚjoinr   r`   r:   Ú
as_contextr™   rf   r<   )r    rg   rY   Zmodule_valuer!   r!   r"   rÌ   Ó  s    zCompletion._complete_code_linesN)F)r   T)T)r$   r%   r&   ri   r<   rt   r£   r¤   r¥   r–   r¢   r»   r¡   r    rs   rÌ   r!   r!   r!   r"   r:   |   s   
+ 

 r:   c             C   s2   g }x(| D ] }|j jdkr g }q
||j7 }q
W |S )NZ
small_stmt)ZdfaZ	from_rulerª   )rB   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 }xà| d k	r| jdkrêd| jksZd| jkrêt| jƒdkrp|| ƒS d }| js°|  ¡ }|d ks¬|jd	ks¬td
d„ |j ¡ D ƒƒs°d }d ‡ fdd„|D ƒ¡|pÌ| |d krØdn|jt	| ˆ ƒ fS | j
ˆ d krúP | d| ¡ |  ¡ } q2W dS )Nc                sŠ   i }| j ˆ d kr$ˆ d | j |d< tj| jf|Ž}|s<dS | d¡}| j ˆ d krnˆ d | j| ¡  k rndS t| ˆ ƒ| ¡ d … | |fS )Nr   rw   Úendpos)NNN)ÚlineÚcolumnÚ_string_startr9   r•   rÎ   Úendr   )rU   rÐ   r9   Ústart)rT   r!   r"   Úreturn_part_of_leafó  s    
$z<_extract_string_while_in_string.<locals>.return_part_of_leaf)NNNrC   r”   ú"ú'rw   r+   c             s   s   | ]}|d kV  qdS )ZrubfNr!   )rG   rJ   r!   r!   r"   r     s    z2_extract_string_while_in_string.<locals>.<genexpr>rÄ   c             3   s   | ]}t |ˆ ƒV  qd S )N)r   )rG   rÉ   )rT   r!   r"   r     s    r   )r˜   r?   r•   r;   Úprefixr—   Úallr8   rÓ   r   rÖ   Úinsert)rU   rT   rÛ   ZleavesZprefix_leafr!   )rT   r"   rq   ò  s2    

rq   c             C   sœ   g }xP|D ]H}x"|j | jdD ]}|| ¡ 7 }qW | ¡ s
t|tƒr
|t| |ƒ7 }q
W t|ƒ}x8|D ]0}||krdx"|j | jdD ]}|| ¡ 7 }q€W qdW |S )N)Zorigin_scope)r¿   rP   r¸   Zis_stubrW   r   Ú_complete_getattrr   )r¾   r¸   rA   r•   rº   Zpython_valuesrJ   r!   r!   r"   r½   "  s    

r½   c             C   s:  |  d¡p|  d¡}t dd„ |D ƒ¡}x
|D ] }|j}|dks0|jdkrPq0xà| ¡ D ]Ô}|jdkrjqZ|jd }|jd	kr€qZ|jd
 }|jd }	t|jƒdksZ|jdksZ|jdkr¸qZ|	jd }
|
jdksZt|
jƒdk rÜqZ| 	¡ }|
jd
 }|
jd }| 
||j¡}tdd„ |D ƒƒsqZ| |¡}t| |ƒS W q0W 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 )N)Úinfer)rG   r+   r!   r!   r"   r   J  s   z$_complete_getattr.<locals>.<genexpr>Nr°   Úreturn_stmtrw   Ú	atom_exprr   é   r+   ÚgetattrrŽ   é   c             s   s   | ]}|j d kV  qdS )ZparamN)rÁ   )rG   r­   r!   r!   r"   r   h  s    )Zget_function_slot_namesr   Z	from_setsrP   r?   Ziter_return_stmtsrš   r;   r•   rÔ   Zgotor˜   rœ   Z
infer_noder½   )r¾   ÚinstancerK   Z	functionsÚfuncrP   rå   ræ   Zatomr‹   rŽ   r\   Zobject_nodeZ	name_nodeZ	name_listZobjectsr!   r!   r"   rá   3  s@    












rá   Fc	          	   c   s@  xv|d d… D ]f}	g }
xB|D ]:}|	|j kr|jd k	rD|jdkrD|rDq|
t|| ¡ ƒ7 }
qW t d|
|d d… ¡ |
}qW |d  ¡ }x¶|D ]®}|j  ¡ }|r¬tj	|||ds¸|sŠ||krŠt
|tƒrÖdd„ | ¡ D ƒ}n|g}|rèt|ƒ}xN|D ]F}|rtj| |d t|ƒ|d}nt | |¡}|r.||jkrî|V  qîW qŠW d S )	Nr„   )ÚmoduleÚ	namespacezdot lookup on search %s from %sé
   )r4   c             S   s   g | ]
}|j ‘qS r!   )r+   )rG   r¼   r!   r!   r"   rL   Š  s    z$search_in_module.<locals>.<listcomp>)rB   Zlike_name_lengthr5   )r   r>   rÁ   r½   rä   r	   r·   r8   r   r9   rW   r   r   r   r:   r;   ÚNamer?   )r@   rS   rK   Zwanted_namesZwanted_typer<   r4   Zignore_importsZconvertÚsZ	new_namesr­   Ú	last_namerC   Zn2Zdef_r!   r!   r"   Úsearch_in_modules  s@    






rò   )FFFF)ArÅ   Útextwrapr   Úinspectr   Zparso.python.tokenr   Zparso.pythonr   Z
parso.treer   r   Zparsor   Zjedir	   r
   Zjedi.apir   r   r   Zjedi.api.stringsr   Zjedi.api.file_namer   Zjedi.inferencer   Zjedi.inference.base_valuer   Zjedi.inference.helpersr   r   Zjedi.inference.contextr   Zjedi.inference.valuer   Zjedi.inference.docstring_utilsr   Zjedi.inference.namesr   r   Z!jedi.inference.gradual.conversionr   r   Zjedi.parser_utilsr   Zjedi.pluginsr   r   r2   r3   rF   rN   rV   r[   Zdecorater^   r:   rŸ   ÚcompilerØ   rq   r½   rá   rò   r!   r!   r!   r"   Ú<module>   sR     k

0A 