a
    a&                     @   sh   d 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mZ G dd	 d	eZdS )
z
Editor Switcher manager.
    N)_)CONF)ima)shorten_pathsget_file_icon)
SymbolKindSYMBOL_KIND_ICONc                   @   s   e Zd ZdZdZdZdZedfddZdd	 Z	d
d Z
dd Z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d  Zd!S )#EditorSwitcherManagerz
    Switcher instance manager to handle base modes for an Editor.

    Symbol mode -> '@'
    Line mode -> ':'
    Files mode -> ''
    @: ZEditorc                 C   s0   || _ || _|| _|| _|| _d| _|   dS )a  
        'get_codeeditor' and 'get_editorstack' params should be callables
        to get the current CodeEditor or EditorStack instance as needed.
        As an example:
            current_codeeditor = get_codeditor()
            current_editorstack = get_editorstack()
        N)_plugin	_switcher_editor_editorstack_section_current_linesetup_switcher)selfZpluginZswitcher_instanceZget_codeeditorZget_editorstacksection r   Clib/python3.9/site-packages/spyder/plugins/editor/utils/switcher.py__init__#   s    	zEditorSwitcherManager.__init__c                 C   s|   | j | jtd | j | jtd | j j| j | j j| j	 | j j
| j | j j| j | j j| j dS )z!Setup switcher modes and signals.z
Go to LinezGo to Symbol in FileN)r   Zadd_mode	LINE_MODEr   SYMBOL_MODEZsig_mode_selectedZconnecthandle_switcher_modesZsig_item_selectedhandle_switcher_selectionZsig_text_changedhandle_switcher_textZsig_rejectedhandle_switcher_rejectionZsig_item_changedhandle_switcher_item_change)r   r   r   r   r   5   s    z$EditorSwitcherManager.setup_switcherc                 C   s>   || j kr|   n&|| jkr(|   n|| jkr:|   dS )z%Handle switcher for registered modes.N)r   create_symbol_switcherr   create_line_switcher
FILES_MODEcreate_editor_switcher)r   moder   r   r   r   A   s    




z+EditorSwitcherManager.handle_switcher_modesc              	   C   s   | j td |  }dd |jD }dd |jD }t||}t|jD ]v\}}|j}t	|}t
|}	t|| dkr|| }nt|j }|d t|jk}
| j j|||	| j||
d qLdS )	z"Populate switcher with open files.z%Start typing the name of an open filec                 S   s   g | ]}|j  qS r   )filenamelower.0datar   r   r   
<listcomp>Q   s   z@EditorSwitcherManager.create_editor_switcher.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   )Znewly_createdr'   r   r   r   r*   S   s   K      )titledescriptioniconr   r)   	last_itemN)r   set_placeholder_textr   r   r)   r   	enumerater%   ospbasenamer   lendirnamer&   add_itemr   )r   editorstackpathsZsave_statusesZshort_pathsidxr)   pathr-   r/   r0   r   r   r   r#   K   s4    


z,EditorSwitcherManager.create_editor_switcherc           
      C   s   |   }|  }| | _| j  | jtd | }|j	}t
|}|j }t|}d}|j||tdd}td}	| jj||	|| j|dd dS )	z!Populate switcher with line info.zSelect linez{title} [{lines} {text}]lines)r-   r<   textz
Go to lineT)r-   r.   r/   r   r)   Zaction_itemN)r   r   get_cursor_line_numberr   r   clearr1   r   Zget_current_finfor%   r3   r4   editorZget_line_countr   formatr7   r   )
r   r@   r8   r)   r;   r-   r<   r/   Zline_template_titler.   r   r   r   r!   i   s,    



