a
    If#                     @   s   d Z ddlZddlZddlZddlZddlZd\ZZddlm	Z	 ddl
mZ ddlmZ dZdZd	d	d	efZd	d	d	efZejej d
 Zej ejrejnd Zde ZG dd dZe  edkrddlmZ eddd dS )zComplete either attribute names or file names.

Either on demand or after a user-selected delay after a key character,
pop up a list of candidates.
    N)r      )autocomplete_w)idleConf)HyperParser)TFTN)FTTNF_ .c                   @   sn   e Zd ZdddZedd Zdd Zddd	Zd
d Zdd Z	dddZ
dd Zdd Zdd Zdd ZdS )AutoCompleteNc                 C   s,   || _ |d ur|j| _d | _d | _d | _d S N)editwintextautocompletewindow_delayed_completion_id_delayed_completion_index)selfr    r   %lib/python3.9/idlelib/autocomplete.py__init__"   s    zAutoComplete.__init__c                 C   s   t jdddddd| _d S )N
extensionsr	   	popupwaitintr   )typedefault)r   Z	GetOptionr   )clsr   r   r   reload-   s    
zAutoComplete.reloadc                 C   s   t | jS r
   )r   ZAutoCompleteWindowr   )r   r   r   r   _make_autocomplete_window2   s    z&AutoComplete._make_autocomplete_windowc                 C   s   | j r| j   d | _ d S r
   )r   Zhide_windowr   eventr   r   r   _remove_autocomplete_window5   s    
z(AutoComplete._remove_autocomplete_windowc                 C   s   |  t dS )zA(^space) Open completion list, even if a function call is needed.break)open_completionsFORCEr   r   r   r   force_open_completions_event:   s    
z)AutoComplete.force_open_completions_eventc                 C   s^   t |dr|js"| jdd s&dS | jrD| j rD| j  dS | t	}|rVdS dS dS )z5(tab) Complete word or open list if multiple options.mc_stateinsert linestartinsertNr   )
hasattrr#   r   getstripr   Z	is_activecompleter    TAB)r   r   Zopenedr   r   r   autocomplete_event?   s    

zAutoComplete.autocomplete_eventc                 C   sd   | j d}|tv r`|dkr tnt}| j d| _| jdurJ| j | j | j 	| j
| j|| _dS )z7(./) Open completion list after pause with no movement.z	insert-1cr   r%   N)r   r'   TRIGGERSTRY_ATRY_Findexr   r   after_cancelZafterr   _delayed_open_completions)r   r   Zlastcharargsr   r   r   try_open_completions_eventM   s    

z'AutoComplete.try_open_completions_eventc                 C   s&   d| _ | jd| jkr"| | dS )z)Call open_completions if index unchanged.Nr%   )r   r   r/   r   r    )r   r2   r   r   r   r1   X   s    z&AutoComplete._delayed_open_completionsc                 C   s  |\}}}}| j dur*| j| j  d| _ t| jd}| jdd}t| }}	| r|rd|tkr| 	  t}|r||d  dt
 vr|d8 }qp|||	 }
|}	|r||d  dvr|d8 }q|||	 }n| r|r|tkr| 	  t}|r*||d  tv s t||d  dkr*|d8 }q|||	 }
|r||d  dkr|dt||d    | }|r|s|d	d
krdS nd}ndS |r|s|
sdS | ||}|d sdS |  | _| j|dt|
 ||| S )aX  Find the completions and create the AutoCompleteWindow.
        Return True if successful (no syntax error or so found).
        If complete is True, then if there's nothing to complete and no
        start of completion, won't open completions and return False.
        If mode is given, will open a completion list only in this mode.
        Nr%   r$   r   z'"   r   z
insert-%dc(r   r   )r   r   r0   r   r   r'   lenZis_in_stringFILESr   SEPSZ
is_in_codeATTRSID_CHARSordZ	set_indexZget_expressionfindfetch_completionsr   r   Zshow_window)r   r2   Z	evalfuncsr)   ZwantwinmodeZhpZcurlineijZ
comp_startZ	comp_whatZ
comp_listsr   r   r   r    ^   sX    


.


zAutoComplete.open_completionsc           
      C   s  z| j jjjj}W n   d}Y n0 |r<|dd||fi S |tkr|dkri tjj	tj	}t
d|}dd tjD }|| |  d|v rtt
d|}nd	d
 |D }nVz@| |}t|}|  d|v rt|j}ndd
 |D }W n   g g f Y S 0 nf|tkrr|dkr$d}z0tj|}	t|	}|  dd
 |D }W n typ   g g f Y S 0 |s||}||fS dS )aO  Return a pair of lists of completions for something. The first list
        is a sublist of the second. Both are sorted.

        If there is a Python subprocess, get the comp. list there.  Otherwise,
        either fetch_completions() is running in the subprocess itself or it
        was called in an IDLE EditorWindow before any script had been run.

        The subprocess environment is that of the most recently run script.  If
        two unrelated modules are being edited some calltips in the current
        module may be inoperative if the module was not the last to run.
        NexecZget_the_completion_listr   zdir()c                 s   s   | ]}|d vr|V  qdS )>   TrueNoneFalseNr   .0sr   r   r   	<genexpr>   s   z1AutoComplete.fetch_completions.<locals>.<genexpr>__all__c                 S   s    g | ]}|d d dkr|qS Nr   r   r   rF   r   r   r   
<listcomp>       z2AutoComplete.fetch_completions.<locals>.<listcomp>c                 S   s    g | ]}|d d dkr|qS rK   r   rF   r   r   r   rL      rM   r   c                 S   s    g | ]}|d d dkr|qS )Nr   r   r   rF   r   r   r   rL      rM   )r   ZflistZpyshellZinterprpccltZ
remotecallr:   __main____builtins____dict__evalkeywordkwlistextendsortsorted
get_entitydirrJ   r8   ospath
expanduserlistdirOSError)
r   Zwhatr?   rN   	namespaceZbiglkwdsZsmalllZentityZexpandedpathr   r   r   r>      sT    








zAutoComplete.fetch_completionsc                 C   s   t |i tjtjS )zBLookup name in a namespace spanning sys.modules and __main.dict__.)rR   sysmodulesrO   rQ   )r   namer   r   r   rX      s    zAutoComplete.get_entity)N)N)N)__name__
__module____qualname__r   classmethodr   r   r   r"   r+   r3   r1   r    r>   rX   r   r   r   r   r	       s   



<<r	   rO   )mainz#idlelib.idle_test.test_autocomplete   )	verbosity)__doc__rO   rS   rZ   stringra   r:   r8   Zidlelibr   Zidlelib.configr   Zidlelib.hyperparserr   r!   r*   r-   r.   Zascii_lettersZdigitsr;   sepaltsepr9   r,   r	   r   rd   Zunittestrh   r   r   r   r   <module>   s,   
 <