a
    a(                     @   sX   d Z ddlZddlmZ dZdZdddZdd	d
ZdddZdd Z	e
dkrTe	  dS )zJ
String search and match utilities useful when filtering a list of texts.
    Nto_text_stringTc                 C   sD   dd | D }d |}d|}|r6t|tj}n
t|}|S )a  Returns a compiled regex pattern to search for query letters in order.

    Parameters
    ----------
    query : str
        String to search in another string (in order of character occurrence).
    ignore_case : True
        Optional value perform a case insensitive search (True by default).

    Returns
    -------
    pattern : SRE_Pattern

    Notes
    -----
    This function adds '.*' between the query characters and compiles the
    resulting regular expression.
    c                 S   s   g | ]}|d kr|qS )  ).0charr   r   :lib/python3.9/site-packages/spyder/utils/stringmatching.py
<listcomp>&       z$get_search_regex.<locals>.<listcomp>z.*z({0}))joinformatrecompile
IGNORECASE)queryignore_caseZ
regex_textZregexpatternr   r   r	   get_search_regex   s    


r   {}c                    s  t |dd}|tf} s|S t  dd t |dd}|rH   | }|rtt |d}t||}|du rp|S nd}	d}
d} fdd	|d
D } fdd	|d
D }t|st|r | }|t	  }||7 }|
 |	t	  d}|d| ||||  ||d  }t|r4|d7 }nt|rH|d7 }ndd	 |D }|rldd	 |D }n|dd }|| d 7 }|dd } D ]\}|dkr||v r||}||| ||< |	||< d
g|d  ||d d  }qd|}g }t|D ]2\}}|d
kr0||	kr0|
}n|}|| qd|}ttdt	 d D ]$}|t	 ||	|  d 7 }qb||	}d|v r|d q||	s|dd }||	s|dd }|D ]*}||d
d 7 }|||
d 7 }q|||fS )a  Returns a tuple with the enriched text (if a template is provided) and
    a score for the match.

    Parameters
    ----------
    query : str
        String with letters to search in choice (in order of appearance).
    choice : str
        Sentence/words in which to search for the 'query' letters.
    ignore_case : bool, optional
        Optional value perform a case insensitive search (True by default).
    apply_regex : bool, optional
        Optional value (True by default) to perform a regex search. Useful
        when this function is called directly.
    template : str, optional
        Optional template string to surround letters found in choices. This is
        useful when using a rich text editor ('{}' by default).
        Examples: '<b>{}</b>', '<code>{}</code>', '<i>{}</i>'

    Returns
    -------
    results : tuple
        Tuples where the first item is the text (enriched if a template was
        used) and the second item is a search score.

    Notes
    -----
    The score is given according the following precedence (high to low):

    - Letters in one word and no spaces with exact match.
      Example: 'up' in 'up stroke'
    - Letters in one word and no spaces with partial match.
      Example: 'up' in 'upstream stroke'
    - Letters in one word but with skip letters.
      Example: 'cls' in 'close up'
    - Letters in two or more words
      Example: 'cls' in 'car lost'
    utf-8encoding)r   N-xr   c                    s   g | ]} t |d dkqS )r   r   r   r   Zwordr   r   r	   r
   s   s   z$get_search_score.<locals>.<listcomp>r   c                    s   g | ]} |v qS r   r   r   r   r   r	   r
   u   r      d   c                 S   s   g | ]}|qS r   r   r   lr   r   r	   r
      r   c                 S   s   g | ]}|  qS r   )lowerr   r   r   r	   r
      r    i r   i'  )r   NOT_FOUND_SCOREr!   r   r   searchsplitanyfindlenreplacer   indexr   	enumerateappendreversedrangecountremove
startswithendswith)r   choicer   apply_regextemplateZoriginal_choiceresultr   rsepZletZscoreZexact_wordsZpartial_wordsZ	pos_startZpos_endtextZenriched_textZ	temp_textr   r*   Zpatterns_textiZnew_charZtempZpatr   r   r	   get_search_score3   s    (








"

"

r;   Fc                 C   s   |  dd} t| |}g }|D ]l}t||}	| rJ|	rJt| ||d|d}
n| rZ||tf}
n
||tf}
|r|
d tkr||
 q||
 q|rt|dd d}|S )	a  Search for query inside choices and return a list of tuples.

    Returns a list of tuples of text with the enriched text (if a template is
    provided) and a score for the match. Lower scores imply a better match.

    Parameters
    ----------
    query : str
        String with letters to search in each choice (in order of appearance).
    choices : list of str
        List of sentences/words in which to search for the 'query' letters.
    ignore_case : bool, optional
        Optional value perform a case insensitive search (True by default).
    template : str, optional
        Optional template string to surround letters found in choices. This is
        useful when using a rich text editor ('{}' by default).
        Examples: '<b>{}</b>', '<code>{}</code>', '<i>{}</i>'

    Returns
    -------
    results : list of tuples
        List of tuples where the first item is the text (enriched if a
        template was used) and a search score. Lower scores means better match.
    r   r"   F)r   r4   r5   r   c                 S   s   | d S )Nr   r   )rowr   r   r	   <lambda>   r   z#get_search_scores.<locals>.<lambda>)key)	r)   r   r   r$   r;   r#   NO_SCOREr,   sorted)r   choicesr   r5   
valid_onlysortr   Zresultsr3   r7   r6   r   r   r	   get_search_scores   s&    

rD   c                  C   s\   d} g d}t d|| d}t d|| ddd}|D ]}t| q0td |D ]}t| qJd S )Nz
<b>{0}</b>)_z
close panezdebug continuez
debug exitzdebug step intozdebug step overzdebug step returnzfullscreen modezlayout preferenceszlock unlock paneszmaximize paneZpreferencesquitZrestartzsave current layoutzswitch to breakpointszswitch to consolezswitch to editorzswitch to explorerzswitch to find_in_fileszswitch to historylogzswitch to helpzswitch to ipython_consolezswitch to onlinehelpzswitch to outline_explorerzswitch to project_explorerzswitch to variable_explorerzuse next layoutzuse previous layoutz
clear linezclear shellinspect current objectZblockcomment
breakpointz	close allzcode completionzconditional breakpointZ	configurecopyz	copy lineZcutdebugzdebug with winpdbdeletezdelete linezduplicate linezend of documentzend of linezfile list managementz	find nextzfind previousz	find textzgo to definitionz
go to linezgo to next filezgo to previous filerF   zkill next wordzkill previous wordzkill to line endzkill to line startzlast edit locationzmove line downzmove line upznew filez	next charznext cursor positionz	next linez	next wordz	open fileZpastezprevious charzprevious cursor positionzprevious linezprevious wordprintzre-run last scriptZredozreplace textzrotate kill ringrunzrun selectionzsave allzsave asz	save filez
select allzshow/hide outlinezshow/hide project explorerzstart of documentzstart of lineztoggle commentZunblockcommentZundoZyankzrun profilerzrun analysisZlay)r5   T)r5   rB   rC   
)rD   rK   )r5   namesabr7   r   r   r	   test   s    

rQ   __main__)T)TTr   )Tr   FF)__doc__r   Zspyder.py3compatr   r#   r?   r   r;   rD   rQ   __name__r   r   r   r	   <module>   s   
   
   
6.