a
    If$                     @   sf   d Z ddlmZ ddlmZ ddddZdZG d	d
 d
Ze  e	dkrbddl
mZ eddd dS )zParenMatch -- for parenthesis matching.

When you hit a right paren, the cursor should move briefly to the left
paren.  Paren here is used generically; the matching applies to
parentheses, square brackets, and curly braces.
    )HyperParser)idleConf([{)]}d   c                   @   s   e Zd ZdZdZdZdd Ze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d Zdd ZeeeedZdd Zd d! ZdS )#
ParenMatcha'  Highlight matching openers and closers, (), [], and {}.

    There are three supported styles of paren matching.  When a right
    paren (opener) is typed:

    opener -- highlight the matching left paren (closer);
    parens -- highlight the left and right parens (opener and closer);
    expression -- highlight the entire expression from opener to closer.
    (For back compatibility, 'default' is a synonym for 'opener').

    Flash-delay is the maximum milliseconds the highlighting remains.
    Any cursor movement (key press or click) before that removes the
    highlight.  If flash-delay is 0, there is no maximum.

    TODO:
    - Augment bell() with mismatch warning in status window.
    - Highlight when cursor is moved to the right of a closer.
      This might be too expensive to check.
    z<<parenmatch-check-restore>>)z
<KeyPress>z<ButtonPress>z<Key-Return>z<Key-BackSpace>c                 C   s0   || _ |j| _|j| j| j d| _d| _d S )Nr   )editwintextZbindRESTORE_VIRTUAL_EVENT_NAMErestore_eventcounteris_restore_active)selfr    r   #lib/python3.9/idlelib/parenmatch.py__init__(   s    
zParenMatch.__init__c                 C   sV   t jddddd| _t jdddddd	| _t jddd
ddd	| _t t  d| _d S )N
extensionsr   Zstyleopener)defaultzflash-delayinti  )typer   bellbool   Zhilite)r   Z	GetOptionSTYLEFLASH_DELAYBELLZGetHighlightZCurrentThemeHILITE_CONFIG)clsr   r   r   reload3   s    


zParenMatch.reloadc                 C   s,   | j s(| jD ]}| j| j| qd| _ dS )z5Activate mechanism to restore text from highlighting.TN)r   RESTORE_SEQUENCESr   Z	event_addr   r   seqr   r   r   activate_restore>   s    
zParenMatch.activate_restorec                 C   s,   | j r(| jD ]}| j| j| qd| _ dS )zRemove restore event bindings.FN)r   r%   r   Zevent_deleter   r&   r   r   r   deactivate_restoreE   s    
zParenMatch.deactivate_restorec                 C   s   t | jd }| | dS )zAHandle editor 'show surrounding parens' event (menu or shortcut).insertbreak)r   r   get_surrounding_bracketsfinish_paren_event)r   eventindicesr   r   r   flash_paren_eventL   s    
zParenMatch.flash_paren_eventc                 C   sN   | j d}|tvrdS t| jd}| s0dS |t| d}| | dS )zHandle user input of closer.z	insert-1cNT)r   get_openersr   r   Z
is_in_coder,   r-   )r   r.   ZcloserZhpr/   r   r   r   paren_closed_eventS   s    
zParenMatch.paren_closed_eventc                 C   sT   |d u r| j r| j  d S |   | j| j| j| | | jrH| j	n| j
  d S N)r!   r   r   r(   tagfuncsr1   r   create_tag_expressionr    set_timeout_lastset_timeout_noner   r/   r   r   r   r-   `   s    
zParenMatch.finish_paren_eventNc                 C   s&   | j d |   |  jd7  _dS )zRemove effect of doing match.parenr   N)r   Z
tag_deleter)   r   )r   r.   r   r   r   r   k   s    zParenMatch.restore_eventc                 C   s   || j kr|   d S r4   )r   r   )r   Ztimer_countr   r   r   handle_restore_timerq   s    
zParenMatch.handle_restore_timerc                 C   s&   | j d|d  | j d| j dS )z'Highlight the single paren that matchesr:   r   N)r   tag_add
tag_configr"   r9   r   r   r   create_tag_openerx   s    zParenMatch.create_tag_openerc                 C   sb   | j |d dv r"|d d }n|d }| j d|d |d d |d | | j d| j dS )z#Highlight the left and right parensr   r   +1cr:   r   z-1cNr   r1   r<   r=   r"   r   r/   Z
rightindexr   r   r   create_tag_parens}   s
    $zParenMatch.create_tag_parensc                 C   sR   | j |d dv r"|d d }n|d }| j d|d | | j d| j dS )zHighlight the entire expressionr   r   r?   r:   r   Nr@   rA   r   r   r   r6      s
    z ParenMatch.create_tag_expression)r   r   ZparensZ
expressionc                 C   s>   |  j d7  _ | | j | jdfdd}| jjt|| dS )zSHighlight will remain until user input turns it off
        or the insert has movedr   r*   c                 S   s2   ||j dkr|| n|jjt| |  d S )Nr*   )r   indexr;   r   
text_frameafterCHECK_DELAY)callmer   crC   r   r   r   rG      s    z+ParenMatch.set_timeout_none.<locals>.callmeN)r   r   rC   r   rD   rE   rF   )r   rG   r   r   r   r8      s
    

zParenMatch.set_timeout_nonec                 C   s0   |  j d7  _ | jj| j| | j fdd dS )zFThe last highlight created will be removed after FLASH_DELAY millisecsr   c                 S   s
   |  |S r4   )r;   )r   rH   r   r   r   <lambda>       z-ParenMatch.set_timeout_last.<locals>.<lambda>N)r   r   rD   rE   r    )r   r   r   r   r7      s
    zParenMatch.set_timeout_last)N)__name__
__module____qualname____doc__r   r%   r   classmethodr$   r(   r)   r0   r3   r-   r   r;   r>   rB   r6   r5   r8   r7   r   r   r   r   r      s.   


	

r   __main__)mainz!idlelib.idle_test.test_parenmatch   )	verbosityN)rN   Zidlelib.hyperparserr   Zidlelib.configr   r2   rF   r   r$   rK   ZunittestrQ   r   r   r   r   <module>   s    &