
    JeF                     ~   d Z ddlmZmZmZmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZmZmZ ddlmc mZ ddlmZ ddd	d
ddddddddddddZ G d de      Z G d dee      Zedk(  rXddlmZ  e       Z e       Zej?                  d       ejA                  d       ejC                          ejE                          yy) z
Simple code editor with syntax highlighting and line number area.

Adapted from:
https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
    )QPointQRectQSizeQtSignal)QColorQPainterQTextCursorQTextFormatQTextOption)QPlainTextEdit	QTextEditQWidgetN)BaseEditMixin)pypywpythonipy)pyxpxipxd)enaml)fforf77)f90f95f2kf03f08)pro)diffpatchrej)popot)nsinsh)htmhtml)ccccppcxxhhhhpphxx)cl)yamlyml)mdmdw) )PythonCythonEnaml	Fortran77FortranIdlDiffGetTextNsisHtmlCppOpenCLYamlMarkdownNonec                   0     e Zd ZdZd fd	Zd Zd Z xZS )LineNumberAreaz\
    Adapted from:
    https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
    c                 N    t         |   |       || _        d| _        d| _        y )N      )super__init___editor_left_padding_right_padding)selfcode_editor	__class__s     A/usr/lib/python3/dist-packages/spyder/widgets/simplecodeeditor.pyrN   zLineNumberArea.__init__5   s'    %"    c                 J    t        | j                  j                         d      S )Nr   )r   rO   linenumberarea_widthrR   s    rU   sizeHintzLineNumberArea.sizeHint>   s    T\\668!<<rV   c                 :    | j                   j                  |       y N)rO   linenumberarea_paint_event)rR   events     rU   
paintEventzLineNumberArea.paintEventA   s    //6rV   r\   )__name__
__module____qualname____doc__rN   rZ   r_   __classcell__rT   s   @rU   rI   rI   /   s    
 =7rV   rI   c                       e Zd ZdZej
                  dfej                  dfej                  dfej                  dfej                  dfej                  dfej                  dfej                  dfej                  dfej                  dfej                  dfej                   dfej"                  dfej$                  dfej&                  dfdZ e       Z	 d& fd		Zd
 Zd Zd Z fdZ fdZ fdZ	 	 	 	 	 	 	 	 d'dZd Zd Z d Z!d Z"d Z#d Z$d&dZ%d Z&d Z'd Z(d Z)d&dZ*d Z+d Z,d Z-d  Z.d! Z/d" Z0d# Z1d$ Z2d% Z3 xZ4S )(SimpleCodeEditorz&Simple editor with highlight features.#r+   !;r8   z//)r9   r:   r<   r=   r>   r?   r@   rA   rB   rE   rC   rD   r;   rF   rG   c                 V   t         |   |       d | _        d| _        d | _        d | _        d | _        d | _        d | _        d| _	        d | _
        t        |       | _        | j                  | j                  j                  t!        t#        |             z          | j%                  d       | j'                          | j(                  j+                  | j$                         | j,                  j+                  | j.                         | j0                  j+                  | j&                         y )Nspyder/darkFr   )rM   rN   _linenumber_enabled_color_scheme	_language_blanks_enabled_scrollpastend_enabled
