a
    zeQ                     @   s   d Z ddlZddlT ddlmZ ddlmZmZ ddlm	Z	 dZ
dZed	fZd
ZdZdZdZdZdZdZG dd dZedkrddlmZ edddd dS )zH
An auto-completion window for IDLE, used by the autocomplete extension
    N)*)	Scrollbar)FILESATTRS)MC_SHIFTz<<autocompletewindow-hide>>z
<FocusOut>z<ButtonPress>z<<autocompletewindow-keypress>>)z<Key>z<Key-BackSpace>z<Key-Return>z	<Key-Tab>z<Key-Up>z
<Key-Down>z
<Key-Home>z	<Key-End>z<Key-Prior>z
<Key-Next>z<Key-Escape>z!<<autocompletewindow-keyrelease>>z<KeyRelease>z<B1-ButtonRelease>z<Configure>z<B1-Double-ButtonRelease>c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!S )"AutoCompleteWindowc                 C   s   || _ d  | _ | _| _d  | _| _d | _d | _d | _d | _	d | _
d | _d | _d  | _ | _ | _ | _ | _| _d| _d| _d S )NF)widgetautocompletewindowlistbox	scrollbarorigselforegroundorigselbackgroundcompletionsmorecompletionsmodestart
startindexlasttypedstartuserwantswindowZhideid
keypressidlistupdateidwinconfigidkeyreleaseiddoubleclickidlastkey_was_tabis_configuring)selfr    r   ,/usr/lib/python3.9/idlelib/autocomplete_w.py__init__   s"    zAutoCompleteWindow.__init__c                 C   s   t t| jt|}d}||k r<| j| || kr<|d7 }q|t| jk rr| jd| j|f d| jt| jf  |t|k r| jd| j|f ||d   || _d S )Nr      %s+%dc)minlenr   r   deleter   insert)r   newstartmin_lenir   r   r   _change_start>   s    

z AutoCompleteWindow._change_startc                 C   sT   d}t | j}||kr@|| d }| j| |kr6|}q|d }qt|t | jd S )zFind the first index in self.completions where completions[i] is
        greater or equal to s, or the last index if there is no such.
        r      r    )r#   r   r"   )r   sr(   jmr   r   r   _binary_searchK   s    
z!AutoCompleteWindow._binary_searchc           
      C   s   |  |}| j| dt| |kr(|S |d }t| j}||krx|| d }| j| dt| |krn|}q:|d }q:|d }||kr| j| S | j| }| j| }tt|t|}	t|}||	k r|| || kr|d7 }q|d| S )zAssuming that s is the prefix of a string in self.completions,
        return the longest string which is a prefix of all the strings which
        s is a prefix of them. If s is not a prefix of a string, return s.
        Nr    r*   )r.   r   r#   r"   )
r   r+   firstr(   r,   r-   ZlastZ
first_compZ	last_compr'   r   r   r   _complete_stringX   s(    






z#AutoCompleteWindow._complete_stringc                 C   sD  t | j d }| j| | j}| j| }| ||krB|}nDtt|t|}d}||k rz|| || krz|d7 }qX|d| }| 	| | j| dt| j
 | j
kr| jj| j| jd n|| jj| jd| jdd | jr@| j| _d| _| jdt | jD ]}| jt| q| j| | j
 |   dS )zzCall when the selection of the Listbox has changed.

        Updates the Listbox display and calls _change_start.
        r   r    N)selectbackgroundselectforegroundbgZfg)intr
   curselectionseer   r   r.   r"   r#   r)   r   Z	configurer   r   cgetr   r$   ENDr%   
select_set_selection_changed)r   curselZltsZselstartr&   r'   r(   itemr   r   r   r:   x   s6    





