3  f( @sdZddlZddlmZddlZddlmZmZm Z ddl m Z dddd d d d d ddddh Z dZ dZejdfddZddZGdddZejedkrddlmZedddd 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)TOPXSUNKEN)idleConfclassdefelifelseexceptfinallyforiftrywhilewithasyncdiz ^(\s*)(\w*)cCs|j|jS)z>Extract the beginning whitespace and first word from codeline.)matchgroups)codelinecr8/opt/alt/python36/lib64/python3.6/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   r!c@sbeZdZdZddZeddZddZdd d Zdd dZ ddZ dddZ ddZ ddZ 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. fontNrF)rr&r%F)editwintexttextfontr"colors contextcolorscontext topvisibleinfoafterUPDATEINTERVAL timer_eventt1CONFIGUPDATEINTERVALconfig_timer_eventt2)selfr'rrr__init__0s zCodeContext.__init__cCs,tjdddddd|_tjtjd|_dS) z!Load class variables from config. extensionsr"Zmaxlinesint)typedefaultr,N)rZ GetOption context_depthZ GetHighlightZ CurrentThemer*)clsrrrreloadMszCodeContext.reloadc Cs2y |jj|j|jj|jWn 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|jd7}||jj|jd7}||jj|jd7}q$Wtj |jj |j |j d|j ddd||t dd |_|jjd |j|jjttd |jjd n|jjd |_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. rpadxborder background foregroundr$disabled) r#ZbgZfgheightwidthrArBZreliefstatezF)ZsideZfillexpandZbeforeNbreak)r,r'r(Z text_frameZtkZgetintZ pack_infoZcgettkinterZTexttopr)r+rZbind jumptolineZpackrrZdestroy)r6eventZwidgetsrArBZwidgetrrrtoggle_code_context_event\s*    z%CodeContext.toggle_code_context_eventr$rc Cs|dks tg}t}xt||ddD]x}|jj|d|d}t|\}} } ||kr&|}| dkrn|d7}| r||kr||kr|j||| | f||kr&Pq&W|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. rr$z.0z.endr r r&)r r )AssertionErrorrranger(getr!appendreverse) r6new_topvisibleZstopline stopindentlines lastindentZlinenumrrr(r rrr get_contexts zCodeContext.get_contextcCsZt|jjdjdd}|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 dD}|drdnd}t |||j d<d |j d <|j j d d |j j d d j ||dd|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|] }|dqS)r).0xrrr sz3CodeContext.update_code_context..rFZnormalrHz1.0end rEr&r&r&r&r&r&r&)r9r(indexsplitr-rYr.extendr=rr,deleteinsertjoin)r6rUrWrXrVZcontext_stringsZ showfirstrrrupdate_code_contexts0       zCodeContext.update_code_contextcCspt|j}|dkrd}n:tt|jjd}td||jd}|j||d}|jj |d|j dS)z+Show clicked context line at top of editor.r$rerz.0N) rr.r9floatr,ramaxr=r(Zyviewrg)r6rNrWZnewtopZ contextlineoffsetrrrrMs zCodeContext.jumptolinecCs$|jr|j|jjt|j|_dS)z>Event on editor text widget triggered every UPDATEINTERVAL ms.N)r,rgr(r/r0r1r2)r6rrrr1szCodeContext.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 newtextfontrrrr4s   zCodeContext.config_timer_event)N)r$r)N)__name__ __module__ __qualname____doc__r7 classmethodr?r@rOrYrgrMr1r4rrrrr"-s  & ( r"__main__)mainz"idlelib.idle_test.test_codecontextr[F) verbosityexit)rnresysrrrKZtkinter.constantsrrrZidlelib.configrrr0r3compilerr!r"r?rkZunittestrqrrrr s"  =