
    G@drX                        d dl Z d dlmZ d dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZ ej         Z! G d d          Z" G d d          Z# G d d          Z$ G d d          Z% G d d          Z& G d d          Z' G d de          Z(d Z)e*dk    r e)             dS dS )    N)Signal)QHBoxLayoutQInputDialogQLabel)on_conf_change_)PluginMainWidget)ONResultsBrowser)MAX_PATH_HISTORYSearchInComboBox)SearchThread)regexp_error_msg)QStylePaletteSpyderPalette)PatternComboBoxc                   *    e Zd ZdZdZdZdZdZdZdZ	dS )	FindInFilesWidgetActionsfind_actionmax_results_actiontoggle_case_actiontoggle_exclude_case_action togle_use_regex_on_exlude_actiontoggle_more_options_action!toggle_use_regex_on_search_actionN)
__name__
__module____qualname__Find
MaxResults
ToggleCaseToggleExcludeCaseToggleExcludeRegexToggleMoreOptionsToggleSearchRegex     Nlib/python3.11/site-packages/spyder/plugins/findinfiles/widgets/main_widget.pyr   r   %   s9        D%J &J4;4;r(   r   c                       e Zd ZdZdZdS )FindInFilesWidgetToolbarsexclude_toolbarlocation_toolbarN)r   r   r   ExcludeLocationr'   r(   r)   r+   r+   2   s        G!HHHr(   r+   c                       e Zd ZdZdS )$FindInFilesWidgetMainToolbarSectionsmain_sectionNr   r   r   Mainr'   r(   r)   r1   r1   7           DDDr(   r1   c                       e Zd ZdZdS )'FindInFilesWidgetExcludeToolbarSectionsr2   Nr3   r'   r(   r)   r7   r7   ;   r5   r(   r7   c                       e Zd ZdZdS )(FindInFilesWidgetLocationToolbarSectionsr2   Nr3   r'   r(   r)   r9   r9   ?   r5   r(   r9   c                   &    e Zd ZdZdZdZdZdZdZdS )FindInFilesWidgetToolbarItemspattern_combosearch_in_labelexclude_labelexclude_pattern_combostretcher_1search_in_comboN)	r   r   r   SearchPatternComboSearchInLabelExcludeLabelExcludePatternCombo
Stretcher1SearchInCombor'   r(   r)   r;   r;   C   s.        (%M"L1J%MMMr(   r;   c                       e Zd ZdZdZdej         dZ ed          Z	 e
eeeee          Z	  e
            Z	  e
            Z	 d$ fd	Zd Zd	 Zd
 Zd Z ed          d             Z ed          d             Zd Zd Zd Zd Zd%dZed             Zed             Zed             Z d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(d" Z)d&d#Z* xZ+S )'FindInFilesWidgetz$
    Find in files main widget.
    Tzbackground-color:;zRegular expression errorNc                     t                                          |||                                dt                                          dt                                          d           _                             d           _        d  _        d _	        d  _
        d  _                             dd          }                     dg           }                     d	          }t          |t          t          f          s|g}t          |t          t          f          s|g}t          |t          t          f          s|g}t           |t           j        
           _         j                                                            t+          d                     t-          t+          d                     _        t           j         j        _        t-          t+          d                     _        t           j         j        _        t9          | t           j        
           _        t           |t+          d          t           j        
           _         tC            j                             d                     _"         j        #                     j                                         dd           }|=|dk    r7| j         $                                k     r j         %                    |                                dd           } j        &                    |           tO                      }	|	(                     j"                    )                    |	            j        j*        +                     j*                    j        j,        +                     fd            j         j,        +                     fd            j"        j-        +                     j-                    j"        j.        +                     j.                    j"        j.        +                     j/                    j        j0        +                     j1                   d S )N)parent
