Ë
    ½lÔhí'  ã                   ó”   — d dl Z d dlZd dlZd dlmZmZmZ d dlmZ ddl	m
Z
mZmZmZ  e j                  e«      ZdZdZ G d„ d	«      Zy)
é    N)ÚAnyÚDictÚMapping)Úfuturesé   )ÚJsonRpcExceptionÚJsonRpcRequestCancelledÚJsonRpcInternalErrorÚJsonRpcMethodNotFoundz2.0z$/cancelRequestc            	       óž   — e Zd Zd„ dfd„Zd„ Zdd„Zdd„Zd„ Zd	„ Zd
„ Z	e
d„ «       Zd„ Ze
deeef   dedeeef   fd„«       Zd„ Zd„ Zdd„Zy)ÚEndpointc                  ó<   — t        t        j                  «       «      S ©N)ÚstrÚuuidÚuuid4© ó    úk/home/josh/.local/share/pipx/venvs/python-lsp-server/lib/python3.12/site-packages/pylsp_jsonrpc/endpoint.pyú<lambda>zEndpoint.<lambda>   s   € Ä#ÄdÇjÁjÃlÓBS€ r   é   c                 ó€   — || _         || _        || _        i | _        i | _        t        j                  |¬«      | _        y)a§  A JSON RPC endpoint for managing messages sent to/from the client.

        Args:
            dispatcher (dict): A dictionary of method name to handler function.
                The handler functions should return either the result or a callable that will be used to asynchronously
                compute the result.
            consumer (fn): A function that consumes JSON RPC message dicts and sends them to the client.
            id_generator (fn, optional): A function used to generate request IDs.
                Defaults to the string value of :func:`uuid.uuid4`.
            max_workers (int, optional): The number of workers in the asynchronous executor pool.
        )Úmax_workersN)Ú_dispatcherÚ	_consumerÚ_id_generatorÚ_client_request_futuresÚ_server_request_futuresr   ÚThreadPoolExecutorÚ_executor_service)ÚselfÚ
dispatcherÚconsumerÚid_generatorr   s        r   Ú__init__zEndpoint.__init__   s>   € ð &ˆÔØ!ˆŒØ)ˆÔà')ˆÔ$Ø')ˆÔ$Ü!(×!;Ñ!;ÈÔ!TˆÕr   c                 ó8   — | j                   j                  «        y r   )r    Úshutdown)r!   s    r   r'   zEndpoint.shutdown(   s   € Ø×Ñ×'Ñ'Õ)r   Nc                 ót   — t         j                  d||«       t        |dœ}|�||d<   | j                  |«       y)zÇSend a JSON RPC notification to the client.

         Args:
             method (str): The method name of the notification to send
             params (any): The payload of the notification
         zSending notification: %s %s)ÚjsonrpcÚmethodNÚparams)ÚlogÚdebugÚJSONRPC_VERSIONr   )r!   r*   r+   Úmessages       r   ÚnotifyzEndpoint.notify+   sB   € ô 	�	‰	Ð/°¸Ô@ô 'Øñ
ˆð ÐØ &ˆG�HÑà�‰�wÕr   c                 ó   — | j                  «       }t        j                  d|||«       t        ||dœ}|�||d<   t	        j
                  «       }|j                  | j                  |«      «       || j                  |<   | j                  |«       |S )a  Send a JSON RPC request to the client.

        Args:
            method (str): The method name of the message to send
            params (any): The payload of the message

        Returns:
            Future that will resolve once a response has been received
        z!Sending request with id %s: %s %s)r)   Úidr*   r+   )
