
    "e$                         d Z ddlmZ ddlmZ ddddZdZ G d	 d
      Zej                          e	dk(  rddl
mZ  edd       yy)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                       e Zd ZdZdZdZd Zed        Zd Z	d Z
d Zd	 Zd
 ZddZd Zd Zd Zd ZeeeedZd Zd Zy)
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                     || _         |j                  | _        |j                  j                  | j                  | j                         d| _        d| _        y )Nr   )editwintextbindRESTORE_VIRTUAL_EVENT_NAMErestore_eventcounteris_restore_active)selfr   s     $lib/python3.12/idlelib/parenmatch.py__init__zParenMatch.__init__(   sH    LL	 	$99,,	.!"    c                    t        j                  dddd      | _        t        j                  ddddd	      | _        t        j                  ddd
dd	      | _        t        j
                  t        j                         d      | _        y )N
extensionsr   styleopener)defaultzflash-delayinti  )typer   bellbool   hilite)r   	GetOptionSTYLEFLASH_DELAYBELLGetHighlightCurrentThemeHILITE_CONFIG)clss    r   reloadzParenMatch.reload3   s{    &&gxA	",,\-eCQ%%\&vqJ$11(2G2G2I2:<r   c                     | j                   s?| j                  D ](  }| j                  j                  | j                  |       * d| _         yy)z5Activate mechanism to restore text from highlighting.TN)r   RESTORE_SEQUENCESr   	event_addr   r   seqs     r   activate_restorezParenMatch.activate_restore>   sI    %%-- J		##D$C$CSIJ%)D" &r   c                     | j                   r?| j                  D ](  }| j                  j                  | j                  |       * d| _         yy)zRemove restore event bindings.FN)r   r0   r   event_deleter   r2   s     r   deactivate_restorezParenMatch.deactivate_restoreE   sI    !!-- M		&&t'F'FLM%*D" "r   c                 n    t        | j                  d      j                         }| j                  |       y)zAHandle editor 'show surrounding parens' event (menu or shortcut).insertbreak)r   r   get_surrounding_bracketsfinish_paren_event)r   eventindicess      r   flash_paren_eventzParenMatch.flash_paren_eventL   s/    t||X6,,. 	(r   c                     | j                   j                  d      }|t        vryt        | j                  d      }|j                         sy|j                  t        |   d      }| j                  |       y)zHandle user input of closer.z	insert-1cNT)r   get_openersr   r   
is_in_coder;   r<   )r   r=   closerhpr>   s        r   paren_closed_eventzParenMatch.paren_closed_eventS   se     {+!{3}}--hv.>E(r   c                 @   |'| j                   r| j                  j                          y | j                           | j                  j                  | j                  | j                        | |        | j                  r| j                          y | j                          y N)r)   r   r"   r4   tagfuncsrA   r'   create_tag_expressionr(   set_timeout_lastset_timeout_noner   r>   s     r   r<   zParenMatch.finish_paren_event`   sy    ?tyyIINNA$**d&@&@A$P
2$"2"2		 	5 11	5r   Nc                     | j                   j                  d       | j                          | xj                  dz  c_        y)zRemove effect of doing match.parenr$   N)r   
tag_deleter7   r   )r   r=   s     r   r   zParenMatch.restore_eventk   s.    		W%!r   c                 D    || j                   k(  r| j                          y y rH   )r   r   )r   timer_counts     r   handle_restore_timerzParenMatch.handle_restore_timerq   s    $,,&  'r   c                     | j                   j                  d|d          | j                   j                  d| j                         y)z'Highlight the single paren that matchesrO   r   N)r   tag_add
tag_configr,   rM   s     r   create_tag_openerzParenMatch.create_tag_openerx   s4    		'71:.		Wd&8&89r   c                    | j                   j                  |d         dv r	|d   dz   }n|d   }| j                   j                  d|d   |d   dz   |dz   |       | j                   j                  d| j                         y)z#Highlight the left and right parensr$   r   +1crO   r   z-1cNr   rA   rU   rV   r,   r   r>   
rightindexs      r   create_tag_parenszParenMatch.create_tag_parens}   sy    99==$7 E)J J		'71:wqz%/?EAQS]^		Wd&8&89r   c                     | j                   j                  |d         dv r	|d   dz   }n|d   }| j                   j                  d|d   |       | j                   j                  d| j                         y)zHighlight the entire expressionr$   r   rY   rO   r   NrZ   r[   s      r   rJ   z ParenMatch.create_tag_expression   sf    99==$7 E)J J		'71:z:		Wd&8&89r   )r   r   parens
expressionc                     | xj                   dz  c_         | | j                   | j                  j                  d      fd}| j                  j                  j                  t        ||       y)zSHighlight will remain until user input turns it off
        or the insert has movedr$   r9   c                     ||j                   j                  d      k7  r|j                  |       y |j                  j                  j                  t        | |        y )Nr9   )r   indexrS   r   
text_frameafterCHECK_DELAY)callmer   crc   s       r   rg   z+ParenMatch.set_timeout_none.<locals>.callme   sA    		11))!,''--k66Jr   N)r   r   rc   r   rd   re   rf   )r   rg   s     r   rL   zParenMatch.set_timeout_none   sO     	 $2	K 	%%k66Br   c                     | xj                   dz  c_         | j                  j                  j                  | j                  | | j                   fd       y)zFThe last highlight created will be removed after FLASH_DELAY millisecsr$   c                 $    | j                  |      S rH   )rS   )r   rh   s     r   <lambda>z-ParenMatch.set_timeout_last.<locals>.<lambda>   s    d.G.G.J r   N)r   r   rd   re   r(   )r   s    r   rK   zParenMatch.set_timeout_last   s@     	%%J	Lr   rH   )__name__
__module____qualname____doc__r   r0   r   classmethodr.   r4   r7   r?   rF   r<   r   rS   rW   r]   rJ   rI   rL   rK    r   r   r   r      s    ( "@<	# < <*+	5!:
:: $$#+	
HCLr   r   __main__)mainz!idlelib.idle_test.test_parenmatch   )	verbosityN)ro   idlelib.hyperparserr   idlelib.configr   rB   rf   r   r.   rl   unittestrs   rq   r   r   <module>ry      s]    , #$bL bLJ     z	,: r   