o
   ~8F                   @   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
 Zed	Zd
d
d
d
dZdd Zdd ZG dd deZdd Zedkrfd dlmZ edddd d dlmZ ee dS 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   7/app/.heroku/python/lib/python3.10/idlelib/colorizer.pyany      r
   c                  C   s  dt dtj d } dddddtj d g d }d	}d
dddddtj d g d }dd ttD }dt d| d }t ddg}d}|d }|d }	|d }
|d }t d|
|||	g}td|||| |||t ddggtjtj	B }|S )Nz\bKEYWORDz,^[ \t]*(?P<MATCH_SOFTKW>match)\b(?![ \t]*(?:r   z[:,;=^&|@~)\]}]z\b(?:z)\bz))zB^[ \t]*(?P<CASE_SOFTKW>case)[ \t]+(?P<CASE_DEFAULT_UNDERSCORE>_\b)z+^[ \t]*(?P<CASE_SOFTKW2>case)\b(?![ \t]*(?:z_\bc                 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'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?STRINGSYNCz\n)
r
   r   r   r   dirbuiltinsrecompileDOTALL	MULTILINE)kwZmatch_softkwZcase_defaultZcase_softkw_and_patternZbuiltinlistbuiltincommentZstringprefixZsqstringZdqstringZ	sq3stringZ	dq3stringstringprogr   r   r	   make_pat   sP   

r#   z\s+(\w+)r   )ZMATCH_SOFTKWZCASE_SOFTKWZCASE_DEFAULT_UNDERSCOREZCASE_SOFTKW2c                 C   s   dd |    D S )z<Get only the non-empty named groups from an re.Match object.c                 s   s     | ]\}}|r||fV  qd S Nr   )r   kvr   r   r	   	<genexpr>N   s    z'matched_named_groups.<locals>.<genexpr>)	groupdictitems)Zre_matchr   r   r	   matched_named_groupsL   r   r*   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_configQ   s   
r1   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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 r$   )r   __init__
init_stater"   idprogLoadTagDefsselfr   r   r	   r3   v   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
colorizingr7   r   r   r	   r4   }   s   
zColorDelegator.init_statec                 C   s^   | j dur
| d t| | |dur'|   | d| j | dd dS 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:   )r8   r?   r   r   r	   r@      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)tagdefsr)   Ztag_configureZ	tag_raise)r8   tagZcnfr   r   r	   rA      s   zColorDelegator.config_colorsc                 C   s~   t  }t |dt |dt |dt |dt |dddddddt |dt |d	d
	| _tr=td| j dS 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ERRORrH   rE   )r   r-   r.   rE   DEBUGprint)r8   r0   r   r   r	   r6      s   






zColorDelegator.LoadTagDefsNc                 C   s6   |  |}| j||| | ||dt|   dS )z:Insert chars into widget at index and mark for colorizing.z+%dcN)indexr?   insertrD   len)r8   rN   charstagsr   r   r	   rO      s   
zColorDelegator.insertc                 C   s&   |  |}| j|| | | dS )z5Delete chars between indexes and mark for colorizing.N)rN   r?   deleterD   r8   Zindex1index2r   r   r	   rS      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r'td | d| j| _dS )zCMark text changes for processing and restart colorizing, if active.rJ   zcolorizing already scheduledNTstop colorizingzschedule colorizing   )	tag_addr9   rL   rM   r<   r;   r:   after
recolorizerT   r   r   r	   rD      s   zColorDelegator.notify_rangec                 C   s8   | j r| j }d | _ trtd | | d| _d| _d S )Ncancel scheduled recolorizerFT)r9   rL   rM   after_cancelr:   r;   )r8   r9   r   r   r	   close   s   

zColorDelegator.closec                 C   s   | j r| j }d| _ trtd | | | jr#| jr#tr td d| _| j | _| jr6| js6| d| j| _ trBt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.
        Nr[   rV   TrW   zauto colorizing turnedZonZoffbreak)	r9   rL   rM   r\   r:   r<   r;   rY   rZ   )r8   eventr9   r   r   r	   rC      s    

