
    [A1em2                     p    d Z ddlmZmZmZ ddlmZ ddZ G d de      Z	 G d	 d
ej                        Zy)z'A navigable completer for the qtconsole    )QtCoreQtGui	QtWidgets   )compute_item_matrixNc           	         | syg }d }d }| D cg c]  }t        t        ||             }}|r|\  }}	d| |   |	   z   dz   ||   |	<   t        |d |D              }d}
d}|r,ddj                  d|z   d	z   t        | d
         z        z   dz   }
|r,ddj                  d|z   d	z   t        | d
         z        z   dz   }d|
z   dj                  |      z   |z   dz   }|S c c}w )z# returnr a string for an html table c                     d| z   dz   S )N<td>  </td> texts    ;/usr/lib/python3/dist-packages/qtconsole/completion_html.py<lambda>zhtml_tableify.<locals>.<lambda>   s    tI-     c                     d| z   dz   S )N<tr></tr>r   r   s    r   r   zhtml_tableify.<locals>.<lambda>   s    tG+ r   z<td class="inverted">r   c              3   >   K   | ]  }d j                  |        yw)r	   N)join).0rows     r   	<genexpr>z html_tableify.<locals>.<genexpr>   s     <3"''#,<s   r   r   z</td>r   r   z?<table class="completion" style="white-space:pre"cellspacing=0>z</table>)listmapr   len)item_matrixselectheaderfooter	html_colstdstrsr   	tds_itemscolheadfoothtmls                r   html_tableifyr*      s7   I
-C
+C0;<c#sm$<I<S5S!#&'	#s C<)<=IDDWWfVmG+SQ-@@AB WWfVmG+SQ-@@ABGGI&(*./1;<D K- =s   Cc                       e Zd ZdZdZdZdZddZed        Z	e	j                  d        Z	ed        Zed        Zed	        Zed
        Zy)SlidingIntervalza 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   r   c                     || _         || _        d| _        || _        | j                  | j                  z   dz   | _        || _        y)zCreate 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)selfmaximumwidthminimumsticky_lenghts        r   __init__zSlidingInterval.__init__7   sA     		[[,Q.
+r   c                     | j                   S )zcurrent cursor position)_currentr4   s    r   currentzSlidingInterval.currentE   s     }}r   c                    t        t        | j                  |      | j                        }|| _        || j
                  kD  r|| _        || j                  z
  | _        n*|| j                  k  r|| _        || j                  z   | _        t        | j                  | j                  z
        | j                  k  r| j                  | _        t        | j
                  | j                  z
        | j                  k  r| j                  | _        yy)zset current cursor positionN)
minmaxr.   r/   r;   r2   r1   r0   absr3   )r4   valuer=   s      r   r=   zSlidingInterval.currentJ   s     c$))U+TYY7TZZ DJ!$++-DKt{{"!DK 4;;.DJt{{TYY&'4+>+>>))DKtzzDII%&$*=*==DJ >r   c                     | j                   S )zbegiiing of interval to show)r0   r<   s    r   startzSlidingInterval.start^   s     {{r   c                     | j                   S )zend of interval to show)r2   r<   s    r   stopzSlidingInterval.stopc   s     zzr   c                 4    | j                   | j                  z
  S N)r2   r0   r<   s    r   r6   zSlidingInterval.widthh   s    zzDKK''r   c                 4    | j                   | j                  z
  S rH   )r=   rD   r<   s    r   nthzSlidingInterval.nthl   s    ||djj((r   N)r      r   r   )__name__
__module____qualname____doc__r.   r/   r;   r9   propertyr=   setterrD   rF   r6   rJ   r   r   r   r,   r,   -   s     DDH,   ^^# #&     ( ( ) )r   r,   c                        e Zd ZdZdZdZdZdZdZdZ	dZ
dZd fd	Z fd	Zd
 Zd Zed        Zd Zd Zd Zd ZddZddZd Zd Z xZS )CompletionHtmlz7 A widget for tab completion,  navigable by arrow keys r   r   r   r   )r   r   N   c                 b   t        |j                  t        j                  t        j                  f      sJ t
        |           |j                  | _        || _        |dkD  r|nd| _	        | j                  j                  |        d| _        d| _        | j                  | j                         y)zg Create a completion widget that is attached to the specified Qt
            text edit widget.
        r   
   N)
