B
    Ç S]=%  ã               @   s¶   d Z ddlZddlZddlZddlZeddƒ\ZZddlm	Z	 ddl
mZ ddlmZ ejej d ZejZejr|eej7 ZG d	d
„ d
ƒZe ¡  edkr²ddlmZ eddd dS )z”Complete 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é   é   )Úautocomplete_w)ÚidleConf)ÚHyperParserÚ_c               @   sv   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„ Z
dd„ Zdd„ Zddd„Zdd„ Zdd„ ZdS )ÚAutoCompleteNc             C   s,   || _ |d k	r(|j| _d | _d | _d | _d S )N)ÚeditwinÚtextÚautocompletewindowÚ_delayed_completion_idÚ_delayed_completion_index)Úselfr	   © r   ú%lib/python3.7/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 )N)r   ZAutoCompleteWindowr
   )r   r   r   r   Ú_make_autocomplete_window.   s    z&AutoComplete._make_autocomplete_windowc             C   s   | j r| j  ¡  d | _ d S )N)r   Zhide_window)r   Úeventr   r   r   Ú_remove_autocomplete_window1   s    
z(AutoComplete._remove_autocomplete_windowc             C   s   |   ddd¡ dS )zqHappens when the user really wants to open a completion list, even
        if a function call is needed.
        TFÚbreak)Úopen_completions)r   r   r   r   r   Úforce_open_completions_event6   s    z)AutoComplete.force_open_completions_eventc             C   sB   | j  d¡}|dkr&|  dddt¡ n|tkr>|  dddt¡ dS )z¥Happens when it would be nice to open a completion list, but not
        really necessary, for example after a dot, so function
        calls won't be made.
        z	insert-1cÚ.FN)r
   ÚgetÚ_open_completions_laterÚCOMPLETE_ATTRIBUTESÚSEPSÚCOMPLETE_FILES)r   r   Zlastcharr   r   r   Útry_open_completions_event=   s    

z'AutoComplete.try_open_completions_eventc             C   sb   t |dƒr|js"| j dd¡ ¡ s&dS | jrD| j ¡ rD| j ¡  dS |  ddd¡}|rZdS dS dS )z¥Happens when the user wants to complete his word, and if necessary,
        open a completion list after that (if there is more than one
        completion)
        Úmc_statezinsert linestartÚinsertNr   FT)	Úhasattrr&   r
   r    Ústripr   Z	is_activeÚcompleter   )r   r   Zopenedr   r   r   Úautocomplete_eventJ   s    
zAutoComplete.autocomplete_eventc             G   sD   | j  d¡| _| jd k	r&| j  | j¡ | j j| j| jf|žŽ | _d S )Nr'   )r
   Úindexr   r   Úafter_cancelZafterr   Ú_delayed_open_completions)r   Úargsr   r   r   r!   [   s
    
z$AutoComplete._open_completions_laterc             G   s&   d | _ | j d¡| jkr"| j|Ž  d S )Nr'   )r   r
   r,   r   r   )r   r/   r   r   r   r.   c   s    z&AutoComplete._delayed_open_completionsc             C   sô  | j dk	r| j | j ¡ d| _ t| jdƒ}| j dd¡}t|ƒ }}| ¡ rÊ|rX|tkrÊ|  	¡  t}x$|rˆ||d  dt
 krˆ|d8 }qfW |||… }	|}x |rº||d  dkrº|d8 }qœW |||… }
nÒ| ¡ r˜|râ|tkr˜|  	¡  t}x:|r(||d  tkst||d  ƒdkr(|d8 }qðW |||… }	|r’||d  dkr’| dt|ƒ|d   ¡ | ¡ }
|
rŒ|s–|
 d	¡d
kr–dS nd}
ndS |r²|
s²|	s²dS |  |
|¡}|d sÌdS |  ¡ | _| j |dt|	ƒ |||¡ S )ah  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.

        Action  Function                Eval   Complete WantWin Mode
        ^space  force_open_completions  True,  False,   True    no
        . or /  try_open_completions    False, False,   False   yes
        tab     autocomplete            False, True,    True    no
        Nr'   zinsert linestartr   z'"é   r   z
insert-%dcú(éÿÿÿÿÚ r   )r   r
   r-   r   r	   r    ÚlenZis_in_stringr$   r   r#   Z
is_in_coder"   ÚID_CHARSÚordZ	set_indexZget_expressionÚfindÚfetch_completionsr   r   Zshow_window)r   Z	evalfuncsr*   ZuserWantsWinÚmodeZhpZcurlineÚiÚjZ
comp_startZ	comp_whatZ
comp_listsr   r   r   r   h   sN    
0

zAutoComplete.open_completionsc       	      C   s`  y| j jjjj}W n   d}Y nX |r<| dd||fi ¡S |tkræ|dkr”tjj	tj	–}t
d|ƒ}| ¡  d|kr„tt
d|ƒƒ}qädd„ |D ƒ}nPy@|  |¡}t|ƒ}| ¡  d|krÄt|jƒ}nd	d„ |D ƒ}W n   g g fS nd|tkrJ|dk rþd
}y0tj |¡}t |¡}| ¡  dd„ |D ƒ}W n tk
rH   g g fS X |sT|}||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_listr3   zdir()Ú__all__c             S   s    g | ]}|d d… dkr|‘qS )Nr   r   r   )Ú.0Úsr   r   r   ú
<listcomp>Å   s    z2AutoComplete.fetch_completions.<locals>.<listcomp>c             S   s    g | ]}|d d… dkr|‘qS )Nr   r   r   )r>   r?   r   r   r   r@   Î   s    r   c             S   s    g | ]}|d d… dkr|‘qS )Nr   r   r   )r>   r?   r   r   r   r@   Ù   s    )r	   ZflistZpyshellZinterpÚrpccltZ
remotecallr"   Ú__main__Ú__builtins__Ú__dict__ÚevalÚsortÚsortedÚ
get_entityÚdirr=   r$   ÚosÚpathÚ
expanduserÚlistdirÚOSError)	r   Zwhatr9   rA   Ú	namespaceZbiglZsmalllZentityZexpandedpathr   r   r   r8   ¨   sL    







zAutoComplete.fetch_completionsc             C   s   t |tjtj–ƒS )zBLookup name in a namespace spanning sys.modules and __main.dict__.)rE   ÚsysÚmodulesrB   rD   )r   Únamer   r   r   rH   á   s    zAutoComplete.get_entity)N)N)N)Ú__name__Ú
__module__Ú__qualname__r   Úclassmethodr   r   r   r   r%   r+   r!   r.   r   r8   rH   r   r   r   r   r      s   


@9r   rB   )Úmainz#idlelib.idle_test.test_autocompleteé   )Ú	verbosity)Ú__doc__rB   rJ   ÚstringrP   Úranger"   r$   Zidlelibr   Zidlelib.configr   Zidlelib.hyperparserr   Zascii_lettersZdigitsr5   Úsepr#   Úaltsepr   r   rS   ZunittestrW   r   r   r   r   Ú<module>   s$   
 K