
    Je(                     d    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	        yy)zJ
String search and match utilities useful when filtering a list of texts.
    N)to_text_stringc                     | D cg c]
  }|dk7  s	| }}dj                  |      }dj                  |      }|r&t        j                  |t        j                        }|S t        j                  |      }|S c c}w )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.
     z.*z({0}))joinformatrecompile
IGNORECASE)queryignore_casechar
regex_textregexpatterns         =/usr/lib/python3/dist-packages/spyder/utils/stringmatching.pyget_search_regexr      sq    & $)84DCK$8J8:&JOOJ'E**UBMM2 N **U#N 9s
   
A7A7c                    t        |d      }|t        f}| s|S t        | d      } t        |d      }|r | j                         } |j                         }|r(t        | |      }t	        j
                  ||      }||S d}	d}
d}|j                  d      D cg c]  }| t        |d      k(   }}|j                  d      D cg c]  }| |v  }}t        |      st        |      rc|j                  |       }|t        |       z   }||z  }|j                  | |	t        |       z  d	      }|d| |j                  |||       z   ||d z   }t        |      r|d	z  }nt        |      r|d
z  }n|D cg c]  }| }}|r|D cg c]  }|j                          }}n|dd }||j                  | d         z  }|dd }| D ]L  }|dk7  s	||v s|j                  |      }|j                  ||         ||<   |	||<   dg|d	z   z  ||d	z   d z   }N dj                        }g }t              D ]%  \  }}|dk7  r||	k7  r|
}n|}|j                  |       ' dj                  |      }t!        t#        d	t        |       d	z               D ](  }|t        |       |j%                  |	|z        z
  dz  z  }* |j                  |	      }d|v r|j'                  d       d|v r|j)                  |	      s|d	d }|j+                  |	      s|dd }|D ]0  }||j%                  d      dz  z  }||j%                  |
      d
z  z  }2 |fS c c}w c c}w c c}w c c}w )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'
    zutf-8)encoding)r   N-xr   r      d    i r   i'  )r   NOT_FOUND_SCORElowerr   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wordexact_wordspartial_words	pos_startpos_endtextenriched_textl	temp_textr   r#   patterns_textinew_chartemppats                               r   get_search_scorerC   3   s   P %Vg>O/F 573EFW5F"5kBIIgv&9M $*<<#57 tg FF 7 739<<3EF4$FF{s=1E*I#e*,GYE>>%SZ;D+JY7	' BCD)*M {QJESLE  //!A/D/0?@1QWWY@	@ G	 Y__U1X..E !GM I3;49#4%OOD1E+3??4;+GM%("%DK!%	 2YuQwx5H HII /  	+GAtt|  *	+ /%3u:>23 	FAc%j=#6#6s1u#==vEEE	F ""3'TkKK Tk'',8D%%c*9D 	(CSYYt_U**ESYYs^C''E	( M500E7F( 0@s   MM1	MMc                 X   | j                  dd      } t        | |      }g }|D ]t  }t        j                  ||      }	| r|	rt	        | ||d|      }
n| r
||t
        f}
n	||t        f}
|r|
d   t
        k7  sR|j                  |
       d|j                  |
       v |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    )rows    r   <lambda>z#get_search_scores.<locals>.<lambda>   s
    #b'     )key)	r"   r   r	   r   rC   r   NO_SCOREr%   sorted)r   choicesr   r.   
valid_onlysortr   resultsr,   r1   r0   s              r   get_search_scoresrQ      s    6 MM#r"Euk2GG #IIgv&Q%eV27(LF  &/: &(3bz_,v&NN6"#" &9:NrI   c                      d} g d}t        d||       }t        d|| dd      }|D ]  }t        |        t        d       |D ]  }t        |        y )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 filerV   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.   rN   rO   
)rQ   r_   )r.   namesabr1   s        r   testrj      sj    H5E> 	%<A%d#	%A  a 
$K arI   __main__)T)TT{})Trl   FF)__doc__r	   spyder.py3compatr   r   rK   r   rC   rQ   rj   __name__rF   rI   r   <module>rp      s]    
 +@ CG"B1J BF-23l,\ zF rI   