
\[\                  @   s   d  Z  d d l Z d d l m Z m Z m Z m Z m Z d d l Z d d l	 m
 Z d d l m Z e d d d d	 d
 d d d d d d g  Z d Z d Z e j d  d d  Z Gd d   d  Z d S)aI  CodeContext - Extension to 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 numlines
variable in the CodeContext section of config-extensions.def. Lines which do
not open blocks are not shown in the context hints pane.

    N)TOPLEFTXWSUNKEN)maxsize)idleConfclassdefelifelseexceptfinallyforiftrywhilewithd   i  z^(\s*)(\w*)c             C   s   | j  |   j   S)N)matchgroups)sc r   )/usr/lib/python3.4/idlelib/CodeContext.py<lambda>   s    r   c               @   s   e  Z d  Z d d  g f g Z e j d d  d d d d d	 Z e j d d  d
 d d d d Z e j d d  d d d d d Z d d   Z	 d d d  Z
 d d   Z d d d d  Z d d   Z d d   Z d d   Z d S)!CodeContextZoptions!Code Conte_xt<<toggle-code-context>>
extensionsZnumlinestypeintdefault   bgcolorstrZ	LightGrayfgcolorZBlackc             C   s   | |  _  | j |  _ |  j d |  _ d  |  _ d g |  _ d |  _ t j d d d d	 d
 d d } | r |  j   |  j  j	 d d  n  |  j j
 t |  j  |  j j
 t |  j  d  S)Nfontr       Fr   r   visibler    boolr"   z<<toggle-code-context>>T)r   r,   r)   F)editwintexttextfontlabelinfo
topvisibler   	GetOptiontoggle_code_context_eventZsetvarafterUPDATEINTERVALtimer_eventFONTUPDATEINTERVALfont_timer_event)selfr-   r*   r   r   r   __init__"   s    			
zCodeContext.__init__Nc             C   s  |  j  sC|  j j |  j j f } d } xM | D]E } | t t | j   d   7} | t t | j d    7} q. Wd } x- | D]% } | t t | j d    7} q Wt j	 |  j j
 d d |  j d d t d t d	 |  j d
 |  j d |  j d d d | d | d t 
|  _  |  j  j d t d t d d d |  j j  n |  j  j   d  |  _  t j d d d t |  j  d  k	   t j   d  S)Nr   padxborderr.   
r(   ZanchorZjustifyr'   ZbgZfgwidthZreliefZsideZfillexpandFZbeforer   r   r*   )r0   r-   r.   Z
text_framer!   r%   Z	pack_infoZcgettkinterZLabeltopcontext_depthr   r   r/   r$   r&   r   Zpackr   r   Zdestroyr   Z	SetOptionZSaveUserCfgFiles)r:   ZeventZwidgetsr<   Zwidgetr=   r   r   r   r4   7   s0    	 ##		z%CodeContext.toggle_code_context_eventc             C   s   |  j  j d | d |  } t |  \ } } | t k o> | } t |  t |  k so | t |  d k rx t } n t |  } | | | f S)zGet the line indent value, text, and any block start keyword

        If the line does not start a block, the keyword value is False.
        The indentation of empty lines (or comment lines) is INFINITY.

        z%d.0z%d.end#)r.   getgetspacesfirstwordBLOCKOPENERSlenINFINITY)r:   linenumr.   ZspacesZ	firstwordopenerindentr   r   r   get_line_info[   s    .	zCodeContext.get_line_infor(   r   c       
      C   s   | d k s t   g  } t } x t | | d d  D] } |  j |  \ } } }	 | | k  r5 | } |	 d k r~ | d 7} n  |	 r | | k  r | | k r | j | | | |	 f  n  | | k r Pq q5 q5 W| j   | | f S)zGet context lines, starting at new_topvisible and working backwards.

        Stop when stopline or stopindent is reached. Return a tuple of context
        data and the indent level at the top of the region inspected.

        r   r(   r   r   r,   )zelsezelif)AssertionErrorrI   rangerM   appendreverse)
r:   new_topvisibleZstopline
stopindentlines
lastindentrJ   rL   r.   rK   r   r   r   get_contextk   s    
zCodeContext.get_contextc             C   s  t  |  j j d  j d  d  } |  j | k r8 d S|  j | k  r |  j | |  j  \ } } x |  j d d | k r |  j d =qe Wn |  j | k r|  j d d d } x6 |  j d d | k r |  j d d } |  j d =q W|  j | |  j d d d |  \ } } n  |  j j |  | |  _ d g t d |  j	 t
 |  j   } | d d   |  j |  j	 d  D 7} d	 j |  |  j d
 <d S)zKUpdate context information and lines visible in the context pane.

        z@0,0.r   Nr(   r)   c             S   s   g  |  ] } | d   q S)   r   ).0xr   r   r   
<listcomp>   s   	 z3CodeContext.update_code_context.<locals>.<listcomp>r>   r.   r,   r,   r,   r,   r,   r,   r,   )r!   r.   indexsplitr2   rV   r1   extendmaxrC   rH   joinr0   )r:   rR   rT   rU   rS   Zcontext_stringsr   r   r   update_code_context   s*    %			&(zCodeContext.update_code_contextc             C   s0   |  j  r |  j   n  |  j j t |  j  d  S)N)r0   ra   r.   r5   r6   r7   )r:   r   r   r   r7      s    	zCodeContext.timer_eventc             C   s[   |  j  d } |  j rA | |  j k rA | |  _ |  j |  j d <n  |  j  j t |  j  d  S)Nr'   )r.   r0   r/   r5   r8   r9   )r:   Znewtextfontr   r   r   r9      s
    	zCodeContext.font_timer_event)r   r   )__name__
__module____qualname__Zmenudefsr   r3   rC   r$   r&   r;   r4   rM   rV   ra   r7   r9   r   r   r   r   r      s   $ r   )__doc__rA   Ztkinter.constantsr   r   r   r   r   resysr   rI   Zidlelib.configHandlerr   setrG   r6   r8   compilerF   r   r   r   r   r   <module>   s   (