B Qx @sdZddlZddlmZddlZddlmZmZddl m Z dddd d d d d ddddh Z e dfddZ ddZGdddZeedkrddlmZedddddS)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. N)maxsize)NSEWSUNKEN)idleConfclassdefifelifelsewhilefortryexceptfinallywithasyncz ^(\s*)(\w*)cCs||S)z>Extract the beginning whitespace and first word from codeline.)matchgroups)codelinecr8/opt/alt/python37/lib64/python3.7/idlelib/codecontext.pyget_spaces_firstwordsrcCsFt|\}}t|}t||ks,||dkr0t}|tko:|}|||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)rZspacesZ firstwordindentopenerrrr get_line_infos   rc@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__.szCodeContext.__init__cCs$d|_d|_d|_d|_dg|_dS)N)rF)contextcell00t1 topvisibleinfo)r%rrrr$Cs zCodeContext._resetcCstjdddddd|_dS)z!Load class variables from config. extensionsr Zmaxlinesint)typedefaultN)rZ GetOption context_depth)clsrrrreloadJszCodeContext.reloadcCs>|jdk r:y|j|jWntjk r2YnXd|_dS)zCancel scheduled events.N)r,r# after_canceltkinterTclError)r%rrr__del__Qs  zCodeContext.__del__Nc Cs~|jdkr(|jj|jjf}d}d}xh|D]`}||jjkrB|n|}||j|d7}||j|d7}||j|d7}q*Wt j |jjddd||t dd}|_| | |d|j||jddtd ttd }t j|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 background)bgZHideZShowZoptionsz* Code Contextz Code Context)ZmenuindexZlabelbreak)r*r"r#Z text_frameZ grid_infoZ pack_infoZtkZgetintZcgetr8ZTextr update_fontupdate_highlight_colorsZbind jumptoline timer_eventZgridrr GetHighlight CurrentThemeZFramer+Zdestroyr7r,r$Zupdate_menu_label) r%eventZwidgetsr;r<Zwidgetr.r*line_number_colorsZ menu_statusrrrtoggle_code_context_eventZsJ       z%CodeContext.toggle_code_context_eventr'rc Cs|dks tg}t}xt||ddD]x}|j|d|d}t|\}} } ||kr&|}| dkrn|d7}| r||kr||kr|||| | f||kr&Pq&W|||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(z.0z.end)r r )AssertionErrorrranger#getrappendreverse) r%new_topvisibleZstopline stopindentlines lastindentZlinenumrrr#rrrr get_contexts zCodeContext.get_contextcCsJ|jd}|j|krdS|j|krX|||j\}}x|jdd|krT|jd=q8Wn`|jddd}x,|jdd|kr|jdd}|jd=qlW|||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..r>Znormalr@z1.0end r=) r"Z getlinenor-rZr.extendr4rr*deleteinsertjoin)r%rVrXrYrWZcontext_stringsZ showfirstrrrupdate_code_contexts0        zCodeContext.update_code_contextcCsy|jdWntjk rt|j}|dkr:d}n:tt|jd}td||j d}|j||d}|j |d| YnXdS)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'rcrz.0N) r*rFr8r9rr.r0floatmaxr4r#Zyviewre)r%rNrXZnewtopZ contextlineoffsetrrrrJs zCodeContext.jumptolinecCs*|jdk r&||j|j|j|_dS)z>Event on editor text widget triggered every UPDATEINTERVAL ms.N)r*rer#ZafterUPDATEINTERVALrKr,)r%rrrrKs zCodeContext.timer_eventcCs(|jdk r$t|jdd}||jd<dS)NmainZ EditorWindowfont)r*rZGetFontr#)r%rkrrrrHs zCodeContext.update_fontcCsf|jdk r6ttd}|d|jd<|d|jd<|jdk rbttd}|jj|dddS)Nr*rDZ foregroundrC)rE)r*rrLrMr+Zconfig)r%ZcolorsrOrrrrIs   z#CodeContext.update_highlight_colors)N)r'r)N)__name__ __module__ __qualname____doc__rir&r$ classmethodr6r:rPrZrerJrKrHrIrrrrr *s  8 ( r __main__)rjz"idlelib.idle_test.test_codecontextr[F) verbosityexit)roresysrrr8Ztkinter.constantsrrZidlelib.configrrcompilerrr r6rlZunittestrjrrrr s  [