
    G@d(                     p    d Z ddlZddlmZ dZdZddZ	 	 ddZ	 	 dd
Zd Z	e
dk    r e	             dS dS )zJ
String search and match utilities useful when filtering a list of texts.
    Nto_text_stringTc                     d | D             }d                     |          }d                    |          }|r t          j        |t          j                  }nt          j        |          }|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                     g | ]
}|d k    |S )  ).0chars     ;lib/python3.11/site-packages/spyder/utils/stringmatching.py
<listcomp>z$get_search_regex.<locals>.<listcomp>&   s    8884DCKK$KKK    z.*z({0}))joinformatrecompile
IGNORECASE)queryignore_case
regex_textregexpatterns        r   get_search_regexr      sj    & 985888J:&&JOOJ''E $*UBM22*U##Nr   {}c                     t          |d          }|t          f} s|S t           d           t          |d          }|r(                                  |                                }|r,t           |          }t	          j        ||          }||S n1d}	d}
d} fd|                    d	          D             } fd
|                    d	          D             }t          |          st          |          r|                               }|t                     z   }||z  }|
                     |	t                     z  d          }|d|         |                    |||                   z   ||d         z   }t          |          r|dz  }nt          |          r|dz  }nd |D             }|rd |D             }n
|dd         }||                     d                   z  }|dd         } D ][}|dk    rS||v rO|                    |          }|                    ||                   ||<   |	||<   d	g|dz   z  ||dz   d         z   }\d                    |          }g }t          |          D ]+\  }}|d	k    r	||	k    r|
}n|}|                    |           ,d                    |          }t!          t#          dt                     dz                       D ]0}|t                     |                    |	|z            z
  dz  z  }1|                    |	          }d|v r|                    d           d|v |                    |	          s
|dd         }|                    |	          s
|dd         }|D ]8}||                    d	          dz  z  }||                    |
          dz  z  }9|||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                 :    g | ]}t          |d           k    S )r   r   r   r
   wordr   s     r   r   z$get_search_score.<locals>.<listcomp>s   s;     7 7 7 tg F F FF 7 7 7r   r   c                     g | ]}|v S r	   r	   r"   s     r   r   z$get_search_score.<locals>.<listcomp>u   s    FFF4$FFFr      d   c                     g | ]}|S r	   r	   r
   ls     r   r   z$get_search_score.<locals>.<listcomp>   s    ///!A///r   c                 6    g | ]}|                                 S r	   )lowerr(   s     r   r   z$get_search_score.<locals>.<listcomp>   s     @@@1QWWYY@@@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templateoriginal_choiceresultr   rsepletscoreexact_wordspartial_words	pos_startpos_endtextenriched_text	temp_textr   r4   patterns_textinew_chartemppats   `                         r   get_search_scorerR   3   s   P %Vg>>>O/F  57333EFW555F   J("5kBBBIgv&&9M  7 7 7 7#)<<#5#57 7 7FFFF6<<3E3EFFF{ 	*s=11 	*E**I#e**,GYE>>%SZZ;;D+JYJ7	'0A BCCD)*M { 	IQJEE 	ISLEE 0////D $@@@@@		 G	 Y__U1X...E !GM I I3;;49#4#4%OOD11E+3??4;+G+GM%("%DK!%	 2YuQwxx5H HI//  	+ 	+GAtt||  ****//%3u::>2233 	F 	FAc%jj=#6#6s1u#=#==vEEEE""3''TkkKK Tkk'',, 	8D%%c** 	9D 	( 	(CSYYt__U**ESYYs^^C''EEM500r   Fc                    |                      dd          } t          | |          }g }|D ]}t          j        ||          }	| r|	rt	          | ||d|          }
n| r||t
          f}
n
||t          f}
|r'|
d         t
          k    r|                    |
           p|                    |
           |rt          |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   r>   r?   r   c                     | d         S )Nr   r	   )rows    r   <lambda>z#get_search_scores.<locals>.<lambda>   s
    #b' r   )key)	r3   r   r   r.   rR   r-   NO_SCOREr6   sorted)r   choicesr   r?   
valid_onlysortr   resultsr=   rB   rA   s              r   get_search_scoresr^      s   6 MM#r""Euk22GG # #Igv&& 	4Q 	4%eV27(L L LFF  4 &/: &(3 	#bz_,,v&&&NN6"""" ;&9&9:::Nr   c                      d} g d}t          d||           }t          d|| dd          }|D ]}t          |           t          d           |D ]}t          |           d 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 panepreferencesquit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 objectblockcomment
breakpointz	close allzcode completionzconditional breakpoint	configurecopyz	copy line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 filerc   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 filepastezprevious charzprevious cursor positionzprevious linezprevious wordprintzre-run last script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 commentunblockcommentundoyankzrun profilerzrun analysislay)r?   T)r?   r[   r\   
)r^   rl   )r?   namesabrB   s        r   testrw      s    H5 5 5E> 	%<<<A%d#	% 	% 	%A   a 
$KKK  a r   __main__)T)TTr   )Tr   FF)__doc__r   spyder.py3compatr   r-   rX   r   rR   r^   rw   __name__r	   r   r   <module>r|      s     
			 + + + + + +   @ CG"B1 B1 B1 B1J BF-23 3 3 3l, , ,\ zDFFFFF r   