
    JezX                     ~   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)        yy)    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	y)	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     P/usr/lib/python3/dist-packages/spyder/plugins/findinfiles/widgets/main_widget.pyr   r   %   s+    D%J &J4;4;r(   r   c                       e Zd ZdZdZy)FindInFilesWidgetToolbarsexclude_toolbarlocation_toolbarN)r   r   r   ExcludeLocationr'   r(   r)   r+   r+   2   s    G!Hr(   r+   c                       e Zd ZdZy)$FindInFilesWidgetMainToolbarSectionsmain_sectionNr   r   r   Mainr'   r(   r)   r1   r1   7       Dr(   r1   c                       e Zd ZdZy)'FindInFilesWidgetExcludeToolbarSectionsr2   Nr3   r'   r(   r)   r7   r7   ;   r5   r(   r7   c                       e Zd ZdZy)(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y)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%Mr(   r;   c                   t    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 errorc                     t         
   |||        j                  dt                j                  dt                j                  d       _         j                  d       _        d  _        d _	        d  _
        d  _         j                  dd      } j                  dg       } j                  d	      }t        |t        t        f      s|g}t        |t        t        f      s|g}t        |t        t        f      s|g}t         |t         j"                  
       _         j$                  j'                         j)                  t+        d             t-        t+        d             _        t         j0                   j.                  _        t-        t+        d             _        t         j6                   j4                  _        t9        | t         j:                  
       _        t         |t+        d      t         j>                  
       _         tC          j                   j                  d             _"         j4                  jG                   j@                          j                  dd       }|=|dk\  r8| j@                  jI                         k  r j@                  jK                  |        j                  dd       } j<                  jM                  |       tO               }	|	jQ                   jD                          jS                  |	        j<                  jT                  jW                   jT                          j$                  jX                  jW                   fd        j@                  jX                  jW                   fd        jD                  jZ                  jW                   jZ                          jD                  j\                  jW                   j\                          jD                  j\                  jW                   j^                          j$                  j`                  jW                   jb                         y )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                 $    j                         S Nfindvalidselfs    r)   <lambda>z,FindInFilesWidget.__init__.<locals>.<lambda>   s    $))+ r(   c                 $    j                         S rY   rZ   r\   s    r)   r_   z,FindInFilesWidget.__init__.<locals>.<lambda>   s    diik 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   sv   vf5lO4l$45 --5#'==1F#G !#' "mmM26}}^R8--	*+e}5&-K'D%=1iG,u6(>L !0-@@!
 	&&(;;$%	'  &ao6"?"M"M#AjM2 = J J$4$-;;%=! %4 -AA	%
! -m4
 	##D$=$=>mmOT:#q(8$";";"A"A"CC%%55kB--(94@!!>>	 ,,-v 	!!>>FF--	/##++,EF!!''//0IJ33;;((	*33;;((	*33;;''	)))11$2C2CDr(   c                     t        d      S )Nr    r   r^   s    r)   	get_titlezFindInFilesWidget.get_title   s    yr(   c                     | j                   S rY   )rm   r   s    r)   get_focus_widgetz"FindInFilesWidget.get_focus_widget   s    $$$r(   c           
           j                  t        j                  t        d      t        d       j	                  d      d j                  d      d       _         j                  t        j                  t        d      t        d       j	                  d	      d j                  d
      d
       _         j                  t        j                  t        d      t        d       j	                  d       j                  d       _         j                  t        j                  t        d      t        d       j	                  d      d j                  d      d       _         j                  t        j                  t        d      t        d       j	                  d	      d j                  d      d       _         j                  t        j                   t        d      t        d       j	                  d      d j                  d      d       _         j                  t        j$                  t        d      t        d      d fd	       _         j)                         } j*                   j                   j                   j                   j"                  fD ]$  } j-                  ||t.        j0                         &  j3                  t4        j6                         _         j;                         }t<        j>                  |_          jB                   jD                   j                  |fD ].  } j-                  | j8                  tF        j0                         0  j3                  t4        jH                        } jJ                   jL                  fD ]$  } j-                  ||tN        j0                         &  jQ                         } jS                   j&                  |       y )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                 $    j                         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   s   $($6$6$66'(+,!!'*MM"67' %7 %
!  --$66#$)*!!"67MM"23# . 
  --$))#$- !!&)ii# . 
 &*%7%7$77'(+,!!'*MM"23# &8 &