isinstance_controlr   	QTextEditQPlainTextEditsuperr9   
_text_edit_console_widget_rowsinstallEventFilter_sliding_interval_justified_itemssetFocusProxy)r4   console_widgetrows	__class__s      r   r9   zCompletionHtml.__init__   s     .11I4G4GIaIa3bccc(11-!AXT2
**40!% $ 	4??+r   c                    || j                   k(  r|j                         }|t        j                  j                  k(  rk|j                         }| j                  dk(  r|t        j                  j                  fv ry| j                  dk(  rB|t        j                  j                  fv r%| j                  dz   | _        | j                          y| j                  dk(  r|t        j                  j                  t        j                  j                  fv r| j                          y|t        j                  j                  fv r!| j                          | j                          y|t        j                  j                  fv r!| j                          | j                          y|t        j                  j                   fv r!| j                          | j                          y|t        j                  j"                  fv r!| j%                          | j                          y|t        j                  j&                  fv r!| j)                          | j                          y|t        j                  j*                  fv r| j-                          y| j-                          n>| j-                          n-|t        j                  j.                  k(  r| j-                          t0        | e  ||      S )zm Reimplemented to handle keyboard input and to auto-hide when the
            text edit loses focus.
        r   Fr   T   )r]   typer   QEventKeyPresskey_consecutive_tabQtKey_Tab_update_list
Key_Return	Key_Enter_complete_currentselect_rightKey_Downselect_down	Key_RightKey_Up	select_upKey_Leftselect_left
Key_Escapecancel_completionFocusOutr\   eventFilter)r4   objeventetyperl   rf   s        r   r   zCompletionHtml.eventFilter   s:    $//!JJLE...iik((A-#&)):K:K9M2M **a/CFII<M<M;O4O,0,A,A!,CD)%%'**a/vyy33VYY5H5HII..0#vyy0022))+))+#&))"4"4 66((*))+#!4!4 66))+))+#&))"2"2 44())+#&))"4"4 66((*))+#&))"6"6 88..0#..0**,&--000&&(w"3..r   c                     d| _         d| _        | j                  j                          d| _        | j
                  rd| _        yy)zCancel 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   rT   N)rm   _slice_startr^   _clear_temporary_buffer_indexra   r<   s    r   r}   z CompletionHtml.cancel_completion   sC     !"446!!%)D" "r   c                    | j                   \  }}|dz
  }|dz
  }||kD  r||k\  s
||k\  r||kD  r| j                  dd       y|dk  r|dk  s
|dk  r|dk  r| j                  ||       y||kD  r| j                  d|dz          y|dk  r| j                  ||dz
         y||kD  r| j                  |dz   d       y|dk  r| j                  |dz
  |       yd|k  r||k  rd|k  r||k  r
