
    Je%                         d Z ddlZddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddZ
d Zd	 Zd
 Z G d de      Z G d de      Zy)a  Outline explorer API.

You need to declare a OutlineExplorerProxy, and a function for handle the
edit_goto Signal.

class OutlineExplorerProxyCustom(OutlineExplorerProxy):
    ...


def handle_go_to(name, line, text):
    ...

outlineexplorer = OutlineExplorerWidget(None)
oe_proxy = OutlineExplorerProxyCustom(name)
outlineexplorer.set_current_editor(oe_proxy, update=True, clear=False)

outlineexplorer.edit_goto.connect(handle_go_to)
    N)SignalQObject)
QTextBlock)_)running_under_pytestc              #   d  K   | j                         sy|r| j                         } n| j                         } | j                         sy|Mt        |      }| j	                         }|r|D ]  \  }}||k\  s|  y|ddd   D ]  \  }}||k  s|  y| j                         r| j                         }|rA|j                  r5|j                  j                  t        j                  k(  r|j                   |r| j                         } n| j                         } | j                         ryyw)ai  
    Get cells oedata before or after block in the document.

    Parameters
    ----------
    forward : bool, optional
        Whether to iterate forward or backward from the current block.
    cell_list: list of tuple containing (block_number, oedata)
        This is the list of all cells in a file to avoid having to parse
        the file every time.
    N)
isValidnextprevioussortedblockNumberuserDataoedatadef_typeOutlineExplorerDataCELL)blockforward	cell_list
block_line	cell_liner   datas          D/usr/lib/python3/dist-packages/spyder/plugins/outlineexplorer/api.pydocument_cellsr   !   s     ==?

 ==?9%	&&(
%. !!	6
* L! 	 &/tt_ !!	6
* L! 	 --/~~KKKK((,?,D,DD++JJLENN$E --/s   A2D05D0BD0.D0c                     | j                         sy| j                         }|xr5 |j                  xr' |j                  j                  t        j
                  k(  S )z*Check if the given block is a cell header.F)r
   r   r   r   r   r   )r   r   s     r   is_cell_headerr   S   sO    ==?>>D AA$$(;(@(@@B    c                 d    t        t        t        | d                  }t        |       r|dz   S |S )z&Get the cell index of the given block.Fr      )lenlistr   r   )r   indexs     r   
cell_indexr%   ]   s/    ^E59:;EeqyLr   c                     t        |       r| j                         j                  }n	 t        t	        | d            }|j                         r|j                  S t        |       S # t
        $ r Y yw xY w)zh
    Get the cell name the block is in.

    If the cell is unnamed, return the cell index instead.
    Fr    r   )	r   r   r   r   r   StopIterationhas_namedef_namer%   )r   headers     r   	cell_namer+   e   sk     e!((	.>?F  %    		s   A% %	A10A1c                        e Zd ZdZ eee      Z ee      Z e       Z	 fdZ
d Zd Zd Zd Zd Zd Zd	 Zd
 Zed        Z xZS )OutlineExplorerProxyz@
    Proxy class between editors and OutlineExplorerWidget.
    c                 8    t         t        |           d | _        y N)superr-   __init__fname)self	__class__s    r   r1   zOutlineExplorerProxy.__init__   s    "D24