" $(#5#5$//+,*+!!"67MM":;+ $6 $
  $(#5#5$66*+)*!!.1MM.1! $6 $
  '+&8&8$//2312;	 '9 '
# '')**D,<,<..0@0@--/ 	D $$<AA % 	 #11%--/ ))+	4??	'')B)B//< 	D$$++?DD % 	  ..%..0))4+D+DE 	D$$(@EE % 	 $$&'' 	 	
r(   c                    | j                   j                  | j                  | j                  rdnd             | j                  rA| j
                  r4| j                  j                  | j
                  j                                y y y )Nstopr[   )r   setIconr   rg   ri   rh   
setVisible	isCheckedr   s    r)   update_actionsz FindInFilesWidget.update_actions<  so      !1!1llF"0 	1 4#;#;**((2246 $<r(   r   )r   c                    | j                   j                  | j                  j                                |r| j	                  d      }t        d      }n| j	                  d      }t        d      }| j                  r| j                  j                  |       | j                  r7| j                  j                  |       | j                  j                  |       y y )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3D+,C##N3D+,C**51##$$,,T2$$//4 $r(   rU   c                 :    | j                   j                  |       y rY   )rs   r   )r^   r   s     r)   on_max_results_updatez'FindInFilesWidget.on_max_results_updateW  s    ++E2r(   c                 V    | j                   j                  |j                                y rY   )rr   r   r   )r^   sizeold_sizes      r)   r   zFindInFilesWidget._update_size]  s    !!11$**,?r(   c                 4   | j                   j                         }| j                  j                         }| j                  j                         }| j                  j                         j                  d       | j                  j                         j                  d       | j                  j                  d       | j                  j                  d       t        | j                  j                               }|sy	 |j                  d      dfg}t        | j                  j                               }|s%|D cg c]  }|d   j!                         |d   f }}| j"                  j%                         }	| j"                  j'                         }
|sc|j)                  d      D cg c]8  }|j+                         dk7  r#t-        j.                  |j+                               : }}dj1                  |      }|rt3        |      }|rl| j                  j                         }|j                  | j4                         | j6                  dz   t        |      z   }| j                  j                  |       yt9        j:                  |      }|rt3        |d   d         }|rj| j                  j                         j                  | j4                         | j6                  dz   t        |      z   }| j                  j                  |       y|D cg c]  }t9        j:                  |d         |d   f! }}|
|	||||fS # t        $ rI g }| j                  D ]4  }	 |j                  |j                  |      |f       '# t        $ r Y 2w xY w Y uw xY wc c}w c c}w c c}w )	z%
        Get search options.
        rQ   Nzutf-8r      ,|z: )r   r   r   r   rm   rn   setStyleSheetrr   r   strcurrentTextencodeUnicodeEncodeErrorrO   appendUnicodeDecodeErrorlowerrq   is_file_searchget_current_searchpathsplitstripfnmatch	translatejoinr   REGEX_INVALIDREGEX_ERRORrecompile)r^   text_re