r   r,   r-   r.   r   ÚFutureÚadd_done_callbackÚ_cancel_callbackr   r   )r!   r*   r+   Úmsg_idr/   Úrequest_futures         r   ÚrequestzEndpoint.request=   s�   € ð ×#Ñ#Ó%ˆÜ�	‰	Ð5°v¸vÀvÔNô 'ØØñ
ˆð
 ÐØ &ˆG�HÑä Ÿ™Ó)ˆØ×(Ñ(¨×)>Ñ)>¸vÓ)FÔGà/=ˆ×$Ñ$ VÑ,Ø�‰�wÔàÐr   c                 ó   ‡ ‡— ˆˆ fd„}|S )z;Construct a cancellation callback for the given request ID.c                 óŠ   •— | j                  «       r2‰j                  t        d‰i«       | j                  t	        «       «       y y )Nr2   )Ú	cancelledr0   ÚCANCEL_METHODÚset_exceptionr	   )ÚfutureÚ
request_idr!   s    €€r   Úcallbackz+Endpoint._cancel_callback.<locals>.callback\   s9   ø€ Ø×ÑÔ!Ø—‘œM¨D°*Ð+=Ô>Ø×$Ñ$Ô%<Ó%>Õ?ð "r   r   ©r!   r?   r@   s   `` r   r5   zEndpoint._cancel_callbackZ   s   ù€ õ	@ð ˆr   c           	      ó‚  — d|vs|d   t         k7  rt        j                  d|«       yd|vr;t        j                  d|«       | j	                  |d   |j                  d«      «       yd|vrKt        j                  d|«       | j                  |d   |j                  d	«      |j                  d
«      «       y	 t        j                  d|«       | j                  |d   |d   |j                  d«      «       y# t        $ rM}t        j                  d|d   «       | j                  t         |d   |j                  «       dœ«       Y d}~yd}~wt        $ rk t        j                  d|d   «       | j                  t         |d   t        j                  t        j                   «       «      j                  «       dœ«       Y yw xY w)z‡Consume a JSON RPC message from the client.

        Args:
            message (dict): The JSON RPC message sent by the client
        r)   zUnknown message type %sNr2   z$Handling notification from client %sr*   r+   z Handling response from client %sÚresultÚerrorzHandling request from client %súFailed to handle request %s)r)   r2   rD   )r.   r,   Úwarningr-   Ú_handle_notificationÚgetÚ_handle_responseÚ_handle_requestr   Ú	exceptionr   Úto_dictÚ	Exceptionr
   ÚofÚsysÚexc_info)r!   r/   Úes      r   ÚconsumezEndpoint.consumeb   sy  € ð ˜GÑ# w¨yÑ'9¼_Ò'LÜ�K‰KÐ1°7Ô;Øà�wÑÜ�I‰IÐ<¸gÔFØ×%Ñ% g¨hÑ&7¸¿¹ÀXÓ9NÕOØ˜WÑ$Ü�I‰IÐ8¸'ÔBØ×!Ñ! '¨$¡-°·±¸XÓ1FÈÏÉÐT[ÓH\Õ]ðÜ—	‘	Ð;¸WÔEØ×$Ñ$ W¨T¡]°G¸HÑ4EÀwÇ{Á{ÐS[ÓG\Õ]øÜ#ò Ü—‘Ð;¸WÀT¹]ÔKØ—‘Ü.Ø! $™-ØŸY™Y›[ñ ÷ ñ ûô
 ò Ü—‘Ð;¸WÀT¹]ÔKØ—‘Ü.Ø! $™-Ü1×4Ñ4´S·\±\³^ÓD×LÑLÓNñ ö ðús    Â7>C6 Ã6	F>Ã?AEÅA4F>Æ=F>c                 óÄ  — |t         k(  r| j                  |d   «       y	 | j                  |   }	  ||«      }t        |«      rSt        j                  d|«       | j                  j                  |«      }|j                  | j                  ||«      «       yy# t        $ r t        j                  d|«       Y yw xY w# t        $ r t        j                  d||«       Y yw xY w)z&Handle a notification from the client.r2   Nz+Ignoring notification for unknown method %sz$Failed to handle notification %s: %sz'Executing async notification handler %s)r<   Ú_handle_cancel_notificationr   ÚKeyErrorr,   rF   rM   rK   Úcallabler-   r    Úsubmitr4   Ú_notification_callback)r!   r*   r+   ÚhandlerÚhandler_resultÚnotification_futures         r   rG   zEndpoint._handle_notification…   sÚ   € à”]Ò"Ø×,Ñ,¨V°D©\Ô:Øð	Ø×&Ñ& vÑ.ˆGð
	Ù$ V›_ˆNô
 �NÔ#Ü�I‰IÐ?ÀÔPØ"&×"8Ñ"8×"?Ñ"?ÀÓ"OÐØ×1Ñ1°$×2MÑ2MÈfÐV\Ó2]Õ^ð $øô ò 	Ü�K‰KÐEÀvÔNÙð	ûô ò 	Ü�M‰MÐ@À&È&ÔQÙð	ús"    B °B< ÂB9Â8B9Â< CÃCc                 ó   ‡ ‡— ˆ ˆfd„}|S )z;Construct a notification callback for the given request ID.c                 ó¢   •— 	 | j                  «        t        j                  d‰‰«       y # t        $ r t        j	                  d‰‰«       Y y w xY w)Nz-Successfully handled async notification %s %sz)Failed to handle async notification %s %s)rC   r,   r-   rM   rK   )r>   r*   r+   s    €€r   r@   z1Endpoint._notification_callback.<locals>.callbackŸ   sG   ø€ ð[Ø—‘”Ü—	‘	ÐIÈ6ÐSYÕZøÜò [Ü—‘ÐIÈ6ÐSYÖZð[ús   ƒ'+ « AÁAr   )r*   r+   r@   s   `` r   rX   zEndpoint._notification_callbackœ   s   ù€ õ	[ð ˆr   c                 ó¼   — | j                   j                  |d«      }|st        j                  d|«       y|j	                  «       rt        j                  d|«       yy)z-Handle a cancel notification from the client.Nz6Received cancel notification for unknown message id %szCancelled request with id %s)r   Úpopr,   rF   Úcancelr-   )r!   r6   r7   s      r   rT   z$Endpoint._handle_cancel_notification§   sR   € à×5Ñ5×9Ñ9¸&À$ÓGˆáÜ�K‰KÐPÐRXÔYØð × Ñ Ô"Ü�I‰IÐ4°fÕ=ð #r   ÚheaderrC   Úreturnc                 ó|   — t        | «      }t        |t         «      rd|v sd|v r|j                  |«       |S ||d<   |S )NrC   rD   )ÚdictÚ
