
    Je$%                         d dl mZ d dlZd dlmZmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZ  ej                  e      Z G d de      Z G d	 d
e      Z G d de      Z G d de      Zy)    )MutableSequenceN)Signal	QFileInfoQObjectQTimerQThread)QApplication)
find_tasks)to_text_stringc                   (     e Zd ZdZ fdZd Z xZS )AnalysisThreadzAnalysis thread.c                 V    t         t        |   |       || _        d| _        || _        y)zInitialize the Analysis thread.N)superr   __init__checkerresultssource_code)selfparentr   r   	__class__s       S/usr/lib/python3/dist-packages/spyder/plugins/editor/widgets/editorstack_helpers.pyr   zAnalysisThread.__init__   s(    nd,V4&    c                     	 | j                  | j                        | _        y# t        $ r!}t        j                  |d       Y d}~yd}~ww xY w)zRun analysis.T)exc_infoN)r   r   r   	Exceptionloggererror)r   es     r   runzAnalysisThread.run    s@    	+<<(8(89DL 	+LLTL**	+s    # 	AAA)__name__
__module____qualname____doc__r   r   __classcell__r   s   @r   r   r      s    '+r   r   c                   <     e Zd ZdZd fd	Zd Zd Zd Zd Z xZ	S )ThreadManagerzAnalysis thread manager.c                 d    t         t        |   |       || _        i | _        g | _        i | _        y)zInitialize the ThreadManager.N)r   r'   r   max_simultaneous_threadsstarted_threadspending_threadsend_callbacks)r   r   r)   r   s      r   r   zThreadManager.__init__*   s2    mT+F3(@%!!r   c                    t         j                  d       |7g | _        g }t        | j                  j                               D ]  }||z  }	 nRt        |      }| j                  D cg c]  \  }}||k7  r||f c}}| _        | j                  j                  |g       }|D ]P  }t         j                  d|z         |j                         s,t        j                          |j                         r%R yc c}}w )z&Close threads associated to parent_id.z$Call ThreadManager's 'close_threads'NzWaiting for thread %r to finish)r   debugr+   listr*   valuesidget	isRunningr	   processEvents)r   r   
threadlistthreads	parent_id_th_idthreads           r   close_threadszThreadManager.close_threads2   s    ;<>#%D J 4 4 ; ; => &g%
& 6
I'+';';$9:C'*i'7 &)#J $9D  --11)R@J  	-FLL:VCD""$**, ""$	-	$9s   )C7c                 P    t         j                  d       | j                  d       y)zClose all threads.z(Call ThreadManager's 'close_all_threads'N)r   r.   r;   r   s    r   close_all_threadszThreadManager.close_all_threadsG   s    ?@4 r   c                    t        |      }t        d||      }|| j                  t        |      <   | j                  j	                  ||f       t
        j                  d|z         t        j                  d| j                         y)zAdd thread to queue.NzAdded thread %r to queue2   )
r1   r   r,   r+   appendr   r.   r   
singleShotupdate_queue)r   r   end_callbackr   r   r7   r:   s          r   
add_threadzThreadManager.add_threadL   sn    vJ	g{;)52f:&##VY$78/&89"d//0r   c                    d}t        | j                  j                               D ]  \  }}g }|D ]~  }|j                         rV| j                  j                  t        |            }|j                   ||j                         |j                  d       d}i|j                  |       |dz  } d}|r|| j                  |<   | j                  j                  |        t        j                  d       t        j                  d|z         t        j                  dt        | j                        z         | j                  r|| j                  k  r| j                  j                  d      \  }}|j                  j!                  | j"                         | j                  j%                  |g       }||gz   | j                  |<   t        j                  d|z         |j'                          yyy)zUpdate queue.r   N   zUpdating queue:z    started: %dz    pending: %dz===>starting: %r)r/   r*   items
isFinishedr,   popr1   r   	setParentrA   r   r.   lenr+   r)   finishedconnectrC   r2   start)r   startedr7   r5   still_runningr:   rD   s          r   rC   zThreadManager.update_queueU   s   %)$*>*>*D*D*F%G 	4!IzM$ 
!$$&#'#5#5#9#9"V*#EL~~1$V^^4$$T*!F!((0qLG
! J2?$$Y/$$((3#	4$ 	&'&01&T-A-A)BBCGd.K.K$K $ 4 4 8 8 ;FIOO##D$5$56--11)R@J.8&.AD  +LL+f45LLN %Lr   )   )
r    r!   r"   r#   r   r;   r>   rE   rC   r$   r%   s   @r   r'   r'   (   s    " -*!
1r   r'   c                       e Zd ZdZ e       Z eee      Z eee      Z	 eeee      Z
 eeee      Z ee      Z ee      Z eee      Zd Zed        Zej&                  d        Zd Zd Zd Zd Zd	 Zd
 Zd Zy)FileInfozFile properties.c                    t        j                  |        || _        || _        || _        d| _        || _        || _        g | _        |ddf| _	        g | _
        t        |      j                         | _        | j                  j                  j                  | j                          | j                  j"                  j                  | j$                         | j                  j&                  j                  | j&                         | j                  j(                  j                  | j*                         | j,                  j                  | j                  j,                         y)zInitialize the FileInfo.FN)r   r   threadmanager	_filenamenewly_createddefaultencodingeditorpathclassestodo_resultsr   lastModifiedlastmodifiedtextChangedrN   text_changedsig_bookmarks_changedbookmarks_changedsig_show_object_infosig_show_completion_object_infosig_send_to_helpsig_filename_changed)r   filenamerZ   r[   newrV   s         r   r   zFileInfo.__init__   s   *!  	 $-%h/<<>''(9(9:))11$2H2HI((001J1JK33;;!!	#!!))$++*J*JKr   c                     | j                   S )zFilename property.)rW   r=   s    r   ri   zFileInfo.filename   s     ~~r   c                 H    || _         | j                  j                  |       y)zFilename setter.N)rW   rh   emit)r   values     r   ri   zFileInfo.filename   s     !!&&u-r   c                     d| _         | j                  j                  | j                  | j                  j                  d             y)zEditor's text has changed.FcursorN)rY   text_changed_atrm   ri   r[   get_positionr=   s    r   rb   zFileInfo.text_changed   s6    !!$--"&++":":8"D	Fr   c                 H    t        | j                  j                               S )z%Return associated editor source code.)r   r[   toPlainTextr=   s    r   get_source_codezFileInfo.get_source_code   s    dkk55788r   c                     | j                   j                         r;| j                  j                  t        | j
                  | j                         |        yy)zRun TODO finder.N)r[   is_python_or_ipythonrV   rE   r
   todo_finishedru   r=   s    r   run_todo_finderzFileInfo.run_todo_finder   sE    ;;++-))**.*<*<*.*>*>*@$H .r   c                 Z    | j                  |       | j                  j                          y)z"Code analysis thread has finished.N)set_todo_resultstodo_results_changedrm   r   r   s     r   rx   zFileInfo.todo_finished   s"    g&!!&&(r   c                 H    || _         | j                  j                  |       y)z.Set TODO results and update markers in editor.N)r^   r[   process_todor}   s     r   r{   zFileInfo.set_todo_results   s    #  )r   c                     g | _         y)zClean-up TODO finder results.N)r^   r=   s    r   cleanup_todo_resultszFileInfo.cleanup_todo_results   s
    r   c                     | j                   j                         }| j                   j                  |k7  rA|| j                   _        | j                  j	                  | j
                  t        |             yy)zBookmarks list has changed.N)r[   get_bookmarks	bookmarkssig_save_bookmarksrm   ri   repr)r   r   s     r   rd   zFileInfo.bookmarks_changed   sU    KK--/	;;  I-$-DKK!##((YH .r   N)r    r!   r"   r#   r   r|   strr   intrq   	edit_gotoboolrg   rh   re   rf   r   propertyri   setterrb   ru   ry   rx   r{   r   rd    r   r   rT   rT   v   s    !8S)S#&OsC%Ic3-!#;!$<&,S#&6#L,   __. .