z%AutoCompleteWindow._selection_changedc                 C   s  |\| _ | _|| _| j|| _| j| jd| _|r| | j}| j}| 	| | 
|}| j | |kr|t| j d ks| j |d  dt| |kr||kS || _| j| _t| j | _}	|	d |	d z|	jdd|	jdd W n ty   Y n0 t|	td	 | _}
t|	|
jd
d | _}| j D ]}|t| q2|d| _|d| _ |
j!|j"d |
j#t$t%d |j#t&t'dd |	(  |	)  | j*| 
| j | +  |	,t-| j.| _/| j,t-| j.| _0|	1t-t2 t3D ]}| j1t-| q| j,t4| j5| _6t7D ]}| j1t4| q| j,t8| j9| _:| j1t8t; |,t<| j=| _>d
| _?|	,t@| jA| _B|,tC| jD| _EdS )zShow the autocomplete list, bind events.

        If complete is True, complete the text, and if there is exactly
        one matching completion, don't open a list.
        r%   r    Nz+10000+10000z!::tk::unsupported::MacWindowStylestylehelpZnoActivates)ZorientF)ZyscrollcommandZexportselectionr2   r1   )Zcommand)sidefillT)r?   r@   expand)Fr   r   r   r   indexr   getr   r0   r)   r.   r#   r   r   ZToplevelr	   wm_geometryZwm_overrideredirectZtkcallZ_wTclErrorr   ZVERTICALr   ZListboxsetr
   r%   r8   r7   r   r   ZconfigZyviewpackZRIGHTYZLEFTZBOTHupdate_idletasksZliftr9   r:   bindHIDE_VIRTUAL_EVENT_NAME
