B
    Zeo[ü1  ã               @   sP   d Z ddlmZ ddlmZmZ d
dd„ZG dd„ deƒZG dd	„ d	ej	ƒZ
dS )z'A navigable completer for the qtconsoleé    N)ÚQtCoreÚQtGuic                sä   | sdS g }dd„ ‰ dd„ }‡ fdd„| D ƒ}|rV|\}}d| | |  d || |< t |d	d
„ |D ƒƒ}d}	d}
|rœdd d| d t| d ƒ ¡ d }	|rÆdd d| d t| d ƒ ¡ d }
d|	 d |¡ |
 d }|S )z# returnr a string for an html tableÚ c             S   s   d|  d S )Nz<td>z  </td>© )Útextr   r   ú8lib/python3.7/site-packages/qtconsole/completion_html.pyÚ<lambda>   s    zhtml_tableify.<locals>.<lambda>c             S   s   d|  d S )Nz<tr>z</tr>r   )r   r   r   r   r      s    c                s   g | ]}t tˆ |ƒƒ‘qS r   )ÚlistÚmap)Ú.0Úrow)Útdsr   r   ú
<listcomp>   s    z!html_tableify.<locals>.<listcomp>z<td class="inverted">z  </td>c             s   s   | ]}d   |¡V  qdS )r   N)Újoin)r   r   r   r   r   ú	<genexpr>   s    z html_tableify.<locals>.<genexpr>z<tr>z<td>z</td>r   z</tr>z?<table class="completion" style="white-space:pre"cellspacing=0>z</table>)r
   r   Úlen)Zitem_matrixÚselectÚheaderÚfooterZ	html_colsZtrsZ	tds_itemsr   ÚcolÚheadÚfootÚhtmlr   )r   r   Úhtml_tableify   s$    &&r   c               @   sp   e Zd ZdZdZdZdZddd„Zedd„ ƒZ	e	j
d	d„ ƒZ	ed
d„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdS )ÚSlidingIntervalz¶a bound interval that follows a cursor
    
    internally used to scoll the completion view when the cursor 
    try to go beyond the edges, and show '...' when rows are hidden
    r   é   é   c             C   s4   || _ || _d| _|| _| j| j d | _|| _dS )z÷Create a new bounded interval
        
        any value return by this will be bound between maximum and 
        minimum. usual width will be 'width', and sticky_length 
        set when the return  interval should expand to max and min
        r   r   N)Ú_minÚ_maxÚ_startÚ_widthÚ_stopÚ_sticky_lenght)ÚselfZmaximumÚwidthZminimumZsticky_lenghtr   r   r   Ú__init__7   s    zSlidingInterval.__init__c             C   s   | j S )zcurrent cursor position)Ú_current)r#   r   r   r   ÚcurrentE   s    zSlidingInterval.currentc             C   s”   t t| j|ƒ| jƒ}|| _|| jkr8|| _|| j | _n|| jk rT|| _|| j | _t| j| j ƒ| j	krr| j| _t| j| j ƒ| j	kr| j| _dS )zset current cursor positionN)
ÚminÚmaxr   r   r&   r!   r    r   Úabsr"   )r#   Úvaluer'   r   r   r   r'   J   s    

c             C   s   | j S )zbegiiing of interval to show)r   )r#   r   r   r   Ústart^   s    zSlidingInterval.startc             C   s   | j S )zend of interval to show)r!   )r#   r   r   r   Ústopc   s    zSlidingInterval.stopc             C   s   | j | j S )N)r!   r   )r#   r   r   r   r$   h   s    zSlidingInterval.widthc             C   s   | j | j S )N)r'   r,   )r#   r   r   r   Únthl   s    zSlidingInterval.nthN)r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r&   r%   Úpropertyr'   Úsetterr,   r-   r$   r.   r   r   r   r   r   -   s   
r   c                   sª   e Zd ZdZdZdZdZdZdZdZ	dZ
dZ‡ fdd	„Z‡ fd
d„Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd#dd„Zdd „ Zd!d"„ Z‡  ZS )$ÚCompletionHtmlz7 A widget for tab completion,  navigable by arrow keys r   )r   r   r   )r   r   Né   c                s\   t |jtjtjfƒst‚tt| ƒ ¡  |j| _	|| _
| j	 | ¡ d| _d| _|  | j	¡ dS )zg Create a completion widget that is attached to the specified Qt
            text edit widget.
        N)Ú
