
    ue3                        U d dl Z d dlZd dlmZmZmZmZmZmZm	Z	 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 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  e j@                  e!      Z"dZ#de#z  Z$dZ%dZ& G d d      Z'edee(ee(ee(ef   f   f   fd       Z)dejT                  dejV                  de,fdZ-dedejT                  dejV                  de,fdZ.dejV                  dejT                  de,fdZ/dejV                  dejT                  de,fdZ0dedejV                  fdZ1	 d<dee   de(d e(d!ed"ed#e(deee(ef   ddf   fd$Z2d%edee(   fd&Z3ed'ed(ed"ed)e	ee(   df   fd*       Z4d+e(de(fd,Z5d-e6d.e(d/e(de6fd0Z7d1e6de(fd2Z8de(fd3Z9ed'ed(ed"ed4ed5edee   fd6       Z:ed'ed(efd7       Z;ed'ed(efd8       Z<ed'ed(ed"efd9       Z=ed'ed(efd:       Z> e'       Z?e'e@d;<   y)=    N)AnyDict	GeneratorListOptionalSetUnion)Script)tree)
NodeOrLeaf)Resource)SearchResult)
AutoImport)hookimpl)Config)Document	Workspace   )PylspTaskHandle   
   i  c            
       p    e Zd ZdZd Z	 	 ddededeee	      dee
   fdZ	 dded	ed
eee      fdZd Zy)AutoimportCachezHandles the cache creation.c                     d | _         y N)threadselfs    ?/usr/lib/python3/dist-packages/pylsp/plugins/rope_autoimport.py__init__zAutoimportCache.__init__    s	        Nconfig	workspacefilessingle_threadc                    | j                         ry |j                  d      j                  dd      }|j                         j                  di       }|j	                  ||      }|d n|D cg c]  }|j                  |       c}}	|r| j                  |||	       y t        j                  | j                  |||	f      | _	        | j                  j                          y c c}w )Nrope_autoimportmemoryFrope)targetargs)
is_blockedplugin_settingsgetsettings_rope_autoimport_rope_resource_reload_cache	threadingThreadr   start)
r   r"   r#   r$   r%   r(   rope_config
autoimportdocument	resourcess
             r   reload_cachezAutoimportCache.reload_cache#   s     ??--.?@DDXuUoo'++FB7//VD
 } GLM8())+6M 	 y*i@ $**))J	0RDK KK Ns   -C"r7   r9   c                 d    t        |      }|j                  ||       |j                  |       y )N)task_handler9   )r<   )r   generate_cachegenerate_modules_cache)r   r#   r7   r9   r<   s        r   r2   zAutoimportCache._reload_cache@   s2     &i0!!kY!O))k)Br!   c                 R    | j                   xr | j                   j                         S r   )r   is_aliver   s    r   r,   zAutoimportCache.is_blockedJ   s    {{5t{{3355r!   )NFr   )__name__
__module____qualname____doc__r    r   r   r   r   r   boolr:   r   r   r2   r,    r!   r   r   r      s    % +/(-     X'	 
  ~ B /3	CC C DN+	C6r!   r   returnc                       ddddddiddidiiS )Npluginsr'   FenabledT)rJ   r(   completionscode_actionsrF   rF   r!   r   pylsp_settingsrM   N   s:     	 t  t!	 
 r!   expr	word_nodec                    |syt        | j                        dk(  ry| j                  d   }t        |t        j                        rd|j
                  v ry||k(  ryt        | j                        dkD  rt        d | j                  D              ryt        |t        j                  t        j                  f      rt        ||      S t        || |      S )z
    Check if we should insert the word_node on the given expr.

    Works for both correct and incorrect code. This is because the
    user is often working on the code as they write it.
    Fr   T#r   c              3   |   K   | ]4  }|j                   d k(  xr d|j                  v xs |j                   dk(   6 yw)operator.trailerN)typevalue).0nodes     r   	<genexpr>z!_should_insert.<locals>.<genexpr>s   s@      
 II#9tzz(9STYY)=SS
s   :<)lenchildren
isinstancer   	EndMarkerprefixanyPythonErrorNode
PythonNode_should_insert_handle_first_child)rN   rO   first_childs      r   rc   rc   a   s     
4==Q--"K+t~~.+$$$i
4==A 

 
 + 4 4dooFG k955{D)<<r!   re   c                 L   t        | t        j                        ryt        | t        j                  t        j                  f      r| j
                  dv ryt        | t        j                        r6| j
                  dk(  rt        ||      S | j
                  dk(  rt        ||      S y)z<Check if we suggest imports given the following first child.F)importfromdefclassT)	r]   r   Import
