B
    kb                 @   s   d Z ddlZddlmZ ddlZddl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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 S t|dkrd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...u   …z..u   ‥.    u   {}.{}…{}.{}r      z{}u   {}…{})replacelensplitosseppopformat)r   r   Zobject_partsZ
file_parts r   7lib/python3.7/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      Nu   …)r   
startswith)r   r   r   Zcut_how_muchr   r   r   _elide_typed:   s    r"   )r   r   r   c             C   s   t t| |d||dS )N)r   )r"   r   )r   r   r   r   r   r   _elideH   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_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 kr|d krt d|| _|| _d S )Nz/Please pass shell=an InteractiveShell instance.)	TypeError_ipy_completershell)selfipy_completerr-   r   r   r   __init__X   s    zIPythonPTCompleter.__init__c             C   s   | j r| j S | jjS d S )N)r,   r-   r   )r.   r   r   r   r/   ^   s    z IPythonPTCompleter.ipy_completerc             c   s   |j  sd S t  t  |j}|j}|j}|j}t|||}y| 	|||| j
E d H  W n` tk
r } zBy t \}	}
}t|	|
| W n tk
r   td Y nX W d d }~X Y nX W d Q R X W d Q R X d S )Nz"Unrecoverable Error in completions)Zcurrent_linestripr
   r   r&   Zcursor_position_rowZcursor_position_colcursor_positionr   _get_completionsr/   	Exceptionsysexc_info	tracebackprint_exceptionAttributeErrorprint)r.   documentZcomplete_eventr'   Z
cursor_rowZ
cursor_colr2   r(   eexc_type	exc_valueZexc_tbr   r   r   get_completionse   s     
z"IPythonPTCompleter.get_completionsc          	   c   s8  t |dd}t| || |}x|D ]}|js4q&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rt||j| t|
d | |j|j |j
|j d	V  q&t||j| t|
| |j|j |j
d	V  q&W d
S )zT
        Private equivalent of get_completions() use only for unit_testing.
        debugFZNFCr   r   )start_positionZfunctionz())rA   ZdisplayZdisplay_metaN)getattrr   completionsr&   unicodedata	normalizer   startr   r)   typer#   endZ	signature)r'   r(   r2   Zipycr@   rC   cr&   Zchar_beforeZ
fixed_textZdisplay_textZadjusted_textr   r   r   r3   {   s(    8z#IPythonPTCompleter._get_completions)NN)
__name__
__module____qualname____doc__r0   propertyr/   r?   staticmethodr3   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)ZHTMLZhtmlZ
javascriptZjsZperlZrubyZlatex)pygments_lexersr	   ZPython3Lexerpython_lexerZ	BashLexershell_lexerZ	HtmlLexerZJavascriptLexerZ	PerlLexerZ	RubyLexerZTexLexermagic_lexers)r.   lr   r   r   r0      s    zIPythonPTLexer.__init__c             C   sl   |j  }| j}|ds$|dr,| j}n6|drbx*| j D ]\}}|d| rB|}P qBW ||S )N!z%%bashz%%)r&   lstriprR   r!   rS   rT   itemslex_document)r.   r;   r&   ZlexermagicrU   r   r   r   rY      s    

zIPythonPTLexer.lex_documentN)rJ   rK   rL   rM   r0   rY   r   r   r   r   rP      s   rP   )r   )rM   rD   r   r5   r7   ZIPython.core.completerr   r   r   Zprompt_toolkit.completionr   r   Zprompt_toolkit.lexersr   r	   Zprompt_toolkit.patch_stdoutr
   Zpygments.lexersZlexersrQ   r   objectZ_completion_sentinelstrr   intr"   r#   r)   r*   rP   r   r   r   r   <module>   s$   N