Format: application/x-python-code
⬇ Download File (18.5 KB)
   1  o
   2  
   3  �h�n�@s�ddlZddlmZddlmZddlmZddlmZddl	m
   4  Z
   5  mZddlm
   6  Z
   7  ddlmZdd	lmZdd
   8  lmZddlmZddlmZdd
   9  lmZddlmZddlmZddlmZddlmZmZddl m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'm(Z(ddl)m*Z*m+Z+ddl,m-Z-ddl.m/Z/Gdd�de'�Z0dd�Z1dd�Z2dd �Z3d!d"�Z4d#d$�Z5d%d&�Z6e/�7�d'd(��Z8Gd)d*�d*�Z9d+d,�Z:e�;d-�Z<d.d/�Z=d0d1�Z>d2d3�Z?	4	4d9d5d6�Z@d7d8�ZAdS):�N)�dedent)�	Parameter)�PythonTokenTypes)�tree)�search_ancestor�Leaf)�split_lines)�debug)�settings)�classes)�helpers)�keywords)�
  10  complete_dict)�complete_file_name)�imports)�ValueSet)�infer_call_of_leaf�parse_dotted_names)�get_global_filters)�TreeInstance)�DocstringModule)�ParamNameWrapper�
  11  SubModuleName)�convert_values�
  12  convert_names��cut_value_at_position)�plugin_managerc@seZdZdd�ZdS)�ParamNameWithEqualscCs
  13  |jdS)N�=)�string_name��self�r#�Ic:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/api/completion.py�get_public_names
  14  z#ParamNameWithEquals.get_public_nameN)�__name__�
  15  __module__�__qualname__r%r#r#r#r$rsrccsd�|D],}t|j�D]$\}}|j}||kr|tjkrq
  16  |tjtjfvr.|j|vr.t|j�Vq
  17  qdS�N)	�	enumerate�params�kindr�POSITIONAL_OR_KEYWORD�KEYWORD_ONLY�namer�_name)�
  18  signatures�positional_count�used_kwargsZcall_sig�i�pr,r#r#r$�_get_signature_param_names"s�
  19  ���r6cCsp|rdSd}|D]-}t|j�D] \}}|j}|tjurdS||kr/|tjtjfvr/d}nq|s5|Sq|S)NTF)r*r+r,r�VAR_POSITIONALr-�POSITIONAL_ONLY)r1r2r3Z
  20  must_be_kwarg�	signaturer4r5r,r#r#r$�_must_be_kwarg.s&
  21  ���r:c	cs��t�}tjr|��}|D]Q}|j}	|	|vr|	|krq
  22  tjr"|	��}	tj|	||d�r^tj|||t	|�||d�}
  23  |
  24  j
  25  |
  26  jf}||vr^|�|�|j
  27  }|dur[|��}
  28  |
  29  dur[|
  30  jdkr[q
  31  |
  32  Vq
  33  dS)N��fuzzy)�is_fuzzy�cached_nameZdel_stmt)�setr
  34  Zcase_insensitive_completion�lowerr r�matchr�
  35  Completion�lenr/�complete�add�	tree_nameZget_definition�type)�inference_state�completion_names�stackZ	like_namer<�imported_namesr>Zcomp_dctr/�string�new�krFZ
  36  definitionr#r#r$�filter_namesDs<��
  37  ��rOcs dd�|D���fdd�|D�S)NcSsh|]}|j�qSr#�r/)�.0�dr#r#r$�	<setcomp>d�z%_remove_duplicates.<locals>.<setcomp>csg|]	}|j�vr|�qSr#rP�rQ�c��namesr#r$�
  38  <listcomp>esz&_remove_duplicates.<locals>.<listcomp>r#)�completionsZother_completionsr#rWr$�_remove_duplicatescsr[cCs|jj|dd�}|�|�S)zK
  39      Returns the scope in which the user resides. This includes flows.
  40      T�Zinclude_prefixes)�	tree_node�get_leaf_for_position�create_context)�module_context�position�leafr#r#r$�get_user_contexths
  41  rccCs<|j|dd�}t|tjtjf�s|j}t|tjtjf�r|S)NTr\)r^�
  42  isinstancerZScopeZFlow�parent)�module_nodera�noder#r#r$�get_flow_scope_nodeps
  43  �rhcC�gSr)r#)�context�
  44  function_nameZdecorator_nodesr#r#r$�complete_param_namesxsrlc@s|eZdZ	ddd�Zdd�Zdd�Zdd	�Zd
  45  d�Zdd
  46  �Zdd�Z	dd�Z
  47  dd�Zd dd�Zd!dd�Z
  48  dd�Zdd�ZdS)"rBFcCsB||_||_|j|_||_t�|j||�|_||_||_	||_
  49  dSr))�_inference_state�_module_contextr]�_module_node�_code_linesrZget_on_completion_name�
  50  _like_name�_original_position�_signatures_callback�_fuzzy)r"rHr`�
  51  code_linesra�signatures_callbackr<r#r#r$�__init__�s
  52  zCompletion.__init__c
  53  s(�jj�jdd�}t|�j�\}}}t�j�j|p|�j|dur"dn||�jd�}|durF|sFtt	�j
  54  �j|||�j�j�j�j�j�
  55  �}|durX|sVd|vrV��
  56  ||�}|S��|�\}}g}|jdurt|jjdvrt|�t|j��tt�j
  57  |�j�j�j||d��}	t||	�t|	�fdd�d	�S)
  58  NTr\r;�
  59  )�import_as_names�dotted_as_names)r>cs.|j��j�|j�d�|j�d�|j��fS)N�__�_)r/�
  60  startswithrqr@)�xr!r#r$�<lambda>�s
  61  
  62  
  63  �z%Completion.complete.<locals>.<lambda>)�key)ror^rr�_extract_string_while_in_stringrrnrprt�listrrmrqrs�_complete_in_string�_complete_pythonrerG�extend�extract_imported_namesrOrJr[�sorted)
  64  r"rbrL�
  65  start_leaf�quoteZprefixed_completionsr>rIrKrZr#r!r$rD�sH��	�
  66  ���zCompletion.completec
  67  s�|jj}d|_}|jd|jdt|j�f|_d}zt�||j	||j�|_}Wn+tj
  68  yU}z|jj}|dkrE|gfWYd}~S||�
  69  �fWYd}~Sd}~wwt|����d�vr�|jj|jdd�}|��}|jd}|j|jkr|jks�n|jd}|dur�|}		t|	dd	d
  70  dd�}	|	dur�n=|	j}
  71  |
  72  dkr�|	jd}t|t�r�|jd
  73  }
  74  |	jd|kr�|
  75  dkrˆddg7�n|
  76  dkr͈gd�7�n	|
  77  d	krֈ�d�q�g}d}
  78  t�fdd�tjtjfD���r�dd�|D�}t|�}|�r
  79  |ddv�r
  80  |t|j dd��fSd|v�r-t!|d|v�\}}d|v�o d|v}||j"|||d�7}n|ddv�rZ|ddk�rZ|j�|j�}|jdk�rL|��}|�#|���\}}||7}nR|�$��rg||�%|�7}nE|ddv�r�|dd v�r�|j&|j�}|�r�|dj'}t|�(��}|�)�}|t*|||�7}t+|||�}
  81  |
  82  �s�||�
  83  �7}||j dd�7}|
  84  �s�|j	|jddd|jd�}||j,�|�p�|dd!v�o�|d"d�d#kd$�7}||fS)%a�
  85          Analyzes the current context of a completion and decides what to
  86          return.
  87  
  88          Technically this works by generating a parser stack and analysing the
  89          current stack for possible grammar nodes.
  90  
  91          Possible enhancements:
  92          - global/nonlocal search global
  93          - yield from / raise from <- could be only exceptions/generators
  94          - In args: */**: no completion
  95          - In params (also lambda): no completion before =
  96          Nr��.�ifTr\Zif_stmtZfor_stmtZ
  97  while_stmtZtry_stmt�
  98  error_nodeZ_stmt�elif�else)�except�finallyr�Fc3s�|]}|�vVqdSr)r#)rQ�t��allowed_transitionsr#r$�	<genexpr>��z.Completion._complete_python.<locals>.<genexpr>cS�g|]}|j�qSr#)�nonterminal)rQ�
  99  stack_noder#r#r$rYrTz/Completion._complete_python.<locals>.<listcomp>�����)�as�def�class)�is_functionZimport_stmtZimport_from�import��only_modules)�trailerZdotted_nameZ	endmarker)�(�,)r��arglist�	decoratorz 	.;�����z...)�only_values)-rm�grammarrJrrrCrq�	_positionrZget_stack_at_positionrpZOnErrorLeaf�
 100  error_leaf�value�_complete_global_scoper�Z)_allowed_transition_names_and_token_typesror^�get_previous_leaf�	start_pos�end_posrrG�childrenrdr�append�anyr�NAME�INDENT�
 101  _gather_nodes�_complete_inheritedr�_get_importer_names�_complete_trailer�_is_parameter_completion�_complete_paramsrsZ
 102  _call_detailsZiter_used_keyword_argumentsZcount_positional_argumentsr6r:�_complete_keywords)r"rbr�rJr>�er��
 103  previous_leaf�indent�stmt�type_�firstrIZkwargs_onlyZnonterminals�nodes�levelrXr��dot�nr1Zcall_detailsr3r2�current_liner#r�r$r��s�
 104  ����
 105  �
 106  
 107  
 108  �
 109  
 110  
 111  
 112  ��
 113  
 114  �
 115  
 116  
 117  
 118  �"��zCompletion._complete_pythoncCsL|jd}|jdkrt|j�dkrdS|jdvrdS|jdvo%|jddkS)Nr�Zlambdefr�T�
 119  parameters)Z
 120  typedargslistZvarargslistr�)rJr�rCr�)r"Ztosr#r#r$r�Ts
 121  
 122  z#Completion._is_parameter_completioncCs�|jd}|jdkr|jd}|jdkrTt|j|j�}t|dd�}|durT|jdkrD|jd}|jdkr8|j}n|jdkrA|g}ng}n|��}|j	d	}t
 123  ||j|�SgS)
 124  N�����r�r��funcdefr�r�
 125  decoratorsr�r�)rJr�rcrnr�rrGr�Zget_decoratorsr�rlr�)r"rbr�rjrgr�r�rkr#r#r$r�as$
 126  
 127  
 128  
 129  
 130  
 131  
 132  
 133  
 134  zCompletion._complete_paramsccs>�|D]}t|t�r|��r|r|dvrt�|j|�VqdS)N)�True�False�None)rd�str�isalphar
 135  ZKeywordNamerm)r"r�r�rNr#r#r$r�xs���zCompletion._complete_keywordscCsTt|j|j�}t�d|�t|j|j�}t||j|�}g}|D]}||��7}q|S)Nzglobal completion scope: %s)	rcrnr�r	�dbgrhror�values)r"rjZflow_scope_node�filtersrI�filterr#r#r$r�~s�z!Completion._complete_global_scopecCsv|j�|�}t||�}tjd|dd�d}t|�dkr4|\}|��r4t|j�dkr4|jd}|dvr4|}||�|�fS)Nztrailer completion values: %sZMAGENTA)�colorr�r)�numpyZ
 136  tensorflowZ
 137  matplotlibZpandas)	rnr_rr	r�rCZ	is_moduleZstring_names�_complete_trailer_for_values)r"r�Zinferred_contextr�r>�v�module_namer#r#r$r��s
 138  
 139  zCompletion._complete_trailercCst|j|j�}t||�Sr))rcrnr��complete_trailer)r"r��user_contextr#r#r$r��s
 140  z'Completion._complete_trailer_for_valuesrTcCs2dd�|D�}t�|j||j|�}|j|j|d�S)NcSr�r#)r��rQr�r#r#r$rY�rTz2Completion._get_importer_names.<locals>.<listcomp>r�)rZImporterrmrnrI)r"rXr�r�r4r#r#r$r��szCompletion._get_importer_namesccs��|jj|jdd�}t�|d�}|durdS|j�|�}|jd|jdkr(dS|jdd�}t	|�|D]}|�
 141  �D]}|jdk|krF|Vq:q4dS)zP
 142          Autocomplete inherited methods when overriding in child class.
 143          Tr\ZclassdefNr�)Zis_instance�function)ror^r�rrrnZcreate_valuer��get_filters�nextr��api_type)r"r�rb�clsZclass_valuer�r�r/r#r#r$r��s"����zCompletion._complete_inheritedcsz�fdd�}t|�}t|dd��t|���}|ddur(dd�|D�}|�|�St�d	�d�}|r;|�|�d
 144  �g�SgS)aU
 145          To make it possible for people to have completions in doctests or
 146          generally in "Python" code in docstrings, we use the following
 147          heuristic:
 148  
 149          - Having an indented block of code
 150          - Having some doctest code that starts with `>>>`
 151          - Having backticks that doesn't have whitespace inside it
 152          c3sT�d}�D]"}|s|�d�s|�d�rt�dd|�VndVtt�d|��}qdS)NFz>>>� z
 153  ^( *>>> ?| +)�z *>>>)r}�re�sub�boolrA)�linesZinclude_next_line�l�rur#r$�iter_relevant_lines�s��z;Completion._complete_in_string.<locals>.iter_relevant_linesT)�keependsr�NcSsg|]
 154  }|dur
 155  dn|�qS)Nrxr#rUr#r#r$rY�sz2Completion._complete_in_string.<locals>.<listcomp>z
 156  `([^`\s]+)r�)rrr��_complete_code_linesr��search�group)r"r�rLr�Zrelevant_code_linesrAr#r�r$r��s
 157  
 158  zCompletion._complete_in_stringcCsN|jj�d�|��}t|j|j||d�}t|j|��||jdd�|j	d��
 159  �S)Nr�)Zin_module_contextrHrfruc_rir)r#)�args�kwargsr#r#r$r�sz1Completion._complete_code_lines.<locals>.<lambda>)rurarvr<)rmr��parse�joinrrnrB�
 160  as_contextr�rtrD)r"rurfZmodule_valuer#r#r$r��s"���zCompletion._complete_code_linesN)F)rT)T)r&r'r(rwrDr�r�r�r�r�r�r�r�r�r�r�r#r#r#r$rBs 
 161  �0
 162  
 163  
 164  !rBcCs.g}|D]}|jjdkrg}q||j7}q|S)NZ
 165  small_stmt)ZdfaZ	from_ruler�)rJr�r�r#r#r$r��sr�z^\w*(\'{3}|"{3}|\'|")cs�fdd�}�|jkr
 166  dS|jdkr||�Sg}|dur�|jdkrsd|jvs+d|jvrst|j�dkr6||�Sd}|jsV|��}|dusT|jd	ksTtd
 167  d�|j��D��sVd}d��fd
 168  d�|D��|pd||durjdn|jt	|��fS|j
 169  �dkr}	dS|�d|�|��}|dusdS)Ncs�i}|j�dkr�d|j|d<tj|jfi|��}|s dS|�d�}|j�dkr9�d|j|��kr9dSt|��|��d�||fS)Nrr��endpos�NNN)�line�column�
 170  _string_startrAr�r��endr)rbr�rA�start�rar#r$�return_part_of_leafs
 171  $z<_extract_string_while_in_string.<locals>.return_part_of_leafr�rLr��"�'r�r/css�|]}|dvVqdS)ZrubfNr#rUr#r#r$r�r�z2_extract_string_while_in_string.<locals>.<genexpr>r�c3s�|]}t|��VqdSr)r)rQr�r�r#r$r� ��r)r�rGr�rC�prefixr��allr@r�rr��insert)rbrar��leavesZprefix_leafr#r�r$r�s<
 172  
 173  �����r�cCs�g}|D]"}|j|jd�D]}||��7}q
 174  |��s&t|t�r&|t||�7}qt|�}|D]}||vrC|j|jd�D]}||��7}q:q-|S)N)Zorigin_scope)r�r]r�Zis_stubrdr�_complete_getattrr)r�r�rIr�r�Z
 175  python_valuesrVr#r#r$r�0s��r�cCs4|�d�p	|�d�}t�dd�|D��}|D]�}|j}|dus$|jdkr%q|��D]m}|jdkr1q)|jd}|jd	kr<q)|jd
 176  }|jd}	t|j�dksW|jdksW|jd
 177  krXq)|	jd}
 178  |
 179  jdksit|
 180  j�dkrjq)|�	�}|
 181  jd
 182  }|
 183  jd}
 184  |�
 185  |
 186  |
 187  j�}tdd�|D��s�q)|�
 188  |�}t||�SqgS)aV
 189      A heuristic to make completion for proxy objects work. This is not
 190      intended to work in all cases. It works exactly in this case:
 191  
 192          def __getattr__(self, name):
 193              ...
 194              return getattr(any_object, name)
 195  
 196      It is important that the return contains getattr directly, otherwise it
 197      won't work anymore. It's really just a stupid heuristic. It will not
 198      work if you write e.g. `return (getatr(o, name))`, because of the
 199      additional parentheses. It will also not work if you move the getattr
 200      to some other place that is not the return statement itself.
 201  
 202      It is intentional that it doesn't work in all cases. Generally it's
 203      really hard to do even this case (as you can see below). Most people
 204      will write it like this anyway and the other ones, well they are just
 205      out of luck I guess :) ~dave.
 206      �__getattr__�__getattribute__css�|]}|��VqdSr))�infer)rQr/r#r#r$r�Ws
 207  ��
 208  �z$_complete_getattr.<locals>.<genexpr>Nr��return_stmtr��	atom_exprr�r/�getattrr��css�|]}|jdkVqdS)�paramN)r�r�r#r#r$r�vr�)Zget_function_slot_namesrZ	from_setsr]rGZiter_return_stmtsr�rCr�r�Zgotor�r�Z
 209  infer_noder�)r��instancerXZ	functions�funcr]rr	Zatomr�r�rjZobject_node�	name_node�	name_list�objectsr#r#r$rAsD
 210  �
 211  �
 212  
 213  
 214  
 215  
 216  
 217  
 218  
 219  
 220  
 221  �rFc		cs.�|dd�D]1}	g}
 222  |D]}|	|jkr*|jdur!|jdvr!|r!q
 223  |
 224  t||���7}
 225  q
 226  t�d|
 227  |dd��|
 228  }q|d��}|D]S}|j��}
 229  |rRtj	|
 230  ||d�sX|s�|
 231  |kr�t
 232  |t�rgdd�|��D�}n|g}|rpt|�}|D]!}|r�t
 233  j||dt|�|d�}nt
 234  �||�}|r�||jkr�|VqrqAdS)	Nr�)�module�	namespacezdot lookup on search %s from %s�
 235  r;cSr�r#rP)rQr�r#r#r$rY�rTz$search_in_module.<locals>.<listcomp>)rJZlike_name_lengthr=)r rFr�r�rr	r�r@rrArdrrrrBrC�NamerG)rHr`rXZwanted_namesZwanted_typerDr<Zignore_imports�convert�sZ	new_namesr��	last_namerLZn2Zdef_r#r#r$�search_in_module�sR�
 236  ���
 237  �
 238  ���rcCs�g}|jdvrBt|j�D]5\}}|jdkr5|dkr.|j|djdkr.|j|djdkr.q|�|j�q|jdkrA|�t|��q|S)N)ryrz�import_as_namer/rr��keywordr�r)rGr*r�r�r�r�r�)rgrK�index�childr#r#r$r��s
 239  
 240  
 241  �r�)FFFF)Br��textwrapr�inspectrZparso.python.tokenrZparso.pythonrZ
 242  parso.treerrZparsorZjedir	r
 243  Zjedi.apirrr
 244  Zjedi.api.stringsrZjedi.api.file_namerZjedi.inferencerZjedi.inference.base_valuerZjedi.inference.helpersrrZjedi.inference.contextrZjedi.inference.valuerZjedi.inference.docstring_utilsrZjedi.inference.namesrrZ!jedi.inference.gradual.conversionrrZjedi.parser_utilsrZjedi.pluginsrrr6r:rOr[rcrhZdecoraterlrBr��compiler�r�r�rrr�r#r#r#r$�<module>sZ
 245  v
 246  
 247  0A
 248  �*