U
    ‡ª/e1  ã                   @   sº  d Z ddlmZ er2ddlZdZddlmZ eZndZddlmZ ddl	m
Z
 ddlZejd	krzzddlZW n   Y nX g ZejD ]Ze e¡ q„i Zej ¡ D ]\ZZeee< q¢ddlZdd
lmZ ddlmZmZ dZdZdZdZ ee B Z!dd„ Z"ddlm#Z# e# $¡ Z%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2da3d d!„ Z4d"d#„ Z5G d$d%„ d%ƒZ6G d&d'„ d'e7ƒZ8G d(d)„ d)ƒZ9e:d*kr¶e;ej<d ƒZ=e9e=ƒZ>e"ed+ eƒ e> ?¡  dS ),z[
Entry-point module to start the code-completion server for PyDev.

@author Fabio Zadrozny
é    )Ú	IS_JYTHONNZjycompletionserver)Ú_pydev_jy_imports_tipperZpycompletionserver)Ú_pydev_imports_tipper©ÚsocketÚdarwin)ÚStringIO)Ú
quote_plusÚunquote_plusé   é   é   é   c                 C   s"   |t @ dkrtj d| f ¡ d S )Nr   z%s
)ÚDEBUGÚsysÚstdoutÚwrite)ÚsZprior© r   úO/tmp/pip-unpacked-wheel-dg3irmqq/debugpy/_vendored/pydevd/pycompletionserver.pyÚdbg6   s    r   )Úpydev_localhostz@@KILL_SERVER_END@@z@@COMPLETIONSzEND@@z@@INVALID_REQUESTz@@JYTHON_INVALID_REQUESTz@@CHANGE_DIR:z@@MSG_OK_END@@z
@@IMPORTS:z@@PYTHONPATH_END@@z@@CHANGE_PYTHONPATH:z@@MSG_JEDI:z@@SEARCHi   c                 C   sB   t dk	r,ttjƒdkr,tjd t kr,tjd= | a tj d| ¡ dS )z
    This is necessary so that we get the imports from the same directory where the file
    we are completing is located.
    Nr   )ÚcurrDirModuleÚlenr   ÚpathÚinsert)Ú	directoryr   r   r   Úcomplete_from_dirS   s
    r   c                 C   s>   |   d¡}g t_|D ]$}| ¡ }t|ƒdkrtj |¡ qdS )zyChanges the pythonpath (clears all the previous pythonpath)

    @param pythonpath: string with paths separated by |
    ú|r   N)Úsplitr   r   Ústripr   Úappend)Ú
pythonpathr   r   r   r   r   Úchange_python_patha   s    
r#   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú	Processorc                 C   s   d S )Nr   )Úselfr   r   r   Ú__init__q   s    zProcessor.__init__c                 C   sV   zt |ƒ}W n tk
r    Y nX |rRz
t|ƒW S    tj d|f ¡ ‚ Y nX dS )Nzerror making quote plus in %s
ú )ÚstrÚUnicodeDecodeErrorr	   r   r   r   ©r%   Úmsgr   r   r   Úremove_invalid_charsu   s    
zProcessor.remove_invalid_charsc                 C   sØ   g }|  d| ¡ |D ]¬}|  d¡ |  d¡ |  t|  |d ¡ƒ¡ |  d¡ |  |  |d ¡¡ t|ƒdkrŽ|  d¡ |  |  |d ¡¡ t|ƒdkr¸|  d¡ |  |  |d ¡¡ |  d¡ qd	td
 |¡tf S )z¬
        Format the completions suggestions in the following format:
        @@COMPLETIONS(modFile(token,description),(token,description),(token,description))END@@
        z%sú,ú(r   r   r   é   ú)z%s(%s)%sÚ )r!   r(   r,   r   ÚMSG_COMPLETIONSÚjoinÚMSG_END)r%   ÚdefFileÚcompletionsListZcompMsgÚtupr   r   r   Úformat_completion_messageƒ   s     




