ó
_y]c           @   s9   d  Z  d d l m Z m Z d e j f d „  ƒ  YZ d S(   s9    Provides bracket matching for Q[Plain]TextEdit widgets.
iÿÿÿÿ(   t   QtCoret   QtGuit   BracketMatcherc           B   sh   e  Z d  Z i d d 6d d 6d d 6Z i d d 6d d 6d d 6Z d „  Z d „  Z d	 „  Z d
 „  Z RS(   sX    Matches square brackets, braces, and parentheses based on cursor
        position.
    t   )t   (t   }t   {t   ]t   [c         C   s|   t  | t j t j f ƒ s! t ‚ t t |  ƒ j ƒ  t j ƒ  |  _	 |  j	 j
 t j d ƒ ƒ | |  _ | j j |  j ƒ d S(   sf    Create a call tip manager that is attached to the specified Qt
            text edit widget.
        t   silverN(   t
   isinstanceR   t	   QTextEditt   QPlainTextEditt   AssertionErrort   superR   t   __init__t   QTextCharFormatt   formatt   setBackgroundt   QColort
   _text_editt   cursorPositionChangedt   connectt   _cursor_position_changed(   t   selft	   text_edit(    (    s8   lib/python2.7/site-packages/qtconsole/bracket_matcher.pyR      s    !	c         C   sú   |  j  j ƒ  } | j | ƒ } |  j j | ƒ } | r? d } n% |  j j | ƒ } | r` d } n d S| } d } xƒ | d k rï | | j ƒ  k  rï | | k rª | d 7} n | | k rÃ | d 8} n  | d k rÓ Pn  | | 7} | j | ƒ } qs Wd } | S(   s    Given a valid position in the text document, try to find the
            position of the matching bracket. Returns -1 if unsuccessful.
        i   iÿÿÿÿi    (   R   t   documentt   characterAtt   _opening_mapt   gett   _closing_mapt   characterCount(   R   t   positionR   t
   start_chart   search_chart	   incrementt   chart   depth(    (    s8   lib/python2.7/site-packages/qtconsole/bracket_matcher.pyt   _find_match'   s,    		!
c         C   s`   t  j j ƒ  } |  j j ƒ  } | j | ƒ | j t  j j t  j j	 ƒ | | _
 |  j | _ | S(   s7    Convenience method for selecting a character.
        (   R   R   t   ExtraSelectionR   t
   textCursort   setPositiont   movePositiont   QTextCursort   NextCharactert
   KeepAnchort   cursorR   (   R   R    t	   selectionR.   (    (    s8   lib/python2.7/site-packages/qtconsole/bracket_matcher.pyt   _selection_for_characterH   s    	c         C   s˜   |  j  j g  ƒ |  j  j ƒ  } | j ƒ  s” | j ƒ  d } |  j | ƒ } | d k r” g  | | f D] } |  j | ƒ ^ qc } |  j  j | ƒ q” n  d S(   sK    Updates the document formatting based on the new cursor position.
        i   iÿÿÿÿN(   R   t   setExtraSelectionsR(   t   hasSelectionR    R&   R0   (   R   R.   R    t   match_positiont   post   extra_selections(    (    s8   lib/python2.7/site-packages/qtconsole/bracket_matcher.pyR   V   s    %(	   t   __name__t
   __module__t   __doc__R   R   R   R&   R0   R   (    (    (    s8   lib/python2.7/site-packages/qtconsole/bracket_matcher.pyR      s   		!	N(   R8   t   qtconsole.qtR    R   t   QObjectR   (    (    (    s8   lib/python2.7/site-packages/qtconsole/bracket_matcher.pyt   <module>   s   