
    =VQ`                     F    d Z ddlmZmZmZ  G d dej
                        Zy)z9 Provides bracket matching for Q[Plain]TextEdit widgets.
    )QtCoreQtGui	QtWidgetsc                   L     e Zd ZdZddddZdddd	Z fd
Zd Zd Zd Z	 xZ
S )BracketMatcherzX Matches square brackets, braces, and parentheses based on cursor
        position.
    )}])({[r   r   r   )r   r	   r
   c                 `   t        |t        j                  t        j                  f      sJ t        |           t        j                         | _        | j                  j                  t        j                  d             || _        |j                  j                  | j                         y)zf Create a call tip manager that is attached to the specified Qt
            text edit widget.
        silverN)
isinstancer   	QTextEditQPlainTextEditsuper__init__r   QTextCharFormatformatsetBackgroundQColor
_text_editcursorPositionChangedconnect_cursor_position_changed)self	text_edit	__class__s     ;/usr/lib/python3/dist-packages/qtconsole/bracket_matcher.pyr   zBracketMatcher.__init__   s     )i&9&99;S;S%TUUU ++-!!%,,x"89#''//0M0MN    c                    | j                   j                         }|j                  |      }| j                  j	                  |      }|rd}n!| j
                  j	                  |      }|rd}ny|}d}|dk\  r_||j                         k  rL||k(  r|dz  }n
||k(  r|dz  }|dk(  r	 |S ||z  }|j                  |      }|dk\  r||j                         k  rLd}|S )z Given a valid position in the text document, try to find the
            position of the matching bracket. Returns -1 if unsuccessful.
           r   )r   documentcharacterAt_opening_mapget_closing_mapcharacterCount)r   positionr%   
start_charsearch_char	incrementchardepths           r    _find_matchzBracketMatcher._find_match'   s    
 ??++-))(3
''++J7I++//
;K	 !m8+B+B+D Dz!
$
z
 	 	!H''1D !m8+B+B+D D Hr!   c                 L   t         j                  j                         }| j                  j	                         }|j                  |       |j                  t        j                  j                  t        j                  j                         ||_        | j                  |_        |S )z7 Convenience method for selecting a character.
        )r   r   ExtraSelectionr   
textCursorsetPositionmovePositionr   QTextCursorNextCharacter
KeepAnchorcursorr   )r   r+   	selectionr:   s       r    _selection_for_characterz'BracketMatcher._selection_for_characterH   s}     ''668	++-8$E--;;!--88	:!	;;	r!   c                 d   | j                   j                  g        | j                   j                         }|j                         sf|j	                         dz
  }| j                  |      }|dk7  r<||fD cg c]  }| j                  |       }}| j                   j                  |       yyyc c}w )zK Updates the document formatting based on the new cursor position.
        r#   r$   N)r   setExtraSelectionsr4   hasSelectionr+   r1   r<   )r   r:   r+   match_positionposextra_selectionss         r    r   z'BracketMatcher._cursor_position_changedV   s     	**2. ++-""$(1,H!--h7N#19>0J$M), &*%B%B3%G $M  $M223CD $ %$Ms   5B-)__name__
__module____qualname____doc__r'   r)   r   r1   r<   r   __classcell__)r   s   @r    r   r      s9    
 #30L#30LO$B
Er!   r   N)rF   qtpyr   r   r   QObjectr    r!   r    <module>rK      s&    * )\EV^^ \Er!   