o =?h,@sdZddlZddlmZddlmZmZmZddl m Z m Z ddl m Z hdZedfd d Zd d ZGd ddZeedkrSddlmZedddddSdS)acodecontext - 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, <> is bound to CodeContext(self). toggle_code_context_event. N)maxsize)FrameTextTclError)NSEWSUNKEN)idleConf> defclassasyncforelsetryexceptiffinallyelifwhilewithz ^(\s*)(\w*)cCs||S)z>Extract the beginning whitespace and first word from codeline.)matchgroups)codelinecr:/opt/alt/python310/lib64/python3.10/idlelib/codecontext.pyget_spaces_firstwordsrcCsFt|\}}t|}t||ks||dkrt}|tvo|}|||fS)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. #)rlenINFINITY BLOCKOPENERS)rspacesZ firstwordindentopenerrrr get_line_infos   r#c@sveZdZdZdZddZddZeddZd d Z dd d Z dddZ ddZ dddZ ddZddZddZd S) CodeContextz,Display block context above the edit window.dcCs||_|j|_|dS)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 <>. 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)editwintext_reset)selfr&rrr__init__1s zCodeContext.__init__cCs$d|_d|_d|_d|_dg|_dS)N)rF)contextcell00t1 topvisibleinfor)rrrr(Fs  zCodeContext._resetcCstjdddddd|_dS)z!Load class variables from config. extensionsr$Zmaxlinesint)typedefaultN)rZ GetOption context_depth)clsrrrreloadMs zCodeContext.reloadcCs>|jdurz |j|jWn tyYnwd|_dSdS)zCancel scheduled events.N)r0r' after_cancelrr3rrr__del__Ts   zCodeContext.__del__Nc Cst|jdur|jj|jjf}d}d}|D]0}||jjur|n|}||j|d7}||j|d7}||j|d7}qt |jjddd||t dd}|_| | | d|j||jddtd ttd }t|jj|d d |_|jjddtd d } n|jd|_|jd|_|j|j|d} |jjdd| dddS)aToggle 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. Nrpadxborderr+disabled)heightwidthZhighlightthicknessr>r?Zreliefstatez)rowcolumnZsticky linenumber backgroundZbgZHideZShowZoptionsz *ode*ontextz Code Context)ZmenuindexZlabelbreak)r.r&r'Z text_frameZ grid_infoZ pack_infoZtkZgetintZcgetrr update_fontupdate_highlight_colorsZbind jumptoline timer_eventZgridrr GetHighlight CurrentThemerr/Zdestroyr<r0r(Zupdate_menu_label) r)eventZwidgetsr>r?Zwidgetr2r.line_number_colorsZ menu_statusrrrtoggle_code_context_event]sV       z%CodeContext.toggle_code_context_eventr+rc Cs|dksJg}t}t||ddD]=}|j|d|d}t|\}} } ||krO|}| dvr6|d7}| rI||krI||krI|||| | f||krOnq|||fS)azReturn 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. rr+r,.0z.end)r r)rranger'getr#appendreverse) r)new_topvisibleZstopline stopindentlines lastindentZlinenumrr!r'r"rrr get_contexts" zCodeContext.get_contextcCsb|jd}|j|kr dS|j|kr2|||j\}}|jdd|kr1|jd=|jdd|ks$n6|jddd}|jdd|krX|jdd}|jd=|jdd|ksD|||jddd|\}}|j|||_dd|j|j dD}|drdnd}t|||jd<d |jd <|j d d |j d d ||dd|jd <dS)aLUpdate 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+rcSsg|]}|dqS)r).0xrrr sz3CodeContext.update_code_context..rAZnormalrCz1.0end r@) r&Z getlinenor1r]r2extendr9rr.deleteinsertjoin)r)rYr[r\rZZcontext_stringsZ showfirstrrrupdate_code_contexts:     zCodeContext.update_code_contextcCsz |jdWdStyIt|j}|dkrd}ntt|jd}td||jd}|j||d}|j |d| YdSw)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+rfrrTN) r.rIrrr2r5floatmaxr9r'Zyviewrh)r)rQr[ZnewtopZ contextlineoffsetrrrrMs  zCodeContext.jumptolinecCs.|jdur||j|j|j|_dSdS)z>Event on editor text widget triggered every UPDATEINTERVAL ms.N)r.rhr'ZafterUPDATEINTERVALrNr0r3rrrrNs zCodeContext.timer_eventcCs,|jdurt|jdd}||jd<dSdS)NmainZ EditorWindowfont)r.rZGetFontr')r)rnrrrrKs zCodeContext.update_fontcCsj|jdurttd}|d|jd<|d|jd<|jdur3ttd}|jj|dddSdS)Nr.rGZ foregroundrFrH)r.rrOrPr/Zconfig)r)ZcolorsrRrrrrLs   z#CodeContext.update_highlight_colors)N)r+r)__name__ __module__ __qualname____doc__rlr*r( classmethodr;r=rSr]rhrMrNrKrLrrrrr$-s  8 ( r$__main__)rmz"idlelib.idle_test.test_codecontextr^F) verbosityexit)rrresysrrZtkinterrrrZtkinter.constantsrrZidlelib.configrrcompilerr#r$r;roZunittestrmrrrrs   [