Ë
    #´˜jœ,  ã                   óæ   — 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j                  d«      fd	„Zd
„ Z G d„ d«      Zej'                  «        edk(  rddlmZ  eddd¬«       yy)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>   ÚifÚdefÚforÚtryÚelifÚelseÚwithÚasyncÚclassÚwhileÚexceptÚfinallyz^(\s*)(\w*)c                 ó@   — |j                  | «      j                  «       S )z>Extract the beginning whitespace and first word from codeline.)ÚmatchÚgroups)ÚcodelineÚcs     ú%lib/python3.12/idlelib/codecontext.pyÚget_spaces_firstwordr      s   € à�7‰7�8Ó×#Ñ#Ó%Ð%ó    c                 óŽ   — t        | «      \  }}t        |«      }t        | «      |k(  s| |   dk(  rt        }|t        v xr |}|| |fS )zÐReturn 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   ÚspacesÚ	firstwordÚindentÚopeners        r   Úget_line_infor'      sU   € ô -¨XÓ6Ñ€FˆIÜ�‹[€FÜ
ˆ8ƒ}˜Ò (¨6Ñ"2°cÒ"9ÜˆØœ,Ð&Ò4¨9€FØ�8˜VÐ#Ð#r   c                   óf   — e Zd ZdZdZd„ Zd„ Zed„ «       Zd„ Z	dd„Z
dd	„Zd
„ Zdd„Zd„ Zd„ Zd„ Zy)ÚCodeContextz,Display block context above the edit window.éd   c                 óT   — || _         |j                  | _        | j                  «        y)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,   s     r   Ú__init__zCodeContext.__init__1   s   € ð" ˆŒØ—L‘LˆŒ	Ø�‰�r   c                 óL   — d | _         d | _        d | _        d| _        dg| _        y )Né   )r   éÿÿÿÿÚ F)ÚcontextÚcell00Út1Ú
topvisibleÚinfo©r/   s    r   r.   zCodeContext._resetF   s(   € ØˆŒØˆŒØˆŒØˆŒØ'Ð(ˆ�	r   c                 óB   — t        j                  ddddd¬«      | _        y)z!Load class variables from config.Ú
extensionsr)   ÚmaxlinesÚinté   )ÚtypeÚdefaultN)r	   Ú	GetOptionÚcontext_depth)Úclss    r   ÚreloadzCodeContext.reloadM   s#   € ô %×.Ñ.¨|¸]Ø/9ÀØ79ô;ˆÕr   c                 ó–   — | j                   �.	 | j                  j                  | j                   «       d| _         yy# t        $ r Y Œw xY w)zCancel scheduled events.N)r7   r-   Úafter_cancelr   r:   s    r   Ú__del__zCodeContext.__del__T   sI   € à�7‰7ÐðØ—	‘	×&Ñ& t§w¡wÔ/ð ˆD�Gð øô ò Ùðús   Ž%< ¼	AÁANc           
      ó>  — | j                   �€ö| j                  j                  | j                  j                  f}d}d}|D ]µ  }|| j                  j                  u r|j	                  «       n|j                  «       }||j                  j                  |d   «      z  }||j                  j                  |j                  d«      «      z  }||j                  j                  |j                  d«      «      z  }Œ· t        | j                  j                  ddd||t        d¬«      x}| _         | j                  «        | j                  «        |j                  d| j                  «       | j                  «        |j!                  ddt"        ¬	«       t%        j&                  t%        j(                  «       d
