a
    äIþf  ã                   @   sF   d Z ddlZddlZG dd„ dƒZedkrBddlmZ eddd	 dS )
a=  Complete the current word before the cursor with words in the editor.

Each menu selection or shortcut key selection replaces the word with a
different word with the same prefix. The search for matches begins
before the target and moves toward the top of the editor. It then starts
after the cursor and moves down. It then returns to the original word and
the cycle starts again.

Changing the current text line or leaving the cursor in a different
place before requesting the next selection causes AutoExpand to reset
its state.

There is only one instance of Autoexpand.
é    Nc                   @   s<   e Zd Zejej d Zdd„ Zdd„ Zdd„ Z	dd	„ Z
d
S )Ú
AutoExpandÚ_c                 C   s   |j | _ | j j| _d | _d S )N)ÚtextÚbellÚstate)ÚselfZeditwin© r   ú#lib/python3.9/idlelib/autoexpand.pyÚ__init__   s    
zAutoExpand.__init__c           
      C   sè   | j  d¡}| j  dd¡}| js.|  ¡ }d}n*| j\}}}}||ksL||krX|  ¡ }d}|sh|  ¡  dS |  ¡ }| j  dt|ƒ d¡ || }	|d t|ƒ }|dkr®|  ¡  | j  	d|	¡ | j  d¡}| j  dd¡}||||f| _dS )z1Replace the current word with the next expansion.Úinsertúinsert linestartzinsert lineendr   Úbreakzinsert - %d charsé   )
r   ÚindexÚgetr   Úgetwordsr   ÚgetprevwordÚdeleteÚlenr   )
r   ZeventZ	curinsertZcurlineÚwordsr   r   ÚlineÚwordZnewwordr   r   r	   Úexpand_word_event   s.    zAutoExpand.expand_word_eventc           	      C   sÒ   |   ¡ }|sg S | j dd¡}t d| d |¡}~| j dd¡}t d| d |¡}~|sd|sdg S g }i }| ¡  |D ]"}| |¡rˆqx| |¡ |||< qx|D ]"}| |¡r°q | |¡ |||< q | |¡ |S )z?Return a list of words that match the prefix before the cursor.z1.0zinsert wordstartz\bz\w+\bzinsert wordendÚend)r   r   r   ÚreÚfindallÚreverseÚappend)	r   r   ZbeforeZwbeforeZafterZwafterr   ÚdictÚwr   r   r	   r   6   s4    






zAutoExpand.getwordsc                 C   sF   | j  dd¡}t|ƒ}|dkr:||d  | jv r:|d }q||d… S )z)Return the word prefix before the cursor.r   r   r   r   N)r   r   r   Ú	wordchars)r   r   Úir   r   r	   r   U   s
    
zAutoExpand.getprevwordN)Ú__name__Ú
__module__Ú__qualname__ÚstringZascii_lettersZdigitsr    r
   r   r   r   r   r   r   r	   r      s
   r   Ú__main__)Úmainz!idlelib.idle_test.test_autoexpandé   )Ú	verbosity)Ú__doc__r   r%   r   r"   Zunittestr'   r   r   r   r	   Ú<module>   s   K