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 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)LanguageServerManagerAPI   c              
   C   s4   |D ]}z| | } W q t tfy   Y  d S w | S N)KeyError	TypeError)objpathcrumb r   _/var/www/edux/Edux_v2/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   r1   r2   
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rightr6   r   r   r   r0   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liner1   r   )r   r1   r2   r7   r,   r   r"   )r   r5   splitr0   )	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;   r1   r7   )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intr5   boolr0   r   rD   propertyrF   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)rG   rH   rI   __doc__r   r   r   r   rR   f   s    rR   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|}	|	 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)   ab  [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 which you control (or wish to override) jupyter-lsp specification you can try switching `requires_documents_on_disk` off. The errors were: %sr:   rQ   contentChangesz0textDocument/didChange is missing contentChangesr   zBLSP warning: up to one change supported for textDocument/didChangerange)language_serversget	WRITE_ONEr   rR   format
startswithrE   r.   r   r   OSErrorPermissionErrorr(   appendlogwarnr
   r   r   rF   rD   r!   )scopemessagelanguage_servermanagerserver_specdocumentrZ   er   editable_filer:   changeschangechange_rangefailuresinitializedshadow_filesystemrT   r   r   shadow_virtual_documentsw   sp   



z9setup_shadow_filesystem.<locals>.shadow_virtual_documents)rb   rR   r   r
   r	   )rT   rw   r   rs   r   setup_shadow_filesystemj   s   
Wrx   N)concurrent.futuresr   pathlibr   shutilr   typingr   tornado.concurrentr   tornado.genr   rk   r	   pathsr
   typesr   rJ   r   r   r`   
ValueErrorrR   r   rx   r   r   r   r   <module>   s   	H