o
    ^i                     @   s   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Zdd ZG dd dZg dZG dd deZdefddZdS )    )ThreadPoolExecutor)Path)rmtree)List)run_on_executor)convert_yielded   )lsp_message_listener)file_uri_to_pathis_relative)LanguageServerManagerAPI   c              
   C   s4   |D ]}z| | } W q t tfy   Y  d S w | S N)KeyError	TypeError)objpathcrumb r   _/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/jupyter_lsp/virtual_documents_shadow.pyextract_or_none   s   r   c                   @   s   e Zd ZeedZdd Zdd Zdd Ze	dd	 Z
e	d
d ZedededefddZedefddZdefddZedd ZdS )EditableFile)max_workersc                 C   s"   t |trt|| _d S || _d S r   )
isinstancestrr   r   )selfr   r   r   r   __init__   s   "zEditableFile.__init__c                    s   t |  I d H | _d S r   )r   
read_lineslinesr   r   r   r   read"   s   zEditableFile.readc                    s   t |  I d H S r   )r   write_linesr   r   r   r   write%   s   zEditableFile.writec                 C   s4   dg}z| j jdd }W |S  ty   Y |S w )N utf-8encoding)r   	read_text
splitlinesFileNotFoundError)r   r   r   r   r   r   (   s   zEditableFile.read_linesc                 C   s.   | j jjddd | j jd| jdd d S )NTparentsexist_ok
r$   r%   )r   parentmkdir
write_textjoinr   r   r   r   r   r!   3   s   zEditableFile.write_linesr   	charactersidec                 C   s4   d}| r| | |d  }| | |krd}|| |< |S )NFTr   )r   r2   r3   
needs_gluetrimmedr   r   r   trim8   s   zEditableFile.trimgluec                 C   s.   |sg S | r
| d nd|r|d  gS d gS )Nr#   r   r   )leftrightr7   r   r   r   r1   B   s   &zEditableFile.jointextc           	      C   s   | j d |d  }| j |d d  }| j||d dd}| j||d dd}|d}|d |r1dnd  | ||| ||r@dnd |rEdnd   | ||| ||rUdnd d   p]dg| _ d S )	Nliner2   r   )r   r2   r3   r8   r-   r   r#   )r   r6   splitr1   )	r   r;   startendbeforeafterneeds_glue_leftneeds_glue_rightinnerr   r   r   apply_changeH   s"   
zEditableFile.apply_changec                 C   s8   ddd}t | j| jrt | jd ndd}||dS )Nr   )r<   r2   r8   )r>   r?   )lenr   )r   r>   r?   r   r   r   
full_rangeY   s
   

zEditableFile.full_rangeN)__name__
__module____qualname__r   MAX_WORKERSexecutorr   r    r"   r   r   r!   staticmethodlistintr6   boolr1   r   rE   propertyrG   r   r   r   r   r      s     



	r   )ztextDocument/didOpentextDocument/didChangeztextDocument/didSavec                   @   s   e Zd ZdZdS )ShadowFilesystemErrorz Error in the shadow file system.N)rH   rI   rJ   __doc__r   r   r   r   rS   f   s    rS   virtual_documents_uric                    sH    dstd dg  tttd fdd}|S )Nzfile:/z6Virtual documents URI has to start with "file:/", got Fclientc              
      s  |j | }|ddsdS |dtvrdS t|ddg}|du r)td|t|dg}|s8td	||s?dS st d
krIdS zjddd t	t
 jddd W n( tttfy } z | t d
kr||jd  W Y d}~dS d}~ww dt|}t|std| dt|}	|	 I dH  t|dg}
|
durd|
ig}n|d dkrdS d|d vrtd|d d }t|dkr|jd |dd D ]}|d|	j}|	j|d fi | q|	 I dH  |S )zIntercept a message with document contents creating a shadow file for it.

        Only create the shadow file if the URI matches the virtual documents URI.
        Returns the path on filesystem where the content was stored.
        requires_documents_on_diskTNmethodparamstextDocumentz#Could not get textDocument from: {}urizCould not get URI from: {}   r*   a\  [lsp] initialization of shadow filesystem failed three times check if the path set by `LanguageServerManager.virtual_documents_dir` or `JP_LSP_VIRTUAL_DIR` is correct; if this is happening with a server for which you control (or wish to override) jupyter-lsp specification you can try switching `requires_documents_on_disk` off. The errors were: %szPath z* is not relative to shadow filesystem rootr;   rR   contentChangesz0textDocument/didChange is missing contentChangesr   zBLSP warning: up to one change supported for textDocument/didChangerange)language_serversget	WRITE_ONEr   rS   format
startswithrF   r/   r   r   OSErrorPermissionErrorr)   appendlogwarnr
   r   r   r    rG   rE   r"   )scopemessagelanguage_servermanagerserver_specdocumentr[   er   editable_filer;   changeschangechange_rangefailuresinitializedshadow_filesystemrU   r   r   shadow_virtual_documentsv   sx   





z9setup_shadow_filesystem.<locals>.shadow_virtual_documents)rc   rS   r   r
   r	   )rU   rx   r   rt   r   setup_shadow_filesystemj   s   
\ry   N)concurrent.futuresr   pathlibr   shutilr   typingr   tornado.concurrentr   tornado.genr   rl   r	   pathsr
   r   typesr   rK   r   r   ra   
ValueErrorrS   r   ry   r   r   r   r   <module>   s   	H