text_color
hist_limitsupported_encodingsFsearch_text path_historyexclude)id_zWrite text to searchz
Search in:zExclude:zExclude patternmax_results)rM   rU   exclude_indexr   search_in_indexc                 ,                                     S Nfindvalidselfs    r)   <lambda>z,FindInFilesWidget.__init__.<locals>.<lambda>   s    $))++ r(   c                 ,                                     S rY   rZ   r\   s    r)   r_   z,FindInFilesWidget.__init__.<locals>.<lambda>   s    diikk r(   )2super__init__set_confMAIN_TEXT_COLORr   get_confrM   rO   search_threadrunningmore_options_actionextras_toolbar
isinstancelisttupler   r;   rB   search_text_editlineEditsetPlaceholderTextr	   r   r=   rC   IDr>   rD   r   rG   path_selection_comborE   exclude_pattern_editr   result_browsersetBuddycountsetCurrentIndexset_current_searchpath_indexr   	addWidget	setLayoutsig_redirect_stdio_requestedconnectr]   sig_edit_goto_requestedsig_max_results_reached_stop_and_reset_threadsig_resized_update_size)r^   namepluginrL   rP   rR   rS   exclude_idxrW   layout	__class__s   `         r)   rb   zFindInFilesWidget.__init__v   s   vf555lO444l$4555 --55#'==1F#G#G !#' "mmM266}}^R88--	**+e}55 	(&-K'D%=11 	 iG,u66 	*(>L !0-@!
 !
 !
 	&&((;;$%%	' 	' 	'  &aoo66"?"M#AjMM22 = J$4$-;%= %= %=! %4  -A	%
 %
 %
! -m44
 
 
 	##D$=>>>mmOT::#q(8(8$";"A"A"C"CCC%55kBBB--(94@@!>>	 	 	 ,---v 	!>FF-	/ 	/ 	/#++,E,E,E,EFFF!'//0I0I0I0IJJJ3;;(	* 	* 	*3;;(	* 	* 	*3;;'	) 	) 	))11$2CDDDDDr(   c                      t          d          S )Nr    r   r^   s    r)   	get_titlezFindInFilesWidget.get_title   s    yyr(   c                     | j         S rY   )rm   r   s    r)   get_focus_widgetz"FindInFilesWidget.get_focus_widget   s    $$r(   c           
      @                          t          j        t          d          t          d                               d          d                     d          d           _                              t          j        t          d          t          d                               d	          d                     d
          d
           _                              t          j	        t          d          t          d                               d           j
        d           _                              t          j        t          d          t          d                               d          d                     d          d           _                              t          j        t          d          t          d                               d	          d                     d          d           _                              t          j        t          d          t          d                               d          d                     d          d           _                              t          j        t          d          t          d          d fd	           _                                         } j         j         j         j         j        fD ]$}                     ||t.          j                   %                     t4          j                   _                                         }t<          j        |_          j!         j"         j        |fD ])}                     | j        tF          j                   *                     t4          j$                  } j%         j&        fD ]$}                     ||tN          j                   % (                                } )                     j        |           d S )NzRegular expressionzUse regular expressionsregexTsearch_text_regexp)texttipicontoggledinitialoptionzCase sensitivezCase sensitive searchformat_letter_casecase_sensitivez&Find in fileszSearch textr[   F)r   r   r   	triggeredregister_shortcutexclude_regexpzExclude case sensitiveexclude_case_sensitiveShow advanced optionsoptions_moremore_optionszSet maximum number of resultsc                 ,                                     S rY   )set_max_results)xr^   s    r)   r_   z)FindInFilesWidget.setup.<locals>.<lambda>  s    T%9%9%;%; r(   )r   r   r   )toolbarsection)menurY   )*create_actionr   r&   r	   create_iconre   search_regexp_actionr#   case_actionr    r[   r   r$   exclude_regexp_actionr"   exclude_case_actionr%   rh   r!   set_max_results_actionget_main_toolbarrm   add_item_to_toolbarr1   r4   create_toolbarr+   r.   ri   create_stretcherr;   rF   rp   r>   rr   r7   r/   r=   rq   r9   get_options_menuadd_item_to_menu)r^   r   item	stretcherr-   r   s   `     r)   setupzFindInFilesWidget.setup   sB   $($6$6$6'((+,,!!'**MM"677' %7 %
 %
!  --$6#$$)**!!"677MM"233# . 
 
  --$)#$$-  !!&))i# . 
 
 &*%7%7$7'((+,,!!'**MM"233# &8 &
 &
" $(#5#5$/+,,*++!!"677MM":;;+ $6 $
 $
  $(#5#5$6*++)**!!.11MM.11! $6 $
 $
  '+&8&8$/233122;;;;;	 '9 '
 '
# ''))*D,<.0@-/ 	 	D $$<A %     #11%-/ / ))++	4?	')B/< 	 	D$$+?D %      ..%.0 0)4+DE 	 	D$$(@E %     $$&&' 	 	
 	
 	
 	
 	
r(   c                     | j                             |                     | j        rdnd                     | j        r:| j        r5| j                            | j                                                   d S d S d S )Nstopr[   )r   setIconr   rg   ri   rh   
setVisible	isCheckedr   s    r)   update_actionsz FindInFilesWidget.update_actions<  s      !1!1l.FF"0 "0 	1 	1 	1  	64#; 	6**(22446 6 6 6 6	6 	6 	6 	6r(   r   )r   c                    | j                             | j                                                   |r%|                     d          }t          d          }n$|                     d          }t          d          }| j        r| j                            |           | j        r6| j        	                    |           | j        
                    |           d S d S )Noptions_lesszHide advanced optionsr   r   )rr   setMinimumWidthrm   widthr   r	   ri   r   rh   r   
