o
    û›h9  ã                	   @   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rGd	 |d
 |d dd… |d dd… |d ¡S t|ƒdkrod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   úNc:\Users\shubh\Videos\agent\nova\Lib\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   Zcut_how_muchr   r   r   Ú_elide_typedA   s   r$   c                 C   s   t t| |d�||d�S )Nr   )r$   r    )r   r!   r   r   r   r   Ú_elideO   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_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 )N)r.   r/   r   )r0   r   r   r   r1   e   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 sƒw   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,   €
ÿ€ü€Pøz"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())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   ‚   s0   €ÿÿ
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}n| d¡r0| j ¡ D ]\}}| d| ¡r/|} n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