
    G@dF                        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    roddlmZ  e            Z e            Ze                    d           e                     d           e!                                 e"                                 dS dS ) 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
    Nc                 t    t                                          |           || _        d| _        d| _        d S )N      )super__init___editor_left_padding_right_padding)selfcode_editor	__class__s     ?lib/python3.11/site-packages/spyder/widgets/simplecodeeditor.pyrN   zLineNumberArea.__init__5   s8    %%%"    c                 P    t          | j                                        d          S )Nr   )r   rO   linenumberarea_widthrR   s    rU   sizeHintzLineNumberArea.sizeHint>   s     T\6688!<<<rV   c                 :    | j                             |           d S N)rO   linenumberarea_paint_event)rR   events     rU   
paintEventzLineNumberArea.paintEventA   s    //66666rV   r\   )__name__
__module____qualname____doc__rN   rZ   r_   __classcell__rT   s   @rU   rI   rI   /   se         
           = = =7 7 7 7 7 7 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   Nc                 t   t                                          |           d | _        d| _        d | _        d | _        d | _        d | _        d | _        d| _	        d | _
        t          |           | _        |                     | j        j        t!          t#          |                     z              |                     d           |                                  | j                            | j                   | j                            | j                   | j                            | j                   d S )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2 	4>2SD]]BCCC((+++**,,, 	&&t'GHHH""4#=>>>"**4+MNNNNNrV   c                 "   | j         }||                    |                                            | j        |                    | j                   |                     |                                |                                           d S d S )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   s    >TYY[[)))!-##D$6777)@)@)B)B)+)@)@)B)B  D D D D D >rV   c                     d|                                  d|                                d|                                d}|                     |           |                                  d S )NzQPlainTextEdit#z {background: z	; color: z;})