isinstanceZ_controlr   Z	QTextEditZQPlainTextEditÚAssertionErrorÚsuperr5   r%   Ú
_text_editÚ_console_widgetZinstallEventFilterÚ_sliding_intervalÚ_justified_itemsZsetFocusProxy)r#   Zconsole_widget)Ú	__class__r   r   r%   €   s    zCompletionHtml.__init__c                sª  || j kr˜| ¡ }|tjjkr‚| ¡ }| jdkrF|tjjfkrFdS | jdkrv|tjjfkrv| jd | _|  	¡  dS | jdkrx|tjj
tjjfkr¢|  ¡  dS |tjjfkrÄ|  ¡  |  	¡  dS |tjjfkræ|  ¡  |  	¡  dS |tjjfkr
|  ¡  |  	¡  dS |tjjfkr.|  ¡  |  	¡  dS |tjjfkrR|  ¡  |  	¡  dS |tjjfkrn|  ¡  dS |  ¡  n|  ¡  n|tjjkr˜|  ¡  tt| ƒ ||¡S )zm Reimplemented to handle keyboard input and to auto-hide when the
            text edit loses focus.
        r   Fr   Té   )r:   Útyper   ZQEventZKeyPressÚkeyÚ_consecutive_tabZQtZKey_TabÚ_update_listZ
Key_ReturnZ	Key_EnterÚ_complete_currentÚselect_rightZKey_DownÚselect_downZ	Key_RightZKey_UpÚ	select_upZKey_LeftÚselect_leftZ
Key_EscapeÚcancel_completionZFocusOutr9   r5   ÚeventFilter)r#   ÚobjZeventÚetyperA   )r>   r   r   rJ   ‘   sT    

zCompletionHtml.eventFilterc             C   s,   d| _ d| _| j ¡  d| _| jr(d| _dS )zÜCancel the completion

        should be called when the completer have to be dismissed

        This reset internal variable, clearing the temporary buffer
        of the console where the completion are shown.
        r   )r   r   N)rB   Ú_slice_startr;   Ú_clear_temporary_bufferÚ_indexr<   )r#   r   r   r   rI   È   s    