setToolTip)r^   valuer   r   s       r)   on_more_options_updatez(FindInFilesWidget.on_more_options_updateD  s    !11!''))	+ 	+ 	+  	-##N33D+,,CC##N33D+,,C 	2**5111# 	5$,,T222$//44444	5 	5r(   rU   c                 :    | j                             |           d S rY   )rs   r   )r^   r   s     r)   on_max_results_updatez'FindInFilesWidget.on_max_results_updateW  s    ++E22222r(   c                 ^    | j                             |                                           d S rY   )rr   r   r   )r^   sizeold_sizes      r)   r   zFindInFilesWidget._update_size]  s'    !11$**,,?????r(   c                 r   | j                                         }| j                                        }| j                                        }| j                                                            d           | j                                                            d           | j                            d           | j                            d           t          | j        
                                          }|sdS 	 |                    d          dfg}nW# t          $ rJ g }| j        D ]=}	 |                    |                    |          |f           .# t          $ r Y :w xY wY nw xY wt          | j        
                                          }|sd |D             }| j                                        }| j                                        }	|s4d |                    d          D             }
d                    |
          }|rt+          |          }|ri| j                                        }|                    | j                   | j        dz   t          |          z   }| j                            |           dS t1          j        |          }|rt+          |d	         d	                   }|rg| j                                                            | j                   | j        dz   t          |          z   }| j                            |           dS d
 |D             }|	|||||fS )z%
        Get search options.
        rQ   Nzutf-8c                 R    g | ]$}|d                                           |d         f%S r      )lower).0r   s     r)   
<listcomp>z2FindInFilesWidget._get_options.<locals>.<listcomp>  s-    BBBDd1gmmootAw/BBBr(   c                     g | ]@}|                                 d k    t          j        |                                           AS )rQ   )stripfnmatch	translate)r   r   s     r)   r   z2FindInFilesWidget._get_options.<locals>.<listcomp>  sH     , , ,

** &tzz||44***r(   ,|z: r   c                 R    g | ]$}t          j        |d                    |d         f%S r   )recompile)r   r   s     r)   r   z2FindInFilesWidget._get_options.<locals>.<listcomp>  s/    AAAa"*QqT**AaD1AAAr(   )r   r   r   r   rm   rn   setStyleSheetrr   r   strcurrentTextencodeUnicodeEncodeErrorrO   appendUnicodeDecodeErrorrq   is_file_searchget_current_searchpathsplitjoinr   REGEX_INVALIDREGEX_ERRORr   r   )r^   text_re
exclude_rer   utexttextsencrS   file_searchpathitems	error_msgexclude_edittooltips                 r)   _get_optionszFindInFilesWidget._get_options`  sh    +5577/99;;
)3355 	&&((66r:::!**,,::2>>>!,,R000((,,,D)557788 	F	ll7++W56EE! 	 	 	E/  LL%,,s"3"3S!9::::)   D 	 d/;;==>> 	CBBEBBBE/>>@@(??AA 	&, ,!(s!3!3, , ,E hhuooG  		.(11I .#8AACC**4+=>>>*T1C	NNB)44W===t*W--  		B(q!55I B%..00>>&( ( (*T1C	NNB%00999tAA5AAAk7E7NKKs6   D E02*EE0
E*'E0)E**E0/E0c                                           d          } fdt           j                                                  D             } fdt           j                                                  D             } j                                        }                     d|                                d|d|                                         d|d|                                         d|| d                                         d j                                                                        d	 j                                                   dS )
zW
        Extract search options from widgets and set the corresponding option.
        rN   c                 ^    g | ])}t          j                            |                    *S r'   )r   rm   itemTextr   indexr^   s     r)   r   z5FindInFilesWidget._update_options.<locals>.<listcomp>  sH     K K K! D1::5AABB K K Kr(   c                 ^    g | ])}t          j                            |                    *S r'   )r   rr   r   r   s     r)   r   z5FindInFilesWidget._update_options.<locals>.<listcomp>  sH     K K K 1::5AABB K K Kr(   rR   rP   NrS   rV   rW   )	re   rangerm   ru   rr   rq   get_external_pathsrc   currentIndex)r^   rN   search_textsexcludesrR   s   `    r)   _update_optionsz!FindInFilesWidget._update_options  s    ]]<00
