a
    ø£Be9  ã                   @   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rŽd	 |d
 |d dd… |d dd… |d ¡S t|ƒdkrÞd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   é   Néþÿÿÿz{}u   {}â€¦{})ÚreplaceÚlenÚsplitÚosÚsepÚpopÚformat)r   r   Zobject_partsZ
file_parts© r   ú[/home/yuhw/wc/wire-cell-python/wcpy/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 sÔ0    Y  W d   ƒ n1 sò0    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     
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r¨t|	|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   ‚   s,    ÿ
ÿ
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}n4| d¡r`| j ¡ D ]\}}| d| ¡r@|} q`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