z#Processor.format_completion_messageN)Ú__name__Ú
__module__Ú__qualname__r&   r,   r8   r   r   r   r   r$   o   s   r$   c                   @   s   e Zd ZdS )ÚExitN)r9   r:   r;   r   r   r   r   r<   Ÿ   s   r<   c                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚCompletionServerc                 C   s$   d| _ || _d | _d| _tƒ | _d S )NFT)ÚendedÚportr   Úexit_process_on_killr$   Ú	processor)r%   r?   r   r   r   r&   ¥   s
    zCompletionServer.__init__c                 C   s`   ddl m} | |j|j¡ | _}z| t| jf¡ W n$   tj 	dt| jf ¡ ‚ Y nX d S )Nr   r   z>Error on connect_to_server with parameters: host: %s port: %s
)
Ú"_pydev_bundle._pydev_saved_modulesr   ÚAF_INETÚSOCK_STREAMÚconnectÚHOSTr?   r   Ústderrr   )r%   r   r   r   r   r   Úconnect_to_server¬   s    z"CompletionServer.connect_to_serverc                 C   s   | j  ||¡S )z/
        get message with completions.
        )rA   r8   )r%   r5   r6   r   r   r   Úget_completions_message¶   s    z(CompletionServer.get_completions_messagec                 C   s6   d}|D ]}|dkr|| }q q$q||  |d ¡fS )z=
        When we receive this, we have 'token):data'
        r1   r0   z):)Úlstrip)r%   ÚdataÚtokenÚcr   r   r   Úget_token_and_data¼   s    
z#CompletionServer.get_token_and_datac                 C   s>   d}d}||k r:| j  ||d … ¡}|dkr0d S || }qd S )Ni P  r   )r   Úsend)r%   r+   ZMSGLENZ	totalsentÚsentr   r   r   Úemulated_sendallÉ   s    z!CompletionServer.emulated_sendallc                 C   s   | j  t|dƒ¡ d S )Núutf-8)r   ÚsendallÚ	bytearrayr*   r   r   r   rO   Ó   s    zCompletionServer.sendc                 C   sö  znddl m} | ¡ }ttdt| jf  tƒ |  ¡  ttd tƒ | j	sXd}| 
t¡dkrŽ| j t¡}t|ƒdkr~tƒ ‚|| d¡ }qRzºzÆ| 
t¡dkr¾ttd tƒ d	| _	tƒ ‚ttd
 tƒ | 
t¡dkrg }tD ]}| |df¡ qä|  |  d |¡¡ nJ|d | t¡… }| t¡rj|ttƒd … }t|ƒ}t ||¡\}}|  |  ||¡¡ nî| t ¡r¤|tt ƒd … }t|ƒ}t!|ƒ |  t"¡ n´| t#¡rÂ|tt#ƒd … }t|ƒ}| $dd¡\}}	}
}}zdd l%}W n    |  |  d dg¡¡ Y n¶X |j&|t'|ƒd t'|	ƒ|
|d}g }| (¡ D ]t}|j)}|dkrRd}nD|dkrbd}n4|dkrrd}n$|dkr‚q8n|dkr’d}nd}| |j*dd|f¡ q8|  |  d|¡¡ n–| t+¡r|tt+ƒd … }t|ƒ}t ,|¡\\}}}}|  |  ||||fg¡¡ nB| t-¡rN|tt-ƒd … }t|ƒ}t.|ƒ |  t"¡ n
|  t/¡ W nì tk
r¶   t0 1¡ d }|  d dg¡}z|  |¡ W n tj2k
r¬   Y nX |‚Y n’   ttd t3ƒ t4ƒ }t5j6|d | 7¡ }ttd t8|ƒ t3ƒ |  d d d!|| 9¡ f dfg¡}z|  |¡ W n tj2k
r@   Y nX Y nX W 5 | ¡  X qF| j :¡  d	| _	tƒ ‚W n€ tk
r˜   | j;r”t0 <d¡ Y nZ   t4ƒ }t0 1¡ }t5j=|d |d |d" d |d# | 7¡ }ttd t8|ƒ t3ƒ ‚ Y nX d S )$Nr   )Ú
_pydev_logz% connecting to java server on %s (%s)z Connected to java serverr1   éÿÿÿÿrR   z kill message receivedTz starting keep alive threadr'   r   r   )zError on import jedizError importing jedir1   r   )ÚsourceÚlineÚcolumnZsource_encodingr   ÚclassÚ1ÚfunctionÚ2ÚimportÚ0ÚkeywordZ	statementÚ3z-1Úempty)zExit:Ú
SystemExitr1   z exception occurred)Úfilez received error: zERROR:z	%s
Log:%sr   )Úlimitrd   )>Ú_pydev_bundlerU   ÚLogr   ÚSERVER_NAMErF   r?   ÚINFO1rH   r>   Úfindr4   r   ÚrecvÚBUFFER_SIZEr   r<   ÚdecodeZ	clear_logÚMSG_KILL_SERVERÚINFO2ÚMSG_PYTHONPATHÚ	_sys_pathr!   rO   rI   ÚrfindÚ
startswithÚMSG_IMPORTSr
   r   Zgenerate_tipÚMSG_CHANGE_PYTHONPATHr#   ÚMSG_OKÚMSG_JEDIr   ÚjediZScriptÚintZcompletionsÚtypeÚnameÚ
MSG_SEARCHZsearch_definitionÚMSG_CHANGE_DIRr   ÚMSG_INVALID_REQUESTr   Úexc_infoÚerrorÚERRORr   Ú	tracebackÚ	print_excÚgetvaluer(   Zget_contentsÚcloser@   ÚexitÚprint_exception)r%   rU   ÚlogrK   ZreceivedÚcompsÚpr5   rX   rY   Úencodingr   rW   rx   ÚscriptÚlstÚ
completionÚtÚfÚcolZfoundAsÚer+   r   Úerrr   r   r   r   ÚrunÖ   sÞ    
ú




 

 zCompletionServer.runN)
r9   r:   r;   r&   rH   rI   rN   rQ   rO   r”   r   r   r   r   r=   £   s   

r=   Ú__main__z will start)@Ú__doc__Z_pydevd_bundle.pydevd_constantsr   Ú	java.langÚjavarh   rf   r   r   rB   r   r   ÚplatformZ_CFrq   r   rŠ   r!   Z_sys_modulesÚmodulesÚitemsr{   Úmodr‚   Úior   Úurllib.parser	   r
   ri   ro   ÚWARNr   r   r   r   Zget_localhostrF   rn   r2   r4   r~   ZMSG_JYTHON_INVALID_REQUESTr}   rv   rt   rp   ru   rw   r|   rl   r   r   r#   r$   Ú	Exceptionr<   r=   r9   ry   Úargvr?   r   r”   r   r   r   r   Ú<module>   sr   


0 L
