B c»Ú¤†ô6ã@sÄddlZddlZddlZddlZddlmZddlmZdZdd„Z dd„Z e  e ƒej ¡Z e  d ej ¡Zd d „ZGd d „d eƒZdd„ZedkrÀddlmZeddddddlmZeeƒdS)éN)ÚidleConf)Ú DelegatorFcCsd|d |¡dS)z9Return a named group pattern matching list of alternates.z(?P<%s>ú|ú))Újoin)ÚnameZ alternates©rú6/opt/alt/python37/lib64/python3.7/idlelib/colorizer.pyÚany sr c Cs¦dtdtjƒd}dd„ttƒDƒ}dtd|ƒd}tddgƒ}d }|d }|d }|d }|d }td||||gƒ} |d|d|d| dtddgƒS)Nz\bÚKEYWORDcSs(g|] }| d¡s|tjkrt|ƒ‘qS)Ú_)Ú startswithÚkeywordÚkwlistÚstr)Ú.0rrrr ú s zmake_pat..z([^.'\"\\#]\b|^)ÚBUILTINÚCOMMENTz#[^\n]*z(?i:r|u|f|fr|rf|b|br|rb)?z'[^'\\\n]*(\\.[^'\\\n]*)*'?z"[^"\\\n]*(\\.[^"\\\n]*)*"?z''''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?z'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?ÚSTRINGrÚSYNCz\n)r rrÚdirÚbuiltins) ÚkwZ builtinlistÚbuiltinÚcommentZ stringprefixZsqstringZdqstringZ sq3stringZ dq3stringÚstringrrr Úmake_pats rz\s+(\w+)cCs^t ¡}t |d¡}t |d¡d}t |d¡}|j|d|d||d|d|dddS)zcSet color options of Text widget. If ColorDelegator is used, this should be called first. ZnormalZcursorÚ foregroundZhiliteÚ background)rrZinsertbackgroundZselectforegroundZselectbackgroundZinactiveselectbackgroundN)rÚ CurrentThemeÚ GetHighlightZconfig)ÚtextÚthemeZ normal_colorsZ cursor_colorZ select_colorsrrr Ú color_config"s  r$c@s€eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zdd d„Z ddd„Z ddd„Z dd„Z d dd„Z dd„Zdd„Zdd„Zd S)!ÚColorDelegatora-Delegator for syntax highlighting (text coloring). Instance variables: delegate: Delegator below this one in the stack, meaning the one this one delegates to. Used to track state: after_id: Identifier for scheduled after event, which is a timer for colorizing the text. allow_colorizing: Boolean toggle for applying colorizing. colorizing: Boolean flag when colorizing is in process. stop_colorizing: Boolean flag to end an active colorizing process. cCs*t |¡| ¡t|_t|_| ¡dS)N)rÚ__init__Ú init_stateÚprogÚidprogÚ LoadTagDefs)Úselfrrr r&Gs  zColorDelegator.__init__cCsd|_d|_d|_d|_dS)z1Initialize variables that track colorizing state.NTF)Úafter_idÚallow_colorizingÚstop_colorizingÚ colorizing)r+rrr r'NszColorDelegator.init_statecCs\|jdk r| d¡t ||¡|dk rL| ¡| d|j¡| dd¡n d|_d|_ dS)a‚Set the delegate for this instance. A delegate is an instance of a Delegator class and each delegate points to the next delegator in the stack. This allows multiple delegators to be chained together for a widget. The bottom delegate for a colorizer is a Text widget. If there is a delegate, also start the colorizing process. Nz<>z1.0ÚendTF) ÚdelegateZunbindrÚ setdelegateÚ config_colorsZbindÚtoggle_colorize_eventÚ notify_ranger.r-)r+r1rrr r2Us   zColorDelegator.setdelegatecCs4x$|j ¡D]\}}|j|f|Žq W| d¡dS)z4Configure text widget tags with colors from tagdefs.ZselN)ÚtagdefsÚitemsZ tag_configureZ tag_raise)r+ÚtagZcnfrrr r3lszColorDelegator.config_colorsc Cszt ¡}t |d¡t |d¡t |d¡t |d¡t |d¡dddœdddœt |d¡t |d ¡d œ |_trvtd |jƒdS) z.Create dictionary of tag names to text colors.rrrrZ definitionN)rrÚerrorÚhit) rr rrÚ DEFINITIONrÚTODOZERRORr:r6)rr r!r6ÚDEBUGÚprint)r+r#rrr r*rs      zColorDelegator.LoadTagDefsNcCs6| |¡}|j |||¡| ||dt|ƒ¡dS)z:Insert chars into widget at index and mark for colorizing.z+%dcN)Úindexr1Úinsertr5Úlen)r+r?ÚcharsZtagsrrr r@„s zColorDelegator.insertcCs&| |¡}|j ||¡| |¡dS)z5Delete chars between indexes and mark for colorizing.N)r?r1Údeleter5)r+Úindex1Úindex2rrr rCŠs zColorDelegator.deletecCsb| d||¡|jr$tr tdƒdS|jrr/r.r-ÚafterÚ recolorize)r+rDrErrr r5szColorDelegator.notify_rangecCs8|jr(|j}d|_trtdƒ| |¡d|_d|_dS)Nzcancel scheduled recolorizerFT)r,r=r>Ú after_cancelr-r.)r+r,rrr Úclosežs zColorDelegator.closecCsˆ|jr(|j}d|_trtdƒ| |¡|jrF|jrFtr@tdƒd|_|j |_|jrl|jsl| d|j¡|_tr„td|jr~dp€dƒd S) zÕToggle colorizing on and off. When toggling off, if colorizing is scheduled or is in process, it will be cancelled and/or stopped. When toggling on, colorizing will be scheduled. Nzcancel scheduled recolorizerzstop colorizingTrFzauto colorizing turnedZonZoffÚbreak) r,r=r>rJr-r/r.rHrI)r+Zeventr,rrr r4§s"    z$ColorDelegator.toggle_colorize_eventcCsÌd|_|jstrtdƒdS|js2tr.tdƒdS|jrHtrDtdƒdSzHd|_d|_trbtdƒt ¡}|  ¡t ¡}trŽtd||ƒWdd|_X|jrÈ|  d d ¡rÈtr¸td ƒ|  d |j ¡|_dS) aSTimer event (every 1ms) to colorize text. Colorizing is only attempted when the text widget exists, when colorizing is toggled on, and when the colorizing process is not already running. After colorizing is complete, some cleanup is done to make sure that all the text has been colorized. Nz no delegatezauto colorizing is offzalready colorizingFTz colorizing...z %.3f secondsr<z1.0zreschedule colorizingrF) r,r1r=r>r-r/r.ÚtimeZ perf_counterÚrecolorize_mainÚ tag_nextrangerHrI)r+Zt0Zt1rrr rI¿s8 zColorDelegator.recolorizecCsòd}xæ| d|¡}|sP|\}}| d||¡| d|¡}|rJ|d}nd}d}|}d}d}xˆ|sè|}| |d|¡}t|dd ƒ}d| |d ¡k}| ||¡} | s²d Sx|jD]} | | ||¡qºW|| }|j  |¡} x¬| rxŽ|   ¡  ¡D]~\} } | rú|   | ¡\}}|  | |d ||d |¡| d krú|j ||¡}|rú|  d¡\}}|  d|d ||d |¡qúW|j  ||  ¡¡} qæWd| |d ¡kr°|}d}nd}|sÆ|  d|¡| ¡|jrbtrâtdƒd SqbWqWd S)z(Evaluate text and apply colorizing tags.z1.0r<rrFÚFz+%d lines linestartéédz-1cNz+%dc)ÚdefÚclassr;zcolorizing stopped)rOÚ tag_removeZ tag_prevranger?ÚminZ tag_namesÚgetr6r(ÚsearchÚ groupdictr7ÚspanrGr)Úmatchr0Úupdater.r=r>)r+ÚnextÚitemÚheadÚtailrBZ lines_to_getÚokZmarkÚliner8ÚmÚkeyÚvalueÚaÚbZm1rrr rNásl           zColorDelegator.recolorize_maincCs"x|jD]}| |dd¡qWdS)zRemove all colorizing tags.z1.0r0N)r6rU)r+r8rrr Ú removecolors#s zColorDelegator.removecolors)N)N)N)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__r&r'r2r3r*r@rCr5rKr4rIrNrhrrrr r%7s    "Br%c Cs¾ddlm}m}ddlm}||ƒ}| d¡tt| ¡  d¡dd…ƒ\}}| d|d|d f¡d }||d d }|j dd d|  d|¡|  ¡t |ƒ||ƒ} tƒ} |  | ¡dS)Nr)ÚToplevelÚText)Ú PercolatorzTest ColorDelegatorú+rFz 700x250+%d+%déé¯abif True: int ('1') # keyword, builtin, string, comment elif False: print(0) else: float(None) if iF + If + IF: 'keyword matching must respect case' if'': x or'' # valid string-keyword no-space combinations async def f(): await g() # All valid prefixes for unicode and byte strings should be colored. 'x', '''x''', "x", """x""" r'x', u'x', R'x', U'x', f'x', F'x' fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x' b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x', rB'x',Rb'x',RB'x' # Invalid combinations of legal characters should be half colored. ur'x', ru'x', uf'x', fu'x', UR'x', ufr'x', rfu'x', xf'x', fx'x' Zwhite)rZboth)ÚexpandZfillr@)ZtkinterrmrnZidlelib.percolatorroÚtitleÚmapÚintZgeometryÚsplitZpackr@Z focus_setr$r%Z insertfilter) ÚparentrmrnroÚtopÚxÚyÚsourcer"ÚpÚdrrr Ú_color_delegator)s     rÚ__main__)Úmainz idlelib.idle_test.test_colorizerrQ)Ú verbosityÚexit)Úrun)rrÚrerMZidlelib.configrZidlelib.delegatorrr=r rÚcompileÚSr(r)r$r%rriZunittestrZidlelib.idle_test.htestr„rrrr Ús&  s"