PythonLeafPythonErrorLeafrW   Keyword_should_import_function_should_import_class)re   rN   rO   s      r   rd   rd      s     +t{{++1E1EFG 22+t||,%*9d;;''	488r!   c                     d }|j                   D ]N  }t        |t        j                        r0t        |t        j                        r|| k(  r|j
                  dk(  r y|}P y)N(TF)r\   r]   r   NameOperatorrW   rO   rN   	prev_noderY   s       r   rp   rp      sV    I dDII&)T]]39$C)?	 r!   c                     d }|j                   D ]B  }t        ||       r yt        |t        j                        r|j
                  dk(  r|| k(  r y|}D y)NTz->F)r\   _handle_argumentr]   r   rt   rW   ru   s       r   ro   ro      sV    I D),i/$&9$	 r!   rY   c                     t        | t        j                        rP| j                  dk(  r| j                  d   |k(  ry| j                  dk(  r| j                  D ]  }t        ||      s y y)Ntfpdef   T
parametersF)r]   r   rb   rV   r\   rx   )rY   rO   	parameters      r   rx   rx      sa    $(99 }}Q9,99$!]]  	#Iy9  r!   suggestionsdoc_uriwordr7   r8   featurec              #     K   | D ]  }|j                  |j                        dz
  }|dd}||d}	|	|j                  dz   d}
t        |j                  |j                  |j                  |      }|t
        kD  rs|dk(  r?|j                  |j                  t        |      d|it        |j                        |
gd	 |d
k(  r$|j                  dd||
giidt        |      id t        d|        y w)Nr   r   )line	character)r5   end
)rangenewTextrK   r   )labelkindsortTextdatadetailadditionalTextEditsrL   quickfixchangesr   )titler   editr   zUnknown feature: )
find_insertion_linesourceimport_statement
_get_scorename
_score_maxitemkind_sort_import	_document
ValueError)r~   r   r   r7   r8   r   
suggestioninsert_liner5   
edit_ranger   scores               r   _process_statementsr      s!     " <
 44X__EI$15$U3
#
0K0Kd0RSz::JOOT
 :m##"++(/"G,#J$?$?@(,v  &#44""Wtf$56 $\%%89  0	:;;=<s   C3C5scriptc                     | j                  d      }t        j                  |       |D ch c]  }|j                   c}S c c}w )z.Get all names to ignore from the current file.T)definitions)	get_nameslogdebugr   )r   	raw_namesr   s      r   r   r      s9      T 2IIIi"+,$DII,,,s   Ar"   r#   ignored_namesc           
         | j                  d      j                  di       j                  dd      rt        j                         rg S |j                  |d      }t        j                  |      }|j                  d|d   f      }t        ||      sg S |j                  }t        j                  d|        | j                  |j                  	      j                  d
i       }	|xs t        |j                  d            }|j!                  |	      }
t#        |
j%                  ||            }t'        t)        ||j*                  ||
|d      d       }t-        |      t.        kD  r	|dt.         }|S )zGet autoimport suggestions.r'   rK   rJ   Tr   r   r    autoimport: searching for word: document_pathr)   )use_document_path)r   c                     | d   S )Nr   rF   	statements    r   <lambda>z#pylsp_completions.<locals>.<lambda>  s    i
3 r!   keyN)r-   r.   cacher,   linesparsoparseget_leaf_for_positionrc   rW   r   r   r/   pathr   jedi_scriptr0   listsearch_fullsortedr   urir[   MAX_RESULTS_COMPLETIONS)r"   r#   r8   positionr   r   rN   rO   r   r6   r7   r~   resultss                r   pylsp_completionsr      sc    ""#45	]B		Y	 
				>>(6*+D;;tD**Ax/D+EFI$	*	??DII078///>BB62NK+ yt40M ++K8Jz--d--PQKtZ=	
 4	G 7|--223Nr!   r   c                     d| z   S )Nz# Auto-Import
rF   )r   s    r   r   r     s     #333r!   r   full_statementsuggested_namec                     t        d      }t        |      |z
  }t        |      t        |      z
  dz  }d| z  }||z   |z   S )Nrg   r{      )r[   )r   r   r   desired_nameimport_lengthfull_statement_scoresuggested_name_scoresource_scores           r   r   r     sQ     MM~.>/#l2CCI;L"66EEr!   r   c                     t        t        | t        dz
        d      } dt        |       j	                  t
        d      z   S )Nr   r   z[z0)maxminr   strrjust
