\d,dZddlZddlmZddlmZmZmZddl m Z m Z ddl m Z hdZejdfd Zd ZGd d Zeed krddlmZeddddSdS)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> ifdeffortryelifelsewithasyncclasswhileexceptfinallyz ^(\s*)(\w*)cP||S)z>Extract the beginning whitespace and first word from codeline.)matchgroups)codelinecs $..\python\lib\idlelib\codecontext.pyget_spaces_firstwordrs 778   # # % %%ct|\}}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)rspaces firstwordindentopeners r get_line_infor'sb -X66FI [[F 8}}(6"2c"9"9 , & 49F 8V ##rcneZdZdZdZdZdZedZdZ ddZ dd Z d Z dd Z dZdZdZdS) CodeContextz,Display block context above the edit window.dcT||_|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,s r__init__zCodeContext.__init__1s%" L  rcNd|_d|_d|_d|_dg|_dS)N)rF)contextcell00t1 topvisibleinfor/s rr.zCodeContext._resetFs,  '( rcBtjddddd|_dS)z!Load class variables from config. extensionsr)maxlinesint)typedefaultN)r GetOption context_depth)clss rreloadzCodeContext.reloadMs0%.|]/979;;;rc|j: |j|jn#t$rYnwxYwd|_dSdS)zCancel scheduled events.N)r7r- after_cancelrr:s r__del__zCodeContext.__del__Ts] 7   &&tw////    DGGG  s ) 66Nc |j|jj|jjf}d}d}|D]}||jjur|n|}||j|dz }||j|dz }||j|dz }t|jjddd||tdx}|_| | | d|j||ddt" t%jt%jd }t+|jj|d  |_|jddt" d } nu|jd|_|jd|_|j|j|d} |jdd| ddS)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. Nrpadxborderr2disabled)heightwidthhighlightthicknessrJrKreliefstatez)rowcolumnsticky linenumber backgroundbgHideShowoptionsz *ode*ontextz Code Context)menuindexlabelbreak)r5r,r- text_frame grid_info pack_infotkgetintcgetrr update_fontupdate_highlight_colorsbind jumptoline timer_eventgridrr GetHighlight CurrentThemerr6destroyrGr7r.update_menu_label) r/eventwidgetsrJrKwidgetr9r5line_number_colors menu_statuss rtoggle_code_context_eventz%CodeContext.toggle_code_context_event]s <  l')@@GDF! B B!T\%666((***#--// ((f666 ((V)<)<===&)**6;;x+@+@AAA%) '#$&z &K&K&K KGdl        ( ( * * * LL,do > > >       LLQqL 6 6 6!)!6x7L7N7N7C"E"E  7+=l+KMMMDK K  1T  : : : KK L " " "DL K   ! ! !DK I " "47 + + + KKMMM K &&I]0;-J-J-J ' L L Lwrr2rcj|dksJg}t}t||dz dD]v}|j|d|d}t |\}} } ||kr:|}| dvr|dz }| r%||kr||kr|||| | f||krnw|||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. rr2r3.0z.end)rr)r!ranger-getr'appendreverse) r/new_topvisiblestopline stopindentlines lastindentlinenumrr%r-r&s r get_contextzCodeContext.get_contexts!|||| ^XaZ<<  Gy}}^^^5E5E5EFFH#0#:#: FD& ""# ---!OJBg666Z;O;OLL'64!@AAA++E j  rc|jd}|j|krdS|j|krU|||j\}}|jdd|kr|jd=|jdd|kn|jdddz}|jdd|kr2|jdd}|jd=|jdd|k2|||jdddz|\}}|j|||_d|j|j dD}|drdnd}t||z |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,0Nr3r2rcg|] }|d S)).0xs r z3CodeContext.update_code_context..sIIIA1Q4IIIrrMnormalrQz1.0end rL) r, getlinenor8rr9extendrCr r5deleteinsertjoin)r/r|rrr~context_strings showfirsts rupdate_code_contextzCodeContext.update_code_contexts//77 ?n , , F ?^ + + $ 0 015!B!B E:)B-"j00IbM)B-"j002q)A-J)B-"n44!Yr]1- IbM)B-"n44!% 0 0152q1A!1C1;!=!= E: (IID4F3F3G3G)HIII(+2AA !$_!5!5 !A X ( W E5))) E499_YZZ-H#I#IJJJ * Wrc |jddS#t$rt|j}|dkrd}net t |jd}td||jz dz }|j||zd}|j |d| YdSwxYw)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.firstr2rrrwN) r5r]rr r9r>floatmaxrCr-yviewr)r/rprnewtop contextlineoffsets rrizCodeContext.jumptolines ' L  { + + + + + ' ' ' NNEzz"% (:(:8(D(D"E"EFF Q(: :;;a?6K#78; IOOvMMM * * *  $ $ & & & & & & 'sB=CCc|j@||j|j|j|_dSdS)z>Event on editor text widget triggered every UPDATEINTERVAL ms.N)r5rr-afterUPDATEINTERVALrjr7r:s rrjzCodeContext.timer_eventsE < #  $ $ & & &iood&94;KLLDGGG $ #rcb|j'tj|jdd}||jd<dSdS)Nmain EditorWindowfont)r5r GetFontr-)r/rs rrfzCodeContext.update_fonts: < ##DIv~FFD#'DL  $ #rc@|jFtjtjd}|d|jd<|d|jd<|jItjtjd}|j|ddSdS)Nr5rV foregroundrUrW)r5r rlrmr6config)r/colorsrss rrgz#CodeContext.update_highlight_colorss < #*8+@+B+BINNF)/ )=DL &)/ )=DL & ; "!)!6x7L7N7N7C"E"E  K  "4\"B  C C C C C # "r)N)r2r)__name__ __module__ __qualname____doc__rr0r. classmethodrErHrurrrirjrfrgrrrr)r)-s22N*)));;[; 6666p!!!!<&+&+&+P''''*MMM ((( D D D D Drr)__main__)rz"idlelib.idle_test.test_codecontextrF) verbosityexit)rresysrr!tkinterrrrtkinter.constantsrridlelib.configr r"compilerr'r)rErunittestrrrrrsK   ######))))))))))********######>>> &0RZ%?%?&&&& $ $ $WDWDWDWDWDWDWDWDt  zD -GGGGGGr