z*EditorSwitcherManager.create_line_switcherc                 C   sn  |   }|j}|dd | | _| j  | jtd |j	j
pHg }tdd}d}t|}t|dd d}|D ]}|d }|d	 }	| d
kr|	tjkr|d8 }qv|	tjkr|s|d8 }qv|	tjkr|s|d8 }qv|d d }
|
d d }|j| }d| }dj||d}tt|	d}||d d}|d |k}| jj||| j||d |d7 }qv| j  dS )z#Populate switcher with symbol info.r   zSelect symbolZoutline_explorerdisplay_variablesc                 S   s   | d d d d S )Nlocationrangestartliner   )xr   r   r   <lambda>       z>EditorSwitcherManager.create_symbol_switcher.<locals>.<lambda>)keynamekindZpythonr,   rC   rD   rE   rF    z{space}{title})r-   spaceZno_match)r-   line_number)r-   r/   r   r)   r0   N)r   languageZupdate_whitespace_countr>   r   r   r?   r1   r   Zoe_proxyinfor   getr5   sortedr&   r   ZMODULEZVARIABLEZFIELDZleading_whitespacesrA   r   r/   r   r7   r   Zsetup)r   r@   rP   Z
oe_symbolsrB   r:   Ztotal_symbolsZsymbolZsymbol_nameZsymbol_kindZsymbol_rangeZsymbol_startZ
fold_levelrN   Zformated_titler/   r)   r0   r   r   r   r       s`    





z,EditorSwitcherManager.create_symbol_switcherc                 C   s`   |  }|dkr| | n@|dkr2| || n*|dkr\| | jkr\| | | j  dS )z&Handle item selection of the switcher.r
   r   r   N)get_datasymbol_switcher_handlerline_switcher_handlerZget_sectionr   editor_switcher_handlerr   Zswitch_to_plugin)r   itemr$   search_textr)   r   r   r   r      s    
z/EditorSwitcherManager.handle_switcher_selectionc                 C   s^   |   }| j }|dkr:| j }| j| |dd n | jrZ|dkrZ|| j d| _dS )z*Handle switcher search text for line mode.r   T)visibler   N)r   r   get_modeZcurrent_itemrV   rT   r   
go_to_line)r   rY   r8   r$   rX   r   r   r   r      s    

z*EditorSwitcherManager.handle_switcher_textc                 C   s$   | j r |  }|| j  d| _ dS )z)Do actions when the Switcher is rejected.N)r   r   r\   )r   r8   r   r   r   r      s    z/EditorSwitcherManager.handle_switcher_rejectionc                 C   s@   |   }| j }|dkr<|dur<t| d }|| dS )zHandle item selection change.r
   NrO   )r   r   r[   intrT   r\   )r   Zcurrentr8   r$   rO   r   r   r   r      s
    
z1EditorSwitcherManager.handle_switcher_item_changec                 C   s"   |   }||j | j  dS )z%Populate switcher with FileInfo data.N)r   set_current_filenamer%   r   hide)r   r)   r8   r   r   r   rW      s    z-EditorSwitcherManager.editor_switcher_handlerFc                 C   sr   |   }||j |dd }z8t|}|| | j| |sXd| _| j	d W n t
yl   Y n0 dS )zHandle line switcher selection.r   Nr   )r   r^   r%   splitr]   r\   r   Z
setVisibler   set_search_text	Exception)r   r)   rY   rZ   r8   rO   r   r   r   rV      s    
z+EditorSwitcherManager.line_switcher_handlerc                 C   s>   |   }|d }|t| d| _| j  | jd dS )z!Handle symbol switcher selection.rO   Nr   )r   r\   r]   r   r   r_   rb   )r   r)   r8   rO   r   r   r   rU      s    
z-EditorSwitcherManager.symbol_switcher_handlerN)F)__name__
__module____qualname____doc__r   r   r"   r   r   r   r   r#   r!   r    r   r   r   r   rW   rV   rU   r   r   r   r   r	      s$   

3
r	   )rg   os.pathr;   r3   Zspyder.config.baser   Zspyder.config.managerr   Zspyder.utils.icon_managerr   Zspyder.utils.switcherr   r   Zspyder.plugins.completion.apir   r   objectr	   r   r   r   r   <module>   s   