
    G@d                      (   d Z ddlZddlZddlm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mZ ddlmZmZmZmZ ddlmZ ddlm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l$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z,m-Z- ddl.m/Z/  G d dee/          Z0dS )zQPlainTextEdit base class    N)to_qvariant)QEventQPointQtSignalSlot)
QClipboardQColorQMouseEventQTextFormatQTextOptionQTextCursor)QApplicationQMainWindowQPlainTextEditQToolTip)get_font)CONF)TextDecorationDRAW_ORDERS)TextDecorationsManager)CompletionWidget)CompletionItemKind)is_cell_headerdocument_cells)to_text_string)SpyderPalette)CallTipWidgetToolTipWidget)BaseEditMixinc                       e Zd ZdZdZ e            Z e            Z e            Z e            Z	 e            Z
 ee          Z ee          Z e            Z e            ZdId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d Zd Zd Z d Z!dJdZ"dJdZ#	 dJdZ$dKdZ%d Z&dIdZ' e(            d             Z) fdZ* fdZ+d  Z,dId!Z-dId"Z.dId#Z/d$ Z0d% Z1d& Z2 fd'Z3d( Z4d) Z5dLd+Z6d, Z7d- Z8dLd.Z9d/ Z:d0 Z;d1 Z<d2 Z=dId3Z>d4 Z?d5 Z@d6 ZAd7 ZBd8 ZCd9 ZDdLd:ZEd; ZFd< ZGd= ZHd> ZId? ZJd@ ZKdIdAZLdB ZMdC ZNdD ZOdE ZPdF ZQdG ZRdH ZS xZTS )MTextEditBaseWidgetzText edit base widget)sofeofNc                 x    t          j         |           t          j                    d _                             t
          j                   d  _        d _        d _	        d _
        d _         j                             j                   d _        d _        |G|}t#          |t$                    s,|                                }|nt#          |t$                    ,||}t)           |           _        d _                                          t1           d           _        t5           d           _        d _        t;          t<          j                                       d           _!        t;          t<          j                                       d           _"        d  _#        t;          t<          j$                   _%        t;          t<          j                   _&        tO                      _(        d  _)         fd	} j*                            |           d  _+        d  _,        d S )
NFTz       )hide_timer_on)
as_tooltip      c                  >    d  _                                           d S N)current_cellhighlight_current_cellselfs   Blib/python3.11/site-packages/spyder/plugins/editor/widgets/base.pyreset_current_cellz7TextEditBaseWidget.__init__.<locals>.reset_current_cellq   s#     $D'')))))    )-r   __init__r    has_cell_separatorssetAttributer   WA_DeleteOnClose_restore_selection_posremove_trailing_spacesremove_trailing_newlinesadd_newlinecode_snippetscursorPositionChangedconnectcursor_position_changedindent_charstab_stop_width_spaces
isinstancer   parentr   completion_widgetcodecompletion_autosetup_completionr   calltip_widgetr   tooltip_widgethighlight_current_cell_enabledr
   r   COLOR_ERROR_2lightercurrentline_colorcurrentcell_colorbraceposCOLOR_SUCCESS_1matched_p_colorunmatched_p_colorr   decorationsr-   textChanged_current_cell_cursor_current_line_block)r0   rC   mainwinr2   s   `   r1   r4   zTextEditBaseWidget.__init__4   s5   f---t$$$#( "-...&*# ',#(-% ! ""**4+GHHH!%&" G +66 !..**? !+66  " !1$!?!?#( +DFFF+DTBBB.3+ "('") "))0 	!''") "))0 	 %m&CDD!'(C!D!D1$77 !	* 	* 	* 	* 	* 	  !3444 %)!#'   r3   c                     t          j        dd          }t                      }| j                            ||           d S )Nmainzcompletion/size)r   getr   rD   setup_appearance)r0   sizefonts      r1   rF   z#TextEditBaseWidget.setup_completion{   s<    x 122zz//d;;;;;r3   c                     || _         d S r,   )r@   )r0   r@   s     r1   set_indent_charsz#TextEditBaseWidget.set_indent_chars   s    (r3   c                 <    || _         |                                  d S r,   )rA   update_tab_stop_width_spaces)r0   rA   s     r1   set_tab_stop_width_spacesz,TextEditBaseWidget.set_tab_stop_width_spaces   s"    %:"))+++++r3   c                     || _         d S r,   )r9   r0   flags     r1   set_remove_trailing_spacesz-TextEditBaseWidget.set_remove_trailing_spaces   s    &*###r3   c                     || _         d S r,   )r;   )r0   r;   s     r1   set_add_newlinez"TextEditBaseWidget.set_add_newline   s    &r3   c                     || _         d S r,   )r:   rc   s     r1   set_remove_trailing_newlinesz/TextEditBaseWidget.set_remove_trailing_newlines   s    (,%%%r3   c                     |                      |                                                     d| j        z                       d S )N )setTabStopWidthfontMetricswidthrA   r/   s    r1   r`   z/TextEditBaseWidget.update_tab_stop_width_spaces   sR    T--//55 4#==? ? 	@ 	@ 	@ 	@ 	@r3   c                 T   |                                  s<|                     | j        j        t	          t          |                     z              d|                                  d|                                d|                                d}|                     |           dS )zh
        Set text editor palette colors:
        background color and caret (text cursor) color
        zQPlainTextEdit#z {background: z	; color: z;}N)
