
W#                 @   s   d  Z  d d l Z d d l Z d d l Z d d l m Z e j e j d Z e	 d d  \ Z
 Z d d l m Z d d l m Z d d l Z e j Z e j r e e j 7Z Gd	 d
   d
  Z e d k r d d l m Z e d d d d S)zAutoComplete.py - An IDLE extension for automatically completing names.

This extension can complete either attribute names or file names. It can pop
a window with all available names, for the user to select from.
    N)idleConf_      )AutoCompleteWindow)HyperParserc               @   s   e  Z d  Z d d! g f g Z e j d d  d d d d d	 Z d
 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)"AutoCompleteZeditShow Completions<<force-open-completions>>
extensions	popupwaittypeintdefaultr   Nc             C   sD   | |  _  | d  k r d  S| j |  _ d  |  _ d  |  _ d  |  _ d  S)N)editwintextautocompletewindow_delayed_completion_id_delayed_completion_index)selfr    r   A/afs/.cs/s/python-3.5.2/amd64_ubu14/lib/python3.5/AutoComplete.py__init__&   s    			zAutoComplete.__init__c             C   s   t  j  |  j  S)N)r   r   )r   r   r   r   _make_autocomplete_window3   s    z&AutoComplete._make_autocomplete_windowc             C   s#   |  j  r |  j  j   d  |  _  d  S)N)r   Zhide_window)r   eventr   r   r   _remove_autocomplete_window6   s    	z(AutoComplete._remove_autocomplete_windowc             C   s   |  j  d d d  d S)zqHappens when the user really wants to open a completion list, even
        if a function call is needed.
        TFN)open_completions)r   r   r   r   r   force_open_completions_event;   s    z)AutoComplete.force_open_completions_eventc             C   s]   |  j  j d  } | d k r7 |  j d d d t  n" | t k rY |  j d d d t  d S)zHappens 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_eventA   s    
z'AutoComplete.try_open_completions_eventc             C   sh   t  | d  r | j r d S|  j rE |  j j   rE |  j j   d S|  j d d d  } | rd d Sd S)zHappens 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_stateNbreakFT)hasattrr%   r   Z	is_activecompleter   )r   r   Zopenedr   r   r   autocomplete_eventN   s    zAutoComplete.autocomplete_eventc             G   s\   |  j  j d  |  _ |  j d  k	 r7 |  j  j |  j  |  j  j |  j |  j |  |  _ d  S)Ninsert)r   indexr   r   after_cancelafterr   _delayed_open_completions)r   argsr   r   r   r    ^   s
    z$AutoComplete._open_completions_laterc             G   s9   d  |  _  |  j j d  |  j k r( d  S|  j |   d  S)Nr*   )r   r   r+   r   r   )r   r/   r   r   r   r.   f   s    	z&AutoComplete._delayed_open_completionsc             C   s  |  j  d k	 r+ |  j j |  j   d |  _  t |  j d  } |  j j d d  } t |  } } | j   r| s | t k r|  j	   t } x, | r | | d d t
 k r | d 8} q W| | |  }	 | } x( | r | | d d k r | d 8} q W| | |  }
 n | j   r| s3| t k r|  j	   t } xB | r| | d t k szt | | d  d k r| d 8} qFW| | |  }	 | r| | d d k r| j d t |  | d  | j   }
 |
 s | r|
 j d	  d k rd Sqd
 }
 n d S| r,|
 r,|	 r,d S|  j |
 |  } | d sLd S|  j   |  _ |  j 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*   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   l   sN    	
!
7	
	zAutoComplete.open_completionsc       	      C   s  y |  j  j j j j } Wn d } Yn X| rL | j d d | | f i   S| t k rK| d k r t j j	   } | j
 t j j  t d |  } | j   d | k r t t d |   } qHd d   | D } qyZ |  j |  } t |  } | j   d | k rt | j  } n d	 d   | D } Wqg  g  f SYqXn | t k r| d k rid
 } yB t j j |  } t j |  } | j   d d   | D } Wn t k
 rg  g  f SYn X| s| } | | f Sd 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_listr2   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   flistZpyshellZinterprpccltZ
remotecallr!   __main____dict__copyupdate__builtins__evalsortsorted
get_entitydirr=   r#   ospath
expanduserlistdirOSError)	r   Zwhatr9   rB   	namespaceZbiglZsmalllZentityZexpandedpathr   r   r   r8      sL    


zAutoComplete.fetch_completionsc             C   s,   t  j j   } | j t j  t | |  S)zALookup name in a namespace spanning sys.modules and __main.dict__)sysmodulesrE   rF   rC   rD   rH   )r   namerR   r   r   r   rK      s    zAutoComplete.get_entity)r	   r
   )__name__
__module____qualname__Zmenudefsr   Z	GetOptionr   r   r   r   r   r$   r)   r    r.   r   r8   rK   r   r   r   r   r      s   ;9r   rC   )mainz#idlelib.idle_test.test_autocomplete	verbosity   )__doc__rM   rS   stringZidlelib.configHandlerr   ascii_lettersdigitsr5   ranger!   r#   Zidlelibr   Zidlelib.HyperParserr   rC   sepr"   altsepr   rV   ZunittestrY   r   r   r   r   <module>   s    		