B
    v9a)                 @   s   d dl Z d dlmZ d dlZd dlmZmZmZ d dl	m
Z
 ddddd	d
ddddddhZdZ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)TOPXSUNKEN)idleConfclassdefelifelseexceptfinallyforiftrywhilewithasyncd   i  z^(\s*)(\w*)c             C   s   | |  S )N)matchgroups)codelinec r   !/usr/lib/python3.7/codecontext.pyget_spaces_firstword   s    r   c             C   sF   t | \}}t|}t| |ks,| | dkr0t}|tko:|}|| |fS )N#)r   lenINFINITYBLOCKOPENERS)r   ZspacesZ	firstwordindentopenerr   r   r   get_line_info   s    r!   c               @   s^   e Z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S )CodeContextc             C   s^   || _ |j| _| jd | _tj| _d | _d| _dg| _| j	t
| j| _| j	t| j| _d S )Nfont   )r    F)editwintexttextfontr"   colorscontextcolorscontext
topvisibleinfoafterUPDATEINTERVALtimer_eventt1CONFIGUPDATEINTERVALconfig_timer_eventt2)selfr'   r   r   r   __init__0   s    zCodeContext.__init__c             C   s,   t jdddddd| _t t  d| _d S )N
extensionsr"   Zmaxlinesint   )typedefaultr,   )r   Z	GetOptioncontext_depthZGetHighlightZCurrentThemer*   )clsr   r   r   reloadM   s    zCodeContext.reloadc             C   s2   y | j | j | j | j W n   Y nX d S )N)r(   Zafter_cancelr2   r5   )r6   r   r   r   __del__T   s
    zCodeContext.__del__Nc             C   s   | j s| jj| jjf}d}d}xP|D ]H}||j| d 7 }||j|d7 }||j|d7 }q$W tj	| jj
| j| jd | jd dd||tdd
| _ | j d	| j | j jttd
| jjd d}n| j   d | _ d}| jjdd| dd dS )Nr   padxborder
background
foregroundr$   disabled)	r#   bgZfgheightwidthrA   rB   Zreliefstatez<ButtonRelease-1>F)ZsidefillexpandZbeforeZHideZShowoptionsz* Code Contextz Code Context)ZmenuindexZlabelbreak)r,   r'   r(   Z
text_frameZtkZgetintZ	pack_infoZcgettkinterZTexttopr)   r+   r   Zbind
jumptolinepackr   r   ZdestroyZupdate_menu_label)r6   eventZwidgetsrA   rB   ZwidgetZmenu_statusr   r   r   toggle_code_context_event\   s2    



z%CodeContext.toggle_code_context_eventr$   r   c             C   s   g }t }xt||d dD ]x}| j| d| d}t|\}}	}
||k r|}|
dkrb|d7 }|
r||k r||kr||||	|
f ||krP qW |  ||fS )Nr$   r%   z.0z.end)r
   r	   )r   ranger(   getr!   appendreverse)r6   new_topvisibleZstopline
stopindentlines
lastindentZlinenumr   r   r(   r    r   r   r   get_context   s    
zCodeContext.get_contextc             C   sZ  t | jddd }| j|kr(d S | j|k rf| || j\}}x| jd d |krb| jd= qFW n`| jd d d }x,| jd d |kr| jd d }| jd= qzW | || jd d d |\}}| j| || _dd | j| j d  D }|d rdnd}t	|| | j
d< d	| j
d
< | j
dd | j
dd||d   d| j
d
< d S )Nz@0,0.r   r%   r$   c             S   s   g | ]}|d  qS )   r   ).0xr   r   r   
<listcomp>   s    z3CodeContext.update_code_context.<locals>.<listcomp>rG   ZnormalrI   z1.0end
rE   )r9   r(   rM   splitr-   r]   r.   extendr=   r   r,   deleteinsertjoin)r6   rY   r[   r\   rZ   Zcontext_stringsZ	showfirstr   r   r   update_code_context   s0    



zCodeContext.update_code_contextc             C   sp   t | j}|dkrd}n:tt| jd}td|| j d }| j||  d }| j	| d | 
  d S )Nr$   rh   r   z.0)r   r.   r9   floatr,   rM   maxr=   r(   Zyviewrj   )r6   rS   r[   ZnewtopZcontextlineoffsetr   r   r   rQ      s    
zCodeContext.jumptolinec             C   s$   | j r|   | jt| j| _d S )N)r,   rj   r(   r/   r0   r1   r2   )r6   r   r   r   r1      s    zCodeContext.timer_eventc             C   sv   | j d }| jr`|| jks&tj| jkr`|| _tj| _| j| jd< | jd | jd< | jd | jd< | j t| j| _	d S )Nr#   rC   rD   )
r(   r,   r)   r"   r*   r+   r/   r3   r4   r5   )r6   Znewtextfontr   r   r   r4      s    
zCodeContext.config_timer_event)N)r$   r   )N)__name__
__module____qualname__r7   classmethodr?   r@   rT   r]   rj   rQ   r1   r4   r   r   r   r   r"   -   s   
*
(
r"   __main__)mainz"idlelib.idle_test.test_codecontextr_   F)	verbosityexit)resysr   r   rO   Ztkinter.constantsr   r   r   Zidlelib.configr   r   r0   r3   compiler   r!   r"   r?   rn   Zunittestrs   r   r   r   r   <module>   s     A