objectNamesetObjectName	__class____name__stridnamesetStyleSheet)r0   
background
foregroundstyles       r1   set_palettezTextEditBaseWidget.set_palette   s        	Ht~6RXXFGGG""""JOO$5$5$5$5z7H7H7H7HJ5!!!!!r3   c                 8    | j                             |g           S )zReturn editor extra selections.

        Args:
            key (str) name of the extra selections group

        Returns:
            list of sourcecode.api.TextDecoration.
        )rR   rY   r0   keys     r1   get_extra_selectionsz'TextEditBaseWidget.get_extra_selections   s     ##C,,,r3   c                     t          j        |          }|t          j        d          }|D ]}||_        ||_        | j                            ||           |                                  dS )a  Set extra selections for a key.

        Also assign draw orders to leave current_cell and current_line
        in the background (and avoid them to cover other decorations)

        NOTE: This will remove previous decorations added to the same key.

        Args:
            key (str) name of the extra selections group.
            extra_selections (list of sourcecode.api.TextDecoration).
        Non_top)r   rY   
draw_orderkindrR   add_keyupdate)r0   r~   extra_selectionsr   	selections        r1   set_extra_selectionsz'TextEditBaseWidget.set_extra_selections   ss     !_S))
$22J) 	! 	!I#-I  INN  &6777r3   c                 b    | j                             |           |                                  dS )zRemove decorations added through set_extra_selections.

        Args:
            key (str) name of the extra selections group.
        N)rR   
remove_keyr   r}   s     r1   clear_extra_selectionsz)TextEditBaseWidget.clear_extra_selections   s,     	##C(((r3   c                 `   |                                                                  }t          |                                                                 dz
  |                                                                 dz
            }|                     |                                          }||fS )z-Get the first and last visible block numbers.   )firstVisibleBlockblockNumberr   viewportrn   heightcursorForPosition)r0   firstbottom_rightlasts       r1   get_visible_block_numbersz,TextEditBaseWidget.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}r3   c                     |                                  \  }}t          ||z
  dz            }||z
  }|dk     rdn|}||z   }||                                 k    r|                                 n|}||fS )z
        Get the first and last block numbers of a region that covers
        the visible one plus a buffer of half that region above and
        below to make more fluid certain operations.
           r   )r   round
blockCount)r0   first_visiblelast_visiblebuffer_heightr   r   s         r1   get_buffer_block_numbersz+TextEditBaseWidget.get_buffer_block_numbers   s     '+&D&D&F&F#||m;q@AA-QYYEm+$(4??+<+<$<$<t   $t}r3   c                    |                                  }|                                }| j        |k    rdS || _        t          |          }|j                            t          j        t          d                     |j        	                    | j
                   |j                                         |                     d|g           dS )zHighlight current lineNTcurrent_line)
textCursorblockrU   r   formatsetPropertyr   FullWidthSelectionr   setBackgroundrL   cursorclearSelectionr   )r0   r   r   r   s       r1   highlight_current_linez)TextEditBaseWidget.highlight_current_line   s    ""#u,,F#( "6**	$$[%C%0%6%6	8 	8 	8&&t'=>>>'')))!!.9+>>>>>r3   c                 >    d| _         |                     d           dS )zUnhighlight current lineNr   )rU   r   r/   s    r1   unhighlight_current_linez+TextEditBaseWidget.unhighlight_current_line   s$    #' ##N33333r3   c                    | j         r| j        s	d| _        dS |                                 \  }}d } || j        |          rdS || _        t	          |          }|j                            t          j        t          d                     |j        
                    | j                   |r|                     d           dS |                     d|g           dS )zHighlight current cellNc                     | |dS |                                  |                                 k    o)|                                 |                                k    S NF)selectionStartselectionEnd)c1c2s     r1   same_selectionzATextEditBaseWidget.highlight_current_cell.<locals>.same_selection  sV    zRZu!!##r'8'8':':: 7!!R__%6%66r3   Tr-   )r5   rI   rT   select_current_cellr   r   r   r   r   r   r   rM   r   r   )r0   r   whole_file_selectedr   r   s        r1   r.   z)TextEditBaseWidget.highlight_current_cell   s   ( 	7	(,D%F&*&>&>&@&@##	 	 	 >$3V<< 	F$*!"6**	$$[%C%0%6%6	8 	8 	8&&t'=>>> 	C''77777%%nykBBBBBr3   c                 >    d| _         |                     d           dS )zUnhighlight current cellNr-   )rT   r   r/   s    r1   unhighlight_current_cellz+TextEditBaseWidget.unhighlight_current_cell  s$    $(!##N33333r3   c                     dS )zReturns True if the given position is inside a comment.

        Trivial default implementation. To be overridden by subclass.
        This function is used to define the default behaviour of
        self.find_brace_match.
        F r0   r   positions      r1   
in_commentzTextEditBaseWidget.in_comment#  	     ur3   c                     dS )zReturns True if the given position is inside a string.

        Trivial default implementation. To be overridden by subclass.
        This function is used to define the default behaviour of
        self.find_brace_match.
        Fr   r   s      r1   	in_stringzTextEditBaseWidget.in_string,  r   r3   c                     |?|=                                r fd}n!                               r fd}n fd} j        \  }}r%dddd	|         }                     |d
          }	n2dddd|         }                     |dz   d
          }	|	ddd         }	fd}
d}d}	 |	                    ||          }|dz   }|dk    rdS | | |
|                    su	 |	                    |||          }|dz   }|dk    r:|-t          d|dz             D ]} | |
|                    r dS  |
|          S | | |
|                    snt)a  Returns position of matching brace.

        Parameters
        ----------
        position : int
            The position of the brace to be matched.
        brace : {'[', ']', '(', ')', '{', '}'}
            The brace character to be matched.
            [ <-> ], ( <-> ), { <-> }
        forward : boolean
            Whether to search forwards or backwards for a match.
        ignore_brace : callable taking int returning boolean, optional
            Whether to ignore a brace (as function of position).
        stop : callable taking int returning boolean, optional
            Whether to stop the search early (as function of position).

        If both *ignore_brace* and *stop* are None, then brace matching
        is handled differently depending on whether *position* is
        inside a string, comment or regular code. If in regular code,
        then any braces inside strings and comments are ignored. If in a
        string/comment, then only braces in the same string/comment are
        considered potential matches. The functions self.in_comment and
        self.in_string are used to determine string/comment/code status
        of characters in this case.

        If exactly one of *ignore_brace* and *stop* is None, then it is
        replaced by a function returning False for every position. I.e.:
            lambda pos: False

        Returns
        -------
        The position of the matching brace. If no matching brace
        exists, then None is returned.
        Nr   c                 2                         |            S Nr   )r   posr0   s    r1   stopz1TextEditBaseWidget.find_brace_match.<locals>.stop]  s    #~~s~;;;;r3   c                 2                         |            S r   )r   r   s    r1   r   z1TextEditBaseWidget.find_brace_match.<locals>.stopa  s    #<<<<r3   c                 \                         |           p                    |           S r   )r   r   r   s    r1   ignore_bracez9TextEditBaseWidget.find_brace_match.<locals>.ignore_bracee  s/     NNCN88 : OOSO99;r3   )]}([{F)remove_newlinesr   r   r   r   r   r   r   c                     r| z   n| z
  S )z+Computes editor position from search index.r   )indexforwardr   s    r1   ind2posz4TextEditBaseWidget.find_brace_match.<locals>.ind2posu  s    )0HHu$$x%7GHr3   )r   r   BRACE_MATCHING_SCOPEget_textfindrange)r0   r   bracer   r   r   	start_posend_posclosing_bracetextr   i_start_closei_start_openi_closei_openis   `` `            r1   find_brace_matchz#TextEditBaseWidget.find_brace_match5  sI   J DL~~x~00 ;< < < < < <(33 ;= = = = = =; ; ; ; ;
 "6	7 	 "%Cc::5AM==7E=JJDD #&Cc::5AM==HQJ=NNDB<D	I 	I 	I 	I 	I 	I 	ii}==G#AIM"}}%\\''':J:J-K-K%!YYulGDDF#)!8L||  +%*1gai%8%8 + +#'4

#3#3 !+$*FF!+&ww///&.*l776??;; /	r3   Fc                    |r|                      d           d S g }|D ]}||                     d          k    r d S t          |                                           }|j                            |           |j                                         |j                            |           |j        	                    t          j        t          j                   |                    |           |                     d|           d S )Nbrace_matchingr$   )r   get_positionr   r   r   r   r   r   setPositionmovePositionr   NextCharacter
KeepAnchorappendr   )r0   	positionscolorcancelr   r   r   s          r1   __highlightzTextEditBaseWidget.__highlight  s    	''(8999F! 		/ 		/H$++E2222&t'8'899I**5111++---((222))+*C*5*@B B B##I....!!"24DEEEEEr3   c                 r   | j         #|                     | j         d           d| _         |                                 }|                                dk    rdS |                    t
          j        t
          j                   t          |	                                          }|dv rd}n	|dv rd}ndS |                                }| 
                    |||          }|,||f| _         |                     | j         | j        	           dS |f| _         |                     | j         | j        	           dS )
zHandle brace matching.NT)r   r   r   Fr   )r   )r   )rN   _TextEditBaseWidget__highlightr   r   r   r   PreviousCharacterr   r   selectedTextr   rP   rQ   )r0   r   r   r   pos1pos2s         r1   r?   z*TextEditBaseWidget.cursor_position_changed  sK    =$T]4888 DM
 ""??!!FK9'2	4 	4 	4f113344?""GG_$$GGF  $$T4$AA !4LDMT]$2FGGGGG!GDMT]$2HIIIIIr3   c                     |dk    rt           j        }n|dk    rt           j        }nt           j        }|                     |           dS )zV
        Set wrap mode
        Valid *mode* values: None, 'word', 'character'
        word	characterN)r   WrapAtWordBoundaryOrAnywhereWrapAnywhereNoWrapsetWordWrapMode)r0   mode	wrap_modes      r1   set_wrap_modez TextEditBaseWidget.set_wrap_mode  sN    
 6>>#@II[  #0II#*IY'''''r3   c                     |                                  r:t          j                                        |                                             dS dS )z]
        Reimplement Qt method
        Copy text to clipboard with correct EOL chars
        N)get_selected_textr   	clipboardsetTextr/   s    r1   copyzTextEditBaseWidget.copy  sT     !!## 	G"$$,,T-C-C-E-EFFFFF	G 	Gr3   c                    t           j        dk    rT|                     dd          }|                    dd                              dd                              dd          S t	          t
          |                                           S )zU
        Reimplement Qt method
        Fix PyQt4 bug on Windows and Python 3
        ntr#   r$   u    
u       )osrv   r   replacesuperr"   toPlainText)r0   r   rr   s     r1   r  zTextEditBaseWidget.toPlainText  su     7d??==..D<<$//77$GG$//0'..::<<<r3   c                 X   |                                 }|                                t          j        z  }|                                t          j        z  }|s|r&|t          j        k    r|                                  d S t          t          |           	                    |           d S r,   )
r~   	modifiersr   ControlModifierMetaModifierKey_Cr  r  r"   keyPressEvent)r0   eventr~   ctrlmetarr   s        r1   r  z TextEditBaseWidget.keyPressEvent  s    iikk  2#55  2?2  	AD 	AcRXooIIKKKKK$d++99%@@@@@r3   c                     g S )zGet all cells.r   r/   s    r1   get_cell_listz TextEditBaseWidget.get_cell_list  s	     	r3   c                 n   |                                  }d }|                     |          \  }}|                     |          }|sdS |                    |          }t	          |          dk    r8 ||                     |                    }d| ||d                   z
  z  |z   }dd}	|                    |          }t          t	          |          dz
  dd          D ]B}
||
         }|                                r ||          }	t          |	          :d|	z  ||
<   Crfd|D             }d}|rJ|d         	                                }|d	k    s|d         d
k    r|dz  }|
                    d           nn|J|                    |           ||z  }||                    |          z   S )zbReturn selected text as a processed text,
        to be executable in a Python/IPython interpreterc                 d    t          |           t          |                                           z
  S r,   )lenlstrip)lines    r1   <lambda>zETextEditBaseWidget.get_selection_as_executable_code.<locals>.<lambda>  s!    s4yyT[[]]););; r3   Nr   rk   r   i  r   c                 $    g | ]}|d          S r,   r   ).0r  
min_indents     r1   
<listcomp>zGTextEditBaseWidget.get_selection_as_executable_code.<locals>.<listcomp>   s"    9994T*++&999r3    #)get_line_separatorget_selection_boundsr  splitr  get_text_liner   stripminr  popr   join)r0   r   ls_indent	line_fromline_tor   linesoriginal_indentcurrent_indentr   r  lines_removed
first_lineleading_lines_strr#  s                  @r1    get_selection_as_executable_codez3TextEditBaseWidget.get_selection_as_executable_code  s    $$&&;;!66v>>	7%%f-- 	F

2u::>>%gd&8&8&C&CDDOq):)::;TAD 


2s5zz!|R,, 	0 	0A8Dzz|| 0!( <<

/a 	:99995999E  	q**JR:a=C#7#7"		!  	 	R . 2775>>11r3   c                    ||                                  }|                                 }|                     |          \  }}|                     |          \  }}|                                }|                                                     |          }t          |          s0|dk    r*|                                                     |dz
            }|                     |          }|||z  |z   }||fS )z(Return cell contents as executable code.Nr   r   )	r   r'  r   r(  r   document	findBlockr   r9  )r0   r   r/  __r1  startr   r   s           r1   get_cell_as_executable_codez.TextEditBaseWidget.get_cell_as_executable_code7  s    >__&&F$$&&--f55
11&99	2%%''))%00e$$ 	9MMOO--eai88E44V<<	>D(DU{r3   c                    ||                                  }| j        r_| j        \  }}|                                }|                                }||                                cxk    r|k     rn n||fS d| _        |                                }	 t          |          r|                                j        }n1t          t          |d|                                                     }|j                                        }d}|                    |           n1# t          $ r$ d}|                    t          j                   Y nw xY w	 t          t          |d|                                                     }	|	j                                        }d}
|                    |t          j                   n<# t          $ r/ d}
|                    t          j        t          j                   Y nw xY w|o|
}||f| _        ||fS )z
        Select cell under cursor in the visible portion of the file
        cell = group of lines separated by CELL_SEPARATORS
        returns
         -the textCursor
         -a boolean indicating if the entire file is selected
        NFr   	cell_listT)r   r-   r   r   r   r   r   userDataoedatanextr   r  r   StopIterationr   r   Startr   End)r0   r   r-   cell_full_filecell_start_poscell_end_positionr   headercell_at_file_startfootercell_at_file_ends              r1   r   z&TextEditBaseWidget.select_current_cellI  sK    >__&&F 	)+/+<(L.)88::N , 9 9 ; ;!2!2FFFF5FFFFFF#^33$(!	3e$$ 5))0n5"00224 4 4 5 5 $\2244N!&~.... 	3 	3 	3!% 122222	3

	I.t,,..0 0 0 1 1F !' 5 5 7 7$0+2HIIII 	I 	I 	I#1GHHHHH	I
 ,@0@#^4~%%s&   B
D +E
EA,F< <6G54G5c                 `   |                                  }|                                }	 t          t          |d|                                                     }|                    |j                                                   n# t          $ r Y dS w xY w|                     |           dS )zGo to the next cell of linesTrA  N)	r   r   rE  r   r  r   r   rF  setTextCursor)r0   r   r   rN  s       r1   go_to_next_cellz"TextEditBaseWidget.go_to_next_cell  s    ""	.t,,..0 0 0 1 1F v|44667777 	 	 	FF	6"""""s   AB 
BBc                    |                                  }|                                }t          |          r|                                }	 t	          t          |d|                                                     }|                    |j                                                   n# t          $ r Y dS w xY w| 
                    |           dS )z Go to the previous cell of linesFrA  N)r   r   r   previousrE  r   r  r   r   rF  rQ  )r0   r   r   rL  s       r1   go_to_previous_cellz&TextEditBaseWidget.go_to_previous_cell  s    ""%   	%NN$$E	.u,,..0 0 0 1 1F v|44667777 	 	 	FF	6"""""s   AB+ +
B98B9c                 *    |                                  S )z!Return document total line number)r   r/   s    r1   get_line_countz!TextEditBaseWidget.get_line_count  s       r3   c                     | j          | j        | j           d| _         t          t          |                               |           dS )z
        Override Qt method to restore text selection after text gets inserted
        at the current position of the cursor.

        See spyder-ide/spyder#11089 for more info.
        N)r8   &_TextEditBaseWidget__restore_selectionr  r"   
paintEvent)r0   err   s     r1   rZ  zTextEditBaseWidget.paintEvent  sN     &2$D$d&ABB*.D' $''22155555r3   c                 z    |                                  }|                                |                                fS )z8Save current cursor selection and return position bounds)r   r   r   )r0   r   s     r1   __save_selectionz#TextEditBaseWidget.__save_selection  s4    ""$$&&(;(;(=(===r3   c                     |                                  }|                    |           |                    |t          j                   |                     |           dS )z-Restore cursor selection from position boundsNr   r   r   r   rQ  )r0   r   r   r   s       r1   __restore_selectionz&TextEditBaseWidget.__restore_selection  sX    ""9%%%7K$:;;;6"""""r3   Tc                    |                                  }|                                 |                                }|                                 \  }}|}t	          |                                          r|                    |           |                    t          j	        t          j
                   t	          |                                          s3|                    t          j                   |                                }|                    |           |                    t          j	                   |                                |k    r|                    t          j        t          j
                   |                                rKt          |          }|                                 |                    |                                            nB|                    t          j        t          j
                   |                                |k    |                                }|                                 |sj|                    |           |                    t          j	                   |t%          |          z  }|t%          |          z  }|t%          |          z  }||k    r
