B
    nôb\¼  ã               @   s¼   d 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 dd	lmZ dd
lmZmZ ddlmZ ddlmZ G dd„ deeƒZdd„ Zedkr¸eƒ  dS )zFind in Files Pluginé    N)ÚQApplication)ÚSignalÚSlotÚQt)ÚQKeySequence)Ú_)Úget_edit_extensions)Úgetcwd_or_home)Úicon_manager)Úcreate_actionÚMENU_SEPARATOR)ÚFindInFilesWidget)ÚSpyderPluginMixinc               @   s´   e Zd ZdZdZeeeƒZee	ƒZ
eeeeƒZee	ƒZd!dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zeƒ dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd"dd „ZdS )#ÚFindInFileszFind in files DockWidgetZfind_in_filesNc                sÊ   ˆ   d¡}ˆ   d¡ˆ _ˆ   d¡}‡ fdd„|D ƒ}|ˆ j7 }ˆ   d¡}ˆ   d¡}ˆ   dd ¡}ˆ   d	¡}ˆ   d
¡}ˆ   d¡}	ˆ   dg ¡}
t ˆ |||||||||	|
¡ t ˆ |¡ ˆ  ¡  ˆ j ˆ j¡ d S )NÚsupported_encodingsÚsearch_text_samplesÚsearch_textc                s   g | ]}|ˆ j kr|‘qS © )r   )Ú.0Ztxt)Úselfr   ú9lib/python3.7/site-packages/spyder/plugins/findinfiles.pyú
<listcomp>,   s    z(FindInFiles.__init__.<locals>.<listcomp>Úsearch_text_regexpÚexcludeÚexclude_idxÚexclude_regexpÚmore_optionsÚcase_sensitiveÚpath_history)	Z
get_optionr   r   Ú__init__r   Zinitialize_pluginÚtoggle_visibilityÚconnectÚtoggle)r   Úparentr   r   r   r   r   r   r   r   r   r   )r   r   r   (   s*    







zFindInFiles.__init__c             C   s   | j r| j  |¡ dS )zToggle widget visibilityN)Ú
dockwidgetÚ
setVisible)r   Ústater   r   r   r"   D   s    zFindInFiles.togglec             C   s   | j  tƒ ¡ dS )zRefresh search directoryN)Úfind_optionsZset_directoryr	   )r   r   r   r   Ú
refreshdirI   s    zFindInFiles.refreshdirc             C   s   | j  |¡ dS )zRefresh current project pathN)r'   Úset_project_path)r   Úpathr   r   r   r)   M   s    zFindInFiles.set_project_pathc             C   s   | j  |¡ dS )z)Get path of current opened file in editorN)r'   Zset_file_path)r   r*   r   r   r   Úset_current_opened_fileQ   s    z#FindInFiles.set_current_opened_filec             C   s   | j  ¡  dS )zRefresh current project pathN)r'   Zdisable_project_search)r   r   r   r   Úunset_project_pathU   s    zFindInFiles.unset_project_pathc             C   sl   t  ¡ }| js$| j d¡ | j ¡  d}y| ¡ r:| ¡ }W n tk
rP   Y nX |  	|¡ |rh|  
¡  dS )zFind in files callbackTÚ N)r   ZfocusWidgetZismaximizedr$   r%   Zraise_Zhas_selected_textZget_selected_textÚAttributeErrorZset_search_textÚfind)r   ÚwidgetÚtextr   r   r   Úfindinfiles_callbackY   s    

z FindInFiles.findinfiles_callbackc             C   s   |   ¡  t | ¡ dS )zRSwitch to plugin
        This method is called when pressing plugin's shortcut keyN)r2   r   Úswitch_to_plugin)r   r   r   r   r3   l   s    zFindInFiles.switch_to_pluginc             C   s   t dƒS )zReturn widget titlezFind in files)r   )r   r   r   r   Úget_plugin_titles   s    zFindInFiles.get_plugin_titlec             C   s   | j jS )zq
        Return the widget to give focus to when
        this plugin's dockwidget is raised on top-level
        )r'   r   )r   r   r   r   Úget_focus_widgetw   s    zFindInFiles.get_focus_widgetc             C   s   g S )z*Return a list of actions related to pluginr   )r   r   r   r   Úget_plugin_actions~   s    zFindInFiles.get_plugin_actionsc          	   C   sØ   | j  | ¡ | j | j jj¡ | j | j j¡ | j jj	 | j
¡ | j jj | j¡ | j jj | j¡ | j jj | j¡ t| tdƒt d¡| jt| jƒtjtdƒd}| j  jt|g7  _| j  jt|g7  _|  
¡  dS )z'Register plugin in Spyder's main windowz&Find in filesZfindfzSearch text in multiple files)ÚiconZ	triggeredÚshortcutÚcontextZtipN)ÚmainZadd_dockwidgetÚ	edit_gotor!   ZeditorÚloadÚredirect_stdioZredirect_internalshell_stdioZworkingdirectoryZrefresh_findinfilesr(   ZprojectsZsig_project_loadedr)   Zsig_project_closedr,   Zopen_file_updater+   r   r   Úimar7   r3   r   r8   r   ZWidgetShortcutZsearch_menu_actionsr   Zsearch_toolbar_actions)r   Zfindinfiles_actionr   r   r   Úregister_plugin‚   s$    

zFindInFiles.register_pluginc             C   s   dS )zRefresh widgetNr   )r   r   r   r   Úrefresh_plugin™   s    zFindInFiles.refresh_pluginFc             C   sÀ   |   ¡  | jjdd}|dk	r¼|\}}}}}}}	}
d}|d|… }|d|… }|
| d… }
|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d	|¡ |  d
|¡ |  d|	¡ |  d|
¡ dS )z3Perform actions before parent main window is closedT)Zto_saveNé   r   r   r   r   r   r   r   r   )Zclosing_widgetr'   Zget_optionsZ
set_option)r   Z
cancelableZoptionsr   Ztext_rer   r   Z
exclude_rer   r   r   Z
hist_limitr   r   r   Úclosing_plugin   s"    zFindInFiles.closing_plugin)N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZCONF_SECTIONr   ÚstrÚobjectZsig_option_changedÚboolr    Úintr;   r=   r   r"   r(   r)   r+   r,   r   r2   r3   r4   r5   r6   r?   r@   rB   r   r   r   r   r       s(   

r   c              C   s2   ddl m}  | ƒ }tƒ }| ¡  t | ¡ ¡ d S )Nr   )Úqapplication)Úspyder.utils.qthelpersrK   r   ZshowÚsysÚexitZexec_)rK   Zappr0   r   r   r   Útestµ   s
    rO   Ú__main__)rF   rM   Zqtpy.QtWidgetsr   Zqtpy.QtCorer   r   r   Z
qtpy.QtGuir   Zspyder.config.baser   Zspyder.config.utilsr   Zspyder.utils.miscr	   Zspyder.utilsr
   r>   rL   r   r   Zspyder.widgets.findinfilesr   Zspyder.pluginsr   r   rO   rC   r   r   r   r   Ú<module>   s     