a
    	3j3                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ dZdd Z	dd Z
ee
 ejZed	ejZd
d ZG dd deZdd Zedkrd dlmZ edddd d dlmZ ee dS )    N)idleConf)	DelegatorFc                 C   s   d|  d | d S )z9Return a named group pattern matching list of alternates.z(?P<%s>|))join)nameZ
alternates r   M/www/server/python_manager/versions/3.9.10/lib/python3.9/idlelib/colorizer.pyany   s    r
   c            
      C   s   dt dtj d } dd ttD }dt d| d }t ddg}d	}|d
 }|d }|d }|d }t d||||g}	| d | d | d |	 d t ddg S )Nz\bKEYWORDc                 S   s(   g | ] }| d s|tjvrt|qS )_)
startswithkeywordkwliststr).0r   r   r   r	   
<listcomp>   s   

zmake_pat.<locals>.<listcomp>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
   r   r   dirbuiltins)
kwZbuiltinlistbuiltincommentZstringprefixZsqstringZdqstringZ	sq3stringZ	dq3stringstringr   r   r	   make_pat   s    
r   z\s+(\w+)c                 C   s^   t  }t |d}t |dd }t |d}| j|d |d ||d |d |d d dS )zcSet color options of Text widget.

    If ColorDelegator is used, this should be called first.
    ZnormalZcursor
foregroundZhilite
background)r   r   ZinsertbackgroundZselectforegroundZselectbackgroundZinactiveselectbackgroundN)r   CurrentThemeGetHighlightZconfig)textthemeZnormal_colorsZcursor_colorZselect_colorsr   r   r	   color_config&   s    r$   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dddZ
dddZdd Zd ddZdd Zdd Zdd Zd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.
    c                 C   s*   t |  |   t| _t| _|   d S )N)r   __init__
init_stateprogidprogLoadTagDefsselfr   r   r	   r&   K   s
    
zColorDelegator.__init__c                 C   s   d| _ d| _d| _d| _dS )z1Initialize variables that track colorizing state.NTF)after_idallow_colorizingstop_colorizing
colorizingr+   r   r   r	   r'   R   s    zColorDelegator.init_statec                 C   s\   | j dur| d t| | |durL|   | d| j | dd nd| _d| _	dS )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<<toggle-auto-coloring>>1.0endTF)
delegateZunbindr   setdelegateconfig_colorsbindtoggle_colorize_eventnotify_ranger/   r.   )r,   r3   r   r   r	   r4   Y   s    

zColorDelegator.setdelegatec                 C   s4   | j  D ]\}}| j|fi | q
| d dS )z4Configure text widget tags with colors from tagdefs.ZselN)tagdefsitemsZtag_configureZ	tag_raise)r,   tagZcnfr   r   r	   r5   p   s    zColorDelegator.config_colorsc                 C   sz   t  }t |dt |dt |dt |dt |dddddddt |dt |d	d
	| _trvtd| j dS )z.Create dictionary of tag names to text colors.r   r   r   r   Z
definitionN)r   r   errorhit)	r   r   r   r   
DEFINITIONr   TODOERRORr=   r9   )r   r    r!   r9   DEBUGprint)r,   r#   r   r   r	   r*   v   s    