isinstanceÚupdate)ra   rC   Úresponses      r   Ú_make_response_payloadzEndpoint._make_response_payload³   sI   € ô ˜“<ˆÜ�fœdÔ#¨°VÑ);¸wÈ&Ñ?PØ�O‰O˜FÔ#ð ˆð "(ˆH�XÑØˆr   c                 óš  — 	 | j                   |   } ||«      }t	        |«      rat
        j                  d|«       | j                  j                  |«      }|| j                  |<   |j                  | j                  |«      «       yt        |t        j                  «      rFt
        j                  d|«       || j                  |<   |j                  | j                  |«      «       yt
        j                  d|«       | j                  t         |dœ|«      }| j#                  |«       y# t        $ r}t        j                  |«      |‚d}~ww xY w)z!Handle a request from the client.Nz"Executing async request handler %sz&Request handler is already a future %sz/Got result from synchronous request handler: %s©r)   r2   )r   rU   r   rN   rV   r,   r-   r    rW   r   r4   Ú_request_callbackre   r   r3   rh   r.   r   )	r!   r6   r*   r+   rY   rQ   rZ   r7   rg   s	            r   rJ   zEndpoint._handle_request¾   s   € ð	:Ø×&Ñ& vÑ.ˆGñ ! ›ˆä�NÔ#Ü�I‰IÐ:¸NÔKØ!×3Ñ3×:Ñ:¸>ÓJˆNØ3AˆD×(Ñ(¨Ñ0Ø×,Ñ,¨T×-CÑ-CÀFÓ-KÕLÜ˜¬¯©Ô7Ü�I‰IÐ>ÀÔOØ3AˆD×(Ñ(¨Ñ0Ø×,Ñ,¨T×-CÑ-CÀFÓ-KÕLä�I‰IÐGÈÔXØ×2Ñ2ä.Ø ñð óˆHð �N‰N˜8Õ$øô/ ò 	:Ü'×*Ñ*¨6Ó2¸Ð9ûð	:ús   ‚D& Ä&	E
