B
    nb\'                 @   sL   d Z ddlZdZdZdddZddd	ZdddZdd ZedkrHe  dS )zK
String search and match utilities usefull when filtering a list of texts.
    NTc             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.7/site-packages/spyder/utils/stringmatching.py
<listcomp>%   s    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 f} s|S |r(   | }|rTt |d}t||}|dk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|r|d
7 }nt|r&|d7 }ndd |D }|rJdd |D }n|dd }|| d 7 }|dd }xd D ]\}|dkrz||krz||}|
|| ||< |	||< dg|d
  ||d
 d  }qzW d|}g }x>t|D ]2\}}|dkr||	kr|
}n|}|| qW d|}x>ttd
t d
 D ]$}|t ||	|  d 7 }qJW ||	}xd|kr|d q~W ||	s|d
d }||	s|dd }x2|D ]*}||dd 7 }|||
d 7 }qW |||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'
    )r   N-xr   c                s   g | ]} |kqS r   r   )r   word)r   r   r   r   o   s    z$get_search_score.<locals>.<listcomp>r   c                s   g | ]} |kqS r   r   )r   r   )r   r   r   r   p   s       d   c             S   s   g | ]}|qS r   r   )r   lr   r   r   r      s    c             S   s   g | ]}|  qS r   )lower)r   r   r   r   r   r      s     i r   i'  )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_score2   sz    (






$

$

r3   Fc             C   s   |  dd} t| |}g }xt|D ]l}t||}	| rL|	rLt| ||d|d}
n| r\||tf}
n
||tf}
|r|
d tkr||
 q ||
 q W |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   r,   r-   r   c             S   s   | d S )Nr   r   )rowr   r   r   <lambda>   s    z#get_search_scores.<locals>.<lambda>)key)	r!   r   r   r   r3   r   NO_SCOREr$   sorted)r   choicesr   r-   
valid_onlysortr   Zresultsr+   r/   r.   r   r   r   get_search_scores   s$    


r<   c           _   C   s   d} dddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_g_}t d`|| da}t d`|| dbdbdc}x|D ]}t| qW tdd x|D ]}t| q
W d S )eNz
<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 shellz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 filez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)r-   T)r-   r:   r;   
)r<   rB   )r-   namesabr/   r   r   r   test   sN    





rH   __main__)T)TTr   )Tr   FF)	__doc__r   r   r7   r   r3   r<   rH   __name__r   r   r   r   <module>	   s   
  
  
5.