K K K K%*4+@+F+F+H+H%I%IK K KK K K K!&t'@'F'F'H'H!I!IK K K0CCEEnl333m\+:+%>???i+:+!6777nlJ;<<&@AAAT6CCEE	G 	G 	Gt8EEGG	I 	I 	I 	I 	Ir(   c                    | j                             t                     | j                                          | j                                          | j        dS | j                                         | j                                        }| 	                                 || j         
                                 |                                  |                                  dS )z5
        Current search thread has finished.
        N)rs   set_sortingr   	set_width	expandAllrf   sig_finishedemitget_resultsr~   showstop_spinnerr   )r^   	completedfounds      r)   _handle_search_completez)FindInFilesWidget._handle_search_complete  s     	''+++%%'''%%'''%F   "..00##%%%$$&&&r(   Fc                    | j         | j                                         rX|r$| j         j                            | j                   | j                                          | j                                          | j                             d           d| _         d| _        | 	                                 | 
                                 dS )z(Stop current search thread and clean-up.NF)rf   	isRunningr   
disconnectsearch_completer   wait	setParentrg   r  r   )r^   ignore_resultss     r)   r~   z(FindInFilesWidget._stop_and_reset_thread  s    )!++-- *! .&3>>,. . ."'')))"'')))((...!%Dr(   c                     | j         j        S )zReturn the current path.)rq   r   r   s    r)   r   zFindInFilesWidget.path  s     (--r(   c                     | j         j        S )z Return the current project path.)rq   project_pathr   s    r)   r  zFindInFilesWidget.project_path  s     (55r(   c                     | j         j        S )zReturn the current file path.rq   	file_pathr   s    r)   r  zFindInFilesWidget.file_path  s     (22r(   c                 B    t          j        |          | j        _        dS )z
        Set directory as current path.

        Parameters
        ----------
        directory: str
            Directory path string.
        N)ospabspathrq   r   )r^   	directorys     r)   set_directoryzFindInFilesWidget.set_directory  s     *-Y)?)?!&&&r(   c                 :    | j                             |           dS )z
        Set path as current project path.

        Parameters
        ----------
        path: str
            Project path string.
        Nrq   set_project_pathr^   r   s     r)   r  z"FindInFilesWidget.set_project_path  s!     	!22488888r(   c                 :    | j                             d           dS )z(Disable project search path in combobox.Nr  r   s    r)   disable_project_searchz(FindInFilesWidget.disable_project_search  s    !22488888r(   c                     || j         _        dS )z
        Set path as current file path.

        Parameters
        ----------
        path: str
            File path string.
        Nr  r  s     r)   set_file_pathzFindInFilesWidget.set_file_path  s     /3!+++r(   c                     |rE| j                             |           | j                                                                          | j                                          dS )a  
        Set current search text.

        Parameters
        ----------
        text: str
            Search string.

        Notes
        -----
        If `text` is empty, focus will be given to the search lineedit and no
        search will be performed.
        N)rm   add_textrn   	selectAllsetFocus)r^   r   s     r)   set_search_textz!FindInFilesWidget.set_search_text  s_      	9!**4000!**,,66888&&(((((r(   c                 h    | j         r|                                  dS |                                  dS )z
        Start/stop find action.

        Notes
        -----
        If there is no search running, this will start the search. If there is
        a search running, this will stop it.
        N)rg   r   startr   s    r)   r[   zFindInFilesWidget.find&  s0     < 	IIKKKKKJJLLLLLr(   c                 .    |                                   dS )zStop find thread.N)r~   r   s    r)   r   zFindInFilesWidget.stop4  s    ##%%%%%r(   c                 |   |                                  }|dS |                     d           | j                                        }|                                  | j                            |d                    d| j        _        d| j        _        d| _	        | 
                                 t          d|| j        |                     d                    | _        | j        j                            | j                   | j        j                            | j        j                   | j        j                            | j        j                   | j                            |            | j        j        |                                    | j                                         |                                  dS )zStart find thread.NT)r  r   rQ   rU   )r   r~   rm   r   r   rs   set_pathlongest_file_itemlongest_line_itemrg   start_spinnerr   rM   re   rf   r   r{   r  sig_file_matchappend_file_resultsig_line_matchappend_resultclear_title
initializer&  r   )r^   optionsrP   s      r)   r&  zFindInFilesWidget.start8  s   ##%%?F##4#888+7799 	 	$$WQZ00002-02- )OMM-((	
 
 	'//0LMMM)112	
 	
 	
 	)11-	
 	
 	
 	''444%%t'8'8':':;;  """r(   c                 :    | j                             |           dS )zf
        Parameters
        ----------
        path: str
            Path to add to combobox.
        N)rq   add_external_pathr  s     r)   r5  z#FindInFilesWidget.add_external_path^  s!     	!33D99999r(   c                 T    |t                     }|                    d           |                    t          d                     |                    t          d                     |                    t           j                   |                    d           |                     	                    d                     |
                    dd           |j                             fd	           |                                 dS                      d|           dS )
z
        Set maximum amount of results to add to the result browser.

        Parameters
        ----------
        value: int, optional
            Number of results. If None an input dialog will be used.
            Default is None.
        NFzMax resultszSet maximum number of results: r   rU      i'  c                 0                         d|           S )NrU   )rc   )r   r^   s    r)   r_   z3FindInFilesWidget.set_max_results.<locals>.<lambda>  s    dmmM5AA r(   )r   setModalsetWindowTitler	   setLabelTextsetInputModeIntInput
setIntStepsetIntValuere   setIntRangeintValueSelectedr{   r  rc   )r^   r   dialogs   `  r)   r   z!FindInFilesWidget.set_max_resultsg  s     =!$''F OOE"""!!!M"2"2333"C D DEEE 5666a   t}}];;<<< q%((( #++AAAAC C C KKMMMMMMM-/////r(   )NNN)FrY   ),r   r   r   __doc__ENABLE_SPINNERr   COLOR_ERROR_2r   r	   r   r   r   intr|   r   r}   rb   r   r   r   r   r   r   r   r   r   r   r  r~   propertyr   r  r  r  r  r  r  r$  r[   r   r&  r5  r   __classcell__)r   s   @r)   rI   rI   N   s         NF(CFFFM!.//K %fS#sC==$ 688L %fhh
VE VE VE VE VE VEt  % % %d
 d
 d