«      }t+        | j                  j                  |d   ¬«      | _        | j,                  j!                  ddt"        ¬	«       d}	ny| j                   j/                  «        d| _         | j,                  j/                  «        d| _        | j                  j1                  | j2                  «       | j5                  «        d}	| j                  j7                  dd|	› d�¬«       y)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Úborderr2   Údisabled)ÚheightÚwidthÚhighlightthicknessrJ   rK   ÚreliefÚstatez<ButtonRelease-1>)ÚrowÚcolumnÚstickyÚ
linenumberÚ
background©ÚbgÚHideÚShowÚoptionsz*ode*ontextz Code Context)ÚmenuÚindexÚlabelÚbreak)r5   r,   r-   Ú
text_frameÚ	grid_infoÚ	pack_infoÚtkÚgetintÚcgetr   r   Úupdate_fontÚupdate_highlight_colorsÚbindÚ
jumptolineÚtimer_eventÚgridr   r	   ÚGetHighlightÚCurrentThemer   r6   ÚdestroyrG   r7   r.   Úupdate_menu_label)
r/   ÚeventÚwidgetsrJ   rK   Úwidgetr9   r5   Úline_number_colorsÚmenu_statuss
             r   Útoggle_code_context_eventz%CodeContext.toggle_code_context_event]   s1  € ð �<‰<Ñð —l‘l×'Ñ'¨¯©×)@Ñ)@Ð@ˆGàˆDØˆFÛ!�à! T§\¡\×%6Ñ%6Ñ6ð ×(Ñ(Ô*à#×-Ñ-Ó/ð ð ˜Ÿ	™	×(Ñ(¨¨f©Ó6Ñ6�Ø˜Ÿ	™	×(Ñ(¨¯©°VÓ)<Ó=Ñ=�Ø˜&Ÿ)™)×*Ñ*¨6¯;©;°xÓ+@ÓAÑA‘ð "ô &*Ø—‘×'Ñ'ØØØ#$Ø &´¸zô&Kð KˆG�d”lð ×ÑÔØ×(Ñ(Ô*Ø�L‰LÐ,¨d¯o©oÔ>à×ÑÔà�L‰L˜Q q´ˆLÔ6ä!)×!6Ñ!6´x×7LÑ7LÓ7NØ7Có"EÐä §¡× 7Ñ 7Ø+=¸lÑ+KôMˆDŒKà�K‰K×Ñ ¨1´TÐÔ:Ø ‰Kà�L‰L× Ñ Ô"ØˆDŒLØ�K‰K×ÑÔ!ØˆDŒKØ�I‰I×"Ñ" 4§7¡7Ô+Ø�K‰KŒMØ ˆKØ�‰×&Ñ&¨I¸]Ø0;¨}¸MÐ-Jð 	'ô 	Làr   c                 óF  — |dkD  sJ ‚g }t         }t        ||dz
  d«      D ]l  }| j                  j                  |› d�|› d�«      }t	        |«      \  }}	}
||k  sŒ:|}|
dv r|dz  }|
r||k  r||k\  r|j                  |||	|
f«       ||k  sŒl n |j                  «        ||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   r2   r3   ú.0z.end)r   r   )r!   Úranger-   Úgetr'   ÚappendÚreverse)r/   Únew_topvisibleÚstoplineÚ
stopindentÚlinesÚ
lastindentÚlinenumr   r%   r-   r&   s              r   Úget_contextzCodeContext.get_context•   sË   € ð ˜!Š|Ðˆ|Øˆäˆ
ô ˜^¨X°a©Z¸Ö<ˆGØ—y‘y—}‘}¨ y° ^¸°yÀÐ5EÓFˆHÜ#0°Ó#:Ñ ˆF�D˜&Ø˜
Ó"Ø#�
ØÐ-Ñ-à !‘O�JÙ˜g¨Ò6¸6ÀZÒ;OØ—L‘L '¨6°4¸Ð!@ÔAØ Ó+Ùð =ð 	�‰ŒØ�jÐ Ð r   c                 óò  — | j                   j                  d«      }| j                  |k(  ry| j                  |k  rX| j                  || j                  «      \  }}| j                  d   d   |k\  r¬| j                  d= | j                  d   d   |k\  rŒ#nˆ| j                  d   d   dz   }| j                  d   d   |k\  r5| j                  d   d   }| j                  d= | j                  d   d   |k\  rŒ5| j                  || j                  d   d   dz   |«      \  }}| j                  j                  |«       || _        | j                  | j                   d D �cg c]  }|d   ‘Œ	 }}|d   rdnd}t        |«      |z
  | j                  d<   d| j                  d	<   | j                  j                  d
