o
    ˷e                  	   @   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rCd	|d
 |d d
 |d d |d S t|dkrgdtj d tj 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      z{}u   {}…{})replacelensplitosseppopformat)r   r   object_parts
file_parts r    O/var/www/ideatree/venv/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   cut_how_muchr    r    r!   _elide_typed:   s   r'   c                 C   s   t t| |d||dS )Nr   )r'   r"   )r   r#   r   r    r    r!   _elideH   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_contextO   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_completerr2   r    r    r!   __init__X   s   
zIPythonPTCompleter.__init__c                 C   s   | j r| j S | jjS )N)r1   r2   r   )r3   r    r    r!   r4   ^   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+   cursor_position_rowcursor_position_colcursor_positionr   _get_completionsr4   	Exceptionsysexc_info	tracebackprint_exceptionAttributeErrorprint)r3   documentcomplete_eventr,   
cursor_row
cursor_colr:   r-   eexc_type	exc_valueexc_tbr    r    r!   get_completionse   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())rN   displaydisplay_metaN)getattrr   completionsr+   unicodedata	normalizer   startr   r.   typer(   end	signature)r,   r-   r:   ipycrL   rS   cr+   char_before
fixed_textdisplay_textadjusted_textr    r    r!   r;   {   s0   
8.z#IPythonPTCompleter._get_completions)NN)
__name__
__module____qualname____doc__r5   propertyr4   rK   staticmethodr;   r    r    r    r!   r/   V   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)r3   lr    r    r!   r5      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+   lstriprp   r%   rr   rx   itemslex_document)r3   rC   r+   lexermagicry   r    r    r!   r}      s   


zIPythonPTLexer.lex_documentN)r`   ra   rb   rc   r5   r}   r    r    r    r!   rf      s    rf   )r   )rc   rT   r   IPython.core.completerr   r   r   prompt_toolkit.completionr   r   prompt_toolkit.lexersr   r	   prompt_toolkit.patch_stdoutr
   pygments.lexerslexersrn   r   r=   r?   object_completion_sentinelstrr"   intr'   r(   r.   r/   rf   r    r    r    r!   <module>   s&    	 N