
    f4                        d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlZd dlmZ d dlmZ d dl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mZmZ d dl m!Z! d dl"m#Z# ddl$m%Z% defdZ& G d de      Z' G d de      Z(defdZ)defdZ*defdZ+defdZ,defdZ-defdZ.defdZ/defdZ0defd Z1defd!Z2defd"Z3defd#Z4defd$Z5defd%Z6eee(df   Z7ded&e(d'efd(Z8defd)Z9defd*Z:d+ Z;y),    N)StringIO)CallableListOptionalUnion	GeneratorTuple)Buffer)KeyPressEvent)named_commands)AutoSuggestFromHistory
Suggestion)Document)History)PromptSession)	ProcessorTransformationTransformationInput)get_ipython)generate_tokens   )pass_throughdocumentc                 4    | j                   | j                     S N)linescursor_position_row)r   s    b/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/terminal/shortcuts/auto_suggest.py
_get_queryr      s    >>(6677    c                   2    e Zd ZdZddeddfdZdedefdZy)	AppendAutoSuggestionInAnyLinez
    Append the auto suggestion to lines other than the last (appending to the
    last line is natively supported by the prompt toolkit).
    stylereturnNc                     || _         y r   )r#   )selfr#   s     r   __init__z&AppendAutoSuggestionInAnyLine.__init__$   s	    
r    tic                    |j                   |j                  j                  dz
  k(  }|j                   |j                  j                  k(  }|sy|rw|j                  j
                  }|j                  r-|j                  j                  r|j                  j                  }nd}t        |j                  | j                  |fgz         S t        |j                        S )Nr    )	fragments)linenor   
line_countr   buffer_controlbuffer
suggestionis_cursor_at_the_end_of_linetextr   r+   r#   )r&   r(   is_last_lineis_active_liner/   r0   s         r   apply_transformationz2AppendAutoSuggestionInAnyLine.apply_transformation'   s    yyBKK$:$:Q$>>bkk&E&EE&&--F  R[[%M%M#..33

!BLLTZZ<T;U,UVV!BLL99r    )zclass:auto-suggestion)	__name__
__module____qualname____doc__strr'   r   r   r5    r    r   r"   r"      s.    
c  :': :~ :r    r"   c                       e Zd ZdZd ZdefdZd ZdefdZ	ded	e
d
ee   fdZd Zdedededed
eeeef   ddf   f
dZdededed
eeeef   ddf   fdZdededefdZdededed
dfdZdededed
dfdZy)NavigableAutoSuggestFromHistoryz
    A subclass of AutoSuggestFromHistory that allow navigation to next/previous
    suggestion from history. To do so it remembers the current position, but it
    state need to carefully be cleared on the right events.
    c                      d| _         g | _        y Nr   )
skip_lines_connected_apps)r&   s    r   r'   z(NavigableAutoSuggestFromHistory.__init__?   s     !r    _c                     d| _         y r?   )r@   )r&   rB   s     r   reset_history_positionz6NavigableAutoSuggestFromHistory.reset_history_positionE   s	    r    c                     | j                   D ]3  }|j                  j                  }|j                  | j                         5 y r   )rA   default_bufferon_text_insertremove_handlerrD   )r&   pt_apptext_insert_events      r   
disconnectz*NavigableAutoSuggestFromHistory.disconnectH   s?    ** 	JF & 5 5 D D,,T-H-HI	Jr    rI   c                     | j                   j                  |       |j                  j                  j	                  | j
                         |j                  j                  j	                  | j                         y r   )rA   appendrF   rG   add_handlerrD   on_cursor_position_changed_dismiss)r&   rI   s     r   connectz'NavigableAutoSuggestFromHistory.connectM   sV    ##F+ 	,,889T9TU88DDT]]Sr    r/   r   r$   c                     t        |      }|j                         r;| j                  || j                  |j                        D ]  \  }}t        |      c S  y r   )r   strip_find_next_matchr@   historyr   )r&   r/   r   r2   r0   rB   s         r   get_suggestionz.NavigableAutoSuggestFromHistory.get_suggestionT   sV     (#::<!%!6!6doov~~" .
A "*--.
 r    c                     d |_         y r   )r0   )r&   r/   argskwargss       r   rP   z(NavigableAutoSuggestFromHistory._dismissa   s
     r    r2   r@   rU   previousNc              #   >  K   d}t        t        |j                                     D ]r  }t        |j                               D ]T  }|dz  }|s||k  r|j	                  |      r)t        |      t        |      kD  r|t        |      d |f |sM||k\  sS  y t yw)ac  
        text : str
            Text content to find a match for, the user cursor is most of the
            time at the end of this text.
        skip_lines : float
            number of items to skip in the search, this is used to indicate how
            far in the list the user has navigated by pressing up or down.
            The float type is used as the base value is +inf
        history : History
            prompt_toolkit History instance to fetch previous entries from.
        previous : bool
            Direction of the search, whether we are looking previous match
            (True), or next match (False).

        Yields
        ------
        Tuple with:
        str:
            current suggestion.
        float:
            will actually yield only ints, which is passed back via skip_lines,
            which may be a +inf (float)


        r   N)reversedlistget_strings