objectNamenamesetStyleSheetrehighlight)rR   r   r   styles       rU   r   zSimpleCodeEditor._set_palette   so     //####Z__%6%6%6%6
8I8I8I8IK5!!!rV   c                    | j         r| j        rg }t          j                    }| j                                         }|j                            |           |j                            t          j	        d           | 
                                |_        |j                                         |                    |           |                     |           d S |                     g            d S )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22I*@@BBJ**:666(()GNNN#00I++---##I...##$455555##B'''''rV   c                 |    | j                                          t                                          |           d S r\   sig_focus_changedemitrM   focusInEventrR   r^   rT   s     rU   r   zSimpleCodeEditor.focusInEvent   6    ##%%%U#####rV   c                 |    | j                                          t                                          |           d S r\   r   r   s     rU   focusOutEventzSimpleCodeEditor.focusOutEvent   r   rV   c           	      h   t                                          |           | j        r|                                 }| j                            t          |                                |                                | 	                                |
                                                     d S d S r\   )rM   resizeEventrm   contentsRectrv   setGeometryr   lefttoprX   height)rR   r^   crrT   s      rU   r   zSimpleCodeEditor.resizeEvent   s    E"""# 		""$$B++GGIIFFHH--//IIKK	     		 		rV   Trl   r   Fc	                 Z   |r|                      |           |                     |           |                     |           |                     |           |                     |           |                     |           |                     |           |                     |           dS )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   s    :  	 MM$''(>???,,,  ---&&777(###l+++d#####rV   c                 `    |r+|                      |           |                                  dS dS )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                 <    || _         |                                  dS )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          |                                          }d| _        t
                                          D ]=\  }}||                                f|z   v r| j        |         \  }}|| _        d| _        > || 	                                | 
                                | j                  | _        |                                  dS )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     9x==&&(("'/5577 	/ 	/LS%CIIKK?U222#9#>"!$*.'$HMMOOTYY[[$*<> >  """""rV   c                 r    || _         | j                            |           |                     d           dS )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                     || _         |                     |           |                     |                                            dS )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  sB     ',#u%%%)))))rV   c                 8    |                      |rdnd           dS )zt
        Set line wrap..

        Parameters
        ----------
        state: bool
            Wrap state.
        wordN)set_wrap_moder   s     rU   r   z!SimpleCodeEditor.toggle_wrap_mode*  s'     	U46655555rV   c                     |dk    rt           j        }n|dk    rt           j        }nt           j        }|                     |           dS )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  sN     6>>#@II[  #0II#*IY'''''rV   c                 <    || _         |                                  dS )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                     || _         |                                                                 }|                    |                                t
          j        z             | j         r5|                    |                                t
          j        z             n5|                    |                                t
          j         z             |                                                     |           | 	                                 dS )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4%KL 	M 	M 	M  	MOOFLLNN[-JJKKKKOOFLLNNk.K-KKLLL,,V444 	rV   c           
      z   | j         rt          | j                  }|                    |                                | j                                                   |                                 }|                                }t          | 
                    |                              |                                                                                     }|t          |                     |                                                    z   }|                                 }|                                                                 }|                                dz   }	|                                r=||                                                                k    r|                                rh||                                                                k    r=|dz   }
|
|	k    r\|                    |j                   |                    |           |                    | j                                                   n[|                    |j                   |                    |           |                    t9          t:          j                             | j        j        }|                     d|| j        !                                |z
  | "                                                                t:          j#        tI          |
                     |%                                }|}|t          |                     |                                                    z   }|dz  }|                                r3||                                                                k    dS dS dS dS dS )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  sE    # *	"t233G

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

uT%;%;E%B%B%I%I%K%KLLL!3 --// "cUZZ\\-@-@-B-B&B&B&B&B#*	" *	""" "&B&B " "rV   c                    d}| j         rd}t          d|                                           }|dk    r|dz  }|dz  }|dk    |                                 }| j        j        | j        j        z   |                    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*9:xx}}v-.E rV   c                 Z    |                      |                                 ddd           dS )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  s0     	 9 9 ; ;Q1EEEEErV   c                    | j         r|r| j                            d|           nX| j                            d|                                | j                                        |                                           |                    |                                 	                                          r| 
                    d           dS dS dS )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     # 	4 M#**1b1111#**tvvxx!4!:!:!<!<dkkmmM M M }}T]]__113344 40033333	4 	44 4rV   c                     |                                  }|                    |           |                    |t          j                   |                     |           dS )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  sZ     ""5!!!3 67776"""""rV   c                     |                                  s*|                     t          j        t          j                   |                                  d S r\   )has_selected_text
moveCursorr
   PreviousCharacterr"  remove_selected_textrY   s    rU   stdkey_backspacez!SimpleCodeEditor.stdkey_backspace  sO    %%'' 	4OOK9'24 4 4!!#####rV   c                     |                      |          }|                      |          }|                                 }|                                }||k     s||k    r|                     |           dS dS )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8''44"" //++]**o.K.K$$[11111 /L.KrV   c                 &   |                      |          }|                                 }|                                |                                }}||k     rt	          ||g          }nt	          ||g          }|                     ||           dS )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  s     ))-88""**,,f.A.A.C.Cs3;;.//EE}c*++C5#&&&&&rV   c                 0    |                      |           dS )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                     |                                  }|                    t          j                   |                    |           dS )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  sC     ""KO,,,$rV   c                 `   |                                                                  }t          |                                                                 dz
  |                                                                 dz
            }|                     |                                          }||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(  s    &&((4466dmmoo33559"mmoo4466:< <%%l33??AAt}rV   c                 J    | j         r| j                                          dS dS )z>
        Reapply syntax highligthing to the document.
        N)ru   r   rY   s    rU   r   zSimpleCodeEditor.rehighlight2  s4      	,))+++++	, 	,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0 ;$;$nc*L#&#BL#&C BC $;%*c"]C(B! * 
O O O O O O:D D D  ( ( ("$ $ $ $ $$ $ $ $ $      "&"/"!&,0%*&$ &$ &$ &$P' ' '
# 
# 
## # #,- - -* * *	6 	6 	6( ( ( ($
- 
- 
-  4." ." ."`  8
F 
F 
F 
F4 4 4 # # # $ $ $2 2 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 8 8 8 8 8 8 8 8 8 8 8 8 N N N N N N N N N N N N N N = = = = = = = = = = - , , , , , , , , / / / / / /
 -#$2$=!  (7 7 7 7 7W 7 7 7,r, r, r, r, r,~} r, r, r,j z333333
,..CF
,,,
OOJ
KKMMMIIKKKKK rV   