F9H)
*
Ir   rT   c                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)StackHistoryzHandles editor stack history.

    Works as a list of numbers corresponding to tab indexes.
    Internally elements are saved using objects id's.
    c                 N    t               | _        t               | _        || _        y)zInitialize the StackHistory.N)r/   historyid_listr[   )r   r[   s     r   r   zStackHistory.__init__   s    vvr   c                     t        | j                  j                  j                               D cg c]0  }t	        | j                  j                  j                  |            2 c}| _        yc c}w )z*Update list of corresponding ids and tabs.N)ranger[   tabscountr1   widgetr   )r   _is     r   _update_id_listzStackHistory._update_id_list   sX     #((8(8(>(>(@"AC 4;;++22267 C Cs   5A.c                     | j                          | j                  dd D ],  }|| j                  vs| j                  j                  |       . y)z(Remove editors that are not longer open.N)r   r   r   remove)r   r9   s     r   refreshzStackHistory.refresh   sC    <<? 	)C$,,&##C(	)r   c                 ,    t        | j                        S )z!Return the length of the history.)rL   r   r=   s    r   __len__zStackHistory.__len__   s    4<<  r   c                     | j                          	 | j                  j                  | j                  |         S # t        $ r | j                          t        w xY w)z(Retrieve the ith element of the history.)r   r   indexr   
ValueErrorr   
IndexErrorr   is     r   __getitem__zStackHistory.__getitem__   sN    	<<%%dll1o66 	LLN	s	   ':  Ac                     | j                   |= y)z&Delete the ith element of the history.N)r   r   s     r   __delitem__zStackHistory.__delitem__   s    LLOr   c                 ~    t        | j                  j                  j                  |            }|| j                  |<   y)z#Set the ith element of the history.N)r1   r[   r   r   r   )r   r   vr9   s       r   __setitem__zStackHistory.__setitem__   s.    !!((+,Qr   c                 *    t        t        |             S )zReturn the str.)r   r/   r=   s    r   __str__zStackHistory.__str__   s    4:r   c                     t        | j                  j                  j                  |            }| j                  j                  ||       y)z;Insert the widget (at tab index) in the position i (index).N)r1   r[   r   r   r   insert)r   r   	tab_indexr9   s       r   r   zStackHistory.insert   s5    !!((34As#r   c                     t        | j                  j                  j                  |            }|| j                  v r| j                  j                  |       yy)z1Remove the widget at the corresponding tab_index.N)r1   r[   r   r   r   r   )r   r   r9   s      r   r   zStackHistory.remove  sD    !!((34$,,LL$ r   c                 Z    || v r| j                  |       || v r| j                  |       y)z=Remove previous entrances of a tab, and add it as the latest.N)r   rA   )r   r   s     r   remove_and_appendzStackHistory.remove_and_append  s*    tmKK tmEr   N)r    r!   r"   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s?    C
)!
$
%r   r   )collections.abcr   loggingqtpy.QtCorer   r   r   r   r   qtpy.QtWidgetsr	   %spyder.plugins.editor.utils.findtasksr
   spyder.py3compatr   	getLoggerr    r   r   r'   rT   r   r   r   r   <module>r      sm    ,  D C ' = +			8	$+W +$KG K\PIw PIfB? Br   