B
    νvV                 @   s*   d Z ddlmZmZ G dd dejZdS )z9 Provides bracket matching for Q[Plain]TextEdit widgets.
    )QtCoreQtGuic                   sP   e Zd ZdZddddZdddd	Z fd
dZdd Zdd Zdd Z	  Z
S )BracketMatcherzX Matches square brackets, braces, and parentheses based on cursor
        position.
    )}])({[r   r	   r
   )r   r   r   c                sX   t |tjtjfsttt|   t | _	| j	
td || _|j| j dS )zf Create a call tip manager that is attached to the specified Qt
            text edit widget.
        ZsilverN)
isinstancer   	QTextEditZQPlainTextEditAssertionErrorsuperr   __init__ZQTextCharFormatformatZsetBackgroundZQColor
_text_editZcursorPositionChangedZconnect_cursor_position_changed)selfZ	text_edit)	__class__ 8lib/python3.7/site-packages/qtconsole/bracket_matcher.pyr      s    
zBracketMatcher.__init__c             C   s   | j  }||}| j|}|r*d}n| j|}|r@d}ndS |}d}xZ|dkr|| k r||krt|d7 }n||kr|d8 }|dkrP ||7 }||}qNW d}|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   documentZcharacterAt_opening_mapget_closing_mapZcharacterCount)r   positionr   Z
start_charZsearch_charZ	incrementcharZdepthr   r   r   _find_match'   s,    


zBracketMatcher._find_matchc             C   sD   t j }| j }|| |t jjt jj	 ||_
| j|_|S )z7 Convenience method for selecting a character.
        )r   r   ZExtraSelectionr   
textCursorZsetPositionZmovePositionZQTextCursorZNextCharacterZ
KeepAnchorcursorr   )r   r   Z	selectionr!   r   r   r   _selection_for_characterH   s    




z'BracketMatcher._selection_for_characterc                sb    j g   j  }| s^| d } |}|dkr^ fdd||fD } j | dS )zK Updates the document formatting based on the new cursor position.
        r   r   c                s   g | ]}  |qS r   )r"   ).0pos)r   r   r   
<listcomp>b   s   z;BracketMatcher._cursor_position_changed.<locals>.<listcomp>N)r   ZsetExtraSelectionsr    ZhasSelectionr   r   )r   r!   r   Zmatch_positionZextra_selectionsr   )r   r   r   V   s    


z'BracketMatcher._cursor_position_changed)__name__
__module____qualname____doc__r   r   r   r   r"   r   __classcell__r   r   )r   r   r      s   !r   N)r)   Zqtconsole.qtr   r   ZQObjectr   r   r   r   r   <module>   s   