_wrap_mode_highlight_current_linesupported_language_highlighterrI   linenumberareasetObjectNamerT   r`   stridupdate_linenumberarea_width_apply_current_line_highlightblockCountChangedconnectupdateRequestupdate_linenumberareacursorPositionChanged)rR   parentrT   s     rU   rN   zSimpleCodeEditor.__init__c   s      $( *#&*#'+$"' !,T2 	4>>22SD]BC((+**, 	&&t'G'GH""4#=#=>""**4+M+MNrV   c                    | j                   }|v|j                  | j                                | j                  |j	                  | j                         | j                  |j                         |j                                y y )N)
background
foreground)ru   setup_formatsfontrn   set_color_scheme_set_paletteget_background_colorget_foreground_color)rR   hls     rU   _apply_color_schemez$SimpleCodeEditor._apply_color_scheme   sv    >TYY[)!!-##D$6$67)@)@)B)+)@)@)B  D rV   c                     d| j                         d|j                         d|j                         d}| j                  |       | j                          y )NzQPlainTextEdit#z {background: z	; color: z;})
objectNamenamesetStyleSheetrehighlight)rR   r   r   styles       rU   r   zSimpleCodeEditor._set_palette   s@    //#Z__%6
8IK5!rV   c                    | j                   r| j                  rg }t        j                         }| j                   j	                         }|j
                  j                  |       |j
                  j                  t        j                  d       | j                         |_        |j                  j                          |j                  |       | j                  |       y | j                  g        y )NT)ru   rs   r   ExtraSelectionget_currentline_colorformatsetBackgroundsetPropertyr   FullWidthSelection
textCursorcursorclearSelectionappendsetExtraSelections)rR   extra_selections	selection
line_colors       rU   r{   z.SimpleCodeEditor._apply_current_line_highlight   s    !=!=!!002I**@@BJ**:6(()G)GN#0I++-##I.##$45##B'rV   c                 X    | j                   j                          t        |   |       y r\   sig_focus_changedemitrM   focusInEventrR   r^   rT   s     rU   r   zSimpleCodeEditor.focusInEvent   "    ##%U#rV   c                 X    | j                   j                          t        |   |       y r\   r   r   s     rU   focusOutEventzSimpleCodeEditor.focusOutEvent   r   rV   c           	         t         |   |       | j                  rp| j                         }| j                  j                  t        |j                         |j                         | j                         |j                                      y y r\   )rM   resizeEventrm   contentsRectrv   setGeometryr   lefttoprX   height)rR   r^   crrT   s      rU   r   zSimpleCodeEditor.resizeEvent   so    E"##""$B++GGIFFH--/IIK	 $rV   c	                    |r| j                  |       | j                  |       | j                  |       | j                  |       | j	                  |       | j                  |       | j                  |       | j                  |       y)a  
        Setup editor options.

        Parameters
        ----------
        color_scheme: str, optional
            Default is "spyder/dark".
        language: str, optional
            Default is "py".
        font: QFont or None
            Default is None.
        show_blanks: bool, optional
            Default is False/
        wrap: bool, optional
            Default is False.
        highlight_current_line: bool, optional
            Default is True.
        scroll_past_end: bool, optional
            Default is False
        N)set_fontset_highlight_current_lineset_blanks_enabledtoggle_line_numbersset_scrollpastend_enabledset_languager   toggle_wrap_mode)	rR   linenumberscolor_schemelanguager   show_blankswraphighlight_current_linescroll_past_ends	            rU   setup_editorzSimpleCodeEditor.setup_editor   sv    : MM$''(>?,  -&&7(#l+d#rV   c                 L    |r"| j                  |       | j                          yy)zz
        Set the editor font.

        Parameters
        ----------
        font: QFont
            Font to use.
        N)setFontr   )rR   r   s     rU   r   zSimpleCodeEditor.set_font   s$     LL$$& rV   c                 2    || _         | j                          y)z
        Set the editor color scheme.

        Parameters
        ----------
        color_scheme: str
            Color scheme to use.
        N)rn   r   )rR   r   s     rU   r   z!SimpleCodeEditor.set_color_scheme   s     *  "rV   c                    t         j                  }t        |      j                         }d| _        t
        j                         D ]<  \  }}||j                         f|z   v s| j                  |   \  }}|| _        d| _        >  || j                         | j                         | j                        | _        | j                          y)z
        Set current syntax highlighting to use `language`.

        Parameters
        ----------
        language: str or None
            Language name or known extensions.
        FTN)shTextSHrx   lowerrt   LANGUAGE_EXTENSIONSitemsLANGUAGE_HIGHLIGHTERSro   documentr   rn   ru   r   )rR   r   sh_classkeyvalue__s         rU   r   zSimpleCodeEditor.set_language   s     99x=&&("'/557 	/LS%CIIK?U22#99#>"!$*.'		/ %MMOTYY[$*<*<>  "rV   c                 j    || _         | j                  j                  |       | j                  d       y)z
        Set visibility of line number area

        Parameters
        ----------
        state: bool
            Visible state of the line number area.
         N)rm   rv   
setVisiblerz   rR   states     rU   r   z$SimpleCodeEditor.toggle_line_numbers  s.     $) &&u-((,rV   c                 r    || _         | j                  |       | j                  | j                                y)z
        Set scroll past end state.

        Parameters
        ----------
        state: bool
            Scroll past end state.
        N)rq   setCenterOnScrollsetDocumentr   r   s     rU   r   z*SimpleCodeEditor.set_scrollpastend_enabled  s.     ',#u%)rV   c                 8    | j                  |rd       yd       y)zt
        Set line wrap..

        Parameters
        ----------
        state: bool
            Wrap state.
        wordN)set_wrap_moder   s     rU   r   z!SimpleCodeEditor.toggle_wrap_mode*  s     	U655rV   c                     |dk(  rt         j                  }n&|dk(  rt         j                  }nt         j                  }| j	                  |       y)z
        Set line wrap mode.

        Parameters
        ----------
        mode: str or None, optional
            "word", or "character". Default is None.
        r   	characterN)r   WrapAtWordBoundaryOrAnywhereWrapAnywhereNoWrapsetWordWrapMode)rR   mode	wrap_modes      rU   r   zSimpleCodeEditor.set_wrap_mode5  sB     6>#@@I[ #00I#**IY'rV   c                 2    || _         | j                          y)z
        Set if the current line is highlighted.

        Parameters
        ----------
        value: bool
            The value of the current line highlight option.
        N)rs   r{   )rR   r   s     rU   r   z+SimpleCodeEditor.set_highlight_current_lineG  s     (-$**,rV   c                    || _         | j                         j                         }|j                  |j	                         t
        j                  z         | j                   r1|j                  |j	                         t
        j                  z         n1|j                  |j	                         t
        j                   z         | j                         j                  |       | j                          y)z
        Show blank spaces.

        Parameters
        ----------
        state: bool
            Blank spaces visibility.
        N)
rp   r   defaultTextOptionsetFlagsflagsr   %AddSpaceForLineAndParagraphSeparatorsShowTabsAndSpacessetDefaultTextOptionr   )rR   r   options      rU   r   z#SimpleCodeEditor.set_blanks_enabledS  s      %224%KKL 	M OOFLLN[-J-JJKOOFLLNk.K.K-KKL,,V4 	rV   c           
      t   | j                   r+t        | j                        }|j                  |j	                         | j
                  j                                | j                         }|j                         }t        | j                  |      j                  | j                               j                               }|t        | j                  |      j                               z   }| j!                         }| j#                         j%                         }|j                         dz   }	|j'                         r||j	                         j)                         k  r|j+                         rP||j	                         j                         k\  r.|dz   }
|
|	k(  rV|j-                  |j.                         |j1                  |       |j3                  | j
                  j5                                nT|j-                  |j6                         |j1                  |       |j3                  t9        t:        j<                               | j                  j>                  }|jA                  d|| j                  jC                         |z
  | jE                         j                         t:        jF                  tI        |
             |jK                         }|}|t        | j                  |      j                               z   }|dz  }|j'                         r$||j	                         j)                         k  ryyyyy)z-
        Paint the line number area.
           r   N)&rm   r	   rv   fillRectrectru   get_sideareas_colorfirstVisibleBlockblockNumberroundblockBoundingGeometry
translatedcontentOffsetr   blockBoundingRectr   r   r   blockisValidbottom	isVisible	setWeightBoldr   setPenr   Normalr   r   darkGrayrQ   drawTextwidthfontMetrics
AlignRightrx   next)rR   r^   painterr   block_numberr   r   r   active_blockactive_line_numbernumberright_paddings               rU   r]   z+SimpleCodeEditor.linenumberarea_paint_eventm  sz    ##t223G

!!557
 **,E ,,.L2259DD""$&&)ce-C5!7!7!>!E!E!GHHF99;D??,224L!-!9!9!;a!?--/cUZZ\-@-@-B&B??$5::<3C3C3E)E)A-F!33tyy1- --BBDF t{{3-vbkk':;$($7$7$F$FM$$++113mC((*113s6{ 

uT%;%;E%B%I%I%KLL!3 --/cUZZ\-@-@-B&B/&B/# $rV   c                 (   d}| j                   rd}t        d| j                               }|dk\  r|dz  }|dz  }|dk\  r| j                         }| j                  j
                  | j                  j                  z   |j                  d      |z  z   }|S )z
        Return the line number area width.

        Returns
        -------
        int
            Line number are width in pixels.

        Notes
        -----
        If the line number area is disabled this will return zero.
        r   r   
   9)rm   max
blockCountr  rv   rP   rQ   r  )rR   r  digitscountfms        rU   rX   z%SimpleCodeEditor.linenumberarea_width  s     ##F4??,-E2+! 2+ !!#B((66**99:xx}v-.E rV   c                 H    | j                  | j                         ddd       y)z
        Update the line number area width based on the number of blocks in
        the document.

        Parameters
        ----------
        new_block_count: int
            The current number of blocks in the document.
        r   N)setViewportMarginsrX   )rR   new_block_counts     rU   rz   z,SimpleCodeEditor.update_linenumberarea_width  s!     	 9 9 ;Q1ErV   c                 ~   | j                   r|r| j                  j                  d|       nR| j                  j                  d|j	                         | j                  j                         |j                                |j                  | j                         j                               r| j                  d       yyy)z=
        Update scroll position of line number area.
        r   N)rm   rv   scrollupdateyr  r   containsviewportr   rz   )rR   r   dys      rU   r   z&SimpleCodeEditor.update_linenumberarea  s     ####**1b1##**tvvx!4!4!:!:!<dkkmM }}T]]_1134003 5 $rV   c                     | j                         }|j                  |       |j                  |t        j                         | j	                  |       y)z
        Set current text selection.

        Parameters
        ----------
        start: int
            Selection start position.
        end: int
            Selection end position.
        N)r   setPositionr
   
KeepAnchorsetTextCursor)rR   startendr   s       rU   set_selectionzSimpleCodeEditor.set_selection  sB     "5!3 6 676"rV   c                     | j                         s.| j                  t        j                  t        j                         | j                          y r\   )has_selected_text
moveCursorr
   PreviousCharacterr"  remove_selected_textrY   s    rU   stdkey_backspacez!SimpleCodeEditor.stdkey_backspace  s7    %%'OOK99'224!!#rV   c                     | j                  |      }| j                  |      }| j                         }|j                         }||k  s||kD  r| j                  |       yy)z
        Restrict the cursor from being inside from and to positions.

        Parameters
        ----------
        position_from: int
            Selection start position.
        position_to: int
            Selection end position.
        N)get_positionr   positionset_cursor_position)rR   position_fromposition_tor   cursor_positions        rU   restrict_cursor_positionz)SimpleCodeEditor.restrict_cursor_position  s_     ))-8''4" //+]*o.K$$[1 /LrV   c                     | j                  |      }| j                         }|j                         |j                         }}||k  rt	        ||g      }nt	        ||g      }| j                  ||       y)z
        Restrict the cursor selection to start from the given position.

        Parameters
        ----------
        position_from: int
            Selection start position.
        N)r.  r   selectionStartselectionEndr  r&  )rR   r1  r   r$  r%  s        rU   truncate_selectionz#SimpleCodeEditor.truncate_selection  sp     ))-8"**,f.A.A.Cs3;./E}c*+C5#&rV   c                 &    | j                  |       y)z
        Set `text` of the document.

        Parameters
        ----------
        text: str
            Text to set.
        N)setPlainText)rR   texts     rU   set_textzSimpleCodeEditor.set_text  s     	$rV   c                     | j                         }|j                  t        j                         |j	                  |       y)z
        Add `text` to the end of the document.

        Parameters
        ----------
        text: str
            Text to append.
        N)r   movePositionr
   End
insertText)rR   r;  r   s      rU   r   zSimpleCodeEditor.append  s0     "KOO,$rV   c                    | j                         j                         }t        | j                         j	                         dz
  | j                         j                         dz
        }| j                  |      j                         }||fS )z-Get the first and last visible block numbers.r   )r   r   r   r  r  r   cursorForPosition)rR   firstbottom_rightlasts       rU   get_visible_block_numbersz*SimpleCodeEditor.get_visible_block_numbers(  ss    &&(446dmmo3359"mmo446:<%%l3??At}rV   c                 R    | j                   r| j                   j                          yy)z>
        Reapply syntax highligthing to the document.
        N)ru   r   rY   s    rU   r   zSimpleCodeEditor.rehighlight2  s$     ))+ rV   r\   )Trl   r   NFFTF)5r`   ra   rb   rc   r   PythonSHCythonSHFortran77SH	FortranSHIdlSHDiffSH	GetTextSHNsisSHHtmlSHYamlSHCppSHOpenCLSHEnamlSH
MarkdownSHr   r   r   r   rN   r   r   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r]   rX   rz   r   r&  r,  r4  r8  r<  r   rF  r   rd   re   s   @rU   rg   rg   E   s   0 ;;$;;$nnc*LL#&#BLL#&C BC $;;%**c"]]C(B!* 
O:D("$$  "&"/"!&,0%*&$P'
##,-*	6($
-4."`8
F4 # $2$'&	  ,rV   rg   __main__)qapplicationmarkdown)r   z# Hello!)#rc   qtpy.QtCorer   r   r   r   r   
qtpy.QtGuir   r	   r
   r   r   qtpy.QtWidgetsr   r   r   spyder.utils.syntaxhighlightersutilssyntaxhighlightersr   spyder.widgets.mixinsr   r   rI   rg   r`   spyder.utils.qthelpersrW  appeditorr   r<  showexec_r   rV   rU   <module>re     s    9 8 N N = = - , /
 -#$2$=! (7W 7,r,~} r,j z3
.CF
,
OOJ
KKMIIK rV   