a
    }Œ@h5  ã                   @   s´   d Z ddlZddlZddlZddlmZ ddlmZ dd„ Z	dd„ Z
G d	d
„ d
ƒZG dd„ deƒZG dd„ deƒZdd„ Zedkr°ddlmZ edddd� ddlmZ eeƒ dS )z†Line numbering implementation for IDLE as an extension.
Includes BaseSideBar which can be extended for other sidebar based extensions
é    N)ÚidleConf)Ú	Delegatorc                 C   s   t t|  d¡ƒƒS )z:Utility to get the last line's number in a Tk text widget.zend-1c)ÚintÚfloatÚindex)Útext© r   ú lib/python3.9/idlelib/sidebar.pyÚget_end_linenumber   s    r
   c                 C   s’   |   ¡ }|dkr|  ¡ }n |dkr,|  ¡ }ntd|› �ƒ‚tt| jj|d |  d¡|  d¡gƒƒ}tt| jj|d |  d¡|  d¡gƒƒ}||fS )z;Get the total padding of a Tk widget, including its border.ÚpackÚgridzUnsupported geometry manager: ÚpadxZborderÚpady)	Zwinfo_managerZ	pack_infoZ	grid_infoÚ
ValueErrorÚsumÚmapÚtkZgetintZcget)ZwidgetZmanagerÚinfor   r   r   r   r	   Úget_widget_padding   s"    


ý
ýr   c                   @   sh   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚBaseSideBarz@
    The base class for extensions which require a sidebar.
    c              	   C   sx   || _ |j| _|j| _t| jƒ\}}tj| jdtjd|ddd�| _| jj	tj
d� | j| jd< |  ¡  |  ¡  d| _d S )Né   é   r   )ÚwidthÚwrapr   r   ZborderwidthZhighlightthickness©ÚstateZyscrollcommandF)ÚeditwinÚ
text_frameÚparentr   r   r   ÚTextÚNONEÚsidebar_textÚconfigÚDISABLEDÚredirect_yscroll_eventÚupdate_fontÚupdate_colorsÚis_shown)Úselfr   Z_padxr   r   r   r	   Ú__init__/   s    þzBaseSideBar.__init__c                 C   s   t  | jdd¡}|  |¡ dS )z;Update the sidebar text font, usually after config changes.ÚmainÚEditorWindowN)r   ÚGetFontr   Ú_update_font©r(   Úfontr   r   r	   r%   ?   s    zBaseSideBar.update_fontc                 C   s   || j d< d S )Nr/   )r!   r.   r   r   r	   r-   D   s    zBaseSideBar._update_fontc                 C   s*   t  t  ¡ d¡}| j|d |d d� dS )ú=Update the sidebar text colors, usually after config changes.ZnormalÚ
foregroundÚ
background©r1   r2   N©r   ZGetHighlightZCurrentThemeÚ_update_colors©r(   Zcolorsr   r   r	   r&   G   s    
ÿzBaseSideBar.update_colorsc                 C   s   | j j|||||d� d S )N)ZfgZbgZselectforegroundZselectbackgroundZinactiveselectbackground)r!   r"   )r(   r1   r2   r   r   r	   r5   M   s
    ýzBaseSideBar._update_colorsc                 C   s$   | j s | jjddtjd� d| _ d S )Nr   r   ©ÚrowÚcolumnZstickyT)r'   r!   r   r   ÚNSEW©r(   r   r   r	   Úshow_sidebarT   s    zBaseSideBar.show_sidebarc                 C   s   | j r| j ¡  d| _ d S )NF)r'   r!   Zgrid_forgetr;   r   r   r	   Úhide_sidebarY   s    