d«       | j                  j                  ddj                  ||d «      «       d| j                  d	<   yc c}w )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,0Nr3   r2   r   é   rM   ÚnormalrQ   z1.0ÚendÚ
rL   )r,   Ú	getlinenor8   r‚   r9   ÚextendrC   r    r5   ÚdeleteÚinsertÚjoin)r/   r|   r   r€   r~   ÚxÚcontext_stringsÚ	showfirsts           r   Úupdate_code_contextzCodeContext.update_code_context³   sê  € ð Ÿ™×/Ñ/°Ó7ˆØ�?‰?˜nÒ,ØØ�?‰?˜^Ò+Ø $× 0Ñ 0°Ø15·±ó!BÑˆE�:ð —)‘)˜B‘- Ñ" jÒ0Ø—I‘I˜b�Mð —)‘)˜B‘- Ñ" jÔ0ð Ÿ™ 2™ qÑ)¨AÑ-ˆJð —)‘)˜B‘- Ñ" nÒ4Ø!ŸY™Y r™]¨1Ñ-�
Ø—I‘I˜b�Mð —)‘)˜B‘- Ñ" nÓ4ð !%× 0Ñ 0°Ø15·±¸2±¸qÑ1AÀ!Ñ1CØ1;ó!=ÑˆE�:ð 	�	‰	×Ñ˜ÔØ(ˆŒà)-¯©°D×4FÑ4FÐ3FÐ3GÑ)HÓIÑ)H A˜1˜Q›4Ð)HˆÐIØ(¨Ò+‘A°ˆ	ä!$ _Ó!5¸	Ñ!Aˆ�‰�XÑØ (ˆ�‰�WÑØ�‰×Ñ˜E 5Ô)Ø�‰×Ñ˜E 4§9¡9¨_¸Y¸ZÐ-HÓ#IÔJØ *ˆ�‰�WÒùò Js   ÅG4c                 ó¬  — 	 | j                   j                  d«       y# t        $ r¬ t        | j                  «      }|dk(  rd}n^t        t        | j                   j                  d«      «      «      }t        d|| j                  z
  «      dz
  }| j                  ||z      d   }| j                  j                  |› d�«       | j                  «        Y yw xY w)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.firstr2   r‹   r   rw   N)r5   r]   r   r    r9   r>   ÚfloatÚmaxrC   r-   Úyviewr�   )r/   rp   r   ÚnewtopÚcontextlineÚoffsets         r   ri   zCodeContext.jumptolineÛ   s·   € ð	'Ø�L‰L×Ñ˜{Õ+øÜò 	'Ü˜Ÿ	™	“NˆEØ˜ŠzØ‘ô "¤%¨¯©×(:Ñ(:¸8Ó(DÓ"EÓF�ä˜Q ¨×(:Ñ(:Ñ :Ó;¸aÑ?�ØŸ™ 6¨KÑ#7Ñ8¸Ñ;�Ø�I‰I�O‰O˜v˜h b˜MÔ*Ø×$Ñ$Ö&ð	'ús   ‚ žB2CÃCc                 ó¨   — | j                   �F| j                  «        | j                  j                  | j                  | j
                  «      | _        yy)z>Event on editor text widget triggered every UPDATEINTERVAL ms.N)r5   r�   r-   ÚafterÚUPDATEINTERVALrj   r7   r:   s    r   rj   zCodeContext.timer_eventð   s?   € à�<‰<Ð#Ø×$Ñ$Ô&Ø—i‘i—o‘o d×&9Ñ&9¸4×;KÑ;KÓLˆD�Gð $r   c                 ó~   — | j                   �1t        j                  | j                  dd«      }|| j                   d<   y y )NÚmainÚEditorWindowÚfont)r5   r	   ÚGetFontr-   )r/   rž   s     r   rf   zCodeContext.update_fontö   s7   € Ø�<‰<Ð#Ü×#Ñ# D§I¡I¨v°~ÓFˆDØ#'ˆD�L‰L˜Ò ð $r   c                 ó\  — | j                   �Lt        j                  t        j                  «       d«      }|d   | j                   d<   |d   | j                   d<   | j                  �Ht        j                  t        j                  «       d«      }| j                  j                  |d   ¬«       y y )Nr5   rV   Ú
foregroundrU   rW   )r5   r	   rl   rm   r6   Úconfig)r/   Úcolorsrs   s      r   rg   z#CodeContext.update_highlight_colorsû   s—   € Ø�<‰<Ð#Ü×*Ñ*¬8×+@Ñ+@Ó+BÀIÓNˆFØ)/°Ñ)=ˆD�L‰L˜Ñ&Ø)/°Ñ)=ˆD�L‰L˜Ñ&à�;‰;Ð"Ü!)×!6Ñ!6´x×7LÑ7LÓ7NØ7Có"EÐà�K‰K×ÑÐ"4°\Ñ"BÐÕCð #r   )N)r2   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rš   r0   r.   ÚclassmethodrE   rH   ru   r‚   r�   ri   rj   rf   rg   © r   r   r)   r)   -   sT   „ Ù2Ø€Nòò*)ð ñ;ó ð;òó6óp!ò<&+óP'ò*Mò(ó
	Dr   r)   Ú__main__)rœ   z"idlelib.idle_test.test_codecontextr„   F)Ú	verbosityÚexit)r§   ÚreÚsysr   r!   Útkinterr   r   r   Útkinter.constantsr   r   Úidlelib.configr	   r"   Úcompiler   r'   r)   rE   r¤   Úunittestrœ   r©   r   r   Ú<module>r´      s{   ðñó 
Ý #ç )Ñ )ß *å #ò>€ð &0 R§Z¡Z°Ó%?ó &ò
$÷WDñ WDðt × Ñ Ô ð ˆzÒÝÙÐ	-¸ÀÖGð r   