a
    lc9                     @   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
eedddZd	d
eeeedddZdeeed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rR|  t|dkrd	|d
 |d dd |d dd |d S t|dkrd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   T/var/www/html/django/DPS/env/lib/python3.9/site-packages/IPython/terminal/ptutils.py_elide_point   s(    
(r    )r   typedr   r   c                C   sd   t | |k r| S t |d }|dk r(| S | |r`t | t |kr`| 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$   )r   r!   r   c                 C   s   t t| |d||dS )Nr   )r$   r    )r   r!   r   r   r   r   _elideO   s    
r%   c                 C   s6   |  dr.t||kr.|| dkr.| d d S | S d 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 d S )N)r.   r/   r   )r0   r   r   r   r1   e   s    z IPythonPTCompleter.ipy_completerc                 c   s   |j  sd S t  t  |j}|j}|j}|j}t|||}z| 	|||| j
E d H  W n` ty } zHz t \}	}
}t|	|
| W n ty   td Y n0 W Y d }~n
d }~0 0 W d    n1 s0    Y  W d    n1 s0    Y  d S )Nz"Unrecoverable Error in completions)current_linestripr
   r   r(   cursor_position_rowcursor_position_colcursor_positionr   _get_completionsr1   	Exceptionsysexc_info	tracebackprint_exceptionAttributeErrorprint)r0   documentcomplete_eventr)   Z
cursor_rowZ
cursor_colr7   r*   eexc_type	exc_valueexc_tbr   r   r   get_completionsl   s     
z"IPythonPTCompleter.get_completionsc              	   c   s0  t |dd}t| || |}|D ]}|js0q"td|j}t|d dkr||j dkr| |jd  }td|| }	t|dd dkrt|	|j| d dV  q"|j}
t	|j| |}|j
dkrt||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())rI   displaydisplay_metaN)getattrr   completionsr(   unicodedata	normalizer   startr   r+   typer%   end	signature)r)   r*   r7   ZipycrG   rN   cr(   Zchar_beforeZ
fixed_textdisplay_textZadjusted_textr   r   r   r8      s,    

8z#IPythonPTCompleter._get_completions)NN)
__name__
__module____qualname____doc__r2   propertyr1   rF   staticmethodr8   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}n4|dr`| j D ]\}}|d| r@|} q`q@||S )N!z%%bashz%%)r(   lstriprg   r#   ri   ro   itemslex_document)r0   r@   r(   lexermagicrp   r   r   r   rt      s    

zIPythonPTLexer.lex_documentN)rW   rX   rY   rZ   r2   rt   r   r   r   r   r]      s   r]   )r   )rZ   rO   r   IPython.core.completerr   r   r   Zprompt_toolkit.completionr   r   prompt_toolkit.lexersr   r	   prompt_toolkit.patch_stdoutr
   pygments.lexerslexersre   r   r:   r<   objectZ_completion_sentinelstrr    intr$   r%   r+   r,   r]   r   r   r   r   <module>   s$   	%N