zBaseSideBar.hide_sidebarc                 O   s"   | j jj|Ž  | j |d ¡ dS )zmRedirect vertical scrolling to the main editor text widget.

        The scroll bar is also updated.
        r   Úbreak)r   ÚvbarÚsetr!   Úyview_moveto)r(   ÚargsÚkwargsr   r   r	   r$   ^   s    z"BaseSideBar.redirect_yscroll_eventc                 C   s   | j  ¡  dS )z8Redirect focus-in events to the main editor text widget.r>   )r   Ú	focus_set©r(   Úeventr   r   r	   Úredirect_focusin_eventg   s    
z"BaseSideBar.redirect_focusin_eventc                 C   s"   | j  ¡  | j j|d|jd� dS )z<Redirect mouse button events to the main editor text widget.r   )ÚxÚyr>   )r   rD   Úevent_generaterI   )r(   rF   Ú
event_namer   r   r	   Úredirect_mousebutton_eventl   s    
z&BaseSideBar.redirect_mousebutton_eventc                 C   s   | j jdd|j|jd� dS )z7Redirect mouse wheel events to the editwin text widget.ú<MouseWheel>r   )rH   rI   Údeltar>   )r   rJ   rI   rN   rE   r   r   r	   Úredirect_mousewheel_eventr   s    
ÿz%BaseSideBar.redirect_mousewheel_eventN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   r%   r-   r&   r5   r<   r=   r$   rG   rL   rO   r   r   r   r	   r   +   s   	r   c                   @   s,   e Zd ZdZdd„ Zd	dd„Zd
dd„ZdS )ÚEndLineDelegatorz\Generate callbacks with the current end line number after
       insert or delete operationsc                 C   s   t  | ¡ || _dS )z·
        changed_callback - Callable, will be called after insert
                           or delete operations with the current
                           end line number.
        N)r   r)   Úchanged_callback)r(   rU   r   r   r	   r)   |   s    
zEndLineDelegator.__init__Nc                 C   s$   | j  |||¡ |  t| j ƒ¡ d S ©N)ÚdelegateÚinsertrU   r
   )r(   r   ÚcharsZtagsr   r   r	   rX   …   s    zEndLineDelegator.insertc                 C   s"   | j  ||¡ |  t| j ƒ¡ d S rV   )rW   ÚdeleterU   r
   )r(   Zindex1Zindex2r   r   r	   rZ   ‰   s    zEndLineDelegator.delete)N)N)rP   rQ   rR   rS   r)   rX   rZ   r   r   r   r	   rT   y   s   	
rT   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚLineNumbersz(Line numbers support for editor windows.c                 C   sä   t  | |¡ d| _t| jd ƒ| _| jjtjd� | j 	ddd¡ | jjtj
d� | jjddd� | jjdtjd	� |  ¡  t| jƒ}|  |¡ t| jƒ}| | jjj¡ | jj |¡ | jjj}||urÚ| ¡  |j}qÂd| _d S )
Nr   r   r   rX   Ú1Ú
linenumberF)Z	takefocusZexportselection)Zjustify)r   r)   Úprev_endÚtyper!   Ú_sidebar_width_typer"   r   ÚNORMALrX   r#   Z
tag_configZRIGHTÚbind_eventsr
   r   Úupdate_sidebar_textrT   Zsetdelegater   ZundorW   ZperÚtopZ
resetcacher'   )r(   r   ÚendZend_line_delegatorZ	delegatorr   r   r	   r)   �   s&    



