B
    \                 @   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e Zd	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_elidec            C   s   |  dd} |  dd} t| |k r(| S | d}| tj}t|dkrrd|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format)stringr   Zobject_partsZ
file_parts r   7lib/python3.7/site-packages/IPython/terminal/ptutils.py_elide   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_context7   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__@   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'   F   s    z IPythonPTCompleter.ipy_completerc          
   c   sr   |j  sd S t T t B |j}|j}|j}|j}t|||}| 	|||| j
E d H  W d Q R X W d Q R X d S )N)Zcurrent_linestripr
   r   r   Zcursor_position_rowZcursor_position_colcursor_positionr   _get_completionsr'   )r&   documentZcomplete_eventr   Z
cursor_rowZ
cursor_colr*   r    r   r   r   get_completionsM   s    
z"IPythonPTCompleter.get_completionsc             c   s  t |dd}t| || |}x|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 d	V  q$t||j| t|
|j
d	V  q$W d
S )zT
        Private equivalent of get_completions() use only for unit_testing.
        debugFZNFCr   r   )start_positionZfunctionz())r/   ZdisplayZdisplay_metaN)getattrr   completionsr   unicodedata	normalizer   startr   r!   typer   Z	signature)r   r    r*   Zipycr.   r1   cr   Zchar_beforeZ
fixed_textZdisplay_textZadjusted_textr   r   r   r+   \   s(    

*z#IPythonPTCompleter._get_completions)NN)
__name__
__module____qualname____doc__r(   propertyr'   r-   staticmethodr+   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)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   r(      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   lstripr?   
startswithr@   rA   itemslex_document)r&   r,   r   ZlexermagicrB   r   r   r   rG      s    

zIPythonPTLexer.lex_documentN)r7   r8   r9   r:   r(   rG   r   r   r   r   r=      s   r=   )r:   r2   r   ZIPython.core.completerr   r   r   Zprompt_toolkit.completionr   r   Zprompt_toolkit.lexersr   r	   Zprompt_toolkit.patch_stdoutr
   Zpygments.lexersZlexersr>   r   objectZ_completion_sentinelr   r!   r"   r=   r   r   r   r   <module>   s   G