z CompletionHtml.cancel_completionc             C   s*  | j \}}|d }|d }||kr*||ks:||krH||krH|  dd¡ nÞ|dkrX|dk sh|dk rv|dkrv|  ||¡ n°||kr|  d|d ¡ n–|dk rª|  ||d ¡ n|||krÄ|  |d d¡ nb|dk rÞ|  |d |¡ nHd|kr||krd|kr||kr||f| _ntd||||f ƒ‚dS )aÒ  Change the selection index, and make sure it stays in the right range

        A little more complicated than just dooing modulo the number of row columns
        to be sure to cycle through all element.

        horizontaly, the element are maped like this :
        to r <-- a b c d e f --> to g
        to f <-- g h i j k l --> to m
        to l <-- m n o p q r --> to a

        and vertically
        a d g j m p
        b e h k n q
        c f i l o r
        r   r   zayou'r trying to go where no completion                           have gone before : %d:%d (%d:%d)N)Ú_sizeÚ_select_indexrO   ÚNotImplementedError)r#   r   r   ZnrZncr   r   r   rQ   â   s&    
  (zCompletionHtml._select_indexc             C   s"   | j | j }|t| jƒkrd S |S )N)rM   Ú
_slice_lenr   Ú_items)r#   Úendr   r   r   Ú
_slice_end  s    zCompletionHtml._slice_endc             C   s   | j \}}|  |d |¡ dS )zmove cursor upr   N)rO   rQ   )r#   ÚrÚcr   r   r   rG     s    
zCompletionHtml.select_upc             C   s   | j \}}|  |d |¡ dS )zmove cursor downr   N)rO   rQ   )r#   rW   rX   r   r   r   rF     s    
zCompletionHtml.select_downc             C   s   | j \}}|  ||d ¡ dS )zmove cursor leftr   N)rO   rQ   )r#   rW   rX   r   r   r   rH   !  s    
zCompletionHtml.select_leftc             C   s   | j \}}|  ||d ¡ dS )zmove cursor rightr   N)rO   rQ   )r#   rW   rX   r   r   r   rE   &  s    
zCompletionHtml.select_rightc                sÆ   |sdS |  ¡ | _d| _| j ¡  ¡ }t | jj	¡ 
d¡}ttd|| d ƒƒ}tj|d|d\}‰ tt|ƒd ƒ| _|| _ˆ d ˆ d f| _|| _d| _‡ fd	d
„}tt||ƒƒ| _| jdd dS )ze Shows the completion widget with 'items' at the position specified
            by 'cursor'.
        Nr   ú é
   )ÚemptyÚdisplaywidthZrows_numbersZcolumns_numbers)r   r   c                s   dd„ t | ˆ d ƒD ƒS )Nc             S   s   g | ]\}}|  |d ¡‘qS )rY   )Úljust)r   ÚyÚwr   r   r   r   ?  s    z?CompletionHtml.show_items.<locals>.<lambda>.<locals>.<listcomp>Zcolumns_width)Úzip)Úx)Úcir   r   r   ?  s    z+CompletionHtml.show_items.<locals>.<lambda>F)Úhilight)ÚpositionÚ_start_positionrB   r:   ZdocumentZ	textWidthr   ZQFontMetricsr;   Zfontr$   Úintr)   r   Zcompute_item_matrixr   r   r<   rT   rP   Ú_old_cursorrO   r	   r
   r=   rC   )r#   ÚcursorÚitemsr$   Z
char_widthr\   Úitems_mZsjoinr   )rb   r   Ú
show_items+  s"    
zCompletionHtml.show_itemsTc             C   s¢   | j d | j_d}d}| jjdkr&d}| jj| jjk r:d}| j| jj| jjd … }| j ¡  |rv| jj	| j d f}nd}t
||||d}| jj| j|dd dS )zM update the list of completion and hilight the currently selected completion r   Nz...r   )r   r   r   T)r   )rO   r<   r'   r,   r-   r   r=   r;   rN   r.   r   Z_fill_temporary_bufferrg   )r#   rc   r   r   rj   ZselZstrngr   r   r   rC   F  s     
zCompletionHtml._update_listc             C   sB   | j }| j|d  |d  }| ¡ }|r6|  ¡  |¡ |  ¡  dS )zB Perform the completion with the currently selected item.
        r   r   N)rO   rT   ÚstripÚ_current_text_cursorZ
insertTextrI   )r#   ÚiÚitemr   r   r   rD   b  s    z CompletionHtml._complete_currentc             C   s.   | j  ¡ }| ¡ | jkr*| | jtjj¡ |S )zn Returns a cursor with text between the start position and the
            current position selected.
        )r:   Z
textCursorrd   re   ZsetPositionr   ZQTextCursorZ
KeepAnchor)r#   rh   r   r   r   rm   l  s
    

z#CompletionHtml._current_text_cursor)T)r/   r0   r1   r2   rT   rO   rB   rP   rg   re   rM   rS   r%   rJ   rI   rQ   r3   rV   rG   rF   rH   rE   rk   rC   rD   rm   Ú__classcell__r   r   )r>   r   r5   p   s,   7.

r5   )NNN)r2   Zipython_genutils.textr   Zqtconsole.qtr   r   r   Úobjectr   ZQWidgetr5   r   r   r   r   Ú<module>   s
   
C