L6 6 6 ^>***5 5 +*5$ ^=)))3 3 *)3
@ @ @BL BL BLHI I I(  &   & . . X. 6 6 X6 3 3 X3	@ 	@ 	@	9 	9 	99 9 9	3 	3 	3) ) )(  & & &$ $ $L: : :#0 #0 #0 #0 #0 #0 #0 #0r(   rI   c                     ddl m}  ddl}ddlm} ddlm}  |            } |            }d|_        t          d|          }d|_        |	                                 |
                                 |                    dd	           |                                  | t                     |  | t                               |  |  | t                                         |  |  |  | t                                                  g}|D ]}|                    |           |                    |                                           dS )
z(
    Run Find in Files widget test.
    r   )dirnameN)	MagicMock)qapplicationfind_in_files)r   i  i  )os.pathrJ  sysunittest.mockrK  spyder.utils.qthelpersrL  CONF_SECTIONrI   _setupr   resizer  __file__r5  exitexec_)	rJ  rO  rK  rL  appplugin_mockwidgetexternal_pathsr   s	            r)   testr\    sw   
  JJJ'''''' 433333
,..C)++K.K{CCCF)F
MMOOO
LLNNN
MM#s
KKMMM!!""))**++ 1 1223344	N  ' '  &&&&HHSYY[[r(   __main__)+r   rN  r   r  r   qtpy.QtCorer   qtpy.QtWidgetsr   r   r   spyder.api.config.decoratorsr   spyder.api.translationsr	   spyder.api.widgets.main_widgetr
   2spyder.plugins.findinfiles.widgets.results_browserr   r   +spyder.plugins.findinfiles.widgets.comboboxr   r   0spyder.plugins.findinfiles.widgets.search_threadr   spyder.utils.miscr   spyder.utils.paletter   r   spyder.widgets.comboboxesr   COLOR_TEXT_1rd   r   r+   r1   r7   r9   r;   rI   r\  r   r'   r(   r)   <module>rj     s          				       < < < < < < < < < < 8 7 7 7 7 7 % % % % % % ; ; ; ; ; ;       ( ( ( ( ( ( ( ( I I I I I I . . . . . . = = = = = = = = 5 5 5 5 5 5
  ,

< 
< 
< 
< 
< 
< 
< 
<" " " " " " " "
                     & & & & & & & &|0 |0 |0 |0 |0( |0 |0 |0B  B zDFFFFF r(   