o
     JAf,                     @   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rRd dlmZ edddd dS dS )    N)maxsize)FrameTextTclError)NSEWSUNKEN)idleConf>   classelifdefforwhiletryfinallyexceptasyncifelsewithz^(\s*)(\w*)c                 C   s   	 | |  S N)matchgroups)codelinec r   */usr/lib/python3.10/idlelib/codecontext.pyget_spaces_firstword   s   r   c                 C   sH   	 t | \}}t|}t| |ks| | dkrt}|tv o|}|| |fS )N#)r   lenINFINITYBLOCKOPENERS)r   spacesZ	firstwordindentopenerr   r   r   get_line_info   s   
r$   c                   @   st   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rz	| j| j  W n	 ty   Y nw d | _ d S 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 ]0}|| 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]   sX   




z%CodeContext.toggle_code_context_eventr,   r   c                 C   s   	 g }t }t||d dD ]=}| j| d| d}t|\}}	}
||k rJ|}|
dv r1|d7 }|
rD||k rD||krD||||	|
f ||krJ nq|  ||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   sd  	 | j d}| j|krd S | j|k r3| || j\}}| jd d |kr2| jd= | jd d |ks%n6| jd d d }| jd d |krY| jd d }| jd= | jd d |ksE| || 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>   s    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   s<   


zCodeContext.update_code_contextc                 C   s   	 z	| j d W d S  tyJ   t| j}|dkrd}ntt| j d}td|| j d }| j||  d }| j	
| d |   Y d S w )Nz	sel.firstr,   rj   r   rX   )r/   rL   r   r   r3   r6   floatmaxr:   r(   Zyviewrl   )r*   rU   r_   ZnewtopZcontextlineoffsetr   r   r   rQ      s   
zCodeContext.jumptolinec                 C   s0   	 | j d ur|   | j| j| j| _d S d S r   )r/   rl   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 d S )NmainZEditorWindowfont)r/   r   ZGetFontr(   )r*   rr   r   r   r   rN      s   
zCodeContext.update_fontc                 C   sj   | j d urtt d}|d | j d< |d | j d< | jd ur3tt d}| jj|d d d S 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_colorsr   )r,   r   )__name__
__module____qualname__rp   r+   r)   classmethodr<   r>   rW   ra   rl   rQ   rR   rN   rO   r   r   r   r   r%   -   s    

	
8
(r%   __main__)rq   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<   rs   Zunittestrq   r   r   r   r   <module>   s      [