o
    º¼tf•  ã                   @   s.   d Z ddlmZmZmZ G dd„ dejƒZdS )z9 Provides bracket matching for Q[Plain]TextEdit widgets.
é    )ÚQtCoreÚQtGuiÚ	QtWidgetsc                       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                    sT   t |tjtjfƒsJ ‚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.
        ÚsilverN)Ú
isinstancer   Ú	QTextEditZQPlainTextEditÚsuperÚ__init__r   ZQTextCharFormatÚformatZsetBackgroundZQColorÚ
_text_editZcursorPositionChangedÚconnectÚ_cursor_position_changed)ÚselfZ	text_edit©Ú	__class__© úb/var/www/html/software/conda/envs/catlas/lib/python3.10/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}|dkr[|| ¡ k r[||kr9|d7 }n||krA|d8 }|dkrH	 |S ||7 }| |¡}|dkr[|| ¡ k s0d}|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_charÚ	incrementÚcharÚdepthr   r   r   Ú_find_match'   s0   


ü
ø
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movePositionr   ZQTextCursorZNextCharacterZ
KeepAnchorÚcursorr   )r   r    Z	selectionr&   r   r   r   Ú_selection_for_characterH   s   



ÿz'BracketMatcher._selection_for_characterc                    sj   ˆ j  g ¡ ˆ j  ¡ }| ¡ s1| ¡ d }ˆ  |¡}|dkr3‡ fdd„||fD ƒ}ˆ j  |¡ dS dS 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qtpyr   r   r   ZQObjectr   r   r   r   r   Ú<module>   s    