zColorDelegator.LoadTagDefsNc                 C   s6   |  |}| j||| | ||dt|   dS )z:Insert chars into widget at index and mark for colorizing.+%dcN)indexr3   insertr8   len)r,   rD   charstagsr   r   r	   rE      s    
zColorDelegator.insertc                 C   s&   |  |}| j|| | | dS )z5Delete chars between indexes and mark for colorizing.N)rD   r3   deleter8   r,   Zindex1index2r   r   r	   rI      s    
zColorDelegator.deletec                 C   sb   |  d|| | jr$tr td dS | jr<d| _tr<td | jr^trNtd | d| j| _dS )zCMark text changes for processing and restart colorizing, if active.r?   zcolorizing already scheduledNTstop colorizingzschedule colorizing   )	tag_addr-   rA   rB   r0   r/   r.   after
recolorizerJ   r   r   r	   r8      s    zColorDelegator.notify_rangec                 C   s8   | j r(| j }d | _ trtd | | d| _d| _d S )Ncancel scheduled recolorizerFT)r-   rA   rB   after_cancelr.   r/   )r,   r-   r   r   r	   close   s    
zColorDelegator.closec                 C   s   | j r(| j }d| _ trtd | | | jrF| jrFtr@td d| _| j | _| jrl| jsl| d| j| _ trtd| jr~dnd d	S )
zToggle 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.
        NrQ   rL   TrM   zauto colorizing turnedZonZoffbreak)	r-   rA   rB   rR   r.   r0   r/   rO   rP   )r,   eventr-   r   r   r	   r7      s     

z$ColorDelegator.toggle_colorize_eventc                 C   s   d| _ | jstrtd dS | js2tr.td dS | jrHtrDtd dS zNd| _d| _trbtd t }| 	  t }trtd||   W d| _nd| _0 | jr| 
d	d
rtrtd | d| j| _ dS )aS  Timer 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 delegatezauto colorizing is offzalready colorizingFTzcolorizing...z%.3f secondsr?   r1   zreschedule colorizingrM   )r-   r3   rA   rB   r.   r0   r/   timeperf_counterrecolorize_maintag_nextrangerO   rP   )r,   t0t1r   r   r	   rP      s,    
zColorDelegator.recolorizec                 C   s  d}|  d|}|sq|\}}| d|| | d|}|rF|d nd}d}|}d}d}|s|}| |d|  }t|d d	}d| |d
 v }| ||}	|	sdS | jD ]}
| |
|| q||	 }| j	|}|r||
  D ]~\}}|r||\}}| ||d|  |d|   |dv r| j||}|r|d\}}| d|d|  |d|   q| j	|| }qd| |d
 v r|}d}nd}|s| d| |   | jrZtrtd dS qZqdS )z(Evaluate text and apply colorizing tags.r1   r?   r   rM    Fz+%d lines linestart   d   z-1cNrC   )defclassr>   zcolorizing stopped)rY   
tag_removeZtag_prevrangerD   minZ	tag_namesgetr9   r(   search	groupdictr:   spanrN   r)   matchr2   updater/   rA   rB   )r,   nextitemheadtailrG   Zlines_to_getokZmarkliner;   mkeyvalueabZm1r   r   r	   rX      sj    




zColorDelegator.recolorize_mainc                 C   s   | j D ]}| |dd qdS )zRemove all colorizing tags.r1   r2   N)r9   ra   )r,   r;   r   r   r	   removecolors'  s    
zColorDelegator.removecolors)N)N)N)N)__name__
__module____qualname____doc__r&   r'   r4   r5   r*   rE   rI   r8   rS   r7   rP   rX   rt   r   r   r   r	   r%   ;   s   


	
"?r%   c                 C   s   ddl m}m} ddlm} || }|d tt|  	ddd  \}}|d|d |d	 f  d
}||dd}|j
ddd |d| |  t| ||}	t }
|	|
 d S )Nr   )ToplevelText)
PercolatorzTest ColorDelegator+rM   z700x250+%d+%d      ab  if 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 keyword-string 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'
Zwhite)r   Zboth)expandfillrE   )tkinterry   rz   Zidlelib.percolatorr{   titlemapintZgeometrysplitpackrE   Z	focus_setr$   r%   Zinsertfilter)parentry   rz   r{   topxysourcer"   pdr   r   r	   _color_delegator-  s     
 r   __main__)mainz idlelib.idle_test.test_colorizerr]   )	verbosityexit)run)r   r   rerV   Zidlelib.configr   Zidlelib.delegatorr   rA   r
   r   compileSr(   r)   r$   r%   r   ru   Zunittestr   Zidlelib.idle_test.htestr   r   r   r   r	   <module>   s&    s"