r   c                     t         )z2Return whether the editor is a python file or not.NotImplementedErrorr3   s    r   	is_pythonzOutlineExplorerProxy.is_python       !!r   c                     t         )z8Return an unique id, used for identify objects in a dictr6   r8   s    r   get_idzOutlineExplorerProxy.get_id   r:   r   c                     t         )z[Give focus to the editor, called when toogling visibility of
        OutlineExplorerWidget.r6   r8   s    r   
give_focuszOutlineExplorerProxy.give_focus   s
     "!r   c                     t         )z/Return the number of lines of the editor (int).r6   r8   s    r   get_line_countz#OutlineExplorerProxy.get_line_count   r:   r   c                      y)z;This is used for diferenciate editors in multi-window mode.N r8   s    r   parentzOutlineExplorerProxy.parent   s    r   c                     t         )zReturn the cursor line number.r6   r8   s    r   get_cursor_line_numberz+OutlineExplorerProxy.get_cursor_line_number   r:   r   c                     t         )z(Returns a list of outline explorer data.r6   r8   s    r   outlineexplorer_data_listz.OutlineExplorerProxy.outlineexplorer_data_list   r:   r   c                     t         )zRequest current editor symbols.r6   r8   s    r   request_symbolsz$OutlineExplorerProxy.request_symbols   r:   r   c                      y)z)Check if the associated editor is cloned.FrB   r8   s    r   	is_clonedzOutlineExplorerProxy.is_cloned   s     r   )__name__
__module____qualname____doc__r   intsig_cursor_position_changedr#   !sig_outline_explorer_data_changedsig_start_outline_spinnerr1   r9   r<   r>   r@   rC   rE   rG   rI   propertyrK   __classcell__r4   s   @r   r-   r-   z   sj     #)c"2(.t% &"""
""""  r   r-   c                        e Zd Z e ed            \  ZZZZZ	dZ
dZ e       Z	 	 d fd	Zd Zd Zd Zd Zd	 Zd
 Zed        Zej.                  d        Zd Zd Zd Zd Z xZS )r      defclassc                     t         t        |           || _        || _        || _        || _        || _        t               r|| _	        yt        |      | _	        y)z
        Args:
            text (str)
            fold_level (int)
            def_type (int): [CLASS, FUNCTION, STATEMENT, COMMENT, CELL]
            def_name (str)
            color (PyQt.QtGui.QTextCharFormat)
        N)r0   r   r1   text
fold_levelr   r)   colorr   r   r   )r3   r   r\   r]   r   r)   r^   r4   s          r   r1   zOutlineExplorerData.__init__   sQ     	!413	$  
!DJ $E*DJr   c                 J    | j                   | j                  | j                  fvS r/   r   CLASSFUNCTIONr8   s    r   is_not_class_nor_functionz-OutlineExplorerData.is_not_class_nor_function   s    }}TZZ$???r   c                 J    | j                   | j                  | j                  fv S r/   r`   r8   s    r   is_class_or_functionz(OutlineExplorerData.is_class_or_function   s    }}T]] ;;;r   c                 J    | j                   | j                  | j                  fv S r/   )r   COMMENTr   r8   s    r   
is_commentzOutlineExplorerData.is_comment   s    }}tyy 999r   c                 N    | j                   | j                  k(  r| j                  S y r/   )r   ra   r)   r8   s    r   get_class_namez"OutlineExplorerData.get_class_name   s!    ==DJJ&==  'r   c                 N    | j                   | j                  k(  r| j                  S y r/   )r   rb   r)   r8   s    r   get_function_namez%OutlineExplorerData.get_function_name   s!    ==DMM)==  *r   c                     | j                   | j                  k(  r| j                  }|S | j                   | j                  k(  r| j                  }S r/   )r   rb   FUNCTION_TOKENra   CLASS_TOKEN)r3   tokens     r   	get_tokenzOutlineExplorerData.get_token   sF    ==DMM)''E  ]]djj($$Er   c                   	
 | j                   | j                  k7  r| j                  S d 	 	|       
g 	
fd}d}t        | j                  d      D ]  } ||      s|dz  } t              }d}t        | j                  d      D ]  } ||      s|dz  } t        ||z   dz         D cg c]  }|dz   vr| }}|||z
     }||z   dkD  r
dj                  |dz         z   S 
S c c}w )	zGet the cell name.c                 8    | j                   }|st        d      }|S )NzUnnamed Cell)	_def_namer   )r   names     r   get_namez.OutlineExplorerData.def_name.<locals>.get_name   s    ##D(Kr   c                      |       }dt        j                        z   dz   }t        j                  ||      }|r0|j                         d   }|rj	                  t        |             yy)N^z(?:, #(\d+))?$r   TF)reescapematchgroupsappendrP   )r   
other_namepatternr{   numberexisting_numbersrv   	self_names        r   check_matchz1OutlineExplorerData.def_name.<locals>.check_match   sd    !&)JBIIi003DDGHHWj1E*$++CK8r   r   Fr    r!   Tz, #{})r   r   rt   r   r   r"   rangeformat)r3   r   N_prevr   N_fix_previousN_nextidxfree_indexesr   rv   r   s           @@@r   r)   zOutlineExplorerData.def_name   s"    ==DII%>>!	 TN		 $TZZ? 	F6"!	 -.$TZZ> 	F6"!	
 (-Vf_q-@'A <7*::  < < 6N23F?Qw~~cAg666<s   3C.c                     || _         y)z	Set name.Nrt   )r3   values     r   r)   zOutlineExplorerData.def_name  s     r   c                 $   | j                   |j                   k(  r| j                  |j                  k(  r|j                  | _        | j                  }|j                  | _        |j                  | _        | j
                  j                          | j                   | j                  k(  rk| j                  |j                  k7  ryt        | j                  d      D ]7  }|j                  | j                  |fv s|j
                  j                          9 yy)z,Try to update to avoid reloading everything.FTr    )r   r]   r\   rt   r^   
sig_updateemitr   
cell_levelr   r   )r3   otherold_def_namer   s       r   updatezOutlineExplorerData.update   s    MMU^^+5#3#33

DI>>L"__DNDJOO  "}}		)??e&6&66 ,TZZF 1F''DNNL+II))..01 r   c                     | j                   }|r|j                         sy|j                         }|rt        |d      sy|j                  | k(  S )z/Check if the oedata has a valid block attached.Fr   )r   r
   r   hasattrr   )r3   r   	user_datas      r   is_validzOutlineExplorerData.is_valid3  sE    

EMMONN$		8 <4''r   c                     | j                   ryy)zCheck if cell has a name.TFr   r8   s    r   r(   zOutlineExplorerData.has_name=  s    >>r   c                 X    | j                         sy| j                  j                         S )zGet the block number.N)r   r   r   r8   s    r   get_block_numberz$OutlineExplorerData.get_block_numberD  s!    }}zz%%''r   )NNNNN)rL   rM   rN   r#   r   ra   rb   	STATEMENTrg   r   rn   ro   r   r   r1   rc   re   rh   rj   rl   rq   rT   r)   setterr   r   r(   r   rU   rV   s   @r   r   r      s    04U1X-E8YNK JCG&*+.@<:!! 2 2h __ &((r   r   )TN)rO   ry   qtpy.QtCorer   r   
qtpy.QtGuir   spyder.config.baser   r   r   r   r%   r+   r-   r   rB   r   r   <module>r      sL   $ 
 ' !   3/%dB!*17 1h[(' [(r   