||f| _        n	||f| _        |                    |           |                                 dS )z'Duplicate current line or selected textN)r   beginEditBlockr   #_TextEditBaseWidget__save_selectionr   r   r   r   r   StartOfBlockr   PreviousBlock
EndOfBlockatEndr   
insertTextr'  	NextBlockr  r8   endEditBlock)	r0   after_current_liner   cur_posr   r   end_pos_origcursor_tempr   s	            r1   __duplicate_line_or_selectionz0TextEditBaseWidget.__duplicate_line_or_selection  s   ""//##!2244	7&--//00 	,w'''  8 + 68 8 8!&"5"5"7"788 ,##K$=>>> //++9%%%K4555oo7** 68NOOO||~~ )&11**,,,&&t'>'>'@'@AAA 5{7MNNN oo7** ""$$! 	!y))) 8999T"ICII%Ls4yy G i+7*CD''+4l*CD'$r3   c                 2    |                      d           dS )z
        Copy current line or selected text and paste the duplicated text
        *after* the current line or selected text.
        Frk  N0_TextEditBaseWidget__duplicate_line_or_selectionr/   s    r1   duplicate_line_downz&TextEditBaseWidget.duplicate_line_down  s!    
 	**e*DDDDDr3   c                 2    |                      d           dS )z
        Copy current line or selected text and paste the duplicated text
        *before* the current line or selected text.
        Trq  Nrr  r/   s    r1   duplicate_line_upz$TextEditBaseWidget.duplicate_line_up  s!    
 	**d*CCCCCr3   c                    |                                  }|                                 |                                 \  }}d}|                    |           |                    t
          j                   |                                }|                    |           |                                r||k    r>|                    t
          j	                   |                    t
          j
                   |                                }|                                r|                                r||k    rd}|                    |           |                                rC|sA|                                 |                     |           |                     ||           dS |                    |t
          j                   |rC|rA|                                 |                     |           |                     ||           dS t#          |                                          }|                                 |rt#          |                                                                          }t,          j        |dd         z   }|                    t
          j	                   |t1          |          dz   z  }|t1          |          z  }|                                s|dz  }n|rH|                                 |t,          j        z   }|                    t
          j                   |dz  }n|                    t
          j                   t#          |                                                                          }|t1          |          dz   z  }|t1          |          dz   z  }|                    |           |                                 |                     |           |                     ||           dS )z"Move current line or selected textFTNr   r   r   )r   rb  rc  r   r   r   rd  r   atBlockStartrf  ri  rg  atStartrj  rQ  rY  r   r   r   removeSelectedTextr   r   r  linesepr  deletePreviousCharre  rh  )r0   rk  r   r   r   	last_linesel_textr   s           r1   __move_line_or_selectionz+TextEditBaseWidget.__move_line_or_selection  s   ""!2244	7	 	9%%%K4555OO%%	 	7###""$$ 	79(<(< 6777 5666//## <<>> 	!&&(( !Gy,@,@ 	 	9%%%>> 	$6 	!!!v&&&$$Y888F7K$:;;; 	+ 	!!!v&&&$$Y888F "&"5"5"7"788!!### 	#!&,,.."5"5"7"788DzHQrTN2H 6777T1$Is4yy G<<>> 1  ?))+++#bj0##K$<===1##K$=>>>!&,,.."5"5"7"788DT1$Is4yy{"G(###6"""  G44444r3   c                 2    |                      d           dS )z%Move up current line or selected textFrq  N+_TextEditBaseWidget__move_line_or_selectionr/   s    r1   move_line_upzTextEditBaseWidget.move_line_upB  s    %%%?????r3   c                 2    |                      d           dS )z'Move down current line or selected textTrq  Nr  r/   s    r1   move_line_downz!TextEditBaseWidget.move_line_downF  s    %%%>>>>>r3   c                     |                      dd           |                     |                                            dS )z7Go to the end of the current line and create a new lineFN)
