3 § fú(ã @sºdZddlZddlmZddlZddlmZmZm Z ddl m Z dddd d d d d ddddh Z dZ dZejdƒfdd„Zdd„ZGdd„dƒZejƒedkr¶ddlmZedddd �dS)!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)ÚTOPÚXÚSUNKEN)ÚidleConfÚclassÚdefÚelifÚelseÚexceptÚfinallyÚforÚifÚtryÚwhileÚwithÚasyncédièz ^(\s*)(\w*)cCs|j|ƒjƒS)z>Extract the beginning whitespace and first word from codeline.)ÚmatchÚgroups)ÚcodelineÚc©rú8/opt/alt/python36/lib64/python3.6/idlelib/codecontext.pyÚget_spaces_firstwordsrcCsFt|ƒ\}}t|ƒ}t|ƒ|ks,||dkr0t}|tko:|}|||fS)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)rZspacesZ firstwordÚindentÚopenerrrrÚ get_line_infos   r!c@sbeZdZdZdd„Zedd„ƒZdd„Zdd d „Zdd d„Z dd„Z ddd„Z dd„Z dd„Z dS)Ú CodeContextz,Display block context above the edit window.cCs^||_|j|_|jd|_tj|_d|_d|_dg|_|jj t |j ƒ|_ |jj t |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.textfont is the editor window font. 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. ÚfontNérÚFéÿÿÿÿ)rr&r%F)ÚeditwinÚtextÚtextfontr"ÚcolorsÚ contextcolorsÚcontextÚ topvisibleÚinfoÚafterÚUPDATEINTERVALÚ timer_eventÚt1ÚCONFIGUPDATEINTERVALÚconfig_timer_eventÚt2)Úselfr'rrrÚ__init__0s zCodeContext.__init__cCs,tjdddddd�|_tjtjƒdƒ|_dS) z!Load class variables from config.Ú extensionsr"ZmaxlinesÚinté)ÚtypeÚdefaultr,N)rZ GetOptionÚ context_depthZ GetHighlightZ CurrentThemer*)ÚclsrrrÚreloadMszCodeContext.reloadc Cs2y |jj|jƒ|jj|jƒWn YnXdS)zCancel scheduled events.N)r(Z after_cancelr2r5)r6rrrÚ__del__Ts zCodeContext.__del__Nc Csà|jsÌ|jj|jjf}d}d}xP|D]H}||jj|jƒdƒ7}||jj|jdƒƒ7}||jj|jdƒƒ7}q$Wtj |jj |j |j d|j ddd||t dd� |_|jjd |jƒ|jjttd |jjd �n|jjƒd |_d S)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. rÚpadxÚborderÚ backgroundÚ foregroundr$Údisabled) r#ZbgZfgÚheightÚwidthrArBZreliefÚstatezF)ZsideZfillÚexpandZbeforeNÚbreak)r,r'r(Z text_frameZtkZgetintZ pack_infoZcgetÚtkinterZTextÚtopr)r+rZbindÚ jumptolineZpackrrZdestroy)r6ÚeventZwidgetsrArBZwidgetrrrÚtoggle_code_context_event\s*    z%CodeContext.toggle_code_context_eventr$rc Cs¦g}t}xŒt||ddƒD]x}|jj|›d�|›d�ƒ}t|ƒ\}} } ||kr|}| dkrb|d7}| rˆ||krˆ||krˆ|j||| | fƒ||krPqW|jƒ||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. r$z.0z.endr r r&)r r )rÚranger(Úgetr!ÚappendÚreverse) r6Únew_topvisibleZstoplineÚ stopindentÚlinesÚ lastindentZlinenumrrr(r rrrÚ get_context‚s zCodeContext.get_contextcCsZt|jjdƒjdƒdƒ}|j|kr(dS|j|krf|j||jƒ\}}x€|jdd|krb|jd=qFWn`|jddd}x,|jdd|kr¤|jdd}|jd=qzW|j||jddd|ƒ\}}|jj|ƒ||_dd„|j|j d…Dƒ}|d�rdnd}t |ƒ||j d<d |j d <|j j d d ƒ|j j d d j ||d…ƒƒd|j d <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,0Ú.rNr$cSsg|] }|d‘qS)ér)Ú.0Úxrrrú ¿sz3CodeContext.update_code_context..rFZnormalrHz1.0ÚendÚ rEr&r&r&r&r&r&r&)r9r(ÚindexÚsplitr-rXr.Úextendr=rr,ÚdeleteÚinsertÚjoin)r6rTrVrWrUZcontext_stringsZ showfirstrrrÚupdate_code_context s0       zCodeContext.update_code_contextcCspt|jƒ}|dkrd}n:tt|jjdƒƒƒ}td||jƒd}|j||d}|jj |›d�ƒ|j ƒdS)z+Show clicked context line at top of editor.r$rdrz.0N) rr.r9Úfloatr,r`Úmaxr=r(Zyviewrf)r6rNrVZnewtopZ contextlineÚoffsetrrrrMÈs zCodeContext.jumptolinecCs$|jr|jƒ|jjt|jƒ|_dS)z>Event on editor text widget triggered every UPDATEINTERVAL ms.N)r,rfr(r/r0r1r2)r6rrrr1ÖszCodeContext.timer_eventcCsv|jd}|jr`||jks&tj|jkr`||_tj|_|j|jd<|jd|jd<|jd|jd<|jjt|jƒ|_ dS)zDEvent on editor text widget triggered every CONFIGUPDATEINTERVAL ms.r#rCrDN) r(r,r)r"r*r+r/r3r4r5)r6Z newtextfontrrrr4Üs   zCodeContext.config_timer_event)N)r$r)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__r7Ú classmethodr?r@rOrXrfrMr1r4rrrrr"-s  & ( r"Ú__main__)Úmainz"idlelib.idle_test.test_codecontextrZF)Ú verbosityÚexit)rmÚreÚsysrrrKZtkinter.constantsrrrZidlelib.configrrr0r3Úcompilerr!r"r?rjZunittestrprrrrÚ s"  =