||f| _        yt        d||||fz        )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_indexr   NotImplementedError)r4   r   r&   nrncs        r   r   zCompletionHtml._select_index   s-   " BTT "Hr	cBhq!$Qh37qSAXr2&2Xq#a%(1Wr3q5)2Xs1ua(1Ws1ub)#X#)SSBY*DK% '=>A3B=O'P R Rr   c                 j    | j                   | j                  z   }|t        | j                        kD  ry |S rH   )r   
_slice_lenr   _items)r4   ends     r   
_slice_endzCompletionHtml._slice_end  s/    /T[[!!
r   c                 L    | j                   \  }}| j                  |dz
  |       y)zmove cursor upr   Nr   r   r4   rcs      r   ry   zCompletionHtml.select_up  #    {{11Q3"r   c                 L    | j                   \  }}| j                  |dz   |       y)zmove cursor downr   Nr   r   s      r   rv   zCompletionHtml.select_down  r   r   c                 L    | j                   \  }}| j                  ||dz
         y)zmove cursor leftr   Nr   r   s      r   r{   zCompletionHtml.select_left"  #    {{11ac"r   c                 L    | j                   \  }}| j                  ||dz          y)zmove cursor rightr   Nr   r   s      r   rt   zCompletionHtml.select_right'  r   r   c                 p  	 |sy|j                  t        j                  j                  |       |j	                         | _        d| _        | j                  j                         j                         }| j                  j                         }t        t        d||z  dz
              }t        |d|      \  }	t        t!        |      dz
  | j"                        | _        || _        	d   	d	   f| _        || _        d
| _        	fd}t/        t1        ||            | _        | j5                  d       y)ze Shows the completion widget with 'items' at the position specified
            by 'cursor'.
        N)nr   rW    )emptydisplaywidth)r6   rows_numberscolumns_numbersrT   c                 t    t        | d         D cg c]  \  }}|j                  |d       c}}S c c}}w )Ncolumns_widthr   )zipljust)xywcis      r   r   z+CompletionHtml.show_items.<locals>.<lambda>B  s/    Q?@S9TUAQWWQ_U Us   4F)hilight)movePositionr   QTextCursorLeftposition_start_positionrm   r]   document	textWidthr^   _get_font_widthintr@   r   r,   r   r_   ra   r   r   _old_cursorr   r   r   rb   rp   )
r4   cursoritemsprefix_lengthr6   
char_widthr   items_msjoinr   s
            @r   
show_itemszCompletionHtml.show_items,  s     	E--22mD%0 !((*446))99;
3rEJ$6!#;<=)%sV!0W1A!T("->*?@
!U $S%8 9%(r   c                 F   | j                   d   | j                  _        d}d}| j                  j                  dkD  rd}| j                  j                  | j                  j
                  k  rd}| j                  | j                  j                  | j                  j                  dz    }| j                  j                          |r&| j                  j                  | j                   d   f}nd}t        ||||      }| j                  j                  | j                  |d       y)zM update the list of completion and hilight the currently selected completion r   Nz...r   )r   r    r!   T)r)   )r   ra   r=   rD   rF   r/   rb   r^   r   rJ   r*   _fill_temporary_bufferr   )r4   r   r'   r(   r   selstrngs          r   rp   zCompletionHtml._update_listI  s    )-Q&!!''!+D!!&&)?)?)D)DDD''--33--2214)
 	446))--t{{1~>CCgc$tL33D4D4DeRV3Wr   c                     | j                   }| j                  |d      |d      }|j                         }|r| j                         j	                  |       | j                          y)zB Perform the completion with the currently selected item.
        r   r   N)r   r   strip_current_text_cursor
insertTextr}   )r4   iitems      r   rs   z CompletionHtml._complete_currente  sY     KK{{1Q4 1&zz|%%'2248 r   c                     | j                   j                         }|j                         | j                  k\  r4|j	                  | j                  t
        j                  j                         |S )zn Returns a cursor with text between the start position and the
            current position selected.
        )r]   
textCursorr   r   setPositionr   r   
KeepAnchor)r4   r   s     r   r   z#CompletionHtml._current_text_cursoro  sU     ++-?? 4 44t33$00;;=r   )rW   )r   )T)rL   rM   rN   rO   r   r   rm   r   r   r   r   r   r9   r   r}   r   rP   r   ry   rv   r{   rt   r   rp   rs   r   __classcell__)rf   s   @r   rS   rS   p   s    A FFEKOLJ,$2/n*4+R\  #
#
#
#
):X8!r   rS   )NNN)rO   qtpyr   r   r   utilr   r*   objectr,   QWidgetrS   r   r   r   <module>r      s;    - * ) %
>A)f A)FGY&& Gr   