stdkey_endinsert_textr'  r/   s    r1   go_to_new_linez!TextEditBaseWidget.go_to_new_lineJ  s<    u%%%002233333r3   c                    |                                  }|                                |                                }}|                    |           |                    |t          j                   |                                rT|                    t          j        t          j                   |                    t          j	        t          j                   | 
                    |           dS )z*Extend current selection to complete linesN)r   r   r   r   r   r   rx  r   re  rf  rQ  r0   r   r   r   s       r1   "extend_selection_to_complete_linesz5TextEditBaseWidget.extend_selection_to_complete_linesO  s    ""#2244f6I6I6K6K7	9%%%7K$:;;;   	8 9 + 68 8 8 6 + 68 8 86"""""r3   c                 d   ||                                  }|                                 rR|                                  |                                |                                }}|                    |           n|                                x}}|                                 |                    |           |                    t          j
                   |                                |k    r|                    t          j        t          j                   |                                rnB|                    t          j        t          j                   |                                |k    |                                 |                                 |                                  dS )zDelete current line.N)r   has_selected_textr  r   r   r   r   rb  r   r   rd  rf  r   rg  ri  rz  rj  ensureCursorVisibler  s       r1   delete_linezTextEditBaseWidget.delete_line\  sz   >__&&F!!## 	433555!'!6!6!8!8&:M:M:O:OwIy))))"(//"3"33I9%%%K4555oo7** 68NOOO||~~  5{7MNNN	 oo7**
 	!!###  """""r3   c                     |                                  }|                    |           |                    |t          j                   |                     |           d S r,   r_  )r0   r>  endr   s       r1   set_selectionz TextEditBaseWidget.set_selectionr  sX    ""5!!!3 67776"""""r3   c                 &   |                      |          }|                                 }|                                |                                }}||k     rt	          ||g          }nt	          ||g          }|                     ||           dS )z.Unselect read-only parts in shell, like promptN)r   r   r   r   maxr  )r0   position_fromr   r>  r  s        r1   truncate_selectionz%TextEditBaseWidget.truncate_selectionx  s    ))-88""**,,f.A.A.C.Cs3;;.//EE}c*++C5#&&&&&r3   c                     |                      |          }|                      |          }|                                 }|                                }||k     s||k    r|                     |           dS dS )z:In shell, avoid editing text except between prompt and EOFN)r   r   r   set_cursor_position)r0   r  position_tor   cursor_positions        r1   restrict_cursor_positionz+TextEditBaseWidget.restrict_cursor_position  s~    ))-88''44"" //++]**o.K.K$$[11111 /L.Kr3   c                 8    | j                                          dS )z1Completion list is active, Enter was just pressedN)rD   item_selectedr/   s    r1   select_completion_listz)TextEditBaseWidget.select_completion_list  s    ,,.....r3   c                 d   |sdS |                                  }|                                 }|                                 \  }}t          |t                    rd|v r|d         d         }|d         }|d         }	t          |d         t                    r&|d         |d         }}
|                     |
|          }t          |d         t                    r&|	d         |	d         }}|                     ||          }	|                    |           |                    |	t          j                   t          |d         d                   }nQ|}d}t          |t                    r|d	         }|d
         }t          |          }| 
                    dd          }||\  }|t                    z   }d}|                                 dk    r	dk    rd}n|t          j        k    r| j        v rd}|t          j        k    rg d}t!          fd|D                       r|}n                    d          rt                    dk    r|dz
  }nˉdk    r|                                  }d}|sd|                    t          j        t          j                   t          |                                          }|                    |          rd}|dt          |                                          |                                }|                                }|sG||cxk    r|k    sn dS |                    |           |                    |t          j                   n5|                                |k    rdS n|                                |k    rdS |r| j                            ||           |                                 |                     |           |                                 dk    r|                     |           dS | j                            |           dS )a2  
        Insert a completion into the editor.

        completion_position is where the completion was generated.

        The replacement range is computed using the (LSP) completion's
        textEdit field if it exists. Otherwise, we replace from the
        start of the word under the cursor.
        NtextEditr   r>  r  r  r   newTextrh  r   TF)
completionvalid_python_variableconsole.)"'/\c                 :    g | ]}                     |          S r   )endswith)r"  ccurrent_texts     r1   r$  z8TextEditBaseWidget.insert_completion.<locals>.<listcomp>  s'    IIIa..q11IIIr3   r   )r   r  get_selection_start_endrB   dictget_position_line_numberr   r   r   rt   get_current_word_and_positionr  rp   r   FILEauto_completion_charactersanyr  r   r   r   
startswithr   r   r   sig_will_remove_selectionemitrz  rQ  r  sig_insert_completion)r0   r  completion_positionr   r  selection_startselection_endcompletion_ranger>  r  
start_line	start_colend_lineend_colr   r   resultstart_positionend_positionis_auto_completion_characterspecial_charscursor_1found_startr   r  s                           @r1   insert_completionz$TextEditBaseWidget.insert_completion  s     	F"" 2244)-)E)E)G)G&j$'' _	J*,D,D)*5g>$W-E"5)C*73T:: M(-fu[7II
55j)LL*73T:: G$'K[1A'33HgFFu%%%sK$:;;;z*-i899DDDD*d++ *!,/!&)t99D 77u 8 > >F!/5,n-L0A0AA 05,??$$	11#s**7;4  2 777$(GGG7;4 -222$9$9$9MIIII=III  ?
 *6%..s33 ?L8I8IA8M8M *6)9%,, $(??#4#4&+ #. 3$11 + = + 6  
 ),H,A,A,C,C(D(DI#y99 3.2 #. 3 (+8+@+@+B+B'C'C)1)@)@)B)B'/'<'<'>'>3  '*=MMMMMMMM&&~666 &&|[5KLLLL ((,??? @ ??$$(;;;F 	P*//OOO!!###6""" ??	))T"""""&++D11111r3   c                 X    	 | j                                         S # t          $ r Y dS w xY w)z0Return True is completion list widget is visibleF)rD   	isVisibleRuntimeErrorr/   s    r1   is_completion_widget_visiblez/TextEditBaseWidget.is_completion_widget_visible  s@    	)33555 	 	 	 55	s    
))c                     	 | j                             |           n# t          $ r Y nw xY wt          j                     dS )z#Hide completion widget and tooltip.focus_to_parentN)rD   hider  r   hideText)r0   r  s     r1   hide_completion_widgetz)TextEditBaseWidget.hide_completion_widget  sY    	"'''HHHH 	 	 	D	s    
++c                     |                                  s*|                     t          j        t          j                   |                                  d S r,   )r  
moveCursorr   r   r   remove_selected_textr/   s    r1   stdkey_clearzTextEditBaseWidget.stdkey_clear%  sI    %%'' 	OOOK5{7MNNN!!#####r3   c                     |                                  s*|                     t          j        t          j                   |                                  d S r,   )r  r  r   r   r   r  r/   s    r1   stdkey_backspacez#TextEditBaseWidget.stdkey_backspace*  sO    %%'' 	4OOK9'24 4 4!!#####r3   c                 6    |rt           j        nt           j        S r,   )r   r   
MoveAnchorr0   shifts     r1   __get_move_modez"TextEditBaseWidget.__get_move_mode0  s    ).J{%%K4JJr3   c                 l    |                      t          j        |                     |                     d S r,   )r  r   Up"_TextEditBaseWidget__get_move_moder  s     r1   	stdkey_upzTextEditBaseWidget.stdkey_up3  s,    (<(<U(C(CDDDDDr3   c                 l    |                      t          j        |                     |                     d S r,   )r  r   Downr  r  s     r1   stdkey_downzTextEditBaseWidget.stdkey_down6  s-    ($*>*>u*E*EFFFFFr3   c                 :    |                      | j                   d S r,   )r  r@   r/   s    r1   
stdkey_tabzTextEditBaseWidget.stdkey_tab9  s    *+++++r3   c                 B   |                      |          }|r"|                     t          j        |           dS |                                 }||                     d          }n|                     |          }|                     |d          }|t          |          z   t          |                                          z
  }|	                                |k    r|
                    ||           n|
                    ||           |                     |           dS )zhSmart HOME feature: cursor is first moved at
        indentation position, then at the start of the lineNsoleol)r  r  r   rG  r   r   r   r  r  r   r   rQ  )	r0   r  r  
prompt_pos	move_moder   r  r   
indent_poss	            r1   stdkey_homezTextEditBaseWidget.stdkey_home<  s    ((//	 	'OOK-y99999__&&F!!%!2!25!9!9!%!2!2:!>!>==77D'D		1#dkkmm2D2DDJ  J.."":y9999"">9===v&&&&&r3   c                     |                      |          }|r"|                     t          j        |           d S |                     t          j        |           d S r,   )r  r  r   rH  rf  )r0   r  r  r  s       r1   r  zTextEditBaseWidget.stdkey_endP  sV    ((//	 	?OOKOY77777OOK2I>>>>>r3   c                    |                                 t          j        k    r| j                                         n;|                                 t          j        k    r| j                                         t          j        	                    d          r|                                 t          j
        k    rp| j                                         |                                  t          t          j        |                                t          j        t          j        t          j                  }t)          j        | |           t)          j        | |           t.          j        }t.          j        }t5          j                                        |          }t5          j                                        |          }t5          j                                        ||           |                                  t5          j                                        ||           dS | j                                         t)          j        | |           dS )zReimplement Qt methodlinux)r   N)buttonr   XButton1sig_prev_cursorr  XButton2sig_next_cursorsysplatformr  	MidButtonrG   r  setFocusr   r   MouseButtonPressr   
LeftButton
NoModifierr   mousePressEventmouseReleaseEventr	   	Clipboard	Selectionr   r  r   r  paste)r0   r  	mode_clipmode_sel	text_cliptext_sels         r1   r  z"TextEditBaseWidget.mousePressEventX  s    <<>>R[(( %%''''\\^^r{** %%'''<""7++ 	8",0N0N$$&&&MMOOO 7 "r}bmM ME*4777,T5999
 #,I!+H$.005595EEI#-//44(4CCH"$$,,XI,FFFJJLLL"$$,,YY,GGGGG$$&&&*477777r3   c                     | j                                          | j                                         t          j        | |           dS zReimplemented to handle focusN)focus_changedr  focus_inr   focusInEventr0   r  s     r1   r  zTextEditBaseWidget.focusInEventw  sD    !!!#D%00000r3   c                 b    | j                                          t          j        | |           dS r  )r	  r  r   focusOutEventr  s     r1   r  z TextEditBaseWidget.focusOutEvent}  s0    !!!$T511111r3   c                 \   t           j        dk    r/|                                t          j        z  rt          |d          r|                                                                dk     r| j        	                                 n|                                                                dk    r| j
        	                                 nst          |d          rc|                                dk     r| j        	                                 n1|                                dk    r| j
        	                                 dS t          j        | |           t          j                    }t!          |t"                    r|                     d           dS |                     d           dS )	z>Reimplemented to emit zoom in/out signals when Ctrl is presseddarwin
angleDeltar   deltaNTr  F)r  r  r  r   r  hasattrr  yzoom_outr  zoom_inr  r   
wheelEventr   focusWidgetrB   r   r  )r0   r  current_widgets      r1   r  zTextEditBaseWidget.wheelEvent  s    <8##  2#55 5,// 	,''))++--11**,,,,))++--//!33))+++UG,, ,{{}}q((**,,,,**))+++!$... &133n&677 	?'''====='''>>>>>r3   c                 x   t          j                    }|                    |                    |                    }|                                |                                |                                |                                f\  }}}}|                                }|rt          ||
                                          }t          ||                                          }t          ||
                                |                                z             }t          ||                                |                                z             }|                                                                 }	|                     |	          }	|                     |	          }	|	
                                |                                z   }
|
|k    r<|	                    |	
                                |                                z
             |	
                                |k     r|	                    |           |	                                |                                z   }|	
                                }||k    r|                                                                 }	|                     |	          }	|	                    |           |	                    |	                                |                                z
             ||                    |	          }	|                    |	           d S r,   )r   desktopavailableGeometryscreenNumberlefttoprightbottomrC   r  xr  r,  rn   r   
cursorRectbottomRightcalculate_real_positionmapToGlobalsetXtopRightsetYmapFromGlobalmove)r0   widgetr  srectr  r  r   r!  ancestorpointwidget_rightwidget_bottom
x_positions                r1   position_widget_at_cursorz,TextEditBaseWidget.position_widget_at_cursor  sz   &(())'*>*>v*F*FGG$)JJLL%))++$)KKMM5<<>>$C c5&==?? 	CtXZZ\\**Dc8::<<((Cxzz||hnn.>.>>??E0A0A!ABBF!!--//,,U33  ''wwyy6<<>>1%JJuwwyy6<<>>12227799tJJt 		FMMOO3WWYY
6!!OO%%..00E$$U++EJJz"""JJuwwyy6==??2333**511EEr3   c                     |S r,   r   )r0   r/  s     r1   r%  z*TextEditBaseWidget.calculate_real_position  s    r3   r,   )NNr   )T)Urs   
__module____qualname____doc__r   r   r
  r  r  