_score_pow)r   s    r   r   r     s8    EJ!+,a0E #e*"":s333r!   c                     |d   d   }t        j                  | j                  |d            j                  d|d   dz   f      j                  S )Nr   r5   r   r   r   )r   r   r   r   rW   )r8   
diagnosticr5   s      r   get_name_or_moduler   $  sN    w(EHNN5=12		5#5#9:	;	r!   r   contextc                    | j                  d      j                  di       j                  dd      rt        j                         rg S t        j                  d| d| d|        g }|j                  dg       D ]	  }d|j                  d	d
      j                         vr't        ||      }t        j                  d|        | j                  |j                        j                  di       }|j                  |      }	t        |	j                  |            }
t        j                  d|
       t        t        |
|j                  ||	|d      d       }t!        |      t"        kD  r	|dt"         }|j%                  |        |S )a  
    Provide code actions through rope.

    Parameters
    ----------
    config : pylsp.config.config.Config
        Current config.
    workspace : pylsp.workspace.Workspace
        Current workspace.
    document : pylsp.workspace.Document
        Document to apply code actions on.
    range : Dict
        Range argument given by pylsp. Not used here.
    context : Dict
        CodeActionContext given as dict.

    Returns
    -------
      List of dicts containing the code actions.
    r'   rL   rJ   TztextDocument/codeAction:  diagnosticszundefined namemessage r   r   r)   zautoimport: suggestions: %sc                     | d   d   S )Nr   r   rF   r   s    r   r   z$pylsp_code_actions.<locals>.<lambda>e  s    )F"3J"? r!   r   N)r-   r.   r   r,   r   r   lowerr   r/   r   r0   r   r   r   r   r   r[   MAX_RESULTS_CODE_ACTIONSextend)r"   r#   r8   r   r   rL   r   r   r6   r7   r~   r   s               r   pylsp_code_actionsr   -  sw   : ""#45	^R	 	Y	 
				II)(1UG1WIFGLkk-4 %
:>>)R#@#F#F#HH!(J7		4TF;<ooHMMoBFFvrR//<
:11$78		/= @

 w<22778GG$1%4 r!   c                 0    t         j                  | |       yzPInitialize AutoImport.

    Generates the cache for local and global items.
    Nr   r:   r"   r#   s     r   pylsp_initializer   o       
vy)r!   c                 0    t         j                  | |       yr   r   r   s     r   pylsp_document_did_openr   x  r   r!   c                 4    t         j                  | ||g       y)z/Update the names associated with this document.Nr   )r"   r#   r8   s      r   pylsp_document_did_saver     s     
vy8*5r!   c                     | j                  d      j                  dd      rt        j                  | |       yt        j                  d       y)z
    Initialize autoimport if it has been enabled through a
    workspace/didChangeConfiguration message from the frontend.

    Generates the cache for local and global items.
    r'   rJ   Fz"autoimport: Skipping cache reload.N)r-   r.   r   r:   r   r   r   s     r   %pylsp_workspace_configuration_changedr     s=     /044YF69-		67r!   r   )rK   )Aloggingr3   typingr   r   r   r   r   r   r	   r   jedir
   parso.pythonr   
parso.treer   rope.base.resourcesr   rope.contrib.autoimport.defsr   rope.contrib.autoimport.sqliter   pylspr   pylsp.config.configr   pylsp.workspacer   r   _rope_task_handler   	getLoggerrA   r   r   r   r   r   r   r   rM   BaseNodeLeafrE   rc   rd   rp   ro   rx   r   r   r   r   intr   r   r   r   r   r   r   r   r   __annotations__rF   r!   r   <module>r      s]     C C C    ! ( 5 5  & / .g!
^
  .6 .6b 
S$sDcN':";;<  
$= =499 = =<#'===AYY	$	DII 	T]] 	t 	
tyy 
 
$ 
	: 	$)) 	$ !&<l#&<&< &< 	&<
 &< &< tCH~tT)*&<R-f -S - 
$$$ $
 S4($ 
$N4 4 4FF!$F69FF4 4 4  
>>> > 	>
 > 
$Z> 
>B 
*V *	 * 
* 
*F *y * 
* 
6F 6y 6H 6 
6
 

8& 
8Y 
8 

8 )* *r!   