hide_eventhideaidhidewidZ	event_addHIDE_FOCUS_OUT_SEQUENCEHIDE_SEQUENCESKEYPRESS_VIRTUAL_EVENT_NAMEkeypress_eventr   KEYPRESS_SEQUENCESKEYRELEASE_VIRTUAL_EVENT_NAMEkeyrelease_eventr   KEYRELEASE_SEQUENCELISTUPDATE_SEQUENCElistselect_eventr   r   WINCONFIG_SEQUENCEwinconfig_eventr   DOUBLECLICK_SEQUENCEdoubleclick_eventr   )r   Z
comp_listsrB   completer   ZuserWantsWinZ	completedr   r(   acwr   r
   r<   seqr   r   r   show_window   s    




zAutoCompleteWindow.show_windowc                 C   sP  | j r
d S d| _ |  sd S z| j}|| j || j\}}}}| j}t 	dr`|
  | |  }}	| |  }
}| t|td|
|  }| | }|||  |	ks||	k r||7 }n||	8 }|d||f  |  W n ty   Y n0 t 	drFz|t| j W n ty>   Y n0 d | _d| _ d S )NTZWindowsr   z+%d+%dF)r   	is_activer   r6   r   Zbboxr	   platformsystem
startswithupdateZwinfo_widthwinfo_heightZwinfo_rootxr"   maxZwinfo_rootyrD   rJ   rF   unbindrZ   r   )r   eventtextxyZcxcyr_   Z	acw_widthZ
acw_height
text_widthZtext_heightZnew_xZnew_yr   r   r   r[      s@    
z"AutoCompleteWindow.winconfig_eventc                 C   s@   | j s
d S z| j  s|   W n ty:   |   Y n0 d S N)r	   Z	focus_gethide_windowKeyErrorr   r   r   r   _hide_event_check  s    
z$AutoCompleteWindow._hide_event_checkc                 C   s>   |   r:|jtjkr&| jd| j n|jtjkr:|   d S )Nr    )	rb   typeZ	EventTypeZFocusOutr   Zafterrt   ZButtonPressrq   r   rj   r   r   r   rM   +  s
    zAutoCompleteWindow.hide_eventc                 C   s4   |   r0d| _t| j d }| | j|  d S )NTr   )rb   r   r4   r
   r5   r)   r   r   rj   r;   r   r   r   rY   8  s    z#AutoCompleteWindow.listselect_eventc                 C   s.   t | j d }| | j|  |   d S )Nr   )r4   r
   r5   r)   r   rq   rw   r   r   r   r]   >  s    z$AutoCompleteWindow.doubleclick_eventc                    s   |   sd S |j t|dr$|j}nd} dkr6d| _t dks` dv s`| jtkrJ dv rJ|t @ sJt dkr| 	| j
   n| dkr| 	| j
d	  nb d
kr| 	| j
d  nH dkr| 	| j
d  n.t| j
dkr|   d S | 	| j
d d  | j
| _| jdt| j d  | j| | j
 |   dS  dkrh|   |   dS | jtkr~ dv s| jtkr dv r|t @ st| j d }| j| d t| j
 | j
kr| jtks| j
r| 	| j|  |   d S  dv r2|s2d| _t| j d } dkr:d}nĈ dkrTt| jd }n dv r| j| j | jd } dkrtd|| }n& dksJ tt| jd || }n@ dkrtd|d }n& dksJ tt| jd |d }| j| | j| |   | 	| j|  dS  dkr|s| jrxt| j d }| 	| j|  |   dS d| _d| _d S nt fdddD rd S |jr|jdkr| 	| j
|j  | j
| _| jdt| j d  | j| | j
 |   dS |   d S d S ) Nmc_stater   ZTabFr    )
underscoreZ	BackSpace)periodminusry   _rz   .r{   -breakZReturn)rz   spaceZ	parenleftZ
parenrightZbracketleftZbracketright)ZslashZ	backslashZquotedblZ
apostrophe)HomeEndPriorNextUpDownTr   r   )r   r   r   r   r   r   c                 3   s   | ]}| v V  qd S rp   r   ).0r+   keysymr   r   	<genexpr>      z4AutoCompleteWindow.keypress_event.<locals>.<genexpr>)ZShiftZControlZAltZMetaZCommandZOption )rb   r   hasattrrx   r   r#   r   r   r   r)   r   rq   r   r
   Zselect_clearr4   r5   r9   r.   r:   r^   r   r   r   Znearestrg   rh   r"   anychar)r   rj   stater;   ZnewselZjumpr   r   r   rS   D  s    

	 






z!AutoCompleteWindow.keypress_eventc                 C   s@   |   sd S | jd| jd| jt| jf kr<|   d S )Nr%   r!   )rb   r   rB   r   r#   r   rq   rv   r   r   r   rV     s    
z#AutoCompleteWindow.keyrelease_eventc                 C   s
   | j d uS rp   )r	   rs   r   r   r   rb     s    zAutoCompleteWindow.is_activec                 C   s   |  | | j d S rp   )r)   r0   r   rs   r   r   r   r^     s    zAutoCompleteWindow.completec                 C   s   |   sd S | jtt tD ]}| jt| q| jt| j | jt| j	 d | _d | _	t
D ]}| jt| qb| jt| j d | _| jtt | jt| j d | _| jt| j d | _| jr| jt| j d | _| j  | j  d | _| j  d | _| j  d | _d S rp   )rb   r	   Zevent_deleterL   rP   rQ   r   ri   rN   rO   rT   rR   r   rU   rW   r   r
   rX   r   r   rZ   Z	focus_setr   Zdestroy)r   r`   r   r   r   rq     s@    



zAutoCompleteWindow.hide_windowN)__name__
__module____qualname__r   r)   r.   r0   r:   ra   r[   rt   rM   rY   r]   rS   rV   rb   r^   rq   r   r   r   r   r      s    ! &M4tr   __main__)mainz%idlelib.idle_test.test_autocomplete_wr*   F)	verbosityexit)__doc__rc   tkinterZtkinter.ttkr   Zidlelib.autocompleter   r   Zidlelib.multicallr   rL   rP   rQ   rR   rT   rU   rW   rX   rZ   r\   r   r   Zunittestr   r   r   r   r   <module>   s,      W