a
    ze,                     @   s   d dl Z d dlmZ d dlmZmZmZ d dlm	Z	m
Z
 d dlmZ h dZe dfdd	Zd
d ZG dd dZe  edkrd dlmZ edddd dS )    N)maxsize)FrameTextTclError)NSEWSUNKEN)idleConf>   finallydeftryclasswithasyncelifexceptwhileforelseifz^(\s*)(\w*)c                 C   s   | |  S N)matchgroups)codelinec r   )/usr/lib/python3.9/idlelib/codecontext.pyget_spaces_firstword   s    r   c                 C   sF   t | \}}t|}t| |ks,| | dkr0t}|tv o:|}|| |fS )N#)r   lenINFINITYBLOCKOPENERS)r   spacesZ	firstwordindentopenerr   r   r   get_line_info   s    r$   c                   @   sr   e Zd ZdZdd Zdd Zedd Zdd	 ZdddZ	dddZ
dd ZdddZdd Zdd Zdd Zd
S )CodeContextd   c                 C   s   || _ |j| _|   d S r   )editwintext_reset)selfr'   r   r   r   __init__1   s    zCodeContext.__init__c                 C   s$   d | _ d | _d | _d| _dg| _d S )N   )r    F)contextcell00t1
topvisibleinfor*   r   r   r   r)   F   s
    zCodeContext._resetc                 C   s   t jdddddd| _d S )N
extensionsr%   Zmaxlinesint   )typedefault)r   Z	GetOptioncontext_depth)clsr   r   r   reloadM   s    zCodeContext.reloadc                 C   s:   | j d ur6z| j| j  W n ty.   Y n0 d | _ d S r   )r1   r(   after_cancelr   r4   r   r   r   __del__T   s    
zCodeContext.__del__Nc           
   
   C   sv  | j d u r | jj| jjf}d}d}|D ]`}|| jju r@| n| }||j|d 7 }||j|d7 }||j|d7 }q(t	| jjddd||t
dd }| _ |   |   |d| j |   |jddtd tt d	}t| jj|d
 d| _| jjddtd d}	n:| j   d | _ | j  d | _| j| j |   d}	| jjdd|	 dd dS )Nr   padxborderr,   disabled)heightwidthZhighlightthicknessr?   r@   Zreliefstatez<ButtonRelease-1>)rowcolumnZsticky
linenumber
backgroundbgZHideZShowoptionsz*ode*ontextz Code Context)ZmenuindexZlabelbreak)r/   r'   r(   Z
text_frameZ	grid_infoZ	pack_infoZtkZgetintZcgetr   r   update_fontupdate_highlight_colorsbind
jumptolinetimer_eventZgridr   r   GetHighlightCurrentThemer   r0   Zdestroyr=   r1   r)   Zupdate_menu_label)
r*   eventZwidgetsr?   r@   Zwidgetr3   r/   line_number_colorsZmenu_statusr   r   r   toggle_code_context_event]   sV    





z%CodeContext.toggle_code_context_eventr,   r   c                 C   s   g }t }t||d dD ]z}| j| d| d}t|\}}	}
||k r|}|
dv r`|d7 }|
r||k r||kr||||	|
f ||kr qq|  ||fS )Nr,   r-   .0z.end)r   r   )r   ranger(   getr$   appendreverse)r*   new_topvisibleZstopline
stopindentlines
lastindentZlinenumr   r"   r(   r#   r   r   r   get_context   s    
zCodeContext.get_contextc                 C   sB  | j d}| j|krd S | j|k rT| || j\}}| jd d |kr| jd= q6n\| jd d d }| jd d |kr| jd d }| jd= qf| || jd d d |\}}| j| || _dd | j| j d  D }|d rdnd}t|| | jd< d| jd	< | j	d
d | j
dd||d   d| jd	< d S )Nz@0,0r-   r,   r   c                 S   s   g | ]}|d  qS )   r   ).0xr   r   r   
<listcomp>       z3CodeContext.update_code_context.<locals>.<listcomp>rB   ZnormalrD   z1.0end
rA   )r'   Z	getlinenor2   ra   r3   extendr:   r   r/   deleteinsertjoin)r*   r]   r_   r`   r^   Zcontext_stringsZ	showfirstr   r   r   update_code_context   s4    



zCodeContext.update_code_contextc                 C   s   z| j d W n~ ty   t| j}|dkr6d}n:tt| j d}td|| j d }| j||  d }| j	
| d |   Y n0 d S )Nz	sel.firstr,   rk   r   rX   )r/   rL   r   r   r3   r6   floatmaxr:   r(   Zyviewrm   )r*   rU   r_   ZnewtopZcontextlineoffsetr   r   r   rQ      s    
zCodeContext.jumptolinec                 C   s*   | j d ur&|   | j| j| j| _d S r   )r/   rm   r(   ZafterUPDATEINTERVALrR   r1   r4   r   r   r   rR      s    
zCodeContext.timer_eventc                 C   s(   | j d ur$t| jdd}|| j d< d S )NmainZEditorWindowfont)r/   r   ZGetFontr(   )r*   rs   r   r   r   rN      s    
zCodeContext.update_fontc                 C   sf   | j d ur6tt d}|d | j d< |d | j d< | jd urbtt d}| jj|d d d S )Nr/   rH   Z
foregroundrG   rI   )r/   r   rS   rT   r0   Zconfig)r*   ZcolorsrV   r   r   r   rO      s    


z#CodeContext.update_highlight_colors)N)r,   r   )N)__name__
__module____qualname__rq   r+   r)   classmethodr<   r>   rW   ra   rm   rQ   rR   rN   rO   r   r   r   r   r%   -   s   
	
8
(
r%   __main__)rr   z"idlelib.idle_test.test_codecontextrb   F)	verbosityexit)resysr   r   tkinterr   r   r   Ztkinter.constantsr   r   Zidlelib.configr   r    compiler   r$   r%   r<   rt   Zunittestrr   r   r   r   r   <module>   s    [