a
    If,                     @   s   d Z 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 )a  codecontext - display the block context above the edit window

Once code has scrolled off the top of a window, it can be difficult to
determine which block you are in.  This extension implements a pane at the top
of each IDLE edit window which provides block structure hints.  These hints are
the lines which contain the block opening keywords, e.g. 'if', for the
enclosing block.  The number of hint lines is determined by the maxlines
variable in the codecontext section of config-extensions.def. Lines which do
not open blocks are not shown in the context hints pane.

For EditorWindows, <<toggle-code-context>> is bound to CodeContext(self).
toggle_code_context_event.
    N)maxsize)FrameTextTclError)NSEWSUNKEN)idleConf>   tryasyncforelsewithwhileifexceptfinallyclassdefelifz^(\s*)(\w*)c                 C   s   | |  S )z>Extract the beginning whitespace and first word from codeline.)matchgroups)codelinec r   $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 )zReturn tuple of (line indent value, codeline, block start keyword).

    The indentation of empty lines (or comment lines) is INFINITY.
    If the line does not start a block, the keyword value is False.
    #)r   lenINFINITYBLOCKOPENERS)r   spacesZ	firstwordindentopenerr   r   r   get_line_info   s    r#   c                   @   sv   e Zd 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 )CodeContextz,Display block context above the edit window.d   c                 C   s   || _ |j| _|   dS )a  Initialize settings for context block.

        editwin is the Editor window for the context block.
        self.text is the editor window text widget.

        self.context displays the code context text above the editor text.
          Initially None, it is toggled via <<toggle-code-context>>.
        self.topvisible is the number of the top text line displayed.
        self.info is a list of (line number, indent level, line text,
          block keyword) tuples for the block structure above topvisible.
          self.info[0] is initialized with a 'dummy' line which
          starts the toplevel 'block' of the module.

        self.t1 and self.t2 are two timer events on the editor text widget to
          monitor for changes to the context text or editor font.
        N)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 )z!Load class variables from config.
extensionsr$   Zmaxlinesint   )typedefaultN)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 )zCancel scheduled events.N)r0   r'   after_cancelr   r3   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 )a  Toggle code context display.

        If self.context doesn't exist, create it to match the size of the editor
        window text (toggle on).  If it does exist, destroy it (toggle off).
        Return 'break' to complete the processing of the binding.
        Nr   padxborderr+   disabled)heightwidthZhighlightthicknessr>   r?   Zreliefstatez<ButtonRelease-1>)rowcolumnZsticky
linenumber
backgroundZbgZHideZShowZ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_colorsZbind
jumptolinetimer_eventZgridr   r   GetHighlightCurrentThemer   r/   Zdestroyr<   r0   r(   Zupdate_menu_label)
r)   eventZwidgetsr>   r?   Zwidgetr2   r.   line_number_colorsZmenu_statusr   r   r   toggle_code_context_event]   sV    





z%CodeContext.toggle_code_context_eventr+   r   c                 C   s   |dksJ g }t }t||d dD ]z}| j| d| d}t|\}}	}
||k r$|}|
dv rl|d7 }|
r||k r||kr||||	|
f ||kr$ qq$|  ||fS )az  Return a list of block line tuples and the 'last' indent.

        The tuple fields are (linenum, indent, text, opener).
        The list represents header lines from new_topvisible back to
        stopline with successively shorter indents > stopindent.
        The list is returned ordered by line number.
        Last indent returned is the smallest indent observed.
        r   r+   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 )aL  Update context information and lines visible in the context pane.

        No update is done if the text hasn't been scrolled.  If the text
        was scrolled, the lines that should be shown in the context will
        be retrieved and the context area will be updated with the code,
        up to the number of maxlines.
        z@0,0Nr,   r+   r   c                 S   s   g | ]}|d  qS )   r   ).0xr   r   r   
<listcomp>       z3CodeContext.update_code_context.<locals>.<listcomp>rA   ZnormalrC   z1.0end
r@   )r&   Z	getlinenor1   r]   r2   extendr9   r   r.   deleteinsertjoin)r)   rY   r[   r\   rZ   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 )z Show clicked context line at top of editor.

        If a selection was made, don't jump; allow copying.
        If no visible context, show the top line of the file.
        z	sel.firstr+   rg   r   rT   N)r.   rI   r   r   r2   r5   floatmaxr9   r'   Zyviewri   )r)   rQ   r[   ZnewtopZcontextlineoffsetr   r   r   rM      s    
zCodeContext.jumptolinec                 C   s*   | j dur&|   | j| j| j| _dS )z>Event on editor text widget triggered every UPDATEINTERVAL ms.N)r.   ri   r'   ZafterUPDATEINTERVALrN   r0   r3   r   r   r   rN      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)   ro   r   r   r   rK      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.   rG   Z
foregroundrF   rH   )r.   r   rO   rP   r/   Zconfig)r)   ZcolorsrR   r   r   r   rL      s    


z#CodeContext.update_highlight_colors)N)r+   r   )N)__name__
__module____qualname____doc__rm   r*   r(   classmethodr;   r=   rS   r]   ri   rM   rN   rK   rL   r   r   r   r   r$   -   s   
	
8
(
r$   __main__)rn   z"idlelib.idle_test.test_codecontextr^   F)	verbosityexit)rs   resysr   r   Ztkinterr   r   r   Ztkinter.constantsr   r   Zidlelib.configr   r   compiler   r#   r$   r;   rp   Zunittestrn   r   r   r   r   <module>   s    [