o
    Zh9                  	   @   s  d Z ddlZddlmZ ddlmZmZmZ ddlmZ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ZddlZddlZe Zd	d
dedefddZd	d
dedededefddZddededefddZdd ZG dd deZG dd deZdS )zfprompt-toolkit utilities

Everything in this module is a private API,
not to be used outside IPython.
    N)wcwidth)provisionalcompletercursor_to_position_deduplicate_completions)	Completer
Completion)Lexer)PygmentsLexer)patch_stdout   	min_elidestringreturnc                C   s   |  dd} |  dd} t| |k r| S | d}| tj}|d dkr)|  t|dkrGd	|d
 |d dd |d dd |d S t|dkrodtj d tj d |d
 |d dd |d dd |d S | S )a  
    If a string is long enough, and has at least 3 dots,
    replace the middle part with ellipses.

    If a string naming a file is long enough, and has at least 3 slashes,
    replace the middle part with ellipses.

    If three consecutive dots, or two consecutive dots are encountered these are
    replaced by the equivalents HORIZONTAL ELLIPSIS or TWO DOT LEADER unicode
    equivalents
    z...   …z..u   ‥.    u   {}.{}…{}.{}r      Nz{}u   {}…{})replacelensplitosseppopformat)r   r   Zobject_partsZ
file_parts r   O/var/www/html/lang_env/lib/python3.10/site-packages/IPython/terminal/ptutils.py_elide_point   s(   
(r    typedr   c                C   sd   t | |k r| S t |d }|dk r| S | |r0t | t |kr0| dd  d| |d  S | S )zT
    Elide the middle of a long string if the beginning has already been typed.
    r      Nr   )r   
startswith)r   r!   r   Zcut_how_muchr   r   r   _elide_typedA   s   r$   c                 C   s   t t| |d||dS )Nr   )r$   r    )r   r!   r   r   r   r   _elideO   s   
r%   c                 C   s2   |  drt||kr|| dkr| d d S | S )N=r   )endswithr   )textbodyoffsetr   r   r   (_adjust_completion_text_based_on_contextV   s   "r+   c                   @   s:   e Zd ZdZdddZedd Zdd Zed	d
 Z	dS )IPythonPTCompleterz8Adaptor to provide IPython completions to prompt_toolkitNc                 C   s(   |d u r|d u rt d|| _|| _d S )Nz/Please pass shell=an InteractiveShell instance.)	TypeError_ipy_completershell)selfipy_completerr/   r   r   r   __init___   s   
zIPythonPTCompleter.__init__c                 C   s   | j r| j S | jjS )N)r.   r/   r   )r0   r   r   r   r1   e   s   z IPythonPTCompleter.ipy_completerc                 c   s   |j  sd S t u t Y |j}|j}|j}|j}t|||}z| 	|||| j
E d H  W n0 ty` } z$zt \}	}
}t|	|
| W n tyU   td Y nw W Y d }~nd }~ww W d    n1 skw   Y  W d    d S W d    d S 1 sw   Y  d S )Nz"Unrecoverable Error in completions)current_linestripr
   r   r(   Zcursor_position_rowZcursor_position_colcursor_positionr   _get_completionsr1   	Exceptionsysexc_info	tracebackprint_exceptionAttributeErrorprint)r0   documentZcomplete_eventr)   Z
cursor_rowZ
cursor_colr5   r*   eexc_type	exc_valueexc_tbr   r   r   get_completionsl   s,   
Pz"IPythonPTCompleter.get_completionsc              	   c   s0   t |dd}t| || |}|D ]}|jsqtd|j}t|d dkrT||j dkrT| |jd  }td|| }	t|dd dkrTt|	|j| d dV  q|j}
t	|j| |}|j
dkrt||j| t|
d | |j|j |j
|j d	V  qt||j| t|
| |j|j |j
d	V  qd
S )zT
        Private equivalent of get_completions() use only for unit_testing.
        debugFNFCr   r   )start_positionfunctionz())rF   displayZdisplay_metaN)getattrr   completionsr(   unicodedata	normalizer   startr   r+   typer%   end	signature)r)   r*   r5   ZipycrD   rJ   cr(   Zchar_beforeZ
fixed_textZdisplay_textZadjusted_textr   r   r   r6      s0   
8.z#IPythonPTCompleter._get_completions)NN)
__name__
__module____qualname____doc__r2   propertyr1   rC   staticmethodr6   r   r   r   r   r,   ]   s    

r,   c                   @   s    e Zd ZdZdd Zdd ZdS )IPythonPTLexerz3
    Wrapper around PythonLexer and BashLexer.
    c                 C   s`   t }t|j| _t|j| _t|jt|jt|jt|jt|jt|j	t|j
d| _d S )N)HTMLhtml
javascriptjsperlrubylatex)pygments_lexersr	   Python3Lexerpython_lexer	BashLexershell_lexer	HtmlLexerJavascriptLexer	PerlLexer	RubyLexerTexLexermagic_lexers)r0   lr   r   r   r2      s   zIPythonPTLexer.__init__c                 C   sj   |j  }| j}|ds|dr| j}n|dr0| j D ]\}}|d| r/|} nq ||S )N!z%%bashz%%)r(   lstriprb   r#   rd   rj   itemslex_document)r0   r>   r(   lexermagicrk   r   r   r   ro      s   


zIPythonPTLexer.lex_documentN)rR   rS   rT   rU   r2   ro   r   r   r   r   rX      s    rX   )r   )rU   rK   r   ZIPython.core.completerr   r   r   Zprompt_toolkit.completionr   r   Zprompt_toolkit.lexersr   r	   Zprompt_toolkit.patch_stdoutr
   Zpygments.lexerslexersr`   r   r8   r:   objectZ_completion_sentinelstrr    intr$   r%   r+   r,   rX   r   r   r   r   <module>   s&    	 %N