z$ColorDelegator.toggle_colorize_eventc                 C   s   d| _ | jstrtd dS | jstrtd dS | jr$tr"td dS z'd| _d| _tr1td t }| 	  t }trGtd||   W d| _nd| _w | jri| 
d	d
rktr_td | d| j| _ dS dS 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 secondsrJ   r=   zreschedule colorizingrW   )r9   r?   rL   rM   r:   r<   r;   timeperf_counterrecolorize_maintag_nextrangerY   rZ   )r8   t0t1r   r   r	   rZ      s0   
zColorDelegator.recolorizec                 C   s@  d}|  d| }r| d|d |d  | d|d }|r#|d nd}d}|}d}d}|s|}| |d|  }t|d	 d
}d| |d v }| ||}	|	sTdS | jD ]	}
| |
|| qW||	7 }| || d| |d v ry|}d}nd}|s| 	d| | 
  | jrtrtd dS |r/|  d| }s
dS dS )z(Evaluate text and apply colorizing tags.r=   rJ   r   r   rW    Fz+%d lines linestart   d   z-1cNzcolorizing stopped)rc   
tag_removeZtag_prevrangerN   minZ	tag_namesgetrE   _add_tags_in_sectionrX   updater;   rL   rM   )r8   nextZtodo_tag_rangeZsync_tag_rangeheadrQ   Zlines_to_getokZmarklinerF   r   r   r	   rb     sF   
zColorDelegator.recolorize_mainc                 C   s:   t ||}| || d|dd| d|dd dS )a  Add a tag to a given range in the text widget.

        This is a utility function, receiving the range as `start` and
        `end` positions, each of which is a number of characters
        relative to the given `head` index in the text widget.

        The tag to add is determined by `matched_group_name`, which is
        the name of a regular expression "named group" as matched by
        by the relevant highlighting regexps.
        +dcN)prog_group_name_to_tagrk   rX   )r8   startr>   ro   Zmatched_group_namerF   r   r   r	   _add_tag>  s   zColorDelegator._add_tagc           	      C   s~   | j |D ]6}t|D ]/\}}||\}}| |||| |dv r;| j|| }r;|d\}}| |||d qqdS )zParse and add highlighting tags to a given part of the text.

        `chars` is a string with the text to parse and to which
        highlighting is to be applied.

            `head` is the index in the text widget where the text is found.
        )defclassrW   rI   N)r"   finditerr*   spanrw   r5   match)	r8   rQ   ro   mr   Zmatched_textabZm1r   r   r	   rl   O  s   z#ColorDelegator._add_tags_in_sectionc                 C   s   | j D ]	}| |dd qdS )zRemove all colorizing tags.r=   r>   N)rE   ri   )r8   rF   r   r   r	   removecolors`  s   
zColorDelegator.removecolorsr$   )__name__
__module____qualname____doc__r3   r4   r@   rA   r6   rO   rS   rD   r]   rC   rZ   rb   rw   rl   r   r   r   r   r	   r2   f   s"    



	",r2   c                 C   s   ddl m}m} ddlm} ddlm} || }|d tt	| 
 ddd  \}}|
d|d	 |d
 f  ||dd}|jddd |d| |  t| ||}	t }
|	|
 d S )Nr   )ToplevelText)source)
PercolatorzTest ColorDelegatorrr   rW   z700x550+%d+%d      Zwhite)r,   Zboth)expandfillrO   )tkinterr   r   Z idlelib.idle_test.test_colorizerr   Zidlelib.percolatorr   titlemapintZgeometrysplitpackrO   Z	focus_setr1   r2   Zinsertfilter)parentr   r   r   r   topxyr/   prs   r   r   r	   _color_delegatorf  s   
 r   __main__)mainz idlelib.idle_test.test_colorizerrg   )	verbosityexit)run)r   r   r   r`   Zidlelib.configr   Zidlelib.delegatorr   rL   r
   r#   r"   r   r5   ru   r*   r1   r2   r   r   Zunittestr   Zidlelib.idle_test.htestr   r   r   r   r	   <module>   s8    1
  