zoom_resetr	  rt   r  sig_eol_chars_changedr  r  r4   rF   r^   ra   re   rg   ri   r`   r{   r   r   r   r   r   r   r   r.   r   r   r   r   r   r?   r  r   r  r  r  r  r9  r?  r   rR  rU  rW  rZ  rc  rY  rs  rt  rv  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r3  r%  __classcell__)rr   s   @r1   r"   r"   '   s;       )vxxHfhhGvxxHJFHHM"F3KK"F3KKfhhOfhhOE( E( E( E(N< < <
) ) ), , ,+ + +' ' '- - -@ @ @" " "$	- 	- 	-  0      $? ? ?4 4 4C C C<4 4 4
       26\ \ \ \|F F F F" J  J  JF( ( ( ( 
TVVG G VG= = = = = 	A 	A 	A 	A 	A  
62 62 62 62p   $4& 4& 4& 4&l# # ## # #! ! !
6 
6 
6 
6 
6> > >
# # #0 0 0 0dE E ED D DI5 I5 I5 I5V@ @ @? ? ?4 4 4
# # ## # # #,# # #	' 	' 	'2 2 2/ / /~2 ~2 ~2@     $ $ $
$ $ $K K KE E EG G G, , ,' ' ' '(? ? ?8 8 8>1 1 12 2 2
? ? ?>& & &P      r3   r"   )1r7  r  r  qtpy.compatr   qtpy.QtCorer   r   r   r   r   
qtpy.QtGuir	   r
   r   r   r   r   qtpy.QtWidgetsr   r   r   r   spyder.config.guir   spyder.config.managerr   $spyder.plugins.editor.api.decorationr   r   &spyder.plugins.editor.utils.decorationr   (spyder.plugins.editor.widgets.completionr   spyder.plugins.completion.apir   "spyder.plugins.outlineexplorer.apir   r   spyder.py3compatr   spyder.utils.paletter   spyder.widgets.calltipr   r   spyder.widgets.mixinsr    r"   r   r3   r1   <module>rJ     s      
			 



 $ # # # # # 8 8 8 8 8 8 8 8 8 8 8 8 8 82 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 N N N N N N N N N N N N ' & & & & & & & & & & & L L L L L L L L I I I I I I E E E E E E < < < < < < M M M M M M M M + + + + + + . . . . . . ? ? ? ? ? ? ? ? / / / / / /c c c c c c c c c cr3   