splitlines
startswithlen)r&   r2   r@   rU   rZ   line_numberstringlines           r   _find_matchz+NavigableAutoSuggestFromHistory._find_matchd   s     8 tG$7$7$9:; 
	F !2!2!45 	q K*$< ??4(SYT-Bs4y{+[88z 9	
	s   BBBBc                 ,    | j                  |||d      S )NFrZ   )rf   r&   r2   r@   rU   s       r   rT   z0NavigableAutoSuggestFromHistory._find_next_match   s     j'EJJr    c           
      P    t        t        | j                  |||d                  S )NTrh   )r]   r^   rf   ri   s       r   _find_previous_matchz4NavigableAutoSuggestFromHistory._find_previous_match   s+    !!$
Gd!KL
 	
r    query
other_thanc                 ~    | j                  || j                  |      D ]  \  }}||z   |k7  s|| _         y  d| _        y r?   )rT   r@   r&   rl   rm   rU   r0   rc   s         r   upz"NavigableAutoSuggestFromHistory.up   sN    '+'<'<4??G(
 	 #J z!Z/"-	   DOr    c                     | j                  || j                  |      D ]  \  }}||z   |k7  s|| _         y  | j                  |t        d      |      D ]  \  }}||z   |k7  s|| _         y  y )NInf)rk   r@   floatro   s         r   downz$NavigableAutoSuggestFromHistory.down   s    '+'@'@4??G(
 	#J z!Z/"-	 ,0+D+DuU|W, '
K :%3&1DOr    )r6   r7   r8   r9   r'   r
   rD   rK   r   rQ   r   r   r   rV   rP   r:   rs   r   boolr   r	   rf   rT   rk   rp   rt   r;   r    r   r=   r=   8   s1   " J
Tm T(0	*	!''%*'5<'HL'	5e$dD0	1'RKK%*K5<K	5e$dD0	1K

 
% 
' 

     w  4   # 3  T r    r=   eventc                 N   | j                   }|j                  }|j                  |j                  d }|j	                  d      }|d   j                         }|j                  }|-|j                  r!|dk(  r|j                  |j                         yt        j                  |        y)z,Apply autosuggestion or jump to end of line.N
r   r*   )
current_bufferr   r2   cursor_positionsplitrS   r0   insert_textncend_of_line)rv   r/   dafter_cursorr   end_of_current_liner0   s          r   accept_or_jump_to_endr      s    !!FA66!++-.Lt$E(..*""JZ__;NRT;T:??+
ur    c                     t        |       S )zyAccept autosuggestion or jump to end of line.

    .. deprecated:: 8.12
        Use `accept_or_jump_to_end` instead.
    )r   rv   s    r   #_deprected_accept_in_vi_insert_moder      s     !''r    c                     | j                   }|j                  }|r|j                  |j                         yt	        j
                  |        y)zAccept autosuggestionN)ry   r0   r|   r2   r}   forward_char)rv   r/   r0   s      r   acceptr      s9    !!F""J:??+