exclude_rer   utexttextsencrS   r   file_searchpathr   items	error_msgexclude_edittooltipr   s                    r)   _get_optionszFindInFilesWidget._get_options`  s2    ++557//99;
))335 	&&(66r:!!**,::2>!!,,R0((,D))5578	ll7+W56E d//;;=><ABDd1gmmotAw/BEB//>>@((??A!(s!3,

* &&tzz|4 ,E , hhuoG (1I#88AAC**4+=+=>**T1C	NB))44W=**W- (q!5I%%..0>>&&(**T1C	NB%%009;@Aa"**QqT*AaD1AAk7E7NKK] " 	E// LL%,,s"3S!9:) 	 C,6 BsH   >L6 ;N%=N$N6N"M43N4	N =N?N  NNc                    | j                  d      }t        | j                  j                               D cg c]&  }t	        | j                  j                  |            ( }}t        | j                  j                               D cg c]&  }t	        | j                  j                  |            ( }}| j                  j                         }| j                  d|       | j                  d|d|        | j                  d|d|        | j                  d|| d        | j                  d| j                  j                                | j                  d| j                  j                                yc c}w c c}w )zW
        Extract search options from widgets and set the corresponding option.
        rN   rR   rP   NrS   rV   rW   )re   rangerm   ru   r   itemTextrr   rq   get_external_pathsrc   currentIndex)r^   rN   indexsearch_textsexcludesrR   s         r)   _update_optionsz!FindInFilesWidget._update_options  sY    ]]<0
%*4+@+@+F+F+H%IK! D11::5AB K K "'t'@'@'F'F'H!IK 11::5AB K K00CCEnl3m\+:%>?i+:!67nlJ;<&@AT66CCE	Gt88EEG	IKKs   +E7	+E<c                    | j                   j                  t               | j                   j                          | j                   j	                          | j
                  y| j                  j                          | j
                  j                         }| j                          || j                   j                          | j                          | j                          y)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     	''+%%'%%'% ""..0##%$$&r(   c                    | j                   | j                   j                         re|r/| j                   j                  j                  | j                         | j                   j                          | j                   j                          | j                   j                  d       d| _         d| _        | j                          | j                          y)z(Stop current search thread and clean-up.NF)rf   	isRunningr   
disconnectr   r   wait	setParentrg   r   r   )r^   ignore_resultss     r)   r~   z(FindInFilesWidget._stop_and_reset_thread  s    )!!++-!&&33>>446""'')""'')((.!%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55r(   c                 .    | j                   j                  S )zReturn the current file path.rq   	file_pathr   s    r)   r  zFindInFilesWidget.file_path  s     ((222r(   c                 L    t        j                  |      | j                  _        y)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                   j                  |       y)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8r(   c                 :    | j                   j                  d       y)z(Disable project search path in combobox.Nr  r   s    r)   disable_project_searchz(FindInFilesWidget.disable_project_search  s    !!2248r(   c                 &    || j                   _        y)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                     |rC| j                   j                  |       | j                   j                         j                          | j                   j	                          y)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  sH     !!**40!!**,668&&(r(   c                 ^    | j                   r| j                          y| j                          y)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&  s     <<IIKJJLr(   c                 $    | j                          y)zStop find thread.N)r~   r   s    r)   r   zFindInFilesWidget.stop4  s    ##%r(   c                    | j                         }|y| j                  d       | j                  j                         }| j	                          | j
                  j                  |d          d| j
                  _        d| j
                  _        d| _	        | j                          t        d|| j                  | j                  d            | _        | j                  j                  j!                  | j"                         | j                  j$                  j!                  | j
                  j&                         | j                  j(                  j!                  | j
                  j*                         | j
                  j-                  |        | j                  j.                  | j                           | j                  j1                          | j3                          y)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   ##%?##4#8++779 	 	$$WQZ002-02- )OOMM-(	
 	''//0L0LM))1122	
 	))11--	
 	''4%%%t'8'8':;  "r(   c                 :    | j                   j                  |       y)zf
        Parameters
        ----------
        path: str
            Path to add to combobox.
        N)rq   add_external_pathr  s     r)   r+  z#FindInFilesWidget.add_external_path^  s     	!!33D9r(   c                     |t               }|j                  d       |j                  t        d             |j	                  t        d             |j                  t         j                         |j                  d       |j                   j                  d             |j                  dd       |j                  j                   fd	       |j                          y j                  d|       y)
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                 (    j                  d|       S )NrU   )rc   )r   r^   s    r)   r_   z3FindInFilesWidget.set_max_results.<locals>.<lambda>  s    dmmM5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3"C DE 5 56a t}}];< q%( ##++AC KKM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  r+  r   __classcell__)r   s   @r)   rI   rI   N   sD    N'(C(C'DAFM./K %S#sC=$ 8L %h
VEt%d
L6 >*5 +5$ =)3 *3
@BLHI(&& . . 6 6 3 3	@	99	3)(&$L:#0r(   rI   c                     ddl m}  ddl}ddlm} ddlm}  |       } |       }d|_        t        d|      }d|_        |j                          |j                          |j                  dd	       |j                           | t               |  | t                     |  |  | t                           |  |  |  | t                                g}|D ]  }|j                  |        |j                  |j!                                y)
z(
    Run Find in Files widget test.
    r   )dirnameN)	MagicMock)qapplicationfind_in_files)r   i  i  )os.pathr@  sysunittest.mockrA  spyder.utils.qthelpersrB  CONF_SECTIONrI   _setupr   resizer   __file__r+  exitexec_)	r@  rE  rA  rB  appplugin_mockwidgetexternal_pathsr   s	            r)   testrR    s    
  ' 4
.C+K.K{CF)F
MMO
LLN
MM#s
KKM!")*+ 1234	N  '  &' HHSYY[r(   __main__)+r   rD  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   rR  r   r'   r(   r)   <module>r`     s      	  < < 8 % ;( I . = 5
  ,,

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