Ä/EÅE
c                 ó   ‡ ‡— ˆˆ fd„}|S )z6Construct a request callback for the given request ID.c                 ó8  •— ‰j                   j                  ‰d «       | j                  «       r| j                  t	        «       «       t
        ‰dœ}	 | j                  «       }‰j                  ||«      }‰j#                  |«       y # t        $ r3}t        j                  d‰«       |j                  «       |d<   Y d }~ŒId }~wt        $ rQ t        j                  d‰«       t        j                  t        j                   «       «      j                  «       |d<   Y Œ¥w xY w)Nrj   rE   rD   )r   r_   r;   r=   r	   r.   rC   rh   r   r,   rK   rL   rM   r
   rN   rO   rP   r   )r>   r/   rC   rQ   r?   r!   s       €€r   r@   z,Endpoint._request_callback.<locals>.callbackÝ   så   ø€ à×(Ñ(×,Ñ,¨Z¸Ô>à×ÑÔ!Ø×$Ñ$Ô%<Ó%>Ô?ô +Ø ñˆGð
UØŸ™›�Ø×5Ñ5°g¸vÓF�ð �N‰N˜7Õ#øô $ò /Ü—‘Ð;¸ZÔHØ#$§9¡9£;�˜Ö ûÜò UÜ—‘Ð;¸ZÔHÜ#7×#:Ñ#:¼3¿<¹<»>Ó#J×#RÑ#RÓ#T�˜Ó ðUús   Á"B Â	DÂ)B<Â<ADÄDr   rA   s   `` r   rk   zEndpoint._request_callbackÛ   s   ù€ õ	$ð0 ˆr   c                 ó:  — | j                   j                  |d«      }|st        j                  d|«       y|�<t        j	                  d||«       |j                  t        j                  |«      «       yt        j	                  d||«       |j                  |«       y)z"Handle a response from the client.Nz*Received response to unknown message id %sz)Received error response to message %s: %sz"Received result for message %s: %s)	r   r_   r,   rF   r-   r=   r   Ú	from_dictÚ
set_result)r!   r6   rC   rD   r7   s        r   rI   zEndpoint._handle_response÷   s‚   € à×5Ñ5×9Ñ9¸&À$ÓGˆáÜ�K‰KÐDÀfÔMØàÐÜ�I‰IÐAÀ6È5ÔQØ×(Ñ(Ô)9×)CÑ)CÀEÓ)JÔKØä�	‰	Ð6¸ÀÔGØ×!Ñ! &Õ)r   r   )NN)Ú__name__Ú
__module__Ú__qualname__r%   r'   r0   r8   r5   rR   rG   ÚstaticmethodrX   rT   r   r   r   r   rh   rJ   rk   rI   r   r   r   r   r      s‘   „ á:SÐabó Uò(*ó ó$ò:ò!òF_ð. ñó ðò
>ð ð t¨C°¨H¡~ð ¸sð ÀwÈsÐTWÈxÑGXò ó ðò%ò:ô8*r   r   )Úloggingr   rO   Útypingr   r   r   Ú
concurrentr   Ú
exceptionsr   r	   r
   r   Ú	getLoggerrq   r,   r.   r<   r   r   r   r   ú<module>rz      sO   ðó Û Û 
ß %Ñ %å ÷Fó Fð €g×Ñ˜Ó!€Ø€Ø!€÷s*ò s*r   