r    c                 *    | j                   }d|_        y)zDiscard autosuggestionN)ry   r0   )rv   r/   s     r   discardr      s    !!FFr    c                     | j                   }|j                  }|rCt        j                  d|j                        }|j                  t        d |D        d             yt        j                  |        y)z#Fill partial autosuggestion by wordz(\S+\s+)c              3   &   K   | ]	  }|s|  y wr   r;   ).0xs     r   	<genexpr>zaccept_word.<locals>.<genexpr>   s      3q 3s   r*   N)	ry   r0   rer{   r2   r|   nextr}   forward_word)rv   r/   r0   ts       r   accept_wordr      sV    !!F""JHH[*//24 3A 3R89
r    c                     | j                   }|j                  }|r,|j                  r|j                  |j                  d          yyy)z(Fill partial autosuggestion by characterr   N)ry   r0   r2   r|   )rv   br0   s      r   accept_characterr      s;    AJjoo	jooa() &zr    c                     | j                   }|j                  }|j                  }|r#|j                  |j                         ||_        yy)z.Accept autosuggestion and keep cursor in placeN)ry   rz   r0   r|   r2   )rv   r/   old_positionr0   s       r   accept_and_keep_cursorr      sE    !!F))L""J:??+!- r    c                 D    t        |        t        j                  |        y)z3Accept autosuggestion and move cursor left in placeN)r   r}   backward_charr   s    r   accept_and_move_cursor_leftr      s    5!Ur    r/   c                 x    | j                   r.| j                   j                  | | j                        }|| _        y y r   )auto_suggestrV   r   r0   )r/   r0   s     r   _update_hintr     s4    ((77P
& r    c                 X    t        j                  |        t        | j                         y)z4Resume autosuggestions after deleting last characterN)r}   backward_delete_charr   ry   r   s    r   backspace_and_resume_hintr     s    E"%%&r    c                 X    t        j                  |        t        | j                         y)zResume autosuggestionsN)r   replyr   ry   r   s    r   resume_hintingr     s     u %%&r    c                 j    | j                   }|j                  | j                         t        |       y)zGo up and update hintcountN)ry   auto_upargr   rv   ry   s     r   up_and_update_hintr     s*    ))N+ r    c                 j    | j                   }|j                  | j                         t        |       y)zGo down and update hintr   N)ry   	auto_downr   r   r   s     r   down_and_update_hintr     s*    ))N599- r    c                 &   | j                   }|j                  }|rat        |j                        }||j                  z   }g d}dg}d}t        t        |      j                        D ]  }|j                  t        j                  k(  rt        |      }	n!|j                  |d   t        |d               }	|j                  |d|	        |d   }
|
j                  |      sx|dk(  r>t        |
      t        |      kD  r'|
t        |      d |d<   |j                  |
       |dz  }|d   ||<   |dk(  r n|dz  } |d   r@|d   }|d   rt        |d         dk(  r|d   }|t        |      d }|j                  |       yt!        j"                  |        y)	z$Fill partial autosuggestion by token)NNNr*   r   r   r\   N   )ry   r0   r   r   r2   r   r   readlinetypetokenizeNEWLINErb   indexrM   ra   r|   r}   r   )rv   r   r0   prefixr2   tokens
substringsitokenr   tokenized_so_farr|   	to_inserts                r   accept_tokenr   &  s   AJAJJ'
'&8T
$Xd^%<%<= 	EzzX---D	

58SB-@Ad6El+)"~**626c"23c&kA 0V ?F1I%%&67FA!!Hq	6Q	" !9$R.KayS^q0(n#CKM2IMM)$OOEr    providerdirection_methodc                     | j                   }|syt        | j                        }||j                  z   } |||| j                         |j                  | | j                        }|| _         y)a  
    We skip most recent history entry (in either direction) if it equals the
    current autosuggestion because if user cycles when auto-suggestion is shown
    they most likely want something else than what was suggested (otherwise
    they would have accepted the suggestion).
    N)rl   rm   rU   )r0   r   r   r2   rU   rV   )r/   r   r   r0   rl   currentnew_suggestions          r   _swap_autosuggestionr   S  s_     ""Jv'Ejoo%G5WfnnM,,VV__EN&Fr    c                     t               }|j                  }t        |t              syt	        | j
                  ||j                        S )z%Get next autosuggestion from history.Nr/   r   r   )r   r   
isinstancer=   r   ry   rp   rv   shellr   s      r   swap_autosuggestion_upr   k  s@    ME!!Hh ?@##h r    c                     t               }|j                  }t        |t              syt	        | j
                  ||j                        S )z)Get previous autosuggestion from history.Nr   )r   r   r   r=   r   ry   rt   r   s      r   swap_autosuggestion_downr   x  sB    ME!!Hh ?@##! r    c                 \    | dk(  r"t        j                  dt        d       t        S t        )Naccept_in_vi_insert_modez`accept_in_vi_insert_mode` is deprecated since IPython 8.12 and renamed to `accept_or_jump_to_end`. Please update your configuration accordinglyr   )
stacklevel)warningswarnDeprecationWarningr   AttributeError)keys    r   __getattr__r     s2    
(( 	
 32
r    )<r   r   ior   typingr   r   r   r   r   r	   r   prompt_toolkit.bufferr
   prompt_toolkit.key_bindingr   #prompt_toolkit.key_binding.bindingsr   r}   prompt_toolkit.auto_suggestr   r   prompt_toolkit.documentr   prompt_toolkit.historyr   prompt_toolkit.shortcutsr    prompt_toolkit.layout.processorsr   r   r   IPython.core.getipythonr   IPython.utils.tokenutilr   filtersr   r   r"   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Providerr   r   r   r   r;   r    r   <module>r      sc   	   D D  ( 4 D J , * 2  0 3 !8 8:I :4}&< }@ (} (- = } *M *.- .} ' ''] ''- '!m !! !' 'T ')H$NO''-' '0
- 
M 
r    