zLineNumbers.__init__c                    s4  ˆj  dˆj¡ ˆj  dˆj¡ ‡fdd„}dD ]j}d|› d�d|› d�d	|› d
�fD ]}|||d� qVd|› d�d|› d�fD ]}||d|› d�d� q€q0d ‰d  ‰‰‡‡fdd„}ˆj  d|¡ ‡‡‡fdd„}ˆj  d|¡ ‡‡fdd„‰ ‡ ‡‡‡fdd„}ˆj  d|¡ ‡ ‡‡‡fdd„}ˆj  d|¡ d S )Nz	<FocusIn>rM   c                    s"   t jˆ j|d�}ˆ j | |¡ d S )N)rK   )Ú	functoolsÚpartialrL   r!   Úbind)rK   Útarget_event_nameZhandlerr;   r   r	   Úbind_mouse_event»   s    ÿz1LineNumbers.bind_events.<locals>.bind_mouse_event)r   é   é   é   z<Button-ú>z<ButtonRelease-z<Bz-Motion>)ri   z<Double-Button-z<Triple-Button-c                    sl   t tˆ j d| j› �¡ƒƒ}ˆ j ddd¡ ˆ j d|› d�|d › d�¡ ˆ j d|d › d�¡ |‰d S )Nú@0,Úselú1.0re   ú.0r   rX   )	r   r   r!   r   rI   r   Ú
tag_removeÚtag_addÚmark_set)rF   Úlineno©r(   Ú
start_liner   r	   Úb1_mousedown_handlerÙ   s
     z5LineNumbers.bind_events.<locals>.b1_mousedown_handlerz
<Button-1>c                    s   d ‰d ‰ d ‰d S rV   r   )rF   )Úlast_yÚ
last_yviewrx   r   r	   Úb1_mouseup_handlerå   s    z3LineNumbers.bind_events.<locals>.b1_mouseup_handlerz<ButtonRelease-1>c                    s‚   t tˆ j d| › �¡ƒƒ}tˆ|gƒ\}}ˆ j ddd¡ ˆ j d|› d�|d › d�¡ ˆ j d||krn|n|d › d�¡ dS )	z6Helper function for drag and selection event handlers.ro   rp   rq   re   rr   r   rX   N)	r   r   r!   r   Úsortedr   rs   rt   ru   )Zy_coordrv   ÚaÚbrw   r   r	   Ú%drag_update_selection_and_insert_markð   s     ÿzFLineNumbers.bind_events.<locals>.drag_update_selection_and_insert_markc                    sJ   | j ‰ˆj ¡ ‰dˆ  kr*ˆj ¡ ks<n ˆj ˆd ¡ ˆ | j ƒ d S ©Nr   )rI   r!   ÚyviewZwinfo_heightr   rA   )rF   rB   ©r€   rz   r{   r(   r   r	   Úb1_drag_handlerÿ   s
    
z0LineNumbers.bind_events.<locals>.b1_drag_handlerz<B1-Motion>c                    s:   ˆd u rd S ˆj  ¡ }|ˆkr6ˆj |d ¡ ˆ ˆƒ d S r�   )r!   r‚   r   rA   )rF   r‚   rƒ   r   r	   Úselection_handler  s    
z2LineNumbers.bind_events.<locals>.selection_handlerz<<Selection>>)r!   rh   rG   rO   )r(   rj   ZbuttonrK   ry   r|   r„   r…   r   )r€   rz   r{   r(   rx   r	   rb   ­   s4    


þ

ÿ
ÿ

	zLineNumbers.bind_eventsc                 C   s*   t  t  ¡ d¡}| j|d |d d� dS )r0   r]   r1   r2   r3   Nr4   r6   r   r   r	   r&     s    
ÿzLineNumbers.update_colorsc                 C   sÞ   || j krdS tt|ƒƒtt| j ƒƒ }|rVtt| jd ƒƒ}|| }|  |¡| jd< | jjtj	d� || j kr¬d 
t dgttt| j d |d ƒƒ¡¡}| j d|d¡ n| j |d › d	�d¡ | jjtjd� || _ dS )
zå
        Perform the following action:
        Each line sidebar_text contains the linenumber for that line
        Synchronize with editwin.text so that both sidebar_text and
        editwin.text contain the same number of linesNr   r   Ú
Ú r   zend -1cr]   z.0 -1c)r^   ÚlenÚstrr   r   r!   r`   r"   r   ra   ÚjoinÚ	itertoolsÚchainr   ÚrangerX   rZ   r#   )r(   re   Zwidth_differenceZ	cur_widthZ	new_widthZnew_textr   r   r	   rc     s"    

þzLineNumbers.update_sidebar_textN)rP   rQ   rR   rS   r)   rb   r&   rc   r   r   r   r	   r[   Ž   s
   jr[   c              	   C   sÐ   ddl m} t | ¡}t |¡}|jtjtjdd� |jddd� |j	ddd� t
 |dd¡}tj|d	d
tj|d�}|jddtjd� ||ƒ}t |¡|_t|ƒ}| ¡  | dd dd„ tddƒD ƒ¡¡ d S )Nr   )ÚDummy_editwinT)ZsideZfillÚexpandr   )Zweightr*   r+   éP   é   )r   Zheightr   r/   r7   rq   r†   c                 s   s   | ]}d | V  qdS )r~   Nr   )Ú.0Úir   r   r	   Ú	<genexpr>M  ó    z._linenumbers_drag_scrolling.<locals>.<genexpr>ée   )Zidlelib.idle_test.test_sidebarrŽ   r   ZToplevelZFramer   ZLEFTZBOTHZrowconfigureZcolumnconfigurer   r,   r   r    r   r:   Z	Scrollbarr?   r[   r<   rX   rŠ   r�   )r   rŽ   Ztoplevelr   r/   r   r   Zlinenumbersr   r   r	   Ú_linenumbers_drag_scrolling:  s    

r—   Ú__main__)r*   zidlelib.idle_test.test_sidebarr   F)Ú	verbosityÚexit)Úrun)rS   rf   r‹   Ztkinterr   Zidlelib.configr   Zidlelib.delegatorr   r
   r   r   rT   r[   r—   rP   Zunittestr*   Zidlelib.idle_test.htestr›   r   r   r   r	   Ú<module>   s"   N -