2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* text_edit.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
2020-01-01 11:16:22 +01:00
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2014-02-10 02:10:30 +01:00
# include "text_edit.h"
2017-08-27 21:07:15 +02:00
2020-11-07 23:33:38 +01:00
# include "core/config/project_settings.h"
2020-04-28 15:19:37 +02:00
# include "core/input/input.h"
2020-11-07 23:33:38 +01:00
# include "core/object/message_queue.h"
# include "core/object/script_language.h"
2018-09-11 18:13:45 +02:00
# include "core/os/keyboard.h"
# include "core/os/os.h"
2020-09-18 09:36:10 +02:00
# include "core/string/translation.h"
2020-03-04 02:51:12 +01:00
# include "scene/main/window.h"
2014-02-10 02:10:30 +01:00
2017-09-17 06:33:44 +02:00
# ifdef TOOLS_ENABLED
# include "editor/editor_scale.h"
# endif
2014-04-27 12:34:37 +02:00
# define TAB_PIXELS
2014-02-10 02:10:30 +01:00
2020-07-27 12:43:20 +02:00
inline bool _is_symbol ( char32_t c ) {
2017-12-23 09:59:54 +01:00
return is_symbol ( c ) ;
2014-02-10 02:10:30 +01:00
}
2020-07-27 12:43:20 +02:00
static bool _is_text_char ( char32_t c ) {
2019-03-09 19:45:06 +01:00
return ! is_symbol ( c ) ;
2014-02-10 02:10:30 +01:00
}
2020-07-27 12:43:20 +02:00
static bool _is_whitespace ( char32_t c ) {
2017-08-01 19:40:43 +02:00
return c = = ' \t ' | | c = = ' ' ;
}
2020-07-27 12:43:20 +02:00
static bool _is_char ( char32_t c ) {
2017-03-05 16:44:50 +01:00
return ( c > = ' a ' & & c < = ' z ' ) | | ( c > = ' A ' & & c < = ' Z ' ) | | c = = ' _ ' ;
2016-03-21 16:45:38 +01:00
}
2020-07-27 12:43:20 +02:00
static bool _is_pair_right_symbol ( char32_t c ) {
2017-03-05 16:44:50 +01:00
return c = = ' " ' | |
c = = ' \' ' | |
c = = ' ) ' | |
c = = ' ] ' | |
c = = ' } ' ;
2014-04-27 12:34:37 +02:00
}
2020-07-27 12:43:20 +02:00
static bool _is_pair_left_symbol ( char32_t c ) {
2017-03-05 16:44:50 +01:00
return c = = ' " ' | |
c = = ' \' ' | |
c = = ' ( ' | |
c = = ' [ ' | |
c = = ' { ' ;
2014-04-27 12:34:37 +02:00
}
2020-07-27 12:43:20 +02:00
static bool _is_pair_symbol ( char32_t c ) {
2015-01-02 19:08:40 +01:00
return _is_pair_left_symbol ( c ) | | _is_pair_right_symbol ( c ) ;
2014-04-27 12:34:37 +02:00
}
2020-07-27 12:43:20 +02:00
static char32_t _get_right_pair_symbol ( char32_t c ) {
2020-05-14 16:41:43 +02:00
if ( c = = ' " ' ) {
2015-01-02 19:08:40 +01:00
return ' " ' ;
2020-05-14 16:41:43 +02:00
}
if ( c = = ' \' ' ) {
2015-01-02 19:08:40 +01:00
return ' \' ' ;
2020-05-14 16:41:43 +02:00
}
if ( c = = ' ( ' ) {
2015-01-02 19:08:40 +01:00
return ' ) ' ;
2020-05-14 16:41:43 +02:00
}
if ( c = = ' [ ' ) {
2015-01-02 19:08:40 +01:00
return ' ] ' ;
2020-05-14 16:41:43 +02:00
}
if ( c = = ' { ' ) {
2015-01-02 19:08:40 +01:00
return ' } ' ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
return 0 ;
2014-04-27 12:34:37 +02:00
}
2019-05-08 23:56:00 +02:00
static int _find_first_non_whitespace_column_of_line ( const String & line ) {
int left = 0 ;
2020-05-14 16:41:43 +02:00
while ( left < line . length ( ) & & _is_whitespace ( line [ left ] ) ) {
2019-05-08 23:56:00 +02:00
left + + ;
2020-05-14 16:41:43 +02:00
}
2019-05-08 23:56:00 +02:00
return left ;
}
2020-07-25 02:15:23 +02:00
///////////////////////////////////////////////////////////////////////////////
2017-03-05 16:44:50 +01:00
void TextEdit : : Text : : set_font ( const Ref < Font > & p_font ) {
font = p_font ;
2014-02-10 02:10:30 +01:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : set_font_size ( int p_font_size ) {
font_size = p_font_size ;
}
2017-04-17 15:24:30 +02:00
void TextEdit : : Text : : set_indent_size ( int p_indent_size ) {
indent_size = p_indent_size ;
2014-02-10 02:10:30 +01:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : set_font_features ( const Dictionary & p_features ) {
opentype_features = p_features ;
}
2015-09-09 18:44:31 +02:00
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : set_direction_and_language ( TextServer : : Direction p_direction , String p_language ) {
direction = p_direction ;
language = p_language ;
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : set_draw_control_chars ( bool p_draw_control_chars ) {
draw_control_chars = p_draw_control_chars ;
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
int TextEdit : : Text : : get_line_width ( int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , 0 ) ;
return text [ p_line ] . data_buf - > get_size ( ) . x ;
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
int TextEdit : : Text : : get_line_height ( int p_line , int p_wrap_index ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , 0 ) ;
return text [ p_line ] . data_buf - > get_line_size ( p_wrap_index ) . y ;
2014-02-10 02:10:30 +01:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : set_width ( float p_width ) {
width = p_width ;
}
int TextEdit : : Text : : get_line_wrap_amount ( int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , 0 ) ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
return text [ p_line ] . data_buf - > get_line_count ( ) - 1 ;
}
Vector < Vector2i > TextEdit : : Text : : get_line_wrap_ranges ( int p_line ) const {
Vector < Vector2i > ret ;
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , ret ) ;
for ( int i = 0 ; i < text [ p_line ] . data_buf - > get_line_count ( ) ; i + + ) {
ret . push_back ( text [ p_line ] . data_buf - > get_line_range ( i ) ) ;
2015-01-02 19:08:40 +01:00
}
2020-09-18 09:36:10 +02:00
return ret ;
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
const Ref < TextParagraph > TextEdit : : Text : : get_line_data ( int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , Ref < TextParagraph > ( ) ) ;
return text [ p_line ] . data_buf ;
2014-02-10 02:10:30 +01:00
}
2020-09-18 09:36:10 +02:00
_FORCE_INLINE_ const String & TextEdit : : Text : : operator [ ] ( int p_line ) const {
return text [ p_line ] . data ;
}
void TextEdit : : Text : : invalidate_cache ( int p_line , int p_column , const String & p_ime_text , const Vector < Vector2i > & p_bidi_override ) {
2018-01-26 02:41:17 +01:00
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
2020-09-18 09:36:10 +02:00
if ( font . is_null ( ) | | font_size < = 0 ) {
return ; // Not in tree?
}
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
text . write [ p_line ] . data_buf - > clear ( ) ;
text . write [ p_line ] . data_buf - > set_width ( width ) ;
text . write [ p_line ] . data_buf - > set_direction ( ( TextServer : : Direction ) direction ) ;
text . write [ p_line ] . data_buf - > set_preserve_control ( draw_control_chars ) ;
if ( p_ime_text . length ( ) > 0 ) {
text . write [ p_line ] . data_buf - > add_string ( p_ime_text , font , font_size , opentype_features , language ) ;
if ( ! p_bidi_override . empty ( ) ) {
TS - > shaped_text_set_bidi_override ( text . write [ p_line ] . data_buf - > get_rid ( ) , p_bidi_override ) ;
}
} else {
text . write [ p_line ] . data_buf - > add_string ( text [ p_line ] . data , font , font_size , opentype_features , language ) ;
if ( ! text [ p_line ] . bidi_override . empty ( ) ) {
TS - > shaped_text_set_bidi_override ( text . write [ p_line ] . data_buf - > get_rid ( ) , text [ p_line ] . bidi_override ) ;
}
}
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
// Apply tab align.
if ( indent_size > 0 ) {
Vector < float > tabs ;
tabs . push_back ( font - > get_char_size ( ' m ' , 0 , font_size ) . width * indent_size ) ;
text . write [ p_line ] . data_buf - > tab_align ( tabs ) ;
}
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : invalidate_all_lines ( ) {
2018-01-26 02:41:17 +01:00
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
2020-09-18 09:36:10 +02:00
text . write [ i ] . data_buf - > set_width ( width ) ;
if ( indent_size > 0 ) {
Vector < float > tabs ;
tabs . push_back ( font - > get_char_size ( ' m ' , 0 , font_size ) . width * indent_size ) ;
text . write [ i ] . data_buf - > tab_align ( tabs ) ;
}
2018-01-26 02:41:17 +01:00
}
}
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : invalidate_all ( ) {
2018-01-26 02:41:17 +01:00
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
2020-09-18 09:36:10 +02:00
invalidate_cache ( i ) ;
2018-01-26 02:41:17 +01:00
}
2014-02-10 02:10:30 +01:00
}
void TextEdit : : Text : : clear ( ) {
2017-01-14 18:03:38 +01:00
text . clear ( ) ;
2020-09-18 09:36:10 +02:00
insert ( 0 , " " , Vector < Vector2i > ( ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-12-07 00:31:09 +01:00
int TextEdit : : Text : : get_max_width ( bool p_exclude_hidden ) const {
2019-08-18 17:56:24 +02:00
// Quite some work, but should be fast enough.
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
int max = 0 ;
2017-12-07 00:31:09 +01:00
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
2020-05-14 16:41:43 +02:00
if ( ! p_exclude_hidden | | ! is_hidden ( i ) ) {
2017-12-07 00:31:09 +01:00
max = MAX ( max , get_line_width ( i ) ) ;
2020-05-14 16:41:43 +02:00
}
2017-12-07 00:31:09 +01:00
}
2015-01-02 19:08:40 +01:00
return max ;
2014-02-10 02:10:30 +01:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : set ( int p_line , const String & p_text , const Vector < Vector2i > & p_bidi_override ) {
2017-03-05 16:44:50 +01:00
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
2016-03-09 00:00:52 +01:00
2018-07-25 03:11:03 +02:00
text . write [ p_line ] . data = p_text ;
2020-09-18 09:36:10 +02:00
text . write [ p_line ] . bidi_override = p_bidi_override ;
invalidate_cache ( p_line ) ;
2014-02-10 02:10:30 +01:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : Text : : insert ( int p_at , const String & p_text , const Vector < Vector2i > & p_bidi_override ) {
2015-01-02 19:08:40 +01:00
Line line ;
2020-07-25 02:15:23 +02:00
line . gutters . resize ( gutter_count ) ;
2017-03-05 16:44:50 +01:00
line . marked = false ;
2017-11-13 00:12:17 +01:00
line . hidden = false ;
2017-03-05 16:44:50 +01:00
line . data = p_text ;
2020-09-18 09:36:10 +02:00
line . bidi_override = p_bidi_override ;
2017-03-05 16:44:50 +01:00
text . insert ( p_at , line ) ;
2020-09-18 09:36:10 +02:00
invalidate_cache ( p_at ) ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2014-02-10 02:10:30 +01:00
void TextEdit : : Text : : remove ( int p_at ) {
2015-01-02 19:08:40 +01:00
text . remove ( p_at ) ;
2014-02-10 02:10:30 +01:00
}
2020-07-25 02:15:23 +02:00
void TextEdit : : Text : : add_gutter ( int p_at ) {
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
if ( p_at < 0 | | p_at > gutter_count ) {
text . write [ i ] . gutters . push_back ( Gutter ( ) ) ;
} else {
text . write [ i ] . gutters . insert ( p_at , Gutter ( ) ) ;
}
}
gutter_count + + ;
}
void TextEdit : : Text : : remove_gutter ( int p_gutter ) {
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
text . write [ i ] . gutters . remove ( p_gutter ) ;
}
gutter_count - - ;
}
void TextEdit : : Text : : move_gutters ( int p_from_line , int p_to_line ) {
text . write [ p_to_line ] . gutters = text [ p_from_line ] . gutters ;
text . write [ p_from_line ] . gutters . clear ( ) ;
text . write [ p_from_line ] . gutters . resize ( gutter_count ) ;
}
////////////////////////////////////////////////////////////////////////////////
2014-02-10 02:10:30 +01:00
void TextEdit : : _update_scrollbars ( ) {
2015-01-02 19:08:40 +01:00
Size2 size = get_size ( ) ;
Size2 hmin = h_scroll - > get_combined_minimum_size ( ) ;
Size2 vmin = v_scroll - > get_combined_minimum_size ( ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
v_scroll - > set_begin ( Point2 ( size . width - vmin . width , cache . style_normal - > get_margin ( MARGIN_TOP ) ) ) ;
v_scroll - > set_end ( Point2 ( size . width , size . height - cache . style_normal - > get_margin ( MARGIN_TOP ) - cache . style_normal - > get_margin ( MARGIN_BOTTOM ) ) ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
h_scroll - > set_begin ( Point2 ( 0 , size . height - hmin . height ) ) ;
h_scroll - > set_end ( Point2 ( size . width - vmin . width , size . height ) ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
int visible_rows = get_visible_rows ( ) ;
2018-01-26 02:41:17 +01:00
int total_rows = get_total_visible_rows ( ) ;
2016-03-07 03:32:51 +01:00
if ( scroll_past_end_of_file_enabled ) {
2017-11-16 05:00:27 +01:00
total_rows + = visible_rows - 1 ;
2016-03-07 03:32:51 +01:00
}
2015-01-02 19:08:40 +01:00
int visible_width = size . width - cache . style_normal - > get_minimum_size ( ) . width ;
2020-07-25 02:15:23 +02:00
int total_width = text . get_max_width ( true ) + vmin . x + gutters_width + gutter_padding ;
2016-03-09 00:00:52 +01:00
2019-08-11 20:31:19 +02:00
if ( draw_minimap ) {
total_width + = cache . minimap_width ;
}
2017-03-05 16:44:50 +01:00
updating_scrolls = true ;
2016-03-09 00:00:52 +01:00
2020-06-20 20:56:17 +02:00
if ( total_rows > visible_rows ) {
2015-01-02 19:08:40 +01:00
v_scroll - > show ( ) ;
2018-01-26 02:41:17 +01:00
v_scroll - > set_max ( total_rows + get_visible_rows_offset ( ) ) ;
v_scroll - > set_page ( visible_rows + get_visible_rows_offset ( ) ) ;
2017-08-19 16:23:45 +02:00
if ( smooth_scroll_enabled ) {
v_scroll - > set_step ( 0.25 ) ;
} else {
v_scroll - > set_step ( 1 ) ;
}
2018-01-26 02:41:17 +01:00
set_v_scroll ( get_v_scroll ( ) ) ;
2017-11-21 15:34:04 +01:00
2017-03-05 16:44:50 +01:00
} else {
2015-01-02 19:08:40 +01:00
cursor . line_ofs = 0 ;
2018-01-26 02:41:17 +01:00
cursor . wrap_ofs = 0 ;
2017-11-16 05:00:27 +01:00
v_scroll - > set_value ( 0 ) ;
2015-01-02 19:08:40 +01:00
v_scroll - > hide ( ) ;
}
2016-03-09 00:00:52 +01:00
2020-06-20 20:56:17 +02:00
if ( total_width > visible_width & & ! is_wrap_enabled ( ) ) {
2015-01-02 19:08:40 +01:00
h_scroll - > show ( ) ;
h_scroll - > set_max ( total_width ) ;
h_scroll - > set_page ( visible_width ) ;
2020-05-14 16:41:43 +02:00
if ( cursor . x_ofs > ( total_width - visible_width ) ) {
2017-12-07 00:31:09 +01:00
cursor . x_ofs = ( total_width - visible_width ) ;
2020-05-14 16:41:43 +02:00
}
2017-02-21 23:45:31 +01:00
if ( fabs ( h_scroll - > get_value ( ) - ( double ) cursor . x_ofs ) > = 1 ) {
h_scroll - > set_value ( cursor . x_ofs ) ;
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
} else {
2017-12-07 00:31:09 +01:00
cursor . x_ofs = 0 ;
h_scroll - > set_value ( 0 ) ;
2015-01-02 19:08:40 +01:00
h_scroll - > hide ( ) ;
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
updating_scrolls = false ;
2014-02-10 02:10:30 +01:00
}
2015-12-09 19:56:41 +01:00
void TextEdit : : _click_selection_held ( ) {
2018-06-14 09:10:30 +02:00
// Warning: is_mouse_button_pressed(BUTTON_LEFT) returns false for double+ clicks, so this doesn't work for MODE_WORD
// and MODE_LINE. However, moving the mouse triggers _gui_input, which calls these functions too, so that's not a huge problem.
// I'm unsure if there's an actual fix that doesn't have a ton of side effects.
2020-10-11 19:44:53 +02:00
if ( Input : : get_singleton ( ) - > is_mouse_button_pressed ( BUTTON_LEFT ) & & selection . selecting_mode ! = SelectionMode : : SELECTION_MODE_NONE ) {
2017-11-05 16:54:00 +01:00
switch ( selection . selecting_mode ) {
2020-10-11 19:44:53 +02:00
case SelectionMode : : SELECTION_MODE_POINTER : {
2017-11-05 16:54:00 +01:00
_update_selection_mode_pointer ( ) ;
} break ;
2020-10-11 19:44:53 +02:00
case SelectionMode : : SELECTION_MODE_WORD : {
2017-11-05 16:54:00 +01:00
_update_selection_mode_word ( ) ;
} break ;
2020-10-11 19:44:53 +02:00
case SelectionMode : : SELECTION_MODE_LINE : {
2017-11-05 16:54:00 +01:00
_update_selection_mode_line ( ) ;
} break ;
default : {
break ;
}
}
} else {
click_select_held - > stop ( ) ;
}
}
2015-12-09 19:56:41 +01:00
2020-09-18 09:36:10 +02:00
Point2 TextEdit : : _get_local_mouse_pos ( ) const {
Point2 mp = get_local_mouse_position ( ) ;
if ( is_layout_rtl ( ) ) {
mp . x = get_size ( ) . width - mp . x ;
}
return mp ;
}
2017-11-05 16:54:00 +01:00
void TextEdit : : _update_selection_mode_pointer ( ) {
2019-08-11 20:31:19 +02:00
dragging_selection = true ;
2020-09-18 09:36:10 +02:00
Point2 mp = _get_local_mouse_pos ( ) ;
2015-12-09 19:56:41 +01:00
2017-11-05 16:54:00 +01:00
int row , col ;
_get_mouse_pos ( Point2i ( mp . x , mp . y ) , row , col ) ;
2015-12-09 19:56:41 +01:00
2017-11-05 16:54:00 +01:00
select ( selection . selecting_line , selection . selecting_column , row , col ) ;
2015-12-09 19:56:41 +01:00
2018-01-26 02:41:17 +01:00
cursor_set_line ( row , false ) ;
2018-06-14 09:10:30 +02:00
cursor_set_column ( col ) ;
2017-11-05 16:54:00 +01:00
update ( ) ;
click_select_held - > start ( ) ;
}
void TextEdit : : _update_selection_mode_word ( ) {
2019-08-11 20:31:19 +02:00
dragging_selection = true ;
2020-09-18 09:36:10 +02:00
Point2 mp = _get_local_mouse_pos ( ) ;
2017-11-05 16:54:00 +01:00
int row , col ;
_get_mouse_pos ( Point2i ( mp . x , mp . y ) , row , col ) ;
2015-12-09 19:56:41 +01:00
2017-11-05 16:54:00 +01:00
String line = text [ row ] ;
int beg = CLAMP ( col , 0 , line . length ( ) ) ;
2019-08-18 17:56:24 +02:00
// If its the first selection and on whitespace make sure we grab the word instead.
2017-11-05 16:54:00 +01:00
if ( ! selection . active ) {
while ( beg > 0 & & line [ beg ] < = 32 ) {
beg - - ;
}
}
int end = beg ;
bool symbol = beg < line . length ( ) & & _is_symbol ( line [ beg ] ) ;
2019-08-18 17:56:24 +02:00
// Get the word end and begin points.
2017-11-05 16:54:00 +01:00
while ( beg > 0 & & line [ beg - 1 ] > 32 & & ( symbol = = _is_symbol ( line [ beg - 1 ] ) ) ) {
beg - - ;
}
while ( end < line . length ( ) & & line [ end + 1 ] > 32 & & ( symbol = = _is_symbol ( line [ end + 1 ] ) ) ) {
end + + ;
}
if ( end < line . length ( ) ) {
end + = 1 ;
}
2015-12-09 19:56:41 +01:00
2019-08-18 17:56:24 +02:00
// Initial selection.
2017-11-05 16:54:00 +01:00
if ( ! selection . active ) {
select ( row , beg , row , end ) ;
selection . selecting_column = beg ;
selection . selected_word_beg = beg ;
selection . selected_word_end = end ;
selection . selected_word_origin = beg ;
2018-06-14 09:10:30 +02:00
cursor_set_line ( selection . to_line , false ) ;
2017-11-05 16:54:00 +01:00
cursor_set_column ( selection . to_column ) ;
2015-12-09 19:56:41 +01:00
} else {
2017-11-05 16:54:00 +01:00
if ( ( col < = selection . selected_word_origin & & row = = selection . selecting_line ) | | row < selection . selecting_line ) {
selection . selecting_column = selection . selected_word_end ;
select ( row , beg , selection . selecting_line , selection . selected_word_end ) ;
2018-06-14 09:10:30 +02:00
cursor_set_line ( selection . from_line , false ) ;
2017-11-05 16:54:00 +01:00
cursor_set_column ( selection . from_column ) ;
} else {
selection . selecting_column = selection . selected_word_beg ;
select ( selection . selecting_line , selection . selected_word_beg , row , end ) ;
2018-06-14 09:10:30 +02:00
cursor_set_line ( selection . to_line , false ) ;
2017-11-05 16:54:00 +01:00
cursor_set_column ( selection . to_column ) ;
}
}
2015-12-09 19:56:41 +01:00
2017-11-05 16:54:00 +01:00
update ( ) ;
2018-06-14 09:10:30 +02:00
2017-11-05 16:54:00 +01:00
click_select_held - > start ( ) ;
}
void TextEdit : : _update_selection_mode_line ( ) {
2019-08-11 20:31:19 +02:00
dragging_selection = true ;
2020-09-18 09:36:10 +02:00
Point2 mp = _get_local_mouse_pos ( ) ;
2017-11-05 16:54:00 +01:00
int row , col ;
_get_mouse_pos ( Point2i ( mp . x , mp . y ) , row , col ) ;
col = 0 ;
if ( row < selection . selecting_line ) {
2019-08-18 17:56:24 +02:00
// Cursor is above us.
2018-01-26 02:41:17 +01:00
cursor_set_line ( row - 1 , false ) ;
2017-11-05 16:54:00 +01:00
selection . selecting_column = text [ selection . selecting_line ] . length ( ) ;
} else {
2019-08-18 17:56:24 +02:00
// Cursor is below us.
2018-01-26 02:41:17 +01:00
cursor_set_line ( row + 1 , false ) ;
2017-11-05 16:54:00 +01:00
selection . selecting_column = 0 ;
col = text [ row ] . length ( ) ;
2015-12-09 19:56:41 +01:00
}
2018-06-14 09:10:30 +02:00
cursor_set_column ( 0 ) ;
2017-11-05 16:54:00 +01:00
select ( selection . selecting_line , selection . selecting_column , row , col ) ;
update ( ) ;
click_select_held - > start ( ) ;
2015-12-09 19:56:41 +01:00
}
2019-08-25 15:38:14 +02:00
void TextEdit : : _update_minimap_click ( ) {
2020-09-18 09:36:10 +02:00
Point2 mp = _get_local_mouse_pos ( ) ;
2019-08-11 20:31:19 +02:00
int xmargin_end = get_size ( ) . width - cache . style_normal - > get_margin ( MARGIN_RIGHT ) ;
if ( ! dragging_minimap & & ( mp . x < xmargin_end - minimap_width | | mp . y > xmargin_end ) ) {
minimap_clicked = false ;
return ;
}
minimap_clicked = true ;
dragging_minimap = true ;
int row ;
_get_minimap_mouse_row ( Point2i ( mp . x , mp . y ) , row ) ;
2019-08-25 15:38:14 +02:00
if ( row > = get_first_visible_line ( ) & & ( row < get_last_visible_line ( ) | | row > = ( text . size ( ) - 1 ) ) ) {
minimap_scroll_ratio = v_scroll - > get_as_ratio ( ) ;
minimap_scroll_click_pos = mp . y ;
can_drag_minimap = true ;
return ;
}
2019-08-11 20:31:19 +02:00
int wi ;
int first_line = row - num_lines_from_rows ( row , 0 , - get_visible_rows ( ) / 2 , wi ) + 1 ;
double delta = get_scroll_pos_for_line ( first_line , wi ) - get_v_scroll ( ) ;
if ( delta < 0 ) {
_scroll_up ( - delta ) ;
} else {
_scroll_down ( delta ) ;
}
}
2019-08-25 15:38:14 +02:00
void TextEdit : : _update_minimap_drag ( ) {
if ( ! can_drag_minimap ) {
return ;
}
2019-08-31 16:43:19 +02:00
int control_height = _get_control_height ( ) ;
int scroll_height = v_scroll - > get_max ( ) * ( minimap_char_size . y + minimap_line_spacing ) ;
if ( control_height > scroll_height ) {
control_height = scroll_height ;
}
2020-09-18 09:36:10 +02:00
Point2 mp = _get_local_mouse_pos ( ) ;
2019-08-31 16:43:19 +02:00
double diff = ( mp . y - minimap_scroll_click_pos ) / control_height ;
2019-08-25 15:38:14 +02:00
v_scroll - > set_as_ratio ( minimap_scroll_ratio + diff ) ;
}
2014-02-10 02:10:30 +01:00
void TextEdit : : _notification ( int p_what ) {
2017-03-05 16:44:50 +01:00
switch ( p_what ) {
2014-12-17 02:31:57 +01:00
case NOTIFICATION_ENTER_TREE : {
_update_caches ( ) ;
2020-05-14 16:41:43 +02:00
if ( cursor_changed_dirty ) {
2017-03-05 16:44:50 +01:00
MessageQueue : : get_singleton ( ) - > push_call ( this , " _cursor_changed_emit " ) ;
2020-05-14 16:41:43 +02:00
}
if ( text_changed_dirty ) {
2017-03-05 16:44:50 +01:00
MessageQueue : : get_singleton ( ) - > push_call ( this , " _text_changed_emit " ) ;
2020-05-14 16:41:43 +02:00
}
2020-09-18 09:36:10 +02:00
_update_wrap_at ( true ) ;
2014-12-17 02:31:57 +01:00
} break ;
case NOTIFICATION_RESIZED : {
2018-01-26 02:41:17 +01:00
_update_scrollbars ( ) ;
2019-08-26 18:47:33 +02:00
_update_wrap_at ( ) ;
2014-12-17 02:31:57 +01:00
} break ;
2019-09-02 06:17:11 +02:00
case NOTIFICATION_VISIBILITY_CHANGED : {
if ( is_visible ( ) ) {
call_deferred ( " _update_scrollbars " ) ;
call_deferred ( " _update_wrap_at " ) ;
}
} break ;
2020-09-18 09:36:10 +02:00
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED :
case NOTIFICATION_TRANSLATION_CHANGED :
2014-12-17 02:31:57 +01:00
case NOTIFICATION_THEME_CHANGED : {
_update_caches ( ) ;
2020-09-18 09:36:10 +02:00
_update_wrap_at ( true ) ;
2016-05-29 16:37:26 +02:00
} break ;
2020-06-30 01:47:18 +02:00
case NOTIFICATION_WM_WINDOW_FOCUS_IN : {
2016-06-19 17:11:16 +02:00
window_has_focus = true ;
draw_caret = true ;
update ( ) ;
} break ;
2020-06-30 01:47:18 +02:00
case NOTIFICATION_WM_WINDOW_FOCUS_OUT : {
2016-06-19 17:11:16 +02:00
window_has_focus = false ;
draw_caret = false ;
update ( ) ;
} break ;
2018-04-11 09:28:14 +02:00
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS : {
2018-01-26 02:41:17 +01:00
if ( scrolling & & get_v_scroll ( ) ! = target_v_scroll ) {
double target_y = target_v_scroll - get_v_scroll ( ) ;
2017-08-19 16:23:45 +02:00
double dist = sqrt ( target_y * target_y ) ;
Fix misc. source comment typos
Found using `codespell -q 3 -S ./thirdparty,*.po -L ang,ba,cas,dof,doubleclick,fave,hist,leapyear,lod,nd,numer,ois,paket,seeked,sinc,switchs,te,uint -D ~/Projects/codespell/codespell_lib/data/dictionary.txt `
2019-09-19 20:36:39 +02:00
// To ensure minimap is responsive override the speed setting.
2019-08-11 20:31:19 +02:00
double vel = ( ( target_y / dist ) * ( ( minimap_clicked ) ? 3000 : v_scroll_speed ) ) * get_physics_process_delta_time ( ) ;
2017-08-19 16:23:45 +02:00
2017-09-02 14:17:13 +02:00
if ( Math : : abs ( vel ) > = dist ) {
2018-01-26 02:41:17 +01:00
set_v_scroll ( target_v_scroll ) ;
2017-08-19 16:23:45 +02:00
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
2018-04-11 09:28:14 +02:00
set_physics_process_internal ( false ) ;
2017-08-19 16:23:45 +02:00
} else {
2018-01-26 02:41:17 +01:00
set_v_scroll ( get_v_scroll ( ) + vel ) ;
2017-08-19 16:23:45 +02:00
}
} else {
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
2018-04-11 09:28:14 +02:00
set_physics_process_internal ( false ) ;
2017-08-19 16:23:45 +02:00
}
} break ;
2014-12-17 02:31:57 +01:00
case NOTIFICATION_DRAW : {
2019-04-12 04:21:48 +02:00
if ( first_draw ) {
2019-08-18 17:56:24 +02:00
// Size may not be the final one, so attempts to ensure cursor was visible may have failed.
2019-04-12 04:21:48 +02:00
adjust_viewport_to_cursor ( ) ;
first_draw = false ;
}
2020-05-03 18:08:15 +02:00
/* Prevent the resource getting lost between the editor and game. */
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
if ( syntax_highlighter . is_valid ( ) & & syntax_highlighter - > get_text_edit ( ) ! = this ) {
syntax_highlighter - > set_text_edit ( this ) ;
}
}
2018-08-20 13:36:34 +02:00
Size2 size = get_size ( ) ;
2020-09-18 09:36:10 +02:00
bool rtl = is_layout_rtl ( ) ;
2016-06-19 17:11:16 +02:00
if ( ( ! has_focus ( ) & & ! menu - > has_focus ( ) ) | | ! window_has_focus ) {
draw_caret = false ;
}
2019-08-11 20:31:19 +02:00
cache . minimap_width = 0 ;
if ( draw_minimap ) {
cache . minimap_width = minimap_width ;
}
2014-12-17 02:31:57 +01:00
_update_scrollbars ( ) ;
2016-03-09 00:00:52 +01:00
2014-12-17 02:31:57 +01:00
RID ci = get_canvas_item ( ) ;
2020-03-27 19:21:27 +01:00
RenderingServer : : get_singleton ( ) - > canvas_item_set_clip ( get_canvas_item ( ) , true ) ;
2020-07-29 23:26:49 +02:00
int xmargin_beg = cache . style_normal - > get_margin ( MARGIN_LEFT ) + gutters_width + gutter_padding ;
2019-08-11 20:31:19 +02:00
int xmargin_end = size . width - cache . style_normal - > get_margin ( MARGIN_RIGHT ) - cache . minimap_width ;
2019-08-18 17:56:24 +02:00
// Let's do it easy for now.
2018-08-20 13:36:34 +02:00
cache . style_normal - > draw ( ci , Rect2 ( Point2 ( ) , size ) ) ;
2017-11-30 04:53:15 +01:00
if ( readonly ) {
2018-08-20 13:36:34 +02:00
cache . style_readonly - > draw ( ci , Rect2 ( Point2 ( ) , size ) ) ;
2017-11-30 04:53:15 +01:00
draw_caret = false ;
}
2020-05-14 16:41:43 +02:00
if ( has_focus ( ) ) {
2018-08-20 13:36:34 +02:00
cache . style_focus - > draw ( ci , Rect2 ( Point2 ( ) , size ) ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-08-20 15:07:54 +02:00
int visible_rows = get_visible_rows ( ) + 1 ;
2016-03-09 00:00:52 +01:00
2019-06-24 05:43:52 +02:00
Color color = readonly ? cache . font_color_readonly : cache . font_color ;
2017-11-30 04:53:15 +01:00
2020-05-03 18:08:15 +02:00
if ( cache . background_color . a > 0.01 ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( Point2i ( ) , get_size ( ) ) , cache . background_color ) ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2020-02-15 02:59:59 +01:00
if ( line_length_guidelines ) {
2020-09-18 09:36:10 +02:00
const int hard_x = xmargin_beg + ( int ) cache . font - > get_char_size ( ' 0 ' , 0 , cache . font_size ) . width * line_length_guideline_hard_col - cursor . x_ofs ;
2020-02-15 02:59:59 +01:00
if ( hard_x > xmargin_beg & & hard_x < xmargin_end ) {
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_line ( ci , Point2 ( size . width - hard_x , 0 ) , Point2 ( size . width - hard_x , size . height ) , cache . line_length_guideline_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_line ( ci , Point2 ( hard_x , 0 ) , Point2 ( hard_x , size . height ) , cache . line_length_guideline_color ) ;
}
2020-02-15 02:59:59 +01:00
}
// Draw a "Soft" line length guideline, less visible than the hard line length guideline.
// It's usually set to a lower column compared to the hard line length guideline.
// Only drawn if its column differs from the hard line length guideline.
2020-09-18 09:36:10 +02:00
const int soft_x = xmargin_beg + ( int ) cache . font - > get_char_size ( ' 0 ' , 0 , cache . font_size ) . width * line_length_guideline_soft_col - cursor . x_ofs ;
2020-02-15 02:59:59 +01:00
if ( hard_x ! = soft_x & & soft_x > xmargin_beg & & soft_x < xmargin_end ) {
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_line ( ci , Point2 ( size . width - soft_x , 0 ) , Point2 ( size . width - soft_x , size . height ) , cache . line_length_guideline_color * Color ( 1 , 1 , 1 , 0.5 ) ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_line ( ci , Point2 ( soft_x , 0 ) , Point2 ( soft_x , size . height ) , cache . line_length_guideline_color * Color ( 1 , 1 , 1 , 0.5 ) ) ;
}
2019-06-30 23:21:11 +02:00
}
}
2017-03-05 16:44:50 +01:00
int brace_open_match_line = - 1 ;
int brace_open_match_column = - 1 ;
bool brace_open_matching = false ;
bool brace_open_mismatch = false ;
int brace_close_match_line = - 1 ;
int brace_close_match_column = - 1 ;
bool brace_close_matching = false ;
bool brace_close_mismatch = false ;
2016-03-09 00:00:52 +01:00
2019-03-05 16:23:25 +01:00
if ( brace_matching_enabled & & cursor . line > = 0 & & cursor . line < text . size ( ) & & cursor . column > = 0 ) {
2017-03-05 16:44:50 +01:00
if ( cursor . column < text [ cursor . line ] . length ( ) ) {
2019-08-18 17:56:24 +02:00
// Check for open.
2020-07-27 12:43:20 +02:00
char32_t c = text [ cursor . line ] [ cursor . column ] ;
char32_t closec = 0 ;
2017-03-05 16:44:50 +01:00
if ( c = = ' [ ' ) {
closec = ' ] ' ;
} else if ( c = = ' { ' ) {
closec = ' } ' ;
} else if ( c = = ' ( ' ) {
closec = ' ) ' ;
2014-12-17 05:53:34 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( closec ! = 0 ) {
int stack = 1 ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = cursor . line ; i < text . size ( ) ; i + + ) {
int from = i = = cursor . line ? cursor . column + 1 : 0 ;
for ( int j = from ; j < text [ i ] . length ( ) ; j + + ) {
2020-07-27 12:43:20 +02:00
char32_t cc = text [ i ] [ j ] ;
2019-08-18 17:56:24 +02:00
// Ignore any brackets inside a string.
2017-03-05 16:44:50 +01:00
if ( cc = = ' " ' | | cc = = ' \' ' ) {
2020-07-27 12:43:20 +02:00
char32_t quotation = cc ;
2015-02-20 01:34:04 +01:00
do {
j + + ;
2017-03-05 16:44:50 +01:00
if ( ! ( j < text [ i ] . length ( ) ) ) {
2015-02-20 01:34:04 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
cc = text [ i ] [ j ] ;
2019-08-18 17:56:24 +02:00
// Skip over escaped quotation marks inside strings.
2017-03-05 16:44:50 +01:00
if ( cc = = ' \\ ' ) {
2015-02-20 01:34:04 +01:00
bool escaped = true ;
2017-03-05 16:44:50 +01:00
while ( j + 1 < text [ i ] . length ( ) & & text [ i ] [ j + 1 ] = = ' \\ ' ) {
escaped = ! escaped ;
2015-02-20 01:34:04 +01:00
j + + ;
}
if ( escaped ) {
j + + ;
continue ;
}
}
2017-03-05 16:44:50 +01:00
} while ( cc ! = quotation ) ;
2020-05-14 16:41:43 +02:00
} else if ( cc = = c ) {
2014-12-17 05:53:34 +01:00
stack + + ;
2020-05-14 16:41:43 +02:00
} else if ( cc = = closec ) {
2014-12-17 05:53:34 +01:00
stack - - ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( stack = = 0 ) {
brace_open_match_line = i ;
brace_open_match_column = j ;
brace_open_matching = true ;
2016-03-09 00:00:52 +01:00
2014-12-17 05:53:34 +01:00
break ;
}
}
2020-05-14 16:41:43 +02:00
if ( brace_open_match_line ! = - 1 ) {
2014-12-17 05:53:34 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 05:53:34 +01:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( ! brace_open_matching ) {
2017-03-05 16:44:50 +01:00
brace_open_mismatch = true ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 05:53:34 +01:00
}
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( cursor . column > 0 ) {
2020-07-27 12:43:20 +02:00
char32_t c = text [ cursor . line ] [ cursor . column - 1 ] ;
char32_t closec = 0 ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( c = = ' ] ' ) {
closec = ' [ ' ;
} else if ( c = = ' } ' ) {
closec = ' { ' ;
} else if ( c = = ' ) ' ) {
closec = ' ( ' ;
2014-12-17 05:53:34 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( closec ! = 0 ) {
int stack = 1 ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = cursor . line ; i > = 0 ; i - - ) {
int from = i = = cursor . line ? cursor . column - 2 : text [ i ] . length ( ) - 1 ;
for ( int j = from ; j > = 0 ; j - - ) {
2020-07-27 12:43:20 +02:00
char32_t cc = text [ i ] [ j ] ;
2019-08-18 17:56:24 +02:00
// Ignore any brackets inside a string.
2017-03-05 16:44:50 +01:00
if ( cc = = ' " ' | | cc = = ' \' ' ) {
2020-07-27 12:43:20 +02:00
char32_t quotation = cc ;
2015-02-20 01:34:04 +01:00
do {
j - - ;
2017-03-05 16:44:50 +01:00
if ( ! ( j > = 0 ) ) {
2015-02-20 01:34:04 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
cc = text [ i ] [ j ] ;
2019-08-18 17:56:24 +02:00
// Skip over escaped quotation marks inside strings.
2017-03-05 16:44:50 +01:00
if ( cc = = quotation ) {
2015-02-20 01:34:04 +01:00
bool escaped = false ;
2017-03-05 16:44:50 +01:00
while ( j - 1 > = 0 & & text [ i ] [ j - 1 ] = = ' \\ ' ) {
escaped = ! escaped ;
2015-02-20 01:34:04 +01:00
j - - ;
}
if ( escaped ) {
2017-03-05 16:44:50 +01:00
cc = ' \\ ' ;
2015-02-20 01:34:04 +01:00
continue ;
}
}
2017-03-05 16:44:50 +01:00
} while ( cc ! = quotation ) ;
2020-05-14 16:41:43 +02:00
} else if ( cc = = c ) {
2014-12-17 05:53:34 +01:00
stack + + ;
2020-05-14 16:41:43 +02:00
} else if ( cc = = closec ) {
2014-12-17 05:53:34 +01:00
stack - - ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( stack = = 0 ) {
brace_close_match_line = i ;
brace_close_match_column = j ;
brace_close_matching = true ;
2016-03-09 00:00:52 +01:00
2014-12-17 05:53:34 +01:00
break ;
}
}
2020-05-14 16:41:43 +02:00
if ( brace_close_match_line ! = - 1 ) {
2014-12-17 05:53:34 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 05:53:34 +01:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( ! brace_close_matching ) {
2017-03-05 16:44:50 +01:00
brace_close_mismatch = true ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 05:53:34 +01:00
}
}
}
2016-03-09 00:00:52 +01:00
2014-12-17 02:31:57 +01:00
Point2 cursor_pos ;
2018-12-31 12:38:42 +01:00
int cursor_insert_offset_y = 0 ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Get the highlighted words.
2016-03-17 20:37:19 +01:00
String highlighted_text = get_selection_text ( ) ;
2016-03-16 22:20:42 +01:00
2019-08-18 17:56:24 +02:00
// Check if highlighted words contains only whitespaces (tabs or spaces).
2019-04-27 18:02:09 +02:00
bool only_whitespaces_highlighted = highlighted_text . strip_edges ( ) = = String ( ) ;
2018-01-26 02:41:17 +01:00
int cursor_wrap_index = get_cursor_wrap_index ( ) ;
2020-09-03 13:22:16 +02:00
//FontDrawer drawer(cache.font, Color(1, 1, 1));
2018-01-26 02:41:17 +01:00
2019-08-11 20:31:19 +02:00
int first_visible_line = get_first_visible_line ( ) - 1 ;
2018-01-26 02:41:17 +01:00
int draw_amount = visible_rows + ( smooth_scroll_enabled ? 1 : 0 ) ;
2019-08-11 20:31:19 +02:00
draw_amount + = times_line_wraps ( first_visible_line + 1 ) ;
// minimap
if ( draw_minimap ) {
int minimap_visible_lines = _get_minimap_visible_rows ( ) ;
int minimap_line_height = ( minimap_char_size . y + minimap_line_spacing ) ;
int minimap_tab_size = minimap_char_size . x * indent_size ;
// calculate viewport size and y offset
int viewport_height = ( draw_amount - 1 ) * minimap_line_height ;
2019-08-25 15:49:13 +02:00
int control_height = _get_control_height ( ) - viewport_height ;
2020-05-11 10:02:16 +02:00
int viewport_offset_y = round ( get_scroll_pos_for_line ( first_visible_line + 1 ) * control_height ) / ( ( v_scroll - > get_max ( ) < = minimap_visible_lines ) ? ( minimap_visible_lines - draw_amount ) : ( v_scroll - > get_max ( ) - draw_amount ) ) ;
2019-08-11 20:31:19 +02:00
// calculate the first line.
int num_lines_before = round ( ( viewport_offset_y ) / minimap_line_height ) ;
int wi ;
int minimap_line = ( v_scroll - > get_max ( ) < = minimap_visible_lines ) ? - 1 : first_visible_line ;
if ( minimap_line > = 0 ) {
minimap_line - = num_lines_from_rows ( first_visible_line , 0 , - num_lines_before , wi ) ;
2019-10-20 14:27:53 +02:00
minimap_line - = ( minimap_line > 0 & & smooth_scroll_enabled ? 1 : 0 ) ;
2019-08-11 20:31:19 +02:00
}
int minimap_draw_amount = minimap_visible_lines + times_line_wraps ( minimap_line + 1 ) ;
// draw the minimap
2019-08-24 15:33:55 +02:00
Color viewport_color = ( cache . background_color . get_v ( ) < 0.5 ) ? Color ( 1 , 1 , 1 , 0.1 ) : Color ( 0 , 0 , 0 , 0.1 ) ;
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( size . width - ( xmargin_end + 2 ) - cache . minimap_width , viewport_offset_y , cache . minimap_width , viewport_height ) , viewport_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( ( xmargin_end + 2 ) , viewport_offset_y , cache . minimap_width , viewport_height ) , viewport_color ) ;
}
2019-08-11 20:31:19 +02:00
for ( int i = 0 ; i < minimap_draw_amount ; i + + ) {
minimap_line + + ;
if ( minimap_line < 0 | | minimap_line > = ( int ) text . size ( ) ) {
break ;
}
while ( is_line_hidden ( minimap_line ) ) {
minimap_line + + ;
if ( minimap_line < 0 | | minimap_line > = ( int ) text . size ( ) ) {
break ;
}
}
2019-10-26 14:37:25 +02:00
if ( minimap_line < 0 | | minimap_line > = ( int ) text . size ( ) ) {
break ;
}
2020-05-03 18:08:15 +02:00
Dictionary color_map = _get_line_syntax_highlighting ( minimap_line ) ;
2019-08-11 20:31:19 +02:00
Color current_color = cache . font_color ;
if ( readonly ) {
current_color = cache . font_color_readonly ;
}
Vector < String > wrap_rows = get_wrap_rows_text ( minimap_line ) ;
int line_wrap_amount = times_line_wraps ( minimap_line ) ;
int last_wrap_column = 0 ;
for ( int line_wrap_index = 0 ; line_wrap_index < line_wrap_amount + 1 ; line_wrap_index + + ) {
if ( line_wrap_index ! = 0 ) {
i + + ;
2020-05-14 16:41:43 +02:00
if ( i > = minimap_draw_amount ) {
2019-08-11 20:31:19 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2019-08-11 20:31:19 +02:00
}
const String & str = wrap_rows [ line_wrap_index ] ;
int indent_px = line_wrap_index ! = 0 ? get_indent_level ( minimap_line ) : 0 ;
if ( indent_px > = wrap_at ) {
indent_px = 0 ;
}
indent_px = minimap_char_size . x * indent_px ;
if ( line_wrap_index > 0 ) {
last_wrap_column + = wrap_rows [ line_wrap_index - 1 ] . length ( ) ;
}
if ( minimap_line = = cursor . line & & cursor_wrap_index = = line_wrap_index & & highlight_current_line ) {
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( size . width - ( xmargin_end + 2 ) - cache . minimap_width , i * 3 , cache . minimap_width , 2 ) , cache . current_line_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( ( xmargin_end + 2 ) , i * 3 , cache . minimap_width , 2 ) , cache . current_line_color ) ;
}
2019-08-11 20:31:19 +02:00
}
Color previous_color ;
int characters = 0 ;
int tabs = 0 ;
for ( int j = 0 ; j < str . length ( ) ; j + + ) {
2020-05-03 18:08:15 +02:00
if ( color_map . has ( last_wrap_column + j ) ) {
current_color = color_map [ last_wrap_column + j ] . get ( " color " ) ;
if ( readonly ) {
current_color . a = cache . font_color_readonly . a ;
2019-08-11 20:31:19 +02:00
}
}
2020-05-03 18:08:15 +02:00
color = current_color ;
2019-08-11 20:31:19 +02:00
if ( j = = 0 ) {
previous_color = color ;
}
int xpos = indent_px + ( ( xmargin_end + minimap_char_size . x ) + ( minimap_char_size . x * j ) ) + tabs ;
bool out_of_bounds = ( xpos > = xmargin_end + cache . minimap_width ) ;
bool is_whitespace = _is_whitespace ( str [ j ] ) ;
if ( ! is_whitespace ) {
characters + + ;
if ( j < str . length ( ) - 1 & & color = = previous_color & & ! out_of_bounds ) {
continue ;
}
// If we've changed colour we are at the start of a new section, therefore we need to go back to the end
// of the previous section to draw it, we'll also add the character back on.
if ( color ! = previous_color ) {
characters - - ;
j - - ;
if ( str [ j ] = = ' \t ' ) {
tabs - = minimap_tab_size ;
}
}
}
if ( characters > 0 ) {
previous_color . a * = 0.6 ;
// take one for zero indexing, and if we hit whitespace / the end of a word.
int chars = MAX ( 0 , ( j - ( characters - 1 ) ) - ( is_whitespace ? 1 : 0 ) ) + 1 ;
int char_x_ofs = indent_px + ( ( xmargin_end + minimap_char_size . x ) + ( minimap_char_size . x * chars ) ) + tabs ;
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( Point2 ( size . width - char_x_ofs - minimap_char_size . x * characters , minimap_line_height * i ) , Point2 ( minimap_char_size . x * characters , minimap_char_size . y ) ) , previous_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( Point2 ( char_x_ofs , minimap_line_height * i ) , Point2 ( minimap_char_size . x * characters , minimap_char_size . y ) ) , previous_color ) ;
}
2019-08-11 20:31:19 +02:00
}
if ( out_of_bounds ) {
break ;
}
if ( str [ j ] = = ' \t ' ) {
tabs + = minimap_tab_size ;
}
previous_color = color ;
characters = 0 ;
}
}
}
}
// draw main text
2020-09-18 09:36:10 +02:00
int row_height = get_row_height ( ) ;
2019-08-11 20:31:19 +02:00
int line = first_visible_line ;
2017-11-21 15:34:04 +01:00
for ( int i = 0 ; i < draw_amount ; i + + ) {
2017-11-13 00:12:17 +01:00
line + + ;
2020-05-14 16:41:43 +02:00
if ( line < 0 | | line > = ( int ) text . size ( ) ) {
2017-11-13 00:12:17 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
while ( is_line_hidden ( line ) ) {
line + + ;
if ( line < 0 | | line > = ( int ) text . size ( ) ) {
break ;
}
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( line < 0 | | line > = ( int ) text . size ( ) ) {
2014-12-17 02:31:57 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-03 18:08:15 +02:00
Dictionary color_map = _get_line_syntax_highlighting ( line ) ;
2019-08-18 17:56:24 +02:00
// Ensure we at least use the font color.
2019-06-24 05:43:52 +02:00
Color current_color = readonly ? cache . font_color_readonly : cache . font_color ;
2017-08-19 16:23:45 +02:00
2020-09-18 09:36:10 +02:00
const Ref < TextParagraph > ldata = text . get_line_data ( line ) ;
2016-03-09 00:00:52 +01:00
2018-12-18 14:01:15 +01:00
Vector < String > wrap_rows = get_wrap_rows_text ( line ) ;
2018-01-26 02:41:17 +01:00
int line_wrap_amount = times_line_wraps ( line ) ;
2016-05-28 18:25:45 +02:00
2020-09-18 09:36:10 +02:00
for ( int line_wrap_index = 0 ; line_wrap_index < = line_wrap_amount ; line_wrap_index + + ) {
2018-01-26 02:41:17 +01:00
if ( line_wrap_index ! = 0 ) {
i + + ;
2020-05-14 16:41:43 +02:00
if ( i > = draw_amount ) {
2018-01-26 02:41:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
}
2016-05-28 18:25:45 +02:00
2018-01-26 02:41:17 +01:00
const String & str = wrap_rows [ line_wrap_index ] ;
int char_margin = xmargin_beg - cursor . x_ofs ;
int ofs_readonly = 0 ;
int ofs_x = 0 ;
if ( readonly ) {
ofs_readonly = cache . style_readonly - > get_offset ( ) . y / 2 ;
ofs_x = cache . style_readonly - > get_offset ( ) . x / 2 ;
2018-04-02 13:41:44 +02:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
int ofs_y = ( i * row_height + cache . line_spacing / 2 ) + ofs_readonly ;
ofs_y - = cursor . wrap_ofs * row_height ;
ofs_y - = get_v_scroll_offset ( ) * row_height ;
2017-10-21 22:35:50 +02:00
2018-01-26 02:41:17 +01:00
if ( text . is_marked ( line ) ) {
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( size . width - ofs_x - xmargin_end , ofs_y , xmargin_end - xmargin_beg , row_height ) , cache . mark_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( xmargin_beg + ofs_x , ofs_y , xmargin_end - xmargin_beg , row_height ) , cache . mark_color ) ;
}
2017-10-21 22:35:50 +02:00
}
2018-01-26 02:41:17 +01:00
if ( str . length ( ) = = 0 ) {
2019-08-18 17:56:24 +02:00
// Draw line background if empty as we won't loop at at all.
2018-01-26 02:41:17 +01:00
if ( line = = cursor . line & & cursor_wrap_index = = line_wrap_index & & highlight_current_line ) {
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( size . width - ofs_x - xmargin_end , ofs_y , xmargin_end , row_height ) , cache . current_line_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( ofs_x , ofs_y , xmargin_end , row_height ) , cache . current_line_color ) ;
}
2018-01-26 02:41:17 +01:00
}
2019-08-18 17:56:24 +02:00
// Give visual indication of empty selected line.
2018-01-26 02:41:17 +01:00
if ( selection . active & & line > = selection . from_line & & line < = selection . to_line & & char_margin > = xmargin_beg ) {
2020-09-18 09:36:10 +02:00
int char_w = cache . font - > get_char_size ( ' m ' , 0 , cache . font_size ) . width ;
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( size . width - xmargin_beg - ofs_x - char_w , ofs_y , char_w , row_height ) , cache . selection_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( xmargin_beg + ofs_x , ofs_y , char_w , row_height ) , cache . selection_color ) ;
}
2018-01-26 02:41:17 +01:00
}
} else {
2019-08-18 17:56:24 +02:00
// If it has text, then draw current line marker in the margin, as line number etc will draw over it, draw the rest of line marker later.
2018-01-26 02:41:17 +01:00
if ( line = = cursor . line & & cursor_wrap_index = = line_wrap_index & & highlight_current_line ) {
2020-09-18 09:36:10 +02:00
if ( rtl ) {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( size . width - ofs_x - xmargin_end , ofs_y , xmargin_end , row_height ) , cache . current_line_color ) ;
} else {
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( ofs_x , ofs_y , xmargin_end , row_height ) , cache . current_line_color ) ;
}
2018-01-26 02:41:17 +01:00
}
2017-12-04 22:48:20 +01:00
}
2017-10-21 22:35:50 +02:00
2018-01-26 02:41:17 +01:00
if ( line_wrap_index = = 0 ) {
2019-08-18 17:56:24 +02:00
// Only do these if we are on the first wrapped part of a line.
2018-01-26 02:41:17 +01:00
2020-07-25 02:15:23 +02:00
int gutter_offset = cache . style_normal - > get_margin ( MARGIN_LEFT ) ;
for ( int g = 0 ; g < gutters . size ( ) ; g + + ) {
const GutterInfo gutter = gutters [ g ] ;
if ( ! gutter . draw | | gutter . width < = 0 ) {
continue ;
}
switch ( gutter . type ) {
case GUTTER_TYPE_STRING : {
const String & text = get_line_gutter_text ( line , g ) ;
if ( text = = " " ) {
break ;
}
2020-09-18 09:36:10 +02:00
Ref < TextLine > tl ;
tl . instance ( ) ;
tl - > add_string ( text , cache . font , cache . font_size ) ;
int yofs = ofs_y + ( row_height - tl - > get_size ( ) . y ) / 2 ;
tl - > draw ( ci , Point2 ( gutter_offset + ofs_x , yofs ) , get_line_gutter_item_color ( line , g ) ) ;
2020-07-25 02:15:23 +02:00
} break ;
case GUTTER_TPYE_ICON : {
const Ref < Texture2D > icon = get_line_gutter_icon ( line , g ) ;
if ( icon . is_null ( ) ) {
break ;
}
2020-09-18 09:36:10 +02:00
Rect2 gutter_rect = Rect2 ( Point2i ( gutter_offset , ofs_y ) , Size2i ( gutter . width , row_height ) ) ;
2020-07-25 02:15:23 +02:00
int horizontal_padding = gutter_rect . size . x / 6 ;
int vertical_padding = gutter_rect . size . y / 6 ;
gutter_rect . position + = Point2 ( horizontal_padding , vertical_padding ) ;
gutter_rect . size - = Point2 ( horizontal_padding , vertical_padding ) * 2 ;
2020-09-18 09:36:10 +02:00
// Correct icon aspect ratio.
float icon_ratio = icon - > get_width ( ) / icon - > get_height ( ) ;
float gutter_ratio = gutter_rect . size . x / gutter_rect . size . y ;
if ( gutter_ratio > icon_ratio ) {
gutter_rect . size . x = floor ( icon - > get_width ( ) * ( gutter_rect . size . y / icon - > get_height ( ) ) ) ;
} else {
gutter_rect . size . y = floor ( icon - > get_height ( ) * ( gutter_rect . size . x / icon - > get_width ( ) ) ) ;
}
if ( rtl ) {
gutter_rect . position . x = size . width - gutter_rect . position . x - gutter_rect . size . x ;
}
2020-07-25 02:15:23 +02:00
icon - > draw_rect ( ci , gutter_rect , false , get_line_gutter_item_color ( line , g ) ) ;
} break ;
case GUTTER_TPYE_CUSTOM : {
if ( gutter . custom_draw_obj . is_valid ( ) ) {
Object * cdo = ObjectDB : : get_instance ( gutter . custom_draw_obj ) ;
if ( cdo ) {
2020-09-18 09:36:10 +02:00
Rect2i gutter_rect = Rect2i ( Point2i ( gutter_offset , ofs_y ) , Size2i ( gutter . width , row_height ) ) ;
if ( rtl ) {
gutter_rect . position . x = size . width - gutter_rect . position . x - gutter_rect . size . x ;
}
2020-07-25 02:15:23 +02:00
cdo - > call ( gutter . custom_draw_callback , line , g , Rect2 ( gutter_rect ) ) ;
}
}
} break ;
}
gutter_offset + = gutter . width ;
}
2016-05-30 17:28:31 +02:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
// Draw line.
RID rid = ldata - > get_line_rid ( line_wrap_index ) ;
float text_height = TS - > shaped_text_get_size ( rid ) . y ;
2016-04-02 21:46:42 +02:00
2020-09-18 09:36:10 +02:00
if ( rtl ) {
char_margin = size . width - char_margin - TS - > shaped_text_get_size ( rid ) . x ;
}
2016-04-05 16:50:54 +02:00
2020-09-18 09:36:10 +02:00
if ( selection . active & & line > = selection . from_line & & line < = selection . to_line ) { // Selection
int sel_from = ( line > selection . from_line ) ? TS - > shaped_text_get_range ( rid ) . x : selection . from_column ;
int sel_to = ( line < selection . to_line ) ? TS - > shaped_text_get_range ( rid ) . y : selection . to_column ;
Vector < Vector2 > sel = TS - > shaped_text_get_selection ( rid , sel_from , sel_to ) ;
for ( int j = 0 ; j < sel . size ( ) ; j + + ) {
Rect2 rect = Rect2 ( sel [ j ] . x + char_margin + ofs_x , ofs_y , sel [ j ] . y - sel [ j ] . x , row_height ) ;
if ( rect . position . x + rect . size . x < = xmargin_beg | | rect . position . x > xmargin_end ) {
continue ;
2016-04-05 16:50:54 +02:00
}
2020-09-18 09:36:10 +02:00
if ( rect . position . x < xmargin_beg ) {
rect . size . x - = ( xmargin_beg - rect . position . x ) ;
rect . position . x = xmargin_beg ;
} else if ( rect . position . x + rect . size . x > xmargin_end ) {
rect . size . x = xmargin_end - rect . position . x ;
}
draw_rect ( rect , cache . selection_color , true ) ;
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
int start = TS - > shaped_text_get_range ( rid ) . x ;
if ( ! search_text . empty ( ) ) { // Search highhlight
int search_text_col = _get_column_pos_of_word ( search_text , str , search_flags , 0 ) ;
while ( search_text_col ! = - 1 ) {
Vector < Vector2 > sel = TS - > shaped_text_get_selection ( rid , search_text_col + start , search_text_col + search_text . length ( ) + start ) ;
for ( int j = 0 ; j < sel . size ( ) ; j + + ) {
Rect2 rect = Rect2 ( sel [ j ] . x + char_margin + ofs_x , ofs_y , sel [ j ] . y - sel [ j ] . x , row_height ) ;
if ( rect . position . x + rect . size . x < = xmargin_beg | | rect . position . x > xmargin_end ) {
continue ;
}
if ( rect . position . x < xmargin_beg ) {
rect . size . x - = ( xmargin_beg - rect . position . x ) ;
rect . position . x = xmargin_beg ;
} else if ( rect . position . x + rect . size . x > xmargin_end ) {
rect . size . x = xmargin_end - rect . position . x ;
}
draw_rect ( rect , cache . search_result_color , true ) ;
draw_rect ( rect , cache . search_result_border_color , false ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
search_text_col = _get_column_pos_of_word ( search_text , str , search_flags , search_text_col + 1 ) ;
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
}
2018-01-14 17:06:27 +01:00
2020-09-18 09:36:10 +02:00
if ( highlight_all_occurrences & & ! only_whitespaces_highlighted & & ! highlighted_text . empty ( ) ) { // Highlight
int highlighted_text_col = _get_column_pos_of_word ( highlighted_text , str , SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS , 0 ) ;
while ( highlighted_text_col ! = - 1 ) {
Vector < Vector2 > sel = TS - > shaped_text_get_selection ( rid , highlighted_text_col + start , highlighted_text_col + highlighted_text . length ( ) + start ) ;
for ( int j = 0 ; j < sel . size ( ) ; j + + ) {
Rect2 rect = Rect2 ( sel [ j ] . x + char_margin + ofs_x , ofs_y , sel [ j ] . y - sel [ j ] . x , row_height ) ;
if ( rect . position . x + rect . size . x < = xmargin_beg | | rect . position . x > xmargin_end ) {
continue ;
}
if ( rect . position . x < xmargin_beg ) {
rect . size . x - = ( xmargin_beg - rect . position . x ) ;
rect . position . x = xmargin_beg ;
} else if ( rect . position . x + rect . size . x > xmargin_end ) {
rect . size . x = xmargin_end - rect . position . x ;
}
draw_rect ( rect , cache . word_highlighted_color ) ;
2018-01-14 17:06:27 +01:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
highlighted_text_col = _get_column_pos_of_word ( highlighted_text , str , SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS , highlighted_text_col + 1 ) ;
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
}
2016-05-28 18:25:45 +02:00
2020-09-18 09:36:10 +02:00
if ( select_identifiers_enabled & & highlighted_word . length ( ) ! = 0 ) { // Highlight word
if ( _is_char ( highlighted_word [ 0 ] ) | | highlighted_word [ 0 ] = = ' . ' ) {
int highlighted_word_col = _get_column_pos_of_word ( highlighted_word , str , SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS , 0 ) ;
while ( highlighted_word_col ! = - 1 ) {
Vector < Vector2 > sel = TS - > shaped_text_get_selection ( rid , highlighted_word_col + start , highlighted_word_col + highlighted_word . length ( ) + start ) ;
for ( int j = 0 ; j < sel . size ( ) ; j + + ) {
Rect2 rect = Rect2 ( sel [ j ] . x + char_margin + ofs_x , ofs_y , sel [ j ] . y - sel [ j ] . x , row_height ) ;
if ( rect . position . x + rect . size . x < = xmargin_beg | | rect . position . x > xmargin_end ) {
continue ;
}
if ( rect . position . x < xmargin_beg ) {
rect . size . x - = ( xmargin_beg - rect . position . x ) ;
rect . position . x = xmargin_beg ;
} else if ( rect . position . x + rect . size . x > xmargin_end ) {
rect . size . x = xmargin_end - rect . position . x ;
}
rect . position . y = TS - > shaped_text_get_ascent ( rid ) + cache . font - > get_underline_position ( cache . font_size ) ;
rect . size . y = cache . font - > get_underline_thickness ( cache . font_size ) ;
draw_rect ( rect , cache . font_color_selected ) ;
}
2016-05-28 18:25:45 +02:00
2020-09-18 09:36:10 +02:00
highlighted_word_col = _get_column_pos_of_word ( highlighted_word , str , SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS , highlighted_word_col + 1 ) ;
2020-05-14 16:41:43 +02:00
}
2016-05-28 18:25:45 +02:00
}
2020-09-18 09:36:10 +02:00
}
2016-05-28 18:25:45 +02:00
2020-09-18 09:36:10 +02:00
ofs_y + = ( row_height - text_height ) / 2 ;
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
const Vector < TextServer : : Glyph > glyphs = TS - > shaped_text_get_glyphs ( rid ) ;
ofs_y + = ldata - > get_line_ascent ( line_wrap_index ) ;
float char_ofs = 0.f ;
for ( int j = 0 ; j < glyphs . size ( ) ; j + + ) {
if ( color_map . has ( glyphs [ j ] . start ) ) {
current_color = color_map [ glyphs [ j ] . start ] . get ( " color " ) ;
if ( readonly & & current_color . a > cache . font_color_readonly . a ) {
current_color . a = cache . font_color_readonly . a ;
2018-01-26 02:41:17 +01:00
}
2017-10-21 22:35:50 +02:00
}
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
if ( selection . active & & line > = selection . from_line & & line < = selection . to_line ) { // Selection
int sel_from = ( line > selection . from_line ) ? TS - > shaped_text_get_range ( rid ) . x : selection . from_column ;
int sel_to = ( line < selection . to_line ) ? TS - > shaped_text_get_range ( rid ) . y : selection . to_column ;
if ( glyphs [ j ] . start > = sel_from & & glyphs [ j ] . end < = sel_to & & override_selected_font_color ) {
current_color = cache . font_color_selected ;
2018-01-26 02:41:17 +01:00
}
2017-10-21 22:35:50 +02:00
}
2017-09-28 16:10:30 +02:00
2018-01-26 02:41:17 +01:00
if ( brace_matching_enabled ) {
2020-09-18 09:36:10 +02:00
if ( ( brace_open_match_line = = line & & brace_open_match_column = = glyphs [ j ] . start ) | |
( cursor . column = = glyphs [ j ] . start & & cursor . line = = line & & cursor_wrap_index = = line_wrap_index & & ( brace_open_matching | | brace_open_mismatch ) ) ) {
2020-05-14 16:41:43 +02:00
if ( brace_open_mismatch ) {
2020-09-18 09:36:10 +02:00
current_color = cache . brace_mismatch_color ;
2020-05-14 16:41:43 +02:00
}
2020-09-18 09:36:10 +02:00
Rect2 rect = Rect2 ( char_ofs + char_margin + ofs_x , ofs_y + cache . font - > get_underline_position ( cache . font_size ) , glyphs [ j ] . advance * glyphs [ j ] . repeat , cache . font - > get_underline_thickness ( cache . font_size ) ) ;
draw_rect ( rect , current_color ) ;
2018-01-26 02:41:17 +01:00
}
2016-05-28 18:25:45 +02:00
2020-09-18 09:36:10 +02:00
if ( ( brace_close_match_line = = line & & brace_close_match_column = = glyphs [ j ] . start ) | |
( cursor . column = = glyphs [ j ] . start + 1 & & cursor . line = = line & & cursor_wrap_index = = line_wrap_index & & ( brace_close_matching | | brace_close_mismatch ) ) ) {
2020-05-14 16:41:43 +02:00
if ( brace_close_mismatch ) {
2020-09-18 09:36:10 +02:00
current_color = cache . brace_mismatch_color ;
2020-05-14 16:41:43 +02:00
}
2020-09-18 09:36:10 +02:00
Rect2 rect = Rect2 ( char_ofs + char_margin + ofs_x , ofs_y + cache . font - > get_underline_position ( cache . font_size ) , glyphs [ j ] . advance * glyphs [ j ] . repeat , cache . font - > get_underline_thickness ( cache . font_size ) ) ;
draw_rect ( rect , current_color ) ;
2018-01-26 02:41:17 +01:00
}
}
2020-09-18 09:36:10 +02:00
if ( draw_tabs & & ( ( glyphs [ j ] . flags & TextServer : : GRAPHEME_IS_TAB ) = = TextServer : : GRAPHEME_IS_TAB ) ) {
int yofs = ( text_height - cache . tab_icon - > get_height ( ) ) / 2 - ldata - > get_line_ascent ( line_wrap_index ) ;
cache . tab_icon - > draw ( ci , Point2 ( char_ofs + char_margin + ofs_x , ofs_y + yofs ) , current_color ) ;
}
if ( draw_spaces & & ( ( glyphs [ j ] . flags & TextServer : : GRAPHEME_IS_SPACE ) = = TextServer : : GRAPHEME_IS_SPACE ) ) {
int yofs = ( text_height - cache . space_icon - > get_height ( ) ) / 2 - ldata - > get_line_ascent ( line_wrap_index ) ;
int xofs = ( glyphs [ j ] . advance * glyphs [ j ] . repeat - cache . space_icon - > get_width ( ) ) / 2 ;
cache . space_icon - > draw ( ci , Point2 ( char_ofs + char_margin + ofs_x + xofs , ofs_y + yofs ) , current_color ) ;
}
for ( int k = 0 ; k < glyphs [ j ] . repeat ; k + + ) {
if ( ( char_ofs + char_margin ) > = xmargin_beg & & ( char_ofs + glyphs [ j ] . advance + char_margin ) < = xmargin_end ) {
if ( glyphs [ j ] . font_rid ! = RID ( ) ) {
TS - > font_draw_glyph ( glyphs [ j ] . font_rid , ci , glyphs [ j ] . font_size , Vector2 ( char_margin + char_ofs + ofs_x + glyphs [ j ] . x_off , ofs_y + glyphs [ j ] . y_off ) , glyphs [ j ] . index , current_color ) ;
} else if ( ( glyphs [ j ] . flags & TextServer : : GRAPHEME_IS_VIRTUAL ) ! = TextServer : : GRAPHEME_IS_VIRTUAL ) {
TS - > draw_hex_code_box ( ci , glyphs [ j ] . font_size , Vector2 ( char_margin + char_ofs + ofs_x + glyphs [ j ] . x_off , ofs_y + glyphs [ j ] . y_off ) , glyphs [ j ] . index , current_color ) ;
}
2016-03-16 22:20:42 +01:00
}
2020-09-18 09:36:10 +02:00
char_ofs + = glyphs [ j ] . advance ;
}
if ( ( char_ofs + char_margin ) > = xmargin_end ) {
break ;
}
}
2016-03-16 22:20:42 +01:00
2020-09-18 09:36:10 +02:00
if ( line_wrap_index = = line_wrap_amount & & is_folded ( line ) ) {
int yofs = ( text_height - cache . folded_eol_icon - > get_height ( ) ) / 2 - ldata - > get_line_ascent ( line_wrap_index ) ;
int xofs = cache . folded_eol_icon - > get_width ( ) / 2 ;
Color eol_color = cache . code_folding_color ;
eol_color . a = 1 ;
cache . folded_eol_icon - > draw ( ci , Point2 ( char_ofs + char_margin + xofs + ofs_x , ofs_y + yofs ) , eol_color ) ;
}
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
// Carets
2018-07-26 23:41:47 +02:00
# ifdef TOOLS_ENABLED
2020-09-18 09:36:10 +02:00
int caret_width = Math : : round ( EDSCALE ) ;
2018-07-26 23:41:47 +02:00
# else
2020-09-18 09:36:10 +02:00
int caret_width = 1 ;
2018-07-26 23:41:47 +02:00
# endif
2020-09-18 09:36:10 +02:00
if ( cursor . line = = line & & ( ( line_wrap_index = = line_wrap_amount ) | | ( cursor . column ! = TS - > shaped_text_get_range ( rid ) . y ) ) ) {
cursor_pos . y = ofs_y + ldata - > get_line_descent ( line_wrap_index ) ;
if ( ime_text . length ( ) = = 0 ) {
Rect2 l_caret , t_caret ;
TextServer : : Direction l_dir , t_dir ;
if ( str . length ( ) ! = 0 ) {
// Get carets.
TS - > shaped_text_get_carets ( rid , cursor . column , l_caret , l_dir , t_caret , t_dir ) ;
} else {
// No carets, add one at the start.
int h = cache . font - > get_height ( cache . font_size ) ;
if ( rtl ) {
l_dir = TextServer : : DIRECTION_RTL ;
l_caret = Rect2 ( Vector2 ( xmargin_end - char_margin + ofs_x , - h / 2 ) , Size2 ( caret_width * 4 , h ) ) ;
} else {
l_dir = TextServer : : DIRECTION_LTR ;
l_caret = Rect2 ( Vector2 ( char_ofs , - h / 2 ) , Size2 ( caret_width * 4 , h ) ) ;
2018-01-26 02:41:17 +01:00
}
2016-03-16 22:20:42 +01:00
}
2018-08-02 09:38:56 +02:00
2020-09-18 09:36:10 +02:00
if ( ( l_caret ! = Rect2 ( ) & & ( l_dir = = TextServer : : DIRECTION_AUTO | | l_dir = = ( TextServer : : Direction ) input_direction ) ) | | ( t_caret = = Rect2 ( ) ) ) {
cursor_pos . x = char_margin + ofs_x + l_caret . position . x ;
} else {
cursor_pos . x = char_margin + ofs_x + t_caret . position . x ;
2018-01-26 02:41:17 +01:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
if ( draw_caret ) {
if ( block_caret | | insert_mode ) {
//Block or underline caret, draw trailing carets at full height.
int h = cache . font - > get_height ( cache . font_size ) ;
if ( t_caret ! = Rect2 ( ) ) {
if ( insert_mode ) {
t_caret . position . y = TS - > shaped_text_get_descent ( rid ) ;
t_caret . size . y = caret_width ;
} else {
t_caret . position . y = - TS - > shaped_text_get_ascent ( rid ) ;
t_caret . size . y = h ;
}
t_caret . position + = Vector2 ( char_margin + ofs_x , ofs_y ) ;
draw_rect ( t_caret , cache . caret_color , false ) ;
} else { // End of the line.
if ( insert_mode ) {
l_caret . position . y = TS - > shaped_text_get_descent ( rid ) ;
l_caret . size . y = caret_width ;
} else {
l_caret . position . y = - TS - > shaped_text_get_ascent ( rid ) ;
l_caret . size . y = h ;
}
l_caret . position + = Vector2 ( char_margin + ofs_x , ofs_y ) ;
l_caret . size . x = cache . font - > get_char_size ( ' m ' , 0 , cache . font_size ) . x ;
2019-04-24 01:33:20 +02:00
2020-09-18 09:36:10 +02:00
draw_rect ( l_caret , cache . caret_color , false ) ;
}
} else {
// Normal caret.
if ( l_caret ! = Rect2 ( ) & & l_dir = = TextServer : : DIRECTION_AUTO ) {
// Draw extra marker on top of mid caret.
Rect2 trect = Rect2 ( l_caret . position . x - 3 * caret_width , l_caret . position . y , 6 * caret_width , caret_width ) ;
trect . position + = Vector2 ( char_margin + ofs_x , ofs_y ) ;
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , trect , cache . caret_color ) ;
}
l_caret . position + = Vector2 ( char_margin + ofs_x , ofs_y ) ;
l_caret . size . x = caret_width ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
draw_rect ( l_caret , cache . caret_color ) ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
t_caret . position + = Vector2 ( char_margin + ofs_x , ofs_y ) ;
t_caret . size . x = caret_width ;
2016-05-09 20:21:55 +02:00
2020-09-18 09:36:10 +02:00
draw_rect ( t_caret , cache . caret_color ) ;
2020-05-14 16:41:43 +02:00
}
2020-09-18 09:36:10 +02:00
}
} else {
{
// IME intermidiet text range.
Vector < Vector2 > sel = TS - > shaped_text_get_selection ( rid , cursor . column , cursor . column + ime_text . length ( ) ) ;
for ( int j = 0 ; j < sel . size ( ) ; j + + ) {
Rect2 rect = Rect2 ( sel [ j ] . x + char_margin + ofs_x , ofs_y , sel [ j ] . y - sel [ j ] . x , text_height ) ;
if ( rect . position . x + rect . size . x < = xmargin_beg | | rect . position . x > xmargin_end ) {
continue ;
}
if ( rect . position . x < xmargin_beg ) {
rect . size . x - = ( xmargin_beg - rect . position . x ) ;
rect . position . x = xmargin_beg ;
} else if ( rect . position . x + rect . size . x > xmargin_end ) {
rect . size . x = xmargin_end - rect . position . x ;
}
rect . size . y = caret_width ;
draw_rect ( rect , cache . caret_color ) ;
cursor_pos . x = rect . position . x ;
2017-08-07 13:09:56 +02:00
}
}
2020-09-18 09:36:10 +02:00
{
// IME caret.
Vector < Vector2 > sel = TS - > shaped_text_get_selection ( rid , cursor . column + ime_selection . x , cursor . column + ime_selection . x + ime_selection . y ) ;
for ( int j = 0 ; j < sel . size ( ) ; j + + ) {
Rect2 rect = Rect2 ( sel [ j ] . x + char_margin + ofs_x , ofs_y , sel [ j ] . y - sel [ j ] . x , text_height ) ;
if ( rect . position . x + rect . size . x < = xmargin_beg | | rect . position . x > xmargin_end ) {
continue ;
}
if ( rect . position . x < xmargin_beg ) {
rect . size . x - = ( xmargin_beg - rect . position . x ) ;
rect . position . x = xmargin_beg ;
} else if ( rect . position . x + rect . size . x > xmargin_end ) {
rect . size . x = xmargin_end - rect . position . x ;
}
rect . size . y = caret_width * 3 ;
draw_rect ( rect , cache . caret_color ) ;
cursor_pos . x = rect . position . x ;
2017-08-07 13:09:56 +02:00
}
2016-05-09 20:21:55 +02:00
}
}
2014-12-17 02:31:57 +01:00
}
2020-09-18 09:36:10 +02:00
ofs_y + = ldata - > get_line_descent ( line_wrap_index ) ;
2014-12-17 02:31:57 +01:00
}
}
2016-03-09 00:00:52 +01:00
2016-03-04 11:05:42 +01:00
bool completion_below = false ;
2020-03-21 09:50:19 +01:00
if ( completion_active & & completion_options . size ( ) > 0 ) {
2019-08-18 17:56:24 +02:00
// Code completion box.
2020-03-12 13:37:40 +01:00
Ref < StyleBox > csb = get_theme_stylebox ( " completion " ) ;
int maxlines = get_theme_constant ( " completion_lines " ) ;
2020-09-18 09:36:10 +02:00
int cmax_width = get_theme_constant ( " completion_max_width " ) * cache . font - > get_char_size ( ' x ' , 0 , cache . font_size ) . x ;
2020-03-12 13:37:40 +01:00
int scrollw = get_theme_constant ( " completion_scroll_width " ) ;
Color scrollc = get_theme_color ( " completion_scroll_color " ) ;
2016-03-09 00:00:52 +01:00
2020-03-21 09:50:19 +01:00
const int completion_options_size = completion_options . size ( ) ;
int lines = MIN ( completion_options_size , maxlines ) ;
2017-03-05 16:44:50 +01:00
int w = 0 ;
2020-09-18 09:36:10 +02:00
int h = lines * row_height ;
2020-09-03 13:22:16 +02:00
int nofs = cache . font - > get_string_size ( completion_base , cache . font_size ) . width ;
2016-03-09 00:00:52 +01:00
2020-03-21 09:50:19 +01:00
if ( completion_options_size < 50 ) {
for ( int i = 0 ; i < completion_options_size ; i + + ) {
2020-09-03 13:22:16 +02:00
int w2 = MIN ( cache . font - > get_string_size ( completion_options [ i ] . display , cache . font_size ) . x , cmax_width ) ;
2020-05-14 16:41:43 +02:00
if ( w2 > w ) {
2017-03-05 16:44:50 +01:00
w = w2 ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 02:31:57 +01:00
}
} else {
2017-03-05 16:44:50 +01:00
w = cmax_width ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Add space for completion icons.
2020-03-12 13:37:40 +01:00
const int icon_hsep = get_theme_constant ( " hseparation " , " ItemList " ) ;
2020-09-18 09:36:10 +02:00
Size2 icon_area_size ( row_height , row_height ) ;
2019-06-13 11:32:03 +02:00
w + = icon_area_size . width + icon_hsep ;
2020-10-22 22:12:00 +02:00
int line_from = CLAMP ( completion_index - lines / 2 , 0 , completion_options_size - lines ) ;
for ( int i = 0 ; i < lines ; i + + ) {
int l = line_from + i ;
ERR_CONTINUE ( l < 0 | | l > = completion_options_size ) ;
if ( completion_options [ l ] . default_value . get_type ( ) = = Variant : : COLOR ) {
w + = icon_area_size . width ;
break ;
}
}
2014-12-17 02:31:57 +01:00
int th = h + csb - > get_minimum_size ( ) . y ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
if ( cursor_pos . y + row_height + th > get_size ( ) . height ) {
2018-12-31 12:38:42 +01:00
completion_rect . position . y = cursor_pos . y - th - ( cache . line_spacing / 2.0f ) - cursor_insert_offset_y ;
2014-12-17 02:31:57 +01:00
} else {
2020-09-18 09:36:10 +02:00
completion_rect . position . y = cursor_pos . y + cache . font - > get_height ( cache . font_size ) + ( cache . line_spacing / 2.0f ) + csb - > get_offset ( ) . y - cursor_insert_offset_y ;
2016-03-04 11:05:42 +01:00
completion_below = true ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( cursor_pos . x - nofs + w + scrollw > get_size ( ) . width ) {
2017-06-04 00:25:13 +02:00
completion_rect . position . x = get_size ( ) . width - w - scrollw ;
2014-12-17 02:31:57 +01:00
} else {
2017-06-04 00:25:13 +02:00
completion_rect . position . x = cursor_pos . x - nofs ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
completion_rect . size . width = w + 2 ;
completion_rect . size . height = h ;
2020-05-14 16:41:43 +02:00
if ( completion_options_size < = maxlines ) {
2017-03-05 16:44:50 +01:00
scrollw = 0 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-06-04 00:25:13 +02:00
draw_style_box ( csb , Rect2 ( completion_rect . position - csb - > get_offset ( ) , completion_rect . size + csb - > get_minimum_size ( ) + Size2 ( scrollw , 0 ) ) ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( cache . completion_background_color . a > 0.01 ) {
2020-03-27 19:21:27 +01:00
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( completion_rect . position , completion_rect . size + Size2 ( scrollw , 0 ) ) , cache . completion_background_color ) ;
2016-06-21 17:50:31 +02:00
}
2020-03-27 19:21:27 +01:00
RenderingServer : : get_singleton ( ) - > canvas_item_add_rect ( ci , Rect2 ( Point2 ( completion_rect . position . x , completion_rect . position . y + ( completion_index - line_from ) * get_row_height ( ) ) , Size2 ( completion_rect . size . width , get_row_height ( ) ) ) , cache . completion_selected_color ) ;
2020-09-18 09:36:10 +02:00
2019-08-15 17:53:28 +02:00
draw_rect ( Rect2 ( completion_rect . position + Vector2 ( icon_area_size . x + icon_hsep , 0 ) , Size2 ( MIN ( nofs , completion_rect . size . width - ( icon_area_size . x + icon_hsep ) ) , completion_rect . size . height ) ) , cache . completion_existing_color ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < lines ; i + + ) {
2014-12-17 02:31:57 +01:00
int l = line_from + i ;
2020-03-21 09:50:19 +01:00
ERR_CONTINUE ( l < 0 | | l > = completion_options_size ) ;
2020-09-18 09:36:10 +02:00
Ref < TextLine > tl ;
tl . instance ( ) ;
tl - > add_string ( completion_options [ l ] . display , cache . font , cache . font_size ) ;
int yofs = ( row_height - tl - > get_size ( ) . y ) / 2 ;
Point2 title_pos ( completion_rect . position . x , completion_rect . position . y + i * row_height + yofs ) ;
2019-06-13 11:32:03 +02:00
2019-08-18 17:56:24 +02:00
// Draw completion icon if it is valid.
2019-06-11 20:43:37 +02:00
Ref < Texture2D > icon = completion_options [ l ] . icon ;
2020-09-18 09:36:10 +02:00
Rect2 icon_area ( completion_rect . position . x , completion_rect . position . y + i * row_height , icon_area_size . width , icon_area_size . height ) ;
2019-06-13 11:32:03 +02:00
if ( icon . is_valid ( ) ) {
const real_t max_scale = 0.7f ;
const real_t side = max_scale * icon_area . size . width ;
real_t scale = MIN ( side / icon - > get_width ( ) , side / icon - > get_height ( ) ) ;
Size2 icon_size = icon - > get_size ( ) * scale ;
draw_texture_rect ( icon , Rect2 ( icon_area . position + ( icon_area . size - icon_size ) / 2 , icon_size ) ) ;
}
title_pos . x = icon_area . position . x + icon_area . size . width + icon_hsep ;
2020-10-22 22:12:00 +02:00
2020-09-18 09:36:10 +02:00
tl - > set_width ( completion_rect . size . width - ( icon_area_size . x + icon_hsep ) ) ;
2020-10-22 22:12:00 +02:00
2020-09-18 09:36:10 +02:00
if ( rtl ) {
if ( completion_options [ l ] . default_value . get_type ( ) = = Variant : : COLOR ) {
draw_rect ( Rect2 ( Point2 ( completion_rect . position . x , icon_area . position . y ) , icon_area_size ) , ( Color ) completion_options [ l ] . default_value ) ;
}
tl - > set_align ( HALIGN_RIGHT ) ;
} else {
if ( completion_options [ l ] . default_value . get_type ( ) = = Variant : : COLOR ) {
draw_rect ( Rect2 ( Point2 ( completion_rect . position . x + completion_rect . size . width - icon_area_size . x , icon_area . position . y ) , icon_area_size ) , ( Color ) completion_options [ l ] . default_value ) ;
}
tl - > set_align ( HALIGN_LEFT ) ;
}
tl - > draw ( ci , title_pos , completion_options [ l ] . font_color ) ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2014-12-17 02:31:57 +01:00
if ( scrollw ) {
2019-08-18 17:56:24 +02:00
// Draw a small scroll rectangle to show a position in the options.
2020-03-21 09:50:19 +01:00
float r = ( float ) maxlines / completion_options_size ;
float o = ( float ) line_from / completion_options_size ;
2017-06-04 00:25:13 +02:00
draw_rect ( Rect2 ( completion_rect . position . x + completion_rect . size . width , completion_rect . position . y + o * completion_rect . size . y , scrollw , completion_rect . size . y * r ) , scrollc ) ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
completion_line_ofs = line_from ;
2014-12-17 02:31:57 +01:00
}
2016-03-04 11:05:42 +01:00
2019-08-18 17:56:24 +02:00
// Check to see if the hint should be drawn.
2016-03-04 11:05:42 +01:00
bool show_hint = false ;
2017-03-05 16:44:50 +01:00
if ( completion_hint ! = " " ) {
2016-03-04 11:05:42 +01:00
if ( completion_active ) {
if ( completion_below & & ! callhint_below ) {
show_hint = true ;
2017-03-05 16:44:50 +01:00
} else if ( ! completion_below & & callhint_below ) {
2016-03-04 11:05:42 +01:00
show_hint = true ;
}
2017-03-05 16:44:50 +01:00
} else {
2016-03-04 11:05:42 +01:00
show_hint = true ;
}
}
2016-03-09 00:00:52 +01:00
2016-03-04 11:05:42 +01:00
if ( show_hint ) {
2020-03-12 13:37:40 +01:00
Ref < StyleBox > sb = get_theme_stylebox ( " panel " , " TooltipPanel " ) ;
2014-12-17 02:31:57 +01:00
Ref < Font > font = cache . font ;
2020-03-12 13:37:40 +01:00
Color font_color = get_theme_color ( " font_color " , " TooltipLabel " ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
int max_w = 0 ;
2014-12-17 02:31:57 +01:00
int sc = completion_hint . get_slice_count ( " \n " ) ;
2017-03-05 16:44:50 +01:00
int offset = 0 ;
int spacing = 0 ;
for ( int i = 0 ; i < sc ; i + + ) {
String l = completion_hint . get_slice ( " \n " , i ) ;
2020-09-03 13:22:16 +02:00
int len = font - > get_string_size ( l , cache . font_size ) . x ;
2017-03-05 16:44:50 +01:00
max_w = MAX ( len , max_w ) ;
if ( i = = 0 ) {
2020-09-03 13:22:16 +02:00
offset = font - > get_string_size ( l . substr ( 0 , l . find ( String : : chr ( 0xFFFF ) ) ) , cache . font_size ) . x ;
2014-12-17 02:31:57 +01:00
} else {
2017-03-05 16:44:50 +01:00
spacing + = cache . line_spacing ;
2014-12-17 02:31:57 +01:00
}
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
Size2 size2 = Size2 ( max_w , sc * font - > get_height ( cache . font_size ) + spacing ) ;
2019-02-12 21:10:08 +01:00
Size2 minsize = size2 + sb - > get_minimum_size ( ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( completion_hint_offset = = - 0xFFFF ) {
completion_hint_offset = cursor_pos . x - offset ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
Point2 hint_ofs = Vector2 ( completion_hint_offset , cursor_pos . y ) + callhint_offset ;
2016-03-04 11:05:42 +01:00
if ( callhint_below ) {
2020-09-18 09:36:10 +02:00
hint_ofs . y + = row_height + sb - > get_offset ( ) . y ;
2017-03-05 16:44:50 +01:00
} else {
2016-03-04 11:05:42 +01:00
hint_ofs . y - = minsize . y + sb - > get_offset ( ) . y ;
}
2017-03-05 16:44:50 +01:00
draw_style_box ( sb , Rect2 ( hint_ofs , minsize ) ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
spacing = 0 ;
for ( int i = 0 ; i < sc ; i + + ) {
int begin = 0 ;
int end = 0 ;
String l = completion_hint . get_slice ( " \n " , i ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( l . find ( String : : chr ( 0xFFFF ) ) ! = - 1 ) {
2020-09-03 13:22:16 +02:00
begin = font - > get_string_size ( l . substr ( 0 , l . find ( String : : chr ( 0xFFFF ) ) ) , cache . font_size ) . x ;
end = font - > get_string_size ( l . substr ( 0 , l . rfind ( String : : chr ( 0xFFFF ) ) ) , cache . font_size ) . x ;
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
Point2 round_ofs = hint_ofs + sb - > get_offset ( ) + Vector2 ( 0 , font - > get_ascent ( cache . font_size ) + font - > get_height ( cache . font_size ) * i + spacing ) ;
2019-10-24 15:48:20 +02:00
round_ofs = round_ofs . round ( ) ;
2020-09-03 13:22:16 +02:00
draw_string ( font , round_ofs , l . replace ( String : : chr ( 0xFFFF ) , " " ) , HALIGN_LEFT , - 1 , cache . font_size , font_color ) ;
2017-03-05 16:44:50 +01:00
if ( end > 0 ) {
2020-09-18 09:36:10 +02:00
Vector2 b = hint_ofs + sb - > get_offset ( ) + Vector2 ( begin , font - > get_height ( cache . font_size ) + font - > get_height ( cache . font_size ) * i + spacing - 1 ) ;
2017-03-05 16:44:50 +01:00
draw_line ( b , b + Vector2 ( end - begin , 0 ) , font_color ) ;
2014-12-17 02:31:57 +01:00
}
2017-03-05 16:44:50 +01:00
spacing + = cache . line_spacing ;
2014-12-17 02:31:57 +01:00
}
}
2016-03-09 00:00:52 +01:00
2017-06-25 17:50:45 +02:00
if ( has_focus ( ) ) {
2020-03-07 17:02:54 +01:00
if ( get_viewport ( ) - > get_window_id ( ) ! = DisplayServer : : INVALID_WINDOW_ID ) {
DisplayServer : : get_singleton ( ) - > window_set_ime_active ( true , get_viewport ( ) - > get_window_id ( ) ) ;
2020-09-18 09:36:10 +02:00
DisplayServer : : get_singleton ( ) - > window_set_ime_position ( get_global_position ( ) + cursor_pos , get_viewport ( ) - > get_window_id ( ) ) ;
2020-03-07 17:02:54 +01:00
}
2017-06-25 17:50:45 +02:00
}
2014-12-17 02:31:57 +01:00
} break ;
case NOTIFICATION_FOCUS_ENTER : {
2019-09-04 13:06:15 +02:00
if ( caret_blink_enabled ) {
caret_blink_timer - > start ( ) ;
} else {
2016-06-20 21:29:58 +02:00
draw_caret = true ;
}
2017-06-25 17:50:45 +02:00
2020-03-07 17:02:54 +01:00
if ( get_viewport ( ) - > get_window_id ( ) ! = DisplayServer : : INVALID_WINDOW_ID ) {
DisplayServer : : get_singleton ( ) - > window_set_ime_active ( true , get_viewport ( ) - > get_window_id ( ) ) ;
2020-09-18 09:36:10 +02:00
DisplayServer : : get_singleton ( ) - > window_set_ime_position ( get_global_position ( ) + _get_cursor_pixel_pos ( ) , get_viewport ( ) - > get_window_id ( ) ) ;
2020-03-07 17:02:54 +01:00
}
2017-06-25 17:50:45 +02:00
2020-08-09 11:06:36 +02:00
if ( DisplayServer : : get_singleton ( ) - > has_feature ( DisplayServer : : FEATURE_VIRTUAL_KEYBOARD ) & & virtual_keyboard_enabled ) {
2020-11-15 13:09:20 +01:00
String text = _base_get_text ( 0 , 0 , selection . selecting_line , selection . selecting_column ) ;
int cursor_start = text . length ( ) ;
int cursor_end = - 1 ;
if ( selection . active ) {
String selected_text = _base_get_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
if ( selected_text . length ( ) > 0 ) {
cursor_end = cursor_start + selected_text . length ( ) ;
}
}
DisplayServer : : get_singleton ( ) - > virtual_keyboard_show ( get_text ( ) , get_global_rect ( ) , true , - 1 , cursor_start , cursor_end ) ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 02:31:57 +01:00
} break ;
case NOTIFICATION_FOCUS_EXIT : {
2019-09-04 13:06:15 +02:00
if ( caret_blink_enabled ) {
caret_blink_timer - > stop ( ) ;
}
2020-03-07 17:02:54 +01:00
if ( get_viewport ( ) - > get_window_id ( ) ! = DisplayServer : : INVALID_WINDOW_ID ) {
DisplayServer : : get_singleton ( ) - > window_set_ime_position ( Point2 ( ) , get_viewport ( ) - > get_window_id ( ) ) ;
DisplayServer : : get_singleton ( ) - > window_set_ime_active ( false , get_viewport ( ) - > get_window_id ( ) ) ;
}
2017-08-07 13:09:56 +02:00
ime_text = " " ;
ime_selection = Point2 ( ) ;
2020-09-18 09:36:10 +02:00
text . invalidate_cache ( cursor . line , cursor . column , ime_text ) ;
2017-06-25 17:50:45 +02:00
2020-08-09 11:06:36 +02:00
if ( DisplayServer : : get_singleton ( ) - > has_feature ( DisplayServer : : FEATURE_VIRTUAL_KEYBOARD ) & & virtual_keyboard_enabled ) {
2020-03-03 14:36:29 +01:00
DisplayServer : : get_singleton ( ) - > virtual_keyboard_hide ( ) ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 02:31:57 +01:00
} break ;
2018-11-23 13:07:48 +01:00
case MainLoop : : NOTIFICATION_OS_IME_UPDATE : {
2019-01-08 21:52:56 +01:00
if ( has_focus ( ) ) {
2020-03-03 14:36:29 +01:00
ime_text = DisplayServer : : get_singleton ( ) - > ime_get_text ( ) ;
ime_selection = DisplayServer : : get_singleton ( ) - > ime_get_selection ( ) ;
2020-09-18 09:36:10 +02:00
String t ;
if ( cursor . column > = 0 ) {
t = text [ cursor . line ] . substr ( 0 , cursor . column ) + ime_text + text [ cursor . line ] . substr ( cursor . column , text [ cursor . line ] . length ( ) ) ;
} else {
t = ime_text ;
}
text . invalidate_cache ( cursor . line , cursor . column , t , structured_text_parser ( st_parser , st_args , t ) ) ;
2019-01-08 21:52:56 +01:00
update ( ) ;
}
2018-11-23 13:07:48 +01:00
} break ;
}
2017-08-07 13:09:56 +02:00
}
2020-07-27 12:43:20 +02:00
void TextEdit : : _consume_pair_symbol ( char32_t ch ) {
2015-01-02 19:08:40 +01:00
int cursor_position_to_move = cursor_get_column ( ) + 1 ;
2016-03-09 00:00:52 +01:00
2020-07-27 12:43:20 +02:00
char32_t ch_single [ 2 ] = { ch , 0 } ;
char32_t ch_single_pair [ 2 ] = { _get_right_pair_symbol ( ch ) , 0 } ;
char32_t ch_pair [ 3 ] = { ch , _get_right_pair_symbol ( ch ) , 0 } ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( is_selection_active ( ) ) {
int new_column , new_line ;
2016-03-09 00:00:52 +01:00
2016-04-06 08:36:29 +02:00
begin_complex_operation ( ) ;
2015-01-02 19:08:40 +01:00
_insert_text ( get_selection_from_line ( ) , get_selection_from_column ( ) ,
2017-03-05 16:44:50 +01:00
ch_single ,
& new_line , & new_column ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
int to_col_offset = 0 ;
2020-05-14 16:41:43 +02:00
if ( get_selection_from_line ( ) = = get_selection_to_line ( ) ) {
2015-01-02 19:08:40 +01:00
to_col_offset = 1 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
_insert_text ( get_selection_to_line ( ) ,
2017-03-05 16:44:50 +01:00
get_selection_to_column ( ) + to_col_offset ,
ch_single_pair ,
& new_line , & new_column ) ;
2016-04-06 08:36:29 +02:00
end_complex_operation ( ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
cursor_set_line ( get_selection_to_line ( ) ) ;
cursor_set_column ( get_selection_to_column ( ) + to_col_offset ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
deselect ( ) ;
update ( ) ;
return ;
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( ( ch = = ' \' ' | | ch = = ' " ' ) & &
2019-01-26 17:35:26 +01:00
cursor_get_column ( ) > 0 & & _is_text_char ( text [ cursor . line ] [ cursor_get_column ( ) - 1 ] ) & & ! _is_pair_right_symbol ( text [ cursor . line ] [ cursor_get_column ( ) ] ) ) {
2015-01-02 19:08:40 +01:00
insert_text_at_cursor ( ch_single ) ;
cursor_set_column ( cursor_position_to_move ) ;
return ;
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( cursor_get_column ( ) < text [ cursor . line ] . length ( ) ) {
if ( _is_text_char ( text [ cursor . line ] [ cursor_get_column ( ) ] ) ) {
2015-01-02 19:08:40 +01:00
insert_text_at_cursor ( ch_single ) ;
cursor_set_column ( cursor_position_to_move ) ;
return ;
}
2017-03-05 16:44:50 +01:00
if ( _is_pair_right_symbol ( ch ) & &
text [ cursor . line ] [ cursor_get_column ( ) ] = = ch ) {
2015-01-02 19:08:40 +01:00
cursor_set_column ( cursor_position_to_move ) ;
return ;
}
}
2016-03-09 00:00:52 +01:00
2020-02-10 23:09:07 +01:00
String line = text [ cursor . line ] ;
bool in_single_quote = false ;
bool in_double_quote = false ;
2020-03-01 23:04:11 +01:00
bool found_comment = false ;
2020-02-10 23:09:07 +01:00
int c = 0 ;
while ( c < line . length ( ) ) {
if ( line [ c ] = = ' \\ ' ) {
c + + ; // Skip quoted anything.
if ( cursor . column = = c ) {
break ;
}
2020-03-01 23:04:11 +01:00
} else if ( ! in_single_quote & & ! in_double_quote & & line [ c ] = = ' # ' ) {
found_comment = true ;
break ;
2020-02-10 23:09:07 +01:00
} else {
if ( line [ c ] = = ' \' ' & & ! in_double_quote ) {
in_single_quote = ! in_single_quote ;
} else if ( line [ c ] = = ' " ' & & ! in_single_quote ) {
in_double_quote = ! in_double_quote ;
}
}
c + + ;
if ( cursor . column = = c ) {
break ;
}
}
2020-03-01 23:04:11 +01:00
// Do not need to duplicate quotes while in comments
if ( found_comment ) {
insert_text_at_cursor ( ch_single ) ;
cursor_set_column ( cursor_position_to_move ) ;
return ;
}
// Disallow inserting duplicated quotes while already in string
2020-02-10 23:09:07 +01:00
if ( ( in_single_quote | | in_double_quote ) & & ( ch = = ' " ' | | ch = = ' \' ' ) ) {
insert_text_at_cursor ( ch_single ) ;
cursor_set_column ( cursor_position_to_move ) ;
return ;
}
2015-01-02 19:08:40 +01:00
insert_text_at_cursor ( ch_pair ) ;
cursor_set_column ( cursor_position_to_move ) ;
2014-04-27 12:34:37 +02:00
}
void TextEdit : : _consume_backspace_for_pair_symbol ( int prev_line , int prev_column ) {
2015-01-02 19:08:40 +01:00
bool remove_right_symbol = false ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( cursor . column < text [ cursor . line ] . length ( ) & & cursor . column > 0 ) {
2020-07-27 12:43:20 +02:00
char32_t left_char = text [ cursor . line ] [ cursor . column - 1 ] ;
char32_t right_char = text [ cursor . line ] [ cursor . column ] ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( right_char = = _get_right_pair_symbol ( left_char ) ) {
2015-01-02 19:08:40 +01:00
remove_right_symbol = true ;
}
}
2017-03-05 16:44:50 +01:00
if ( remove_right_symbol ) {
_remove_text ( prev_line , prev_column , cursor . line , cursor . column + 1 ) ;
2015-01-02 19:08:40 +01:00
} else {
2017-03-05 16:44:50 +01:00
_remove_text ( prev_line , prev_column , cursor . line , cursor . column ) ;
2015-01-02 19:08:40 +01:00
}
2014-04-27 12:34:37 +02:00
}
2014-02-10 02:10:30 +01:00
void TextEdit : : backspace_at_cursor ( ) {
2020-05-14 16:41:43 +02:00
if ( readonly ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
2020-05-14 16:41:43 +02:00
if ( cursor . column = = 0 & & cursor . line = = 0 ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
int prev_line = cursor . column ? cursor . line : cursor . line - 1 ;
int prev_column = cursor . column ? ( cursor . column - 1 ) : ( text [ cursor . line - 1 ] . length ( ) ) ;
2017-11-13 00:12:17 +01:00
2020-07-25 02:15:23 +02:00
if ( cursor . line ! = prev_line ) {
for ( int i = 0 ; i < gutters . size ( ) ; i + + ) {
if ( ! gutters [ i ] . overwritable ) {
continue ;
}
if ( text . get_line_gutter_text ( cursor . line , i ) ! = " " ) {
text . set_line_gutter_text ( prev_line , i , text . get_line_gutter_text ( cursor . line , i ) ) ;
text . set_line_gutter_item_color ( prev_line , i , text . get_line_gutter_item_color ( cursor . line , i ) ) ;
}
if ( text . get_line_gutter_icon ( cursor . line , i ) . is_valid ( ) ) {
text . set_line_gutter_icon ( prev_line , i , text . get_line_gutter_icon ( cursor . line , i ) ) ;
text . set_line_gutter_item_color ( prev_line , i , text . get_line_gutter_item_color ( cursor . line , i ) ) ;
}
if ( text . get_line_gutter_metadata ( cursor . line , i ) ! = " " ) {
text . set_line_gutter_metadata ( prev_line , i , text . get_line_gutter_metadata ( cursor . line , i ) ) ;
}
if ( text . is_line_gutter_clickable ( cursor . line , i ) ) {
text . set_line_gutter_clickable ( prev_line , i , true ) ;
}
}
}
2020-05-14 16:41:43 +02:00
if ( is_line_hidden ( cursor . line ) ) {
2017-11-13 00:12:17 +01:00
set_line_as_hidden ( prev_line , true ) ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
2017-03-05 16:44:50 +01:00
if ( auto_brace_completion_enabled & &
2015-01-02 19:08:40 +01:00
cursor . column > 0 & &
_is_pair_left_symbol ( text [ cursor . line ] [ cursor . column - 1 ] ) ) {
_consume_backspace_for_pair_symbol ( prev_line , prev_column ) ;
} else {
2019-08-18 17:56:24 +02:00
// Handle space indentation.
2019-05-08 23:56:00 +02:00
if ( cursor . column ! = 0 & & indent_using_spaces ) {
2019-08-18 17:56:24 +02:00
// Check if there are no other chars before cursor, just indentation.
2017-04-17 15:24:30 +02:00
bool unindent = true ;
int i = 0 ;
while ( i < cursor . column & & i < text [ cursor . line ] . length ( ) ) {
2019-05-08 23:56:00 +02:00
if ( ! _is_whitespace ( text [ cursor . line ] [ i ] ) ) {
2017-04-17 15:24:30 +02:00
unindent = false ;
break ;
}
i + + ;
}
2019-08-18 17:56:24 +02:00
// Then we can remove all spaces as a single character.
2017-04-17 15:24:30 +02:00
if ( unindent ) {
2019-08-18 17:56:24 +02:00
// We want to remove spaces up to closest indent, or whole indent if cursor is pointing at it.
2019-05-08 23:56:00 +02:00
int spaces_to_delete = _calculate_spaces_till_next_left_indent ( cursor . column ) ;
prev_column = cursor . column - spaces_to_delete ;
_remove_text ( cursor . line , prev_column , cursor . line , cursor . column ) ;
2017-04-17 15:24:30 +02:00
} else {
_remove_text ( prev_line , prev_column , cursor . line , cursor . column ) ;
}
} else {
_remove_text ( prev_line , prev_column , cursor . line , cursor . column ) ;
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-11-13 00:12:17 +01:00
cursor_set_line ( prev_line , true , true ) ;
2015-01-02 19:08:40 +01:00
cursor_set_column ( prev_column ) ;
2014-02-10 02:10:30 +01:00
}
2017-12-14 10:10:53 +01:00
void TextEdit : : indent_right ( ) {
int start_line ;
int end_line ;
2019-05-08 23:56:00 +02:00
2019-08-18 17:56:24 +02:00
// This value informs us by how much we changed selection position by indenting right.
// Default is 1 for tab indentation.
2019-05-08 23:56:00 +02:00
int selection_offset = 1 ;
2016-04-06 17:37:03 +02:00
begin_complex_operation ( ) ;
2017-12-14 10:10:53 +01:00
if ( is_selection_active ( ) ) {
start_line = get_selection_from_line ( ) ;
end_line = get_selection_to_line ( ) ;
} else {
start_line = cursor . line ;
end_line = start_line ;
}
2016-04-06 17:37:03 +02:00
2019-08-18 17:56:24 +02:00
// Ignore if the cursor is not past the first column.
2017-12-14 10:10:53 +01:00
if ( is_selection_active ( ) & & get_selection_to_column ( ) = = 0 ) {
2019-12-23 14:16:57 +01:00
selection_offset = 0 ;
2016-04-06 17:37:03 +02:00
end_line - - ;
}
for ( int i = start_line ; i < = end_line ; i + + ) {
String line_text = get_line ( i ) ;
2020-08-14 15:53:34 +02:00
if ( line_text . size ( ) = = 0 & & is_selection_active ( ) ) {
continue ;
}
2017-04-17 15:24:30 +02:00
if ( indent_using_spaces ) {
2019-08-18 17:56:24 +02:00
// We don't really care where selection is - we just need to know indentation level at the beginning of the line.
2019-05-08 23:56:00 +02:00
int left = _find_first_non_whitespace_column_of_line ( line_text ) ;
int spaces_to_add = _calculate_spaces_till_next_right_indent ( left ) ;
2019-08-18 17:56:24 +02:00
// Since we will add this much spaces we want move whole selection and cursor by this much.
2019-05-08 23:56:00 +02:00
selection_offset = spaces_to_add ;
2020-05-14 16:41:43 +02:00
for ( int j = 0 ; j < spaces_to_add ; j + + ) {
2019-05-08 23:56:00 +02:00
line_text = ' ' + line_text ;
2020-05-14 16:41:43 +02:00
}
2017-04-17 15:24:30 +02:00
} else {
line_text = ' \t ' + line_text ;
}
2016-04-06 17:37:03 +02:00
set_line ( i , line_text ) ;
}
2019-08-18 17:56:24 +02:00
// Fix selection and cursor being off after shifting selection right.
2017-12-14 10:10:53 +01:00
if ( is_selection_active ( ) ) {
2019-05-08 23:56:00 +02:00
select ( selection . from_line , selection . from_column + selection_offset , selection . to_line , selection . to_column + selection_offset ) ;
2017-12-14 10:10:53 +01:00
}
2019-05-08 23:56:00 +02:00
cursor_set_column ( cursor . column + selection_offset , false ) ;
2016-04-06 17:37:03 +02:00
end_complex_operation ( ) ;
update ( ) ;
}
2017-12-14 10:10:53 +01:00
void TextEdit : : indent_left ( ) {
int start_line ;
int end_line ;
2019-05-08 23:56:00 +02:00
2019-08-18 17:56:24 +02:00
// Moving cursor and selection after unindenting can get tricky because
// changing content of line can move cursor and selection on it's own (if new line ends before previous position of either),
// therefore we just remember initial values and at the end of the operation offset them by number of removed characters.
2019-05-08 23:56:00 +02:00
int removed_characters = 0 ;
int initial_selection_end_column = selection . to_column ;
int initial_cursor_column = cursor . column ;
2016-04-06 17:37:03 +02:00
begin_complex_operation ( ) ;
2017-12-14 10:10:53 +01:00
if ( is_selection_active ( ) ) {
start_line = get_selection_from_line ( ) ;
end_line = get_selection_to_line ( ) ;
} else {
start_line = cursor . line ;
end_line = start_line ;
}
2016-04-06 17:37:03 +02:00
2019-08-18 17:56:24 +02:00
// Ignore if the cursor is not past the first column.
2017-12-14 10:10:53 +01:00
if ( is_selection_active ( ) & & get_selection_to_column ( ) = = 0 ) {
2016-04-06 17:37:03 +02:00
end_line - - ;
}
String last_line_text = get_line ( end_line ) ;
for ( int i = start_line ; i < = end_line ; i + + ) {
String line_text = get_line ( i ) ;
if ( line_text . begins_with ( " \t " ) ) {
line_text = line_text . substr ( 1 , line_text . length ( ) ) ;
set_line ( i , line_text ) ;
2019-05-08 23:56:00 +02:00
removed_characters = 1 ;
} else if ( line_text . begins_with ( " " ) ) {
2019-08-18 17:56:24 +02:00
// When unindenting we aim to remove spaces before line that has selection no matter what is selected,
2019-05-08 23:56:00 +02:00
// so we start of by finding first non whitespace character of line
int left = _find_first_non_whitespace_column_of_line ( line_text ) ;
2019-08-18 17:56:24 +02:00
// Here we remove only enough spaces to align text to nearest full multiple of indentation_size.
// In case where selection begins at the start of indentation_size multiple we remove whole indentation level.
2019-05-08 23:56:00 +02:00
int spaces_to_remove = _calculate_spaces_till_next_left_indent ( left ) ;
line_text = line_text . substr ( spaces_to_remove , line_text . length ( ) ) ;
2016-04-06 17:37:03 +02:00
set_line ( i , line_text ) ;
2019-05-08 23:56:00 +02:00
removed_characters = spaces_to_remove ;
2016-04-06 17:37:03 +02:00
}
}
2019-08-18 17:56:24 +02:00
// Fix selection and cursor being off by one on the last line.
2017-12-14 10:10:53 +01:00
if ( is_selection_active ( ) & & last_line_text ! = get_line ( end_line ) ) {
2019-05-08 23:56:00 +02:00
select ( selection . from_line , selection . from_column - removed_characters ,
selection . to_line , initial_selection_end_column - removed_characters ) ;
2016-04-06 17:37:03 +02:00
}
2019-05-08 23:56:00 +02:00
cursor_set_column ( initial_cursor_column - removed_characters , false ) ;
2016-04-06 17:37:03 +02:00
end_complex_operation ( ) ;
update ( ) ;
}
2014-02-10 02:10:30 +01:00
2019-05-08 23:56:00 +02:00
int TextEdit : : _calculate_spaces_till_next_left_indent ( int column ) {
int spaces_till_indent = column % indent_size ;
2020-05-14 16:41:43 +02:00
if ( spaces_till_indent = = 0 ) {
2019-05-08 23:56:00 +02:00
spaces_till_indent = indent_size ;
2020-05-14 16:41:43 +02:00
}
2019-05-08 23:56:00 +02:00
return spaces_till_indent ;
}
int TextEdit : : _calculate_spaces_till_next_right_indent ( int column ) {
return indent_size - column % indent_size ;
}
2017-03-05 16:44:50 +01:00
void TextEdit : : _get_mouse_pos ( const Point2i & p_mouse , int & r_row , int & r_col ) const {
float rows = p_mouse . y ;
2018-01-18 15:52:51 +01:00
rows - = cache . style_normal - > get_margin ( MARGIN_TOP ) ;
2017-03-05 16:44:50 +01:00
rows / = get_row_height ( ) ;
2018-01-26 02:41:17 +01:00
rows + = get_v_scroll_offset ( ) ;
int first_vis_line = get_first_visible_line ( ) ;
2018-01-18 15:52:51 +01:00
int row = first_vis_line + Math : : floor ( rows ) ;
2018-01-26 02:41:17 +01:00
int wrap_index = 0 ;
if ( is_wrap_enabled ( ) | | is_hiding_enabled ( ) ) {
2018-06-14 09:10:30 +02:00
int f_ofs = num_lines_from_rows ( first_vis_line , cursor . wrap_ofs , rows + ( 1 * SGN ( rows ) ) , wrap_index ) - 1 ;
2020-05-14 16:41:43 +02:00
if ( rows < 0 ) {
2018-06-14 09:10:30 +02:00
row = first_vis_line - f_ofs ;
2020-05-14 16:41:43 +02:00
} else {
2018-06-14 09:10:30 +02:00
row = first_vis_line + f_ofs ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
}
2020-05-14 16:41:43 +02:00
if ( row < 0 ) {
2020-09-18 09:36:10 +02:00
row = 0 ;
2020-05-14 16:41:43 +02:00
}
2015-11-07 13:39:03 +01:00
2017-03-05 16:44:50 +01:00
int col = 0 ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( row > = text . size ( ) ) {
row = text . size ( ) - 1 ;
col = text [ row ] . size ( ) ;
2015-01-02 19:08:40 +01:00
} else {
2020-07-29 23:26:49 +02:00
int colx = p_mouse . x - ( cache . style_normal - > get_margin ( MARGIN_LEFT ) + gutters_width + gutter_padding ) ;
2018-01-26 02:41:17 +01:00
colx + = cursor . x_ofs ;
2020-09-18 09:36:10 +02:00
RID text_rid = text . get_line_data ( row ) - > get_line_rid ( wrap_index ) ;
if ( is_layout_rtl ( ) ) {
colx = TS - > shaped_text_get_size ( text_rid ) . x - colx ;
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
col = TS - > shaped_text_hit_test_position ( text_rid , colx ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
r_row = row ;
r_col = col ;
2014-02-10 02:10:30 +01:00
}
2019-06-22 20:22:52 +02:00
Vector2i TextEdit : : _get_cursor_pixel_pos ( ) {
adjust_viewport_to_cursor ( ) ;
2020-09-18 09:36:10 +02:00
int row = 1 ;
2019-06-22 20:22:52 +02:00
for ( int i = get_first_visible_line ( ) ; i < cursor . line ; i + + ) {
2020-09-18 09:36:10 +02:00
if ( ! is_line_hidden ( i ) ) {
row + = times_line_wraps ( i ) + 1 ;
2019-06-22 20:22:52 +02:00
}
}
2020-09-18 09:36:10 +02:00
row + = cursor . wrap_ofs ;
2019-06-22 20:22:52 +02:00
// Calculate final pixel position
2020-09-18 09:36:10 +02:00
int y = ( row - get_v_scroll_offset ( ) ) * get_row_height ( ) ;
2020-07-29 23:26:49 +02:00
int x = cache . style_normal - > get_margin ( MARGIN_LEFT ) + gutters_width + gutter_padding - cursor . x_ofs ;
2020-09-18 09:36:10 +02:00
Rect2 l_caret , t_caret ;
TextServer : : Direction l_dir , t_dir ;
RID text_rid = text . get_line_data ( cursor . line ) - > get_line_rid ( cursor . wrap_ofs ) ;
TS - > shaped_text_get_carets ( text_rid , cursor . column , l_caret , l_dir , t_caret , t_dir ) ;
if ( ( l_caret ! = Rect2 ( ) & & ( l_dir = = TextServer : : DIRECTION_AUTO | | l_dir = = ( TextServer : : Direction ) input_direction ) ) | | ( t_caret = = Rect2 ( ) ) ) {
x + = l_caret . position . x ;
} else {
x + = t_caret . position . x ;
2019-06-22 20:22:52 +02:00
}
return Vector2i ( x , y ) ;
}
2019-08-11 20:31:19 +02:00
void TextEdit : : _get_minimap_mouse_row ( const Point2i & p_mouse , int & r_row ) const {
float rows = p_mouse . y ;
rows - = cache . style_normal - > get_margin ( MARGIN_TOP ) ;
rows / = ( minimap_char_size . y + minimap_line_spacing ) ;
rows + = get_v_scroll_offset ( ) ;
// calculate visible lines
int minimap_visible_lines = _get_minimap_visible_rows ( ) ;
int visible_rows = get_visible_rows ( ) + 1 ;
int first_visible_line = get_first_visible_line ( ) - 1 ;
int draw_amount = visible_rows + ( smooth_scroll_enabled ? 1 : 0 ) ;
draw_amount + = times_line_wraps ( first_visible_line + 1 ) ;
int minimap_line_height = ( minimap_char_size . y + minimap_line_spacing ) ;
// calculate viewport size and y offset
int viewport_height = ( draw_amount - 1 ) * minimap_line_height ;
2019-08-25 15:49:13 +02:00
int control_height = _get_control_height ( ) - viewport_height ;
2019-08-11 20:31:19 +02:00
int viewport_offset_y = round ( get_scroll_pos_for_line ( first_visible_line ) * control_height ) / ( ( v_scroll - > get_max ( ) < = minimap_visible_lines ) ? ( minimap_visible_lines - draw_amount ) : ( v_scroll - > get_max ( ) - draw_amount ) ) ;
// calculate the first line.
int num_lines_before = round ( ( viewport_offset_y ) / minimap_line_height ) ;
int wi ;
int minimap_line = ( v_scroll - > get_max ( ) < = minimap_visible_lines ) ? - 1 : first_visible_line ;
2019-08-22 21:07:16 +02:00
if ( first_visible_line > 0 & & minimap_line > = 0 ) {
2019-08-11 20:31:19 +02:00
minimap_line - = num_lines_from_rows ( first_visible_line , 0 , - num_lines_before , wi ) ;
2019-10-20 14:27:53 +02:00
minimap_line - = ( minimap_line > 0 & & smooth_scroll_enabled ? 1 : 0 ) ;
2019-08-11 20:31:19 +02:00
} else {
minimap_line = 0 ;
}
int row = minimap_line + Math : : floor ( rows ) ;
int wrap_index = 0 ;
if ( is_wrap_enabled ( ) | | is_hiding_enabled ( ) ) {
int f_ofs = num_lines_from_rows ( minimap_line , cursor . wrap_ofs , rows + ( 1 * SGN ( rows ) ) , wrap_index ) - 1 ;
if ( rows < 0 ) {
row = minimap_line - f_ofs ;
} else {
row = minimap_line + f_ofs ;
}
}
if ( row < 0 ) {
row = 0 ;
}
if ( row > = text . size ( ) ) {
row = text . size ( ) - 1 ;
}
r_row = row ;
}
2017-05-20 17:38:03 +02:00
void TextEdit : : _gui_input ( const Ref < InputEvent > & p_gui_input ) {
2019-05-21 18:53:29 +02:00
double prev_v_scroll = v_scroll - > get_value ( ) ;
double prev_h_scroll = h_scroll - > get_value ( ) ;
2017-05-20 17:38:03 +02:00
Ref < InputEventMouseButton > mb = p_gui_input ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( mb . is_valid ( ) ) {
2020-09-18 09:36:10 +02:00
Vector2i mpos = mb - > get_position ( ) ;
if ( is_layout_rtl ( ) ) {
mpos . x = get_size ( ) . x - mpos . x ;
}
if ( ime_text . length ( ) ! = 0 ) {
// Ignore mouse clicks in IME input mode.
return ;
}
if ( completion_active & & completion_rect . has_point ( mpos ) ) {
2020-05-14 16:41:43 +02:00
if ( ! mb - > is_pressed ( ) ) {
2017-05-20 17:38:03 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_UP ) {
if ( completion_index > 0 ) {
completion_index - - ;
completion_current = completion_options [ completion_index ] ;
update ( ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
}
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_DOWN ) {
if ( completion_index < completion_options . size ( ) - 1 ) {
completion_index + + ;
completion_current = completion_options [ completion_index ] ;
update ( ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_LEFT ) {
2020-09-18 09:36:10 +02:00
completion_index = CLAMP ( completion_line_ofs + ( mpos . y - completion_rect . position . y ) / get_row_height ( ) , 0 , completion_options . size ( ) - 1 ) ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
completion_current = completion_options [ completion_index ] ;
update ( ) ;
2020-05-14 16:41:43 +02:00
if ( mb - > is_doubleclick ( ) ) {
2017-05-20 17:38:03 +02:00
_confirm_completion ( ) ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
return ;
} else {
_cancel_completion ( ) ;
_cancel_code_hint ( ) ;
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > is_pressed ( ) ) {
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_UP & & ! mb - > get_command ( ) ) {
2018-01-03 23:21:54 +01:00
if ( mb - > get_shift ( ) ) {
h_scroll - > set_value ( h_scroll - > get_value ( ) - ( 100 * mb - > get_factor ( ) ) ) ;
2020-06-20 20:56:17 +02:00
} else if ( v_scroll - > is_visible ( ) ) {
2018-01-03 23:21:54 +01:00
_scroll_up ( 3 * mb - > get_factor ( ) ) ;
}
2017-05-20 17:38:03 +02:00
}
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_DOWN & & ! mb - > get_command ( ) ) {
2018-01-03 23:21:54 +01:00
if ( mb - > get_shift ( ) ) {
h_scroll - > set_value ( h_scroll - > get_value ( ) + ( 100 * mb - > get_factor ( ) ) ) ;
2020-06-20 20:56:17 +02:00
} else if ( v_scroll - > is_visible ( ) ) {
2018-01-03 23:21:54 +01:00
_scroll_down ( 3 * mb - > get_factor ( ) ) ;
}
2017-05-20 17:38:03 +02:00
}
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_LEFT ) {
h_scroll - > set_value ( h_scroll - > get_value ( ) - ( 100 * mb - > get_factor ( ) ) ) ;
}
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_RIGHT ) {
h_scroll - > set_value ( h_scroll - > get_value ( ) + ( 100 * mb - > get_factor ( ) ) ) ;
}
if ( mb - > get_button_index ( ) = = BUTTON_LEFT ) {
_reset_caret_blink_timer ( ) ;
int row , col ;
2020-09-18 09:36:10 +02:00
_get_mouse_pos ( Point2i ( mpos . x , mpos . y ) , row , col ) ;
2016-05-09 20:21:55 +02:00
2020-07-25 02:15:23 +02:00
int left_margin = cache . style_normal - > get_margin ( MARGIN_LEFT ) ;
for ( int i = 0 ; i < gutters . size ( ) ; i + + ) {
if ( ! gutters [ i ] . draw | | gutters [ i ] . width < = 0 ) {
continue ;
}
2020-09-18 09:36:10 +02:00
if ( mpos . x > left_margin & & mpos . x < = ( left_margin + gutters [ i ] . width ) - 3 ) {
2020-07-25 02:15:23 +02:00
emit_signal ( " gutter_clicked " , row , i ) ;
return ;
}
left_margin + = gutters [ i ] . width ;
}
2019-08-18 17:56:24 +02:00
// Unfold on folded icon click.
2017-11-16 05:00:27 +01:00
if ( is_folded ( row ) ) {
2020-07-29 23:26:49 +02:00
left_margin + = gutter_padding + text . get_line_width ( row ) - cursor . x_ofs ;
2020-09-18 09:36:10 +02:00
if ( mpos . x > left_margin & & mpos . x < = left_margin + cache . folded_eol_icon - > get_width ( ) + 3 ) {
2017-11-16 05:00:27 +01:00
unfold_line ( row ) ;
2017-11-13 00:12:17 +01:00
return ;
}
}
2019-08-11 20:31:19 +02:00
// minimap
if ( draw_minimap ) {
2019-08-25 15:38:14 +02:00
_update_minimap_click ( ) ;
2019-08-11 20:31:19 +02:00
if ( dragging_minimap ) {
return ;
}
}
2017-05-20 17:38:03 +02:00
int prev_col = cursor . column ;
int prev_line = cursor . line ;
2016-05-26 15:17:14 +02:00
2017-11-13 00:12:17 +01:00
cursor_set_line ( row , true , false ) ;
2017-05-20 17:38:03 +02:00
cursor_set_column ( col ) ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_shift ( ) & & ( cursor . column ! = prev_col | | cursor . line ! = prev_line ) ) {
if ( ! selection . active ) {
selection . active = true ;
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_POINTER ;
2017-05-20 17:38:03 +02:00
selection . from_column = prev_col ;
selection . from_line = prev_line ;
selection . to_column = cursor . column ;
selection . to_line = cursor . line ;
if ( selection . from_line > selection . to_line | | ( selection . from_line = = selection . to_line & & selection . from_column > selection . to_column ) ) {
SWAP ( selection . from_column , selection . to_column ) ;
SWAP ( selection . from_line , selection . to_line ) ;
selection . shiftclick_left = false ;
} else {
selection . shiftclick_left = true ;
}
selection . selecting_line = prev_line ;
selection . selecting_column = prev_col ;
update ( ) ;
} else {
if ( cursor . line < selection . selecting_line | | ( cursor . line = = selection . selecting_line & & cursor . column < selection . selecting_column ) ) {
if ( selection . shiftclick_left ) {
2017-03-05 16:44:50 +01:00
SWAP ( selection . from_column , selection . to_column ) ;
SWAP ( selection . from_line , selection . to_line ) ;
2017-05-20 17:38:03 +02:00
selection . shiftclick_left = ! selection . shiftclick_left ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
selection . from_column = cursor . column ;
selection . from_line = cursor . line ;
2015-08-13 00:34:07 +02:00
2017-05-20 17:38:03 +02:00
} else if ( cursor . line > selection . selecting_line | | ( cursor . line = = selection . selecting_line & & cursor . column > selection . selecting_column ) ) {
if ( ! selection . shiftclick_left ) {
SWAP ( selection . from_column , selection . to_column ) ;
SWAP ( selection . from_line , selection . to_line ) ;
selection . shiftclick_left = ! selection . shiftclick_left ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
selection . to_column = cursor . column ;
selection . to_line = cursor . line ;
2015-08-13 00:34:07 +02:00
2017-05-20 17:38:03 +02:00
} else {
selection . active = false ;
2015-01-02 19:08:40 +01:00
}
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
update ( ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} else {
selection . active = false ;
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_POINTER ;
2017-05-20 17:38:03 +02:00
selection . selecting_line = row ;
selection . selecting_column = col ;
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( ! mb - > is_doubleclick ( ) & & ( OS : : get_singleton ( ) - > get_ticks_msec ( ) - last_dblclk ) < 600 & & cursor . line = = prev_line ) {
2019-08-18 17:56:24 +02:00
// Triple-click select line.
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_LINE ;
2017-11-05 16:54:00 +01:00
_update_selection_mode_line ( ) ;
2017-05-20 17:38:03 +02:00
last_dblclk = 0 ;
} else if ( mb - > is_doubleclick ( ) & & text [ cursor . line ] . length ( ) ) {
2019-08-18 17:56:24 +02:00
// Double-click select word.
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_WORD ;
2017-11-05 16:54:00 +01:00
_update_selection_mode_word ( ) ;
2017-05-20 17:38:03 +02:00
last_dblclk = OS : : get_singleton ( ) - > get_ticks_msec ( ) ;
2016-05-17 01:25:17 +02:00
}
2015-12-09 19:56:41 +01:00
2017-05-20 17:38:03 +02:00
update ( ) ;
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_RIGHT & & context_menu_enabled ) {
2017-12-20 02:36:47 +01:00
_reset_caret_blink_timer ( ) ;
int row , col ;
2020-09-18 09:36:10 +02:00
_get_mouse_pos ( Point2i ( mpos . x , mpos . y ) , row , col ) ;
2017-12-20 02:36:47 +01:00
if ( is_right_click_moving_caret ( ) ) {
if ( is_selection_active ( ) ) {
int from_line = get_selection_from_line ( ) ;
int to_line = get_selection_to_line ( ) ;
int from_column = get_selection_from_column ( ) ;
int to_column = get_selection_to_column ( ) ;
if ( row < from_line | | row > to_line | | ( row = = from_line & & col < from_column ) | | ( row = = to_line & & col > to_column ) ) {
2019-08-18 17:56:24 +02:00
// Right click is outside the selected text.
2017-12-20 02:36:47 +01:00
deselect ( ) ;
}
}
if ( ! is_selection_active ( ) ) {
cursor_set_line ( row , true , false ) ;
cursor_set_column ( col ) ;
}
}
2020-09-18 09:36:10 +02:00
menu - > set_position ( get_screen_transform ( ) . xform ( mpos ) ) ;
2017-05-20 17:38:03 +02:00
menu - > set_size ( Vector2 ( 1 , 1 ) ) ;
2020-03-12 13:37:40 +01:00
// menu->set_scale(get_global_transform().get_scale());
2017-05-20 17:38:03 +02:00
menu - > popup ( ) ;
grab_focus ( ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
} else {
2019-08-11 20:31:19 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_LEFT ) {
2019-08-30 16:19:25 +02:00
if ( mb - > get_command ( ) & & highlighted_word ! = String ( ) ) {
int row , col ;
2020-09-18 09:36:10 +02:00
_get_mouse_pos ( Point2i ( mpos . x , mpos . y ) , row , col ) ;
2019-08-30 16:19:25 +02:00
emit_signal ( " symbol_lookup " , highlighted_word , row , col ) ;
return ;
}
2019-08-11 20:31:19 +02:00
dragging_minimap = false ;
dragging_selection = false ;
2019-08-25 15:38:14 +02:00
can_drag_minimap = false ;
2017-05-20 17:38:03 +02:00
click_select_held - > stop ( ) ;
2019-08-11 20:31:19 +02:00
}
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Notify to show soft keyboard.
2017-05-20 17:38:03 +02:00
notification ( NOTIFICATION_FOCUS_ENTER ) ;
}
}
2016-03-09 00:00:52 +01:00
2017-11-01 21:49:39 +01:00
const Ref < InputEventPanGesture > pan_gesture = p_gui_input ;
if ( pan_gesture . is_valid ( ) ) {
const real_t delta = pan_gesture - > get_delta ( ) . y ;
if ( delta < 0 ) {
_scroll_up ( - delta ) ;
} else {
_scroll_down ( delta ) ;
}
h_scroll - > set_value ( h_scroll - > get_value ( ) + pan_gesture - > get_delta ( ) . x * 100 ) ;
2020-05-14 16:41:43 +02:00
if ( v_scroll - > get_value ( ) ! = prev_v_scroll | | h_scroll - > get_value ( ) ! = prev_h_scroll ) {
2019-08-18 17:56:24 +02:00
accept_event ( ) ; // Accept event if scroll changed.
2020-05-14 16:41:43 +02:00
}
2019-05-21 18:53:29 +02:00
2017-11-01 21:49:39 +01:00
return ;
}
2017-05-20 17:38:03 +02:00
Ref < InputEventMouseMotion > mm = p_gui_input ;
2016-09-12 15:52:29 +02:00
2017-05-20 17:38:03 +02:00
if ( mm . is_valid ( ) ) {
2020-09-18 09:36:10 +02:00
Vector2i mpos = mm - > get_position ( ) ;
if ( is_layout_rtl ( ) ) {
mpos . x = get_size ( ) . x - mpos . x ;
}
2017-05-20 17:38:03 +02:00
if ( select_identifiers_enabled ) {
2019-09-21 14:45:20 +02:00
if ( ! dragging_minimap & & ! dragging_selection & & mm - > get_command ( ) & & mm - > get_button_mask ( ) = = 0 ) {
2020-09-18 09:36:10 +02:00
String new_word = get_word_at_pos ( mpos ) ;
2017-05-20 17:38:03 +02:00
if ( new_word ! = highlighted_word ) {
2020-03-01 09:13:41 +01:00
emit_signal ( " symbol_validate " , new_word ) ;
2017-05-20 17:38:03 +02:00
}
} else {
if ( highlighted_word ! = String ( ) ) {
2020-03-01 09:13:41 +01:00
set_highlighted_word ( String ( ) ) ;
2016-09-12 15:52:29 +02:00
}
}
2017-05-20 17:38:03 +02:00
}
2016-09-12 15:52:29 +02:00
2019-08-18 17:56:24 +02:00
if ( mm - > get_button_mask ( ) & BUTTON_MASK_LEFT & & get_viewport ( ) - > gui_get_drag_data ( ) = = Variant ( ) ) { // Ignore if dragging.
2017-11-05 16:54:00 +01:00
_reset_caret_blink_timer ( ) ;
2016-03-09 00:00:52 +01:00
2019-08-11 20:31:19 +02:00
if ( draw_minimap & & ! dragging_selection ) {
2019-08-25 15:38:14 +02:00
_update_minimap_drag ( ) ;
2019-08-11 20:31:19 +02:00
}
if ( ! dragging_minimap ) {
switch ( selection . selecting_mode ) {
2020-10-11 19:44:53 +02:00
case SelectionMode : : SELECTION_MODE_POINTER : {
2019-08-11 20:31:19 +02:00
_update_selection_mode_pointer ( ) ;
} break ;
2020-10-11 19:44:53 +02:00
case SelectionMode : : SELECTION_MODE_WORD : {
2019-08-11 20:31:19 +02:00
_update_selection_mode_word ( ) ;
} break ;
2020-10-11 19:44:53 +02:00
case SelectionMode : : SELECTION_MODE_LINE : {
2019-08-11 20:31:19 +02:00
_update_selection_mode_line ( ) ;
} break ;
default : {
break ;
}
2017-11-05 16:54:00 +01:00
}
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
}
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( v_scroll - > get_value ( ) ! = prev_v_scroll | | h_scroll - > get_value ( ) ! = prev_h_scroll ) {
2019-08-18 17:56:24 +02:00
accept_event ( ) ; // Accept event if scroll changed.
2020-05-14 16:41:43 +02:00
}
2019-05-21 18:53:29 +02:00
2017-05-20 17:38:03 +02:00
Ref < InputEventKey > k = p_gui_input ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( k . is_valid ( ) ) {
2019-08-18 17:56:24 +02:00
k = k - > duplicate ( ) ; // It will be modified later on.
2016-09-12 15:52:29 +02:00
# ifdef OSX_ENABLED
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_META ) {
2016-09-12 15:52:29 +02:00
# else
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_CONTROL ) {
2016-09-12 15:52:29 +02:00
# endif
2017-05-20 17:38:03 +02:00
if ( select_identifiers_enabled ) {
2019-09-21 14:45:20 +02:00
if ( k - > is_pressed ( ) & & ! dragging_minimap & & ! dragging_selection ) {
2020-09-18 09:36:10 +02:00
Point2 mp = _get_local_mouse_pos ( ) ;
emit_signal ( " symbol_validate " , get_word_at_pos ( mp ) ) ;
2017-05-20 17:38:03 +02:00
} else {
2020-03-01 09:13:41 +01:00
set_highlighted_word ( String ( ) ) ;
2016-09-12 15:52:29 +02:00
}
}
2017-05-20 17:38:03 +02:00
}
2016-09-12 15:52:29 +02:00
2020-05-14 16:41:43 +02:00
if ( ! k - > is_pressed ( ) ) {
2017-05-20 17:38:03 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( completion_active ) {
2020-05-14 16:41:43 +02:00
if ( readonly ) {
2017-05-20 17:38:03 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2015-01-05 02:39:21 +01:00
2017-05-20 17:38:03 +02:00
bool valid = true ;
2020-05-14 16:41:43 +02:00
if ( k - > get_command ( ) | | k - > get_metakey ( ) ) {
2017-05-20 17:38:03 +02:00
valid = false ;
2020-05-14 16:41:43 +02:00
}
2015-01-05 02:39:21 +01:00
2017-05-20 17:38:03 +02:00
if ( valid ) {
if ( ! k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_UP ) {
2017-05-20 17:38:03 +02:00
if ( completion_index > 0 ) {
completion_index - - ;
2018-03-08 03:47:42 +01:00
} else {
completion_index = completion_options . size ( ) - 1 ;
2015-01-02 19:08:40 +01:00
}
2018-03-08 03:47:42 +01:00
completion_current = completion_options [ completion_index ] ;
update ( ) ;
2017-05-20 17:38:03 +02:00
accept_event ( ) ;
return ;
}
2015-01-05 02:39:21 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_DOWN ) {
2017-05-20 17:38:03 +02:00
if ( completion_index < completion_options . size ( ) - 1 ) {
completion_index + + ;
2018-03-08 03:47:42 +01:00
} else {
completion_index = 0 ;
2015-01-02 19:08:40 +01:00
}
2018-03-08 03:47:42 +01:00
completion_current = completion_options [ completion_index ] ;
update ( ) ;
2017-05-20 17:38:03 +02:00
accept_event ( ) ;
return ;
}
2015-01-05 02:39:21 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_PAGEUP ) {
2020-03-12 13:37:40 +01:00
completion_index - = get_theme_constant ( " completion_lines " ) ;
2020-05-14 16:41:43 +02:00
if ( completion_index < 0 ) {
2017-03-05 16:44:50 +01:00
completion_index = 0 ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
completion_current = completion_options [ completion_index ] ;
update ( ) ;
accept_event ( ) ;
return ;
}
2015-01-05 02:39:21 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_PAGEDOWN ) {
2020-03-12 13:37:40 +01:00
completion_index + = get_theme_constant ( " completion_lines " ) ;
2020-05-14 16:41:43 +02:00
if ( completion_index > = completion_options . size ( ) ) {
2017-03-05 16:44:50 +01:00
completion_index = completion_options . size ( ) - 1 ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
completion_current = completion_options [ completion_index ] ;
update ( ) ;
accept_event ( ) ;
return ;
}
2015-01-05 02:39:21 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_HOME & & completion_index > 0 ) {
2017-05-20 17:38:03 +02:00
completion_index = 0 ;
completion_current = completion_options [ completion_index ] ;
update ( ) ;
accept_event ( ) ;
return ;
}
2015-01-05 02:39:21 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_END & & completion_index < completion_options . size ( ) - 1 ) {
2017-05-20 17:38:03 +02:00
completion_index = completion_options . size ( ) - 1 ;
completion_current = completion_options [ completion_index ] ;
update ( ) ;
accept_event ( ) ;
return ;
}
2015-01-05 02:39:21 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_KP_ENTER | | k - > get_keycode ( ) = = KEY_ENTER | | k - > get_keycode ( ) = = KEY_TAB ) {
2017-05-20 17:38:03 +02:00
_confirm_completion ( ) ;
accept_event ( ) ;
return ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_BACKSPACE ) {
2016-05-09 20:21:55 +02:00
_reset_caret_blink_timer ( ) ;
2017-05-20 17:38:03 +02:00
backspace_at_cursor ( ) ;
_update_completion_candidates ( ) ;
accept_event ( ) ;
return ;
}
2016-04-03 16:21:16 +02:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_SHIFT ) {
2017-05-20 17:38:03 +02:00
accept_event ( ) ;
return ;
}
}
2016-04-03 16:21:16 +02:00
2017-05-20 17:38:03 +02:00
if ( k - > get_unicode ( ) > 32 ) {
_reset_caret_blink_timer ( ) ;
2016-04-03 16:21:16 +02:00
2020-07-27 12:43:20 +02:00
const char32_t chr [ 2 ] = { ( char32_t ) k - > get_unicode ( ) , 0 } ;
2017-05-20 17:38:03 +02:00
if ( auto_brace_completion_enabled & & _is_pair_symbol ( chr [ 0 ] ) ) {
_consume_pair_symbol ( chr [ 0 ] ) ;
} else {
2019-08-18 17:56:24 +02:00
// Remove the old character if in insert mode.
2017-05-20 17:38:03 +02:00
if ( insert_mode ) {
begin_complex_operation ( ) ;
2019-08-18 17:56:24 +02:00
// Make sure we don't try and remove empty space.
2017-05-20 17:38:03 +02:00
if ( cursor . column < get_line ( cursor . line ) . length ( ) ) {
_remove_text ( cursor . line , cursor . column , cursor . line , cursor . column + 1 ) ;
2015-01-02 19:08:40 +01:00
}
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
_insert_text_at_cursor ( chr ) ;
if ( insert_mode ) {
end_complex_operation ( ) ;
}
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
_update_completion_candidates ( ) ;
accept_event ( ) ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
return ;
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
_cancel_completion ( ) ;
}
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
/* TEST CONTROL FIRST! */
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Some remaps for duplicate functions.
2018-04-05 19:59:35 +02:00
if ( k - > get_command ( ) & & ! k - > get_shift ( ) & & ! k - > get_alt ( ) & & ! k - > get_metakey ( ) & & k - > get_keycode ( ) = = KEY_INSERT ) {
k - > set_keycode ( KEY_C ) ;
2017-05-20 17:38:03 +02:00
}
2018-04-05 19:59:35 +02:00
if ( ! k - > get_command ( ) & & k - > get_shift ( ) & & ! k - > get_alt ( ) & & ! k - > get_metakey ( ) & & k - > get_keycode ( ) = = KEY_INSERT ) {
k - > set_keycode ( KEY_V ) ;
2017-05-20 17:38:03 +02:00
k - > set_command ( true ) ;
k - > set_shift ( false ) ;
}
2019-05-02 19:57:37 +02:00
# ifdef APPLE_STYLE_KEYS
if ( k - > get_control ( ) & & ! k - > get_shift ( ) & & ! k - > get_alt ( ) & & ! k - > get_command ( ) ) {
2019-05-04 16:05:49 +02:00
uint32_t remap_key = KEY_UNKNOWN ;
2018-04-05 19:59:35 +02:00
switch ( k - > get_keycode ( ) ) {
2019-05-02 19:57:37 +02:00
case KEY_F : {
2019-05-04 16:05:49 +02:00
remap_key = KEY_RIGHT ;
2019-05-02 19:57:37 +02:00
} break ;
case KEY_B : {
2019-05-04 16:05:49 +02:00
remap_key = KEY_LEFT ;
2019-05-02 19:57:37 +02:00
} break ;
case KEY_P : {
2019-05-04 16:05:49 +02:00
remap_key = KEY_UP ;
2019-05-02 19:57:37 +02:00
} break ;
case KEY_N : {
2019-05-04 16:05:49 +02:00
remap_key = KEY_DOWN ;
} break ;
case KEY_D : {
remap_key = KEY_DELETE ;
} break ;
case KEY_H : {
remap_key = KEY_BACKSPACE ;
2019-05-02 19:57:37 +02:00
} break ;
}
2019-05-04 16:05:49 +02:00
if ( remap_key ! = KEY_UNKNOWN ) {
2018-04-05 19:59:35 +02:00
k - > set_keycode ( remap_key ) ;
2019-05-02 19:57:37 +02:00
k - > set_control ( false ) ;
}
}
# endif
2016-09-12 15:52:29 +02:00
2018-08-10 11:23:13 +02:00
_reset_caret_blink_timer ( ) ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Save here for insert mode, just in case it is cleared in the following section.
2017-05-20 17:38:03 +02:00
bool had_selection = selection . active ;
2015-01-02 19:08:40 +01:00
2019-08-18 17:56:24 +02:00
// Stuff to do when selection is active.
2019-02-16 07:56:24 +01:00
if ( ! readonly & & selection . active ) {
2017-05-20 17:38:03 +02:00
bool clear = false ;
bool unselect = false ;
bool dobreak = false ;
2016-03-09 00:00:52 +01:00
2018-04-05 19:59:35 +02:00
switch ( k - > get_keycode ( ) ) {
2017-05-20 17:38:03 +02:00
case KEY_TAB : {
if ( k - > get_shift ( ) ) {
2017-12-14 10:10:53 +01:00
indent_left ( ) ;
2017-05-20 17:38:03 +02:00
} else {
2017-12-14 10:10:53 +01:00
indent_right ( ) ;
2017-05-20 17:38:03 +02:00
}
dobreak = true ;
accept_event ( ) ;
} break ;
case KEY_X :
case KEY_C :
2019-08-18 17:56:24 +02:00
// Special keys often used with control, wait.
2017-05-20 17:38:03 +02:00
clear = ( ! k - > get_command ( ) | | k - > get_shift ( ) | | k - > get_alt ( ) ) ;
break ;
case KEY_DELETE :
if ( ! k - > get_shift ( ) ) {
2015-01-02 19:08:40 +01:00
accept_event ( ) ;
2017-03-05 16:44:50 +01:00
clear = true ;
dobreak = true ;
2017-05-20 17:38:03 +02:00
} else if ( k - > get_command ( ) | | k - > get_alt ( ) ) {
dobreak = true ;
}
break ;
case KEY_BACKSPACE :
accept_event ( ) ;
clear = true ;
dobreak = true ;
break ;
case KEY_LEFT :
case KEY_RIGHT :
case KEY_UP :
case KEY_DOWN :
case KEY_PAGEUP :
case KEY_PAGEDOWN :
case KEY_HOME :
case KEY_END :
2019-08-18 17:56:24 +02:00
// Ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys).
2020-05-14 16:41:43 +02:00
if ( k - > get_command ( ) | | k - > get_shift ( ) | | k - > get_alt ( ) ) {
2015-01-02 19:08:40 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
unselect = true ;
break ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
default :
2020-05-14 16:41:43 +02:00
if ( k - > get_unicode ( ) > = 32 & & ! k - > get_command ( ) & & ! k - > get_alt ( ) & & ! k - > get_metakey ( ) ) {
2017-05-20 17:38:03 +02:00
clear = true ;
2020-05-14 16:41:43 +02:00
}
if ( auto_brace_completion_enabled & & _is_pair_left_symbol ( k - > get_unicode ( ) ) ) {
2017-05-20 17:38:03 +02:00
clear = false ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( unselect ) {
selection . active = false ;
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_NONE ;
2017-05-20 17:38:03 +02:00
update ( ) ;
}
if ( clear ) {
if ( ! dobreak ) {
begin_complex_operation ( ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
selection . active = false ;
update ( ) ;
_remove_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
2017-11-13 00:12:17 +01:00
cursor_set_line ( selection . from_line , true , false ) ;
2017-05-20 17:38:03 +02:00
cursor_set_column ( selection . from_column ) ;
update ( ) ;
2015-01-02 19:08:40 +01:00
}
2020-05-14 16:41:43 +02:00
if ( dobreak ) {
2017-05-20 17:38:03 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
selection . selecting_text = false ;
2016-03-09 00:00:52 +01:00
2018-04-05 19:59:35 +02:00
bool keycode_handled = true ;
2016-03-09 00:00:52 +01:00
2018-04-05 19:59:35 +02:00
// Special keycode test.
2016-03-09 00:00:52 +01:00
2018-04-05 19:59:35 +02:00
switch ( k - > get_keycode ( ) ) {
2017-08-06 15:26:07 +02:00
case KEY_KP_ENTER :
case KEY_ENTER : {
2020-05-14 16:41:43 +02:00
if ( readonly ) {
2017-05-20 17:38:03 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
2017-05-20 17:38:03 +02:00
String ins = " \n " ;
2019-08-18 17:56:24 +02:00
// Keep indentation.
2017-05-20 17:38:03 +02:00
int space_count = 0 ;
2017-08-30 20:35:38 +02:00
for ( int i = 0 ; i < cursor . column ; i + + ) {
if ( text [ cursor . line ] [ i ] = = ' \t ' ) {
2017-05-20 17:38:03 +02:00
if ( indent_using_spaces ) {
ins + = space_indent ;
} else {
ins + = " \t " ;
}
space_count = 0 ;
2017-08-30 20:35:38 +02:00
} else if ( text [ cursor . line ] [ i ] = = ' ' ) {
2017-05-20 17:38:03 +02:00
space_count + + ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( space_count = = indent_size ) {
2017-04-17 15:24:30 +02:00
if ( indent_using_spaces ) {
ins + = space_indent ;
} else {
ins + = " \t " ;
}
space_count = 0 ;
}
2017-05-20 17:38:03 +02:00
} else {
break ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
}
2017-09-02 16:59:30 +02:00
2020-05-14 16:41:43 +02:00
if ( is_folded ( cursor . line ) ) {
2017-11-13 00:12:17 +01:00
unfold_line ( cursor . line ) ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
2017-09-02 16:59:30 +02:00
bool brace_indent = false ;
2019-08-18 17:56:24 +02:00
// No need to indent if we are going upwards.
2017-09-02 16:59:30 +02:00
if ( auto_indent & & ! ( k - > get_command ( ) & & k - > get_shift ( ) ) ) {
2019-10-18 11:02:05 +02:00
// Indent once again if previous line will end with ':','{','[','(' and the line is not a comment
2019-08-18 17:56:24 +02:00
// (i.e. colon/brace precedes current cursor position).
2019-10-18 11:02:05 +02:00
if ( cursor . column > 0 ) {
2019-10-25 15:01:43 +02:00
bool indent_char_found = false ;
bool should_indent = false ;
2019-10-25 16:14:24 +02:00
char indent_char = ' : ' ;
char c = text [ cursor . line ] [ cursor . column ] ;
2019-10-25 15:01:43 +02:00
for ( int i = 0 ; i < cursor . column ; i + + ) {
c = text [ cursor . line ] [ i ] ;
switch ( c ) {
case ' : ' :
case ' { ' :
case ' [ ' :
case ' ( ' :
indent_char_found = true ;
should_indent = true ;
indent_char = c ;
continue ;
}
2017-09-02 16:59:30 +02:00
2020-09-18 09:36:10 +02:00
if ( indent_char_found & & is_line_comment ( cursor . line ) ) {
2019-10-25 15:01:43 +02:00
should_indent = true ;
break ;
} else if ( indent_char_found & & ! _is_whitespace ( c ) ) {
should_indent = false ;
indent_char_found = false ;
}
}
if ( ! is_line_comment ( cursor . line ) & & should_indent ) {
if ( indent_using_spaces ) {
ins + = space_indent ;
} else {
ins + = " \t " ;
}
// No need to move the brace below if we are not taking the text with us.
2020-07-27 12:43:20 +02:00
char32_t closing_char = _get_right_pair_symbol ( indent_char ) ;
2019-10-25 15:01:43 +02:00
if ( ( closing_char ! = 0 ) & & ( closing_char = = text [ cursor . line ] [ cursor . column ] ) & & ! k - > get_command ( ) ) {
brace_indent = true ;
ins + = " \n " + ins . substr ( 1 , ins . length ( ) - 2 ) ;
}
2017-09-02 16:59:30 +02:00
}
2015-11-29 17:02:35 +01:00
}
2017-05-20 17:38:03 +02:00
}
2017-10-31 21:42:53 +01:00
begin_complex_operation ( ) ;
2017-05-20 17:38:03 +02:00
bool first_line = false ;
if ( k - > get_command ( ) ) {
if ( k - > get_shift ( ) ) {
if ( cursor . line > 0 ) {
cursor_set_line ( cursor . line - 1 ) ;
2016-03-04 20:10:31 +01:00
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
2017-05-20 17:38:03 +02:00
} else {
cursor_set_column ( 0 ) ;
first_line = true ;
2016-03-04 20:10:31 +01:00
}
2017-05-20 17:38:03 +02:00
} else {
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
2016-03-04 20:10:31 +01:00
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-10-31 21:42:53 +01:00
insert_text_at_cursor ( ins ) ;
2016-03-04 20:10:31 +01:00
2017-05-20 17:38:03 +02:00
if ( first_line ) {
cursor_set_line ( 0 ) ;
2017-09-02 16:59:30 +02:00
} else if ( brace_indent ) {
cursor_set_line ( cursor . line - 1 ) ;
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
2017-05-20 17:38:03 +02:00
}
2017-10-31 21:42:53 +01:00
end_complex_operation ( ) ;
2017-05-20 17:38:03 +02:00
} break ;
case KEY_ESCAPE : {
if ( completion_hint ! = " " ) {
completion_hint = " " ;
update ( ) ;
} else {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
}
} break ;
case KEY_TAB : {
2020-05-14 16:41:43 +02:00
if ( k - > get_command ( ) ) {
2020-05-10 12:56:01 +02:00
break ; // Avoid tab when command.
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( readonly ) {
2017-05-20 17:38:03 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-12-14 10:10:53 +01:00
if ( is_selection_active ( ) ) {
if ( k - > get_shift ( ) ) {
indent_left ( ) ;
} else {
indent_right ( ) ;
}
2017-05-20 17:38:03 +02:00
} else {
if ( k - > get_shift ( ) ) {
2019-08-18 17:56:24 +02:00
// Simple unindent.
2017-05-20 17:38:03 +02:00
int cc = cursor . column ;
2017-12-23 09:28:02 +01:00
const String & line = text [ cursor . line ] ;
2019-05-08 23:56:00 +02:00
int left = _find_first_non_whitespace_column_of_line ( line ) ;
2017-12-23 09:28:02 +01:00
cc = MIN ( cc , left ) ;
2020-05-14 16:41:43 +02:00
while ( cc < indent_size & & cc < left & & line [ cc ] = = ' ' ) {
2017-12-23 09:28:02 +01:00
cc + + ;
2020-05-14 16:41:43 +02:00
}
2017-12-23 09:28:02 +01:00
2017-05-20 17:38:03 +02:00
if ( cc > 0 & & cc < = text [ cursor . line ] . length ( ) ) {
2017-12-23 09:28:02 +01:00
if ( text [ cursor . line ] [ cc - 1 ] = = ' \t ' ) {
2019-08-18 17:56:24 +02:00
// Tabs unindentation.
2017-12-23 09:28:02 +01:00
_remove_text ( cursor . line , cc - 1 , cursor . line , cc ) ;
2020-05-14 16:41:43 +02:00
if ( cursor . column > = left ) {
2017-12-23 09:28:02 +01:00
cursor_set_column ( MAX ( 0 , cursor . column - 1 ) ) ;
2020-05-14 16:41:43 +02:00
}
2017-12-23 09:28:02 +01:00
update ( ) ;
2017-05-20 17:38:03 +02:00
} else {
2019-08-18 17:56:24 +02:00
// Spaces unindentation.
2019-05-08 23:56:00 +02:00
int spaces_to_remove = _calculate_spaces_till_next_left_indent ( cc ) ;
if ( spaces_to_remove > 0 ) {
_remove_text ( cursor . line , cc - spaces_to_remove , cursor . line , cc ) ;
2020-05-14 16:41:43 +02:00
if ( cursor . column > left - spaces_to_remove ) { // Inside text?
2019-05-08 23:56:00 +02:00
cursor_set_column ( MAX ( 0 , cursor . column - spaces_to_remove ) ) ;
2020-05-14 16:41:43 +02:00
}
2017-12-23 09:28:02 +01:00
update ( ) ;
2017-04-17 15:24:30 +02:00
}
2015-01-02 19:08:40 +01:00
}
2017-12-23 09:28:02 +01:00
} else if ( cc = = 0 & & line . length ( ) > 0 & & line [ 0 ] = = ' \t ' ) {
_remove_text ( cursor . line , 0 , cursor . line , 1 ) ;
update ( ) ;
2017-05-20 17:38:03 +02:00
}
} else {
2019-08-18 17:56:24 +02:00
// Simple indent.
2017-05-20 17:38:03 +02:00
if ( indent_using_spaces ) {
2019-08-18 17:56:24 +02:00
// Insert only as much spaces as needed till next indentation level.
2019-05-08 23:56:00 +02:00
int spaces_to_add = _calculate_spaces_till_next_right_indent ( cursor . column ) ;
String indent_to_insert = String ( ) ;
2020-05-14 16:41:43 +02:00
for ( int i = 0 ; i < spaces_to_add ; i + + ) {
2019-05-08 23:56:00 +02:00
indent_to_insert = ' ' + indent_to_insert ;
2020-05-14 16:41:43 +02:00
}
2019-05-08 23:56:00 +02:00
_insert_text_at_cursor ( indent_to_insert ) ;
2015-01-02 19:08:40 +01:00
} else {
2017-05-20 17:38:03 +02:00
_insert_text_at_cursor ( " \t " ) ;
2015-01-02 19:08:40 +01:00
}
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_BACKSPACE : {
2020-05-14 16:41:43 +02:00
if ( readonly ) {
2017-05-20 17:38:03 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2015-11-12 17:35:48 +01:00
# ifdef APPLE_STYLE_KEYS
2017-08-01 19:40:43 +02:00
if ( k - > get_alt ( ) & & cursor . column > 1 ) {
2015-11-12 17:35:48 +01:00
# else
2017-05-20 17:38:03 +02:00
if ( k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2017-08-01 19:40:43 +02:00
} else if ( k - > get_command ( ) & & cursor . column > 1 ) {
2015-11-12 17:35:48 +01:00
# endif
2017-05-20 17:38:03 +02:00
int line = cursor . line ;
int column = cursor . column ;
2015-11-12 17:35:48 +01:00
2020-09-18 09:36:10 +02:00
Vector < Vector2i > words = TS - > shaped_text_get_word_breaks ( text . get_line_data ( line ) - > get_rid ( ) ) ;
for ( int i = words . size ( ) - 1 ; i > = 0 ; i - - ) {
if ( words [ i ] . x < column ) {
column = words [ i ] . x ;
2017-08-01 19:40:43 +02:00
break ;
}
2016-01-07 21:38:38 +01:00
}
2017-05-20 17:38:03 +02:00
_remove_text ( line , column , cursor . line , cursor . column ) ;
cursor_set_line ( line ) ;
cursor_set_column ( column ) ;
2018-04-24 15:23:04 +02:00
# ifdef APPLE_STYLE_KEYS
} else if ( k - > get_command ( ) ) {
int cursor_current_column = cursor . column ;
cursor . column = 0 ;
_remove_text ( cursor . line , 0 , cursor . line , cursor_current_column ) ;
# endif
2017-05-20 17:38:03 +02:00
} else {
2020-05-14 16:41:43 +02:00
if ( cursor . line > 0 & & is_line_hidden ( cursor . line - 1 ) ) {
2017-11-13 00:12:17 +01:00
unfold_line ( cursor . line - 1 ) ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
backspace_at_cursor ( ) ;
2016-01-07 21:38:38 +01:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_KP_4 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_LEFT : {
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_pre_shift_selection ( ) ;
2016-06-23 23:03:32 +02:00
# ifdef APPLE_STYLE_KEYS
2020-05-14 16:41:43 +02:00
} else {
2016-06-23 23:03:32 +02:00
# else
2020-05-14 16:41:43 +02:00
} else if ( ! k - > get_alt ( ) ) {
2016-06-23 23:03:32 +02:00
# endif
2017-05-20 17:38:03 +02:00
deselect ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
# ifdef APPLE_STYLE_KEYS
2017-05-20 17:38:03 +02:00
if ( k - > get_command ( ) ) {
2019-04-28 17:51:47 +02:00
// Start at first column (it's slightly faster that way) and look for the first non-whitespace character.
int new_cursor_pos = 0 ;
for ( int i = 0 ; i < text [ cursor . line ] . length ( ) ; + + i ) {
if ( ! _is_whitespace ( text [ cursor . line ] [ i ] ) ) {
new_cursor_pos = i ;
break ;
}
}
if ( new_cursor_pos = = cursor . column ) {
// We're already at the first text character, so move to the very beginning of the line.
cursor_set_column ( 0 ) ;
} else {
// We're somewhere to the right of the first text character; move to the first one.
cursor_set_column ( new_cursor_pos ) ;
}
2017-05-20 17:38:03 +02:00
} else if ( k - > get_alt ( ) ) {
2014-02-10 02:10:30 +01:00
# else
2017-05-20 17:38:03 +02:00
if ( k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
} else if ( k - > get_command ( ) ) {
2014-02-10 02:10:30 +01:00
# endif
2017-05-20 17:38:03 +02:00
int cc = cursor . column ;
2017-08-18 20:53:03 +02:00
if ( cc = = 0 & & cursor . line > 0 ) {
cursor_set_line ( cursor . line - 1 ) ;
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
2017-12-23 15:47:04 +01:00
} else {
2020-09-18 09:36:10 +02:00
Vector < Vector2i > words = TS - > shaped_text_get_word_breaks ( text . get_line_data ( cursor . line ) - > get_rid ( ) ) ;
for ( int i = words . size ( ) - 1 ; i > = 0 ; i - - ) {
if ( words [ i ] . x < cc ) {
cc = words [ i ] . x ;
2017-12-23 15:47:04 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2017-12-23 15:47:04 +01:00
}
cursor_set_column ( cc ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} else if ( cursor . column = = 0 ) {
if ( cursor . line > 0 ) {
2017-11-16 05:00:27 +01:00
cursor_set_line ( cursor . line - num_lines_from ( CLAMP ( cursor . line - 1 , 0 , text . size ( ) - 1 ) , - 1 ) ) ;
2017-05-20 17:38:03 +02:00
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
2016-01-07 21:38:38 +01:00
}
2017-05-20 17:38:03 +02:00
} else {
2020-09-18 09:36:10 +02:00
if ( mid_grapheme_caret_enabled ) {
cursor_set_column ( cursor_get_column ( ) - 1 ) ;
} else {
cursor_set_column ( TS - > shaped_text_prev_grapheme_pos ( text . get_line_data ( cursor . line ) - > get_rid ( ) , cursor_get_column ( ) ) ) ;
}
2017-05-20 17:38:03 +02:00
}
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
} break ;
case KEY_KP_6 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2016-01-07 21:38:38 +01:00
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_RIGHT : {
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_pre_shift_selection ( ) ;
2016-06-23 23:03:32 +02:00
# ifdef APPLE_STYLE_KEYS
2020-05-14 16:41:43 +02:00
} else {
2016-06-23 23:03:32 +02:00
# else
2020-05-14 16:41:43 +02:00
} else if ( ! k - > get_alt ( ) ) {
2016-06-23 23:03:32 +02:00
# endif
2017-05-20 17:38:03 +02:00
deselect ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
# ifdef APPLE_STYLE_KEYS
2017-05-20 17:38:03 +02:00
if ( k - > get_command ( ) ) {
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
} else if ( k - > get_alt ( ) ) {
2014-02-10 02:10:30 +01:00
# else
2017-05-20 17:38:03 +02:00
if ( k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
} else if ( k - > get_command ( ) ) {
2014-02-10 02:10:30 +01:00
# endif
2017-05-20 17:38:03 +02:00
int cc = cursor . column ;
2017-08-18 20:53:03 +02:00
if ( cc = = text [ cursor . line ] . length ( ) & & cursor . line < text . size ( ) - 1 ) {
cursor_set_line ( cursor . line + 1 ) ;
cursor_set_column ( 0 ) ;
2017-12-23 15:47:04 +01:00
} else {
2020-09-18 09:36:10 +02:00
Vector < Vector2i > words = TS - > shaped_text_get_word_breaks ( text . get_line_data ( cursor . line ) - > get_rid ( ) ) ;
for ( int i = 0 ; i < words . size ( ) ; i + + ) {
if ( words [ i ] . y > cc ) {
cc = words [ i ] . y ;
2017-12-23 15:47:04 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2017-12-23 15:47:04 +01:00
}
cursor_set_column ( cc ) ;
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} else if ( cursor . column = = text [ cursor . line ] . length ( ) ) {
if ( cursor . line < text . size ( ) - 1 ) {
2017-11-16 05:00:27 +01:00
cursor_set_line ( cursor_get_line ( ) + num_lines_from ( CLAMP ( cursor . line + 1 , 0 , text . size ( ) - 1 ) , 1 ) , true , false ) ;
2017-05-20 17:38:03 +02:00
cursor_set_column ( 0 ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
} else {
2020-09-18 09:36:10 +02:00
if ( mid_grapheme_caret_enabled ) {
cursor_set_column ( cursor_get_column ( ) + 1 ) ;
} else {
cursor_set_column ( TS - > shaped_text_next_grapheme_pos ( text . get_line_data ( cursor . line ) - > get_rid ( ) , cursor_get_column ( ) ) ) ;
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_KP_8 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2016-01-07 21:38:38 +01:00
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_UP : {
if ( k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2016-04-27 19:32:14 +02:00
# ifndef APPLE_STYLE_KEYS
2017-05-20 17:38:03 +02:00
if ( k - > get_command ( ) ) {
2016-04-27 19:32:14 +02:00
# else
2017-05-20 17:38:03 +02:00
if ( k - > get_command ( ) & & k - > get_alt ( ) ) {
2019-02-04 11:55:45 +01:00
# endif
2017-05-20 17:38:03 +02:00
_scroll_lines_up ( ) ;
break ;
}
2016-04-27 19:32:14 +02:00
2019-02-04 11:55:45 +01:00
if ( k - > get_shift ( ) ) {
_pre_shift_selection ( ) ;
}
# ifdef APPLE_STYLE_KEYS
2018-01-26 02:41:17 +01:00
if ( k - > get_command ( ) ) {
2017-05-20 17:38:03 +02:00
cursor_set_line ( 0 ) ;
2018-01-26 02:41:17 +01:00
} else
2014-02-10 02:10:30 +01:00
# endif
2018-01-26 02:41:17 +01:00
{
int cur_wrap_index = get_cursor_wrap_index ( ) ;
if ( cur_wrap_index > 0 ) {
cursor_set_line ( cursor . line , true , false , cur_wrap_index - 1 ) ;
} else if ( cursor . line = = 0 ) {
cursor_set_column ( 0 ) ;
} else {
int new_line = cursor . line - num_lines_from ( cursor . line - 1 , - 1 ) ;
if ( line_wraps ( new_line ) ) {
cursor_set_line ( new_line , true , false , times_line_wraps ( new_line ) ) ;
} else {
cursor_set_line ( new_line , true , false ) ;
}
}
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
_cancel_code_hint ( ) ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_KP_2 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2016-01-07 21:38:38 +01:00
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_DOWN : {
if ( k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2016-04-27 19:32:14 +02:00
# ifndef APPLE_STYLE_KEYS
2017-05-20 17:38:03 +02:00
if ( k - > get_command ( ) ) {
2016-04-27 19:32:14 +02:00
# else
2017-05-20 17:38:03 +02:00
if ( k - > get_command ( ) & & k - > get_alt ( ) ) {
2019-02-04 11:55:45 +01:00
# endif
2017-05-20 17:38:03 +02:00
_scroll_lines_down ( ) ;
break ;
}
2016-04-27 19:32:14 +02:00
2019-02-04 11:55:45 +01:00
if ( k - > get_shift ( ) ) {
_pre_shift_selection ( ) ;
}
# ifdef APPLE_STYLE_KEYS
2018-01-26 02:41:17 +01:00
if ( k - > get_command ( ) ) {
cursor_set_line ( get_last_unhidden_line ( ) , true , false , 9999 ) ;
} else
2014-02-10 02:10:30 +01:00
# endif
2018-01-26 02:41:17 +01:00
{
int cur_wrap_index = get_cursor_wrap_index ( ) ;
if ( cur_wrap_index < times_line_wraps ( cursor . line ) ) {
cursor_set_line ( cursor . line , true , false , cur_wrap_index + 1 ) ;
} else if ( cursor . line = = get_last_unhidden_line ( ) ) {
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
2017-12-26 07:53:16 +01:00
} else {
2018-01-26 02:41:17 +01:00
int new_line = cursor . line + num_lines_from ( CLAMP ( cursor . line + 1 , 0 , text . size ( ) - 1 ) , 1 ) ;
cursor_set_line ( new_line , true , false , 0 ) ;
2017-12-26 07:53:16 +01:00
}
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
_cancel_code_hint ( ) ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_DELETE : {
2020-05-14 16:41:43 +02:00
if ( readonly ) {
2017-05-20 17:38:03 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2016-03-15 13:03:38 +01:00
2019-08-08 02:09:46 +02:00
if ( k - > get_shift ( ) & & ! k - > get_command ( ) & & ! k - > get_alt ( ) & & is_shortcut_keys_enabled ( ) ) {
2017-05-20 17:38:03 +02:00
cut ( ) ;
break ;
}
2016-03-15 13:03:38 +01:00
2017-05-20 17:38:03 +02:00
int curline_len = text [ cursor . line ] . length ( ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( cursor . line = = text . size ( ) - 1 & & cursor . column = = curline_len ) {
2019-08-18 17:56:24 +02:00
break ; // Nothing to do.
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
int next_line = cursor . column < curline_len ? cursor . line : cursor . line + 1 ;
int next_column ;
2015-11-12 17:35:48 +01:00
# ifdef APPLE_STYLE_KEYS
2017-08-01 19:40:43 +02:00
if ( k - > get_alt ( ) & & cursor . column < curline_len - 1 ) {
2015-11-12 17:35:48 +01:00
# else
2017-05-20 17:38:03 +02:00
if ( k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2017-08-01 19:40:43 +02:00
} else if ( k - > get_command ( ) & & cursor . column < curline_len - 1 ) {
2015-11-12 17:35:48 +01:00
# endif
2017-05-20 17:38:03 +02:00
int line = cursor . line ;
int column = cursor . column ;
2015-11-12 17:35:48 +01:00
2020-09-18 09:36:10 +02:00
Vector < Vector2i > words = TS - > shaped_text_get_word_breaks ( text . get_line_data ( line ) - > get_rid ( ) ) ;
for ( int i = 0 ; i < words . size ( ) ; i + + ) {
if ( words [ i ] . y > column ) {
column = words [ i ] . y ;
2017-08-01 19:40:43 +02:00
break ;
}
2015-11-12 17:35:48 +01:00
}
2017-05-20 17:38:03 +02:00
next_line = line ;
next_column = column ;
2018-04-24 15:23:04 +02:00
# ifdef APPLE_STYLE_KEYS
} else if ( k - > get_command ( ) ) {
next_column = curline_len ;
next_line = cursor . line ;
# endif
2017-05-20 17:38:03 +02:00
} else {
2020-09-18 09:36:10 +02:00
if ( mid_grapheme_caret_enabled ) {
next_column = cursor . column < curline_len ? ( cursor . column + 1 ) : 0 ;
} else {
next_column = cursor . column < curline_len ? TS - > shaped_text_next_grapheme_pos ( text . get_line_data ( cursor . line ) - > get_rid ( ) , ( cursor . column ) ) : 0 ;
}
2017-05-20 17:38:03 +02:00
}
2015-11-12 17:35:48 +01:00
2017-05-20 17:38:03 +02:00
_remove_text ( cursor . line , cursor . column , next_line , next_column ) ;
update ( ) ;
} break ;
case KEY_KP_7 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2016-01-07 22:45:28 +01:00
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_HOME : {
2019-04-05 14:06:16 +02:00
# ifdef APPLE_STYLE_KEYS
2017-05-20 17:38:03 +02:00
if ( k - > get_shift ( ) )
_pre_shift_selection ( ) ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
cursor_set_line ( 0 ) ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( k - > get_shift ( ) )
_post_shift_selection ( ) ;
else if ( k - > get_command ( ) | | k - > get_control ( ) )
deselect ( ) ;
2014-02-10 02:10:30 +01:00
# else
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_pre_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( k - > get_command ( ) ) {
cursor_set_line ( 0 ) ;
cursor_set_column ( 0 ) ;
} else {
2019-08-18 17:56:24 +02:00
// Move cursor column to start of wrapped row and then to start of text.
2018-01-26 02:41:17 +01:00
Vector < String > rows = get_wrap_rows_text ( cursor . line ) ;
int wi = get_cursor_wrap_index ( ) ;
int row_start_col = 0 ;
for ( int i = 0 ; i < wi ; i + + ) {
row_start_col + = rows [ i ] . length ( ) ;
2017-02-03 08:01:41 +01:00
}
2018-01-26 02:41:17 +01:00
if ( cursor . column = = row_start_col | | wi = = 0 ) {
2019-08-18 17:56:24 +02:00
// Compute whitespace symbols seq length.
2018-01-26 02:41:17 +01:00
int current_line_whitespace_len = 0 ;
while ( current_line_whitespace_len < text [ cursor . line ] . length ( ) ) {
2020-07-27 12:43:20 +02:00
char32_t c = text [ cursor . line ] [ current_line_whitespace_len ] ;
2020-05-14 16:41:43 +02:00
if ( c ! = ' \t ' & & c ! = ' ' ) {
2018-01-26 02:41:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
current_line_whitespace_len + + ;
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( cursor_get_column ( ) = = current_line_whitespace_len ) {
2018-01-26 02:41:17 +01:00
cursor_set_column ( 0 ) ;
2020-05-14 16:41:43 +02:00
} else {
2018-01-26 02:41:17 +01:00
cursor_set_column ( current_line_whitespace_len ) ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
} else {
cursor_set_column ( row_start_col ) ;
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
} else if ( k - > get_command ( ) | | k - > get_control ( ) ) {
2017-05-20 17:38:03 +02:00
deselect ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-05-20 17:38:03 +02:00
_cancel_completion ( ) ;
completion_hint = " " ;
2016-01-07 22:45:28 +01:00
# endif
2019-04-05 14:06:16 +02:00
} break ;
2017-05-20 17:38:03 +02:00
case KEY_KP_1 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2016-01-07 22:45:28 +01:00
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_END : {
2019-04-05 14:06:16 +02:00
# ifdef APPLE_STYLE_KEYS
2017-05-20 17:38:03 +02:00
if ( k - > get_shift ( ) )
_pre_shift_selection ( ) ;
2016-01-07 22:45:28 +01:00
2018-01-26 02:41:17 +01:00
cursor_set_line ( get_last_unhidden_line ( ) , true , false , 9999 ) ;
2016-01-07 22:45:28 +01:00
2017-05-20 17:38:03 +02:00
if ( k - > get_shift ( ) )
_post_shift_selection ( ) ;
else if ( k - > get_command ( ) | | k - > get_control ( ) )
deselect ( ) ;
2016-01-07 22:45:28 +01:00
# else
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_pre_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_command ( ) ) {
2018-01-26 02:41:17 +01:00
cursor_set_line ( get_last_unhidden_line ( ) , true , false , 9999 ) ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
2019-08-18 17:56:24 +02:00
// Move cursor column to end of wrapped row and then to end of text.
2018-01-26 02:41:17 +01:00
Vector < String > rows = get_wrap_rows_text ( cursor . line ) ;
int wi = get_cursor_wrap_index ( ) ;
int row_end_col = - 1 ;
for ( int i = 0 ; i < wi + 1 ; i + + ) {
row_end_col + = rows [ i ] . length ( ) ;
}
if ( wi = = rows . size ( ) - 1 | | cursor . column = = row_end_col ) {
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
} else {
cursor_set_column ( row_end_col ) ;
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
} else if ( k - > get_command ( ) | | k - > get_control ( ) ) {
2017-05-20 17:38:03 +02:00
deselect ( ) ;
2020-05-14 16:41:43 +02:00
}
2015-06-27 20:52:39 +02:00
2017-05-20 17:38:03 +02:00
_cancel_completion ( ) ;
completion_hint = " " ;
2014-02-10 02:10:30 +01:00
# endif
2019-04-05 14:06:16 +02:00
} break ;
2017-05-20 17:38:03 +02:00
case KEY_KP_9 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2016-01-07 22:45:28 +01:00
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_PAGEUP : {
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_pre_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2018-01-26 02:41:17 +01:00
int wi ;
int n_line = cursor . line - num_lines_from_rows ( cursor . line , get_cursor_wrap_index ( ) , - get_visible_rows ( ) , wi ) + 1 ;
cursor_set_line ( n_line , true , false , wi ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2015-06-27 20:52:39 +02:00
2017-05-20 17:38:03 +02:00
_cancel_completion ( ) ;
completion_hint = " " ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_KP_3 : {
if ( k - > get_unicode ( ) ! = 0 ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
2016-01-07 22:45:28 +01:00
}
2020-02-22 20:47:50 +01:00
[[fallthrough]] ;
2017-05-20 17:38:03 +02:00
}
case KEY_PAGEDOWN : {
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_pre_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2018-01-26 02:41:17 +01:00
int wi ;
int n_line = cursor . line + num_lines_from_rows ( cursor . line , get_cursor_wrap_index ( ) , get_visible_rows ( ) , wi ) - 1 ;
cursor_set_line ( n_line , true , false , wi ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2017-05-20 17:38:03 +02:00
_post_shift_selection ( ) ;
2020-05-14 16:41:43 +02:00
}
2015-06-27 20:52:39 +02:00
2017-05-20 17:38:03 +02:00
_cancel_completion ( ) ;
completion_hint = " " ;
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_A : {
2018-04-05 18:58:15 +02:00
# ifndef APPLE_STYLE_KEYS
2018-05-18 19:46:40 +02:00
if ( ! k - > get_control ( ) | | k - > get_shift ( ) | | k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2019-08-08 02:09:46 +02:00
if ( is_shortcut_keys_enabled ( ) ) {
select_all ( ) ;
}
2018-04-05 18:58:15 +02:00
# else
2018-05-18 19:46:40 +02:00
if ( ( ! k - > get_command ( ) & & ! k - > get_control ( ) ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2018-04-05 18:58:15 +02:00
break ;
}
2019-08-08 02:09:46 +02:00
if ( ! k - > get_shift ( ) & & k - > get_command ( ) & & is_shortcut_keys_enabled ( ) )
2018-04-05 18:58:15 +02:00
select_all ( ) ;
else if ( k - > get_control ( ) ) {
if ( k - > get_shift ( ) )
_pre_shift_selection ( ) ;
int current_line_whitespace_len = 0 ;
while ( current_line_whitespace_len < text [ cursor . line ] . length ( ) ) {
2020-07-27 12:43:20 +02:00
char32_t c = text [ cursor . line ] [ current_line_whitespace_len ] ;
2018-04-05 18:58:15 +02:00
if ( c ! = ' \t ' & & c ! = ' ' )
break ;
current_line_whitespace_len + + ;
}
if ( cursor_get_column ( ) = = current_line_whitespace_len )
cursor_set_column ( 0 ) ;
else
cursor_set_column ( current_line_whitespace_len ) ;
if ( k - > get_shift ( ) )
_post_shift_selection ( ) ;
else if ( k - > get_command ( ) | | k - > get_control ( ) )
deselect ( ) ;
}
} break ;
case KEY_E : {
if ( ! k - > get_control ( ) | | k - > get_command ( ) | | k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2018-04-05 18:58:15 +02:00
break ;
}
2016-03-09 00:00:52 +01:00
2018-04-05 18:58:15 +02:00
if ( k - > get_shift ( ) )
_pre_shift_selection ( ) ;
if ( k - > get_command ( ) )
cursor_set_line ( text . size ( ) - 1 , true , false ) ;
cursor_set_column ( text [ cursor . line ] . length ( ) ) ;
if ( k - > get_shift ( ) )
_post_shift_selection ( ) ;
else if ( k - > get_command ( ) | | k - > get_control ( ) )
deselect ( ) ;
_cancel_completion ( ) ;
completion_hint = " " ;
# endif
2017-05-20 17:38:03 +02:00
} break ;
2020-09-18 09:36:10 +02:00
case ( KEY_QUOTELEFT ) : { // Swap current input direction (primary cursor)
if ( ! k - > get_command ( ) ) {
keycode_handled = false ;
break ;
}
if ( input_direction = = TEXT_DIRECTION_LTR ) {
input_direction = TEXT_DIRECTION_RTL ;
} else {
input_direction = TEXT_DIRECTION_LTR ;
}
cursor_set_column ( cursor . column ) ;
update ( ) ;
} break ;
2017-05-20 17:38:03 +02:00
case KEY_X : {
if ( readonly ) {
break ;
}
if ( ! k - > get_command ( ) | | k - > get_shift ( ) | | k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2019-08-08 02:09:46 +02:00
if ( is_shortcut_keys_enabled ( ) ) {
cut ( ) ;
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_C : {
if ( ! k - > get_command ( ) | | k - > get_shift ( ) | | k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2016-03-09 00:00:52 +01:00
2019-08-08 02:09:46 +02:00
if ( is_shortcut_keys_enabled ( ) ) {
copy ( ) ;
}
2016-03-15 13:02:38 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_Z : {
2019-08-25 08:57:59 +02:00
if ( readonly ) {
break ;
}
2017-05-20 17:38:03 +02:00
if ( ! k - > get_command ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2016-03-09 00:00:52 +01:00
2019-08-08 02:09:46 +02:00
if ( is_shortcut_keys_enabled ( ) ) {
2020-05-14 16:41:43 +02:00
if ( k - > get_shift ( ) ) {
2019-08-08 02:09:46 +02:00
redo ( ) ;
2020-05-14 16:41:43 +02:00
} else {
2019-08-08 02:09:46 +02:00
undo ( ) ;
2020-05-14 16:41:43 +02:00
}
2019-08-08 02:09:46 +02:00
}
2017-05-20 17:38:03 +02:00
} break ;
2017-08-24 07:22:00 +02:00
case KEY_Y : {
2019-08-25 08:57:59 +02:00
if ( readonly ) {
break ;
}
2017-08-24 07:22:00 +02:00
if ( ! k - > get_command ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-08-24 07:22:00 +02:00
break ;
}
2019-08-08 02:09:46 +02:00
if ( is_shortcut_keys_enabled ( ) ) {
redo ( ) ;
}
2017-08-24 07:22:00 +02:00
} break ;
2017-05-20 17:38:03 +02:00
case KEY_V : {
if ( readonly ) {
break ;
}
if ( ! k - > get_command ( ) | | k - > get_shift ( ) | | k - > get_alt ( ) ) {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
break ;
}
2016-03-09 00:00:52 +01:00
2019-08-08 02:09:46 +02:00
if ( is_shortcut_keys_enabled ( ) ) {
paste ( ) ;
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
case KEY_SPACE : {
2015-01-04 15:03:31 +01:00
# ifdef OSX_ENABLED
2019-08-18 17:56:24 +02:00
if ( completion_enabled & & k - > get_metakey ( ) ) { // cmd-space is spotlight shortcut in OSX
2015-01-04 15:03:31 +01:00
# else
2017-05-20 17:38:03 +02:00
if ( completion_enabled & & k - > get_command ( ) ) {
2015-01-04 15:03:31 +01:00
# endif
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
query_code_comple ( ) ;
2018-04-05 19:59:35 +02:00
keycode_handled = true ;
2017-05-20 17:38:03 +02:00
} else {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
} break ;
2016-03-09 00:00:52 +01:00
2019-06-22 20:22:52 +02:00
case KEY_MENU : {
if ( context_menu_enabled ) {
2020-09-18 09:36:10 +02:00
menu - > set_position ( get_screen_transform ( ) . xform ( _get_cursor_pixel_pos ( ) ) ) ;
2019-06-22 20:22:52 +02:00
menu - > set_size ( Vector2 ( 1 , 1 ) ) ;
menu - > popup ( ) ;
menu - > grab_focus ( ) ;
}
} break ;
2017-05-20 17:38:03 +02:00
default : {
2018-04-05 19:59:35 +02:00
keycode_handled = false ;
2017-05-20 17:38:03 +02:00
} break ;
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( keycode_handled ) {
2017-05-20 17:38:03 +02:00
accept_event ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2018-04-05 19:59:35 +02:00
if ( k - > get_keycode ( ) = = KEY_INSERT ) {
2017-05-20 17:38:03 +02:00
set_insert_mode ( ! insert_mode ) ;
accept_event ( ) ;
return ;
}
2016-03-09 00:00:52 +01:00
2020-04-10 19:49:51 +02:00
if ( ! keycode_handled & & ( ! k - > get_command ( ) | | ( k - > get_command ( ) & & k - > get_alt ( ) ) ) ) { // For German keyboards.
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( k - > get_unicode ( ) > = 32 ) {
2020-05-14 16:41:43 +02:00
if ( readonly ) {
2017-05-20 17:38:03 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-31 21:49:30 +02:00
2019-08-18 17:56:24 +02:00
// Remove the old character if in insert mode and no selection.
2017-05-20 17:38:03 +02:00
if ( insert_mode & & ! had_selection ) {
begin_complex_operation ( ) ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Make sure we don't try and remove empty space.
2017-05-20 17:38:03 +02:00
if ( cursor . column < get_line ( cursor . line ) . length ( ) ) {
_remove_text ( cursor . line , cursor . column , cursor . line , cursor . column + 1 ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2020-07-27 12:43:20 +02:00
const char32_t chr [ 2 ] = { ( char32_t ) k - > get_unicode ( ) , 0 } ;
2016-04-05 14:52:18 +02:00
2017-05-20 17:38:03 +02:00
if ( completion_hint ! = " " & & k - > get_unicode ( ) = = ' ) ' ) {
completion_hint = " " ;
}
if ( auto_brace_completion_enabled & & _is_pair_symbol ( chr [ 0 ] ) ) {
_consume_pair_symbol ( chr [ 0 ] ) ;
2015-01-02 19:08:40 +01:00
} else {
2017-05-20 17:38:03 +02:00
_insert_text_at_cursor ( chr ) ;
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
if ( insert_mode & & ! had_selection ) {
end_complex_operation ( ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
if ( selection . active ! = had_selection ) {
end_complex_operation ( ) ;
}
accept_event ( ) ;
2015-01-02 19:08:40 +01:00
}
2017-05-20 17:38:03 +02:00
}
2016-03-09 00:00:52 +01:00
2017-05-20 17:38:03 +02:00
return ;
2015-01-02 19:08:40 +01:00
}
2014-02-10 02:10:30 +01:00
}
2017-11-01 21:49:39 +01:00
void TextEdit : : _scroll_up ( real_t p_delta ) {
2019-08-11 20:31:19 +02:00
if ( scrolling & & smooth_scroll_enabled & & SGN ( target_v_scroll - v_scroll - > get_value ( ) ) ! = SGN ( - p_delta ) ) {
2017-12-28 15:49:37 +01:00
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
}
2017-12-28 15:49:37 +01:00
2017-11-01 21:49:39 +01:00
if ( scrolling ) {
target_v_scroll = ( target_v_scroll - p_delta ) ;
} else {
2018-01-26 02:41:17 +01:00
target_v_scroll = ( get_v_scroll ( ) - p_delta ) ;
2017-11-01 21:49:39 +01:00
}
if ( smooth_scroll_enabled ) {
if ( target_v_scroll < = 0 ) {
target_v_scroll = 0 ;
}
2017-12-28 15:49:37 +01:00
if ( Math : : abs ( target_v_scroll - v_scroll - > get_value ( ) ) < 1.0 ) {
v_scroll - > set_value ( target_v_scroll ) ;
} else {
scrolling = true ;
2018-04-11 09:28:14 +02:00
set_physics_process_internal ( true ) ;
2017-12-28 15:49:37 +01:00
}
2017-11-01 21:49:39 +01:00
} else {
2018-01-26 02:41:17 +01:00
set_v_scroll ( target_v_scroll ) ;
2017-11-01 21:49:39 +01:00
}
}
void TextEdit : : _scroll_down ( real_t p_delta ) {
2019-08-11 20:31:19 +02:00
if ( scrolling & & smooth_scroll_enabled & & SGN ( target_v_scroll - v_scroll - > get_value ( ) ) ! = SGN ( p_delta ) ) {
2017-12-28 15:49:37 +01:00
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
}
2017-12-28 15:49:37 +01:00
2017-11-01 21:49:39 +01:00
if ( scrolling ) {
target_v_scroll = ( target_v_scroll + p_delta ) ;
} else {
2018-01-26 02:41:17 +01:00
target_v_scroll = ( get_v_scroll ( ) + p_delta ) ;
2017-11-01 21:49:39 +01:00
}
if ( smooth_scroll_enabled ) {
2018-10-05 22:50:49 +02:00
int max_v_scroll = round ( v_scroll - > get_max ( ) - v_scroll - > get_page ( ) ) ;
2017-11-01 21:49:39 +01:00
if ( target_v_scroll > max_v_scroll ) {
target_v_scroll = max_v_scroll ;
}
2017-12-28 15:49:37 +01:00
if ( Math : : abs ( target_v_scroll - v_scroll - > get_value ( ) ) < 1.0 ) {
v_scroll - > set_value ( target_v_scroll ) ;
} else {
scrolling = true ;
2018-04-11 09:28:14 +02:00
set_physics_process_internal ( true ) ;
2017-12-28 15:49:37 +01:00
}
2017-11-01 21:49:39 +01:00
} else {
2018-01-26 02:41:17 +01:00
set_v_scroll ( target_v_scroll ) ;
2017-11-01 21:49:39 +01:00
}
}
2014-02-10 02:10:30 +01:00
void TextEdit : : _pre_shift_selection ( ) {
2020-10-11 19:44:53 +02:00
if ( ! selection . active | | selection . selecting_mode = = SelectionMode : : SELECTION_MODE_NONE ) {
2017-03-05 16:44:50 +01:00
selection . selecting_line = cursor . line ;
selection . selecting_column = cursor . column ;
selection . active = true ;
2015-01-02 19:08:40 +01:00
}
2015-08-13 00:34:07 +02:00
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_SHIFT ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : _post_shift_selection ( ) {
2020-10-11 19:44:53 +02:00
if ( selection . active & & selection . selecting_mode = = SelectionMode : : SELECTION_MODE_SHIFT ) {
2017-03-05 16:44:50 +01:00
select ( selection . selecting_line , selection . selecting_column , cursor . line , cursor . column ) ;
2015-01-02 19:08:40 +01:00
update ( ) ;
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
selection . selecting_text = true ;
2014-02-10 02:10:30 +01:00
}
2016-04-27 19:32:14 +02:00
void TextEdit : : _scroll_lines_up ( ) {
2017-08-22 21:02:08 +02:00
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
2017-08-22 21:02:08 +02:00
2019-08-18 17:56:24 +02:00
// Adjust the vertical scroll.
2018-01-26 02:41:17 +01:00
set_v_scroll ( get_v_scroll ( ) - 1 ) ;
2019-08-18 17:56:24 +02:00
// Adjust the cursor to viewport.
2018-01-26 02:41:17 +01:00
if ( ! selection . active ) {
int cur_line = cursor . line ;
int cur_wrap = get_cursor_wrap_index ( ) ;
int last_vis_line = get_last_visible_line ( ) ;
int last_vis_wrap = get_last_visible_line_wrap_index ( ) ;
2016-04-27 19:32:14 +02:00
2018-01-26 02:41:17 +01:00
if ( cur_line > last_vis_line | | ( cur_line = = last_vis_line & & cur_wrap > last_vis_wrap ) ) {
cursor_set_line ( last_vis_line , false , false , last_vis_wrap ) ;
}
2016-04-27 19:32:14 +02:00
}
}
void TextEdit : : _scroll_lines_down ( ) {
2017-08-22 21:02:08 +02:00
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
2017-08-22 21:02:08 +02:00
2019-08-18 17:56:24 +02:00
// Adjust the vertical scroll.
2018-01-26 02:41:17 +01:00
set_v_scroll ( get_v_scroll ( ) + 1 ) ;
2016-04-27 19:32:14 +02:00
2019-08-18 17:56:24 +02:00
// Adjust the cursor to viewport.
2018-01-26 02:41:17 +01:00
if ( ! selection . active ) {
int cur_line = cursor . line ;
int cur_wrap = get_cursor_wrap_index ( ) ;
int first_vis_line = get_first_visible_line ( ) ;
int first_vis_wrap = cursor . wrap_ofs ;
if ( cur_line < first_vis_line | | ( cur_line = = first_vis_line & & cur_wrap < first_vis_wrap ) ) {
cursor_set_line ( first_vis_line , false , false , first_vis_wrap ) ;
}
2016-04-27 19:32:14 +02:00
}
}
2014-02-10 02:10:30 +01:00
/**** TEXT EDIT CORE API ****/
2017-03-05 16:44:50 +01:00
void TextEdit : : _base_insert_text ( int p_line , int p_char , const String & p_text , int & r_end_line , int & r_end_column ) {
2019-08-18 17:56:24 +02:00
// Save for undo.
2017-03-05 16:44:50 +01:00
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
ERR_FAIL_COND ( p_char < 0 ) ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
/* STEP 1: Remove \r from source text and separate in substrings. */
2018-05-30 14:02:51 +02:00
Vector < String > substrings = p_text . replace ( " \r " , " " ) . split ( " \n " ) ;
// Is this just a new empty line?
bool shift_first_line = p_char = = 0 & & p_text . replace ( " \r " , " " ) = = " \n " ;
2020-07-26 16:57:23 +02:00
/* STEP 2: Add spaces if the char is greater than the end of the line. */
2017-03-05 16:44:50 +01:00
while ( p_char > text [ p_line ] . length ( ) ) {
2020-09-18 09:36:10 +02:00
text . set ( p_line , text [ p_line ] + String : : chr ( ' ' ) , structured_text_parser ( st_parser , st_args , text [ p_line ] + String : : chr ( ' ' ) ) ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2020-07-26 16:57:23 +02:00
/* STEP 3: Separate dest string in pre and post text. */
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
String preinsert_text = text [ p_line ] . substr ( 0 , p_char ) ;
String postinsert_text = text [ p_line ] . substr ( p_char , text [ p_line ] . size ( ) ) ;
2016-03-09 00:00:52 +01:00
2019-02-12 21:10:08 +01:00
for ( int j = 0 ; j < substrings . size ( ) ; j + + ) {
2019-08-18 17:56:24 +02:00
// Insert the substrings.
2016-03-09 00:00:52 +01:00
2019-02-12 21:10:08 +01:00
if ( j = = 0 ) {
2020-09-18 09:36:10 +02:00
text . set ( p_line , preinsert_text + substrings [ j ] , structured_text_parser ( st_parser , st_args , preinsert_text + substrings [ j ] ) ) ;
2015-01-02 19:08:40 +01:00
} else {
2020-09-18 09:36:10 +02:00
text . insert ( p_line + j , substrings [ j ] , structured_text_parser ( st_parser , st_args , substrings [ j ] ) ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2019-02-12 21:10:08 +01:00
if ( j = = substrings . size ( ) - 1 ) {
2020-09-18 09:36:10 +02:00
text . set ( p_line + j , text [ p_line + j ] + postinsert_text , structured_text_parser ( st_parser , st_args , text [ p_line + j ] + postinsert_text ) ) ;
2015-01-02 19:08:40 +01:00
}
}
2016-03-09 00:00:52 +01:00
2018-05-30 14:02:51 +02:00
if ( shift_first_line ) {
2020-07-25 02:15:23 +02:00
text . move_gutters ( p_line , p_line + 1 ) ;
2017-11-13 00:12:17 +01:00
text . set_hidden ( p_line + 1 , text . is_hidden ( p_line ) ) ;
2019-04-20 13:51:25 +02:00
2017-11-13 00:12:17 +01:00
text . set_hidden ( p_line , false ) ;
}
2020-09-18 09:36:10 +02:00
text . invalidate_cache ( p_line ) ;
2018-01-26 02:41:17 +01:00
2017-03-05 16:44:50 +01:00
r_end_line = p_line + substrings . size ( ) - 1 ;
r_end_column = text [ r_end_line ] . length ( ) - postinsert_text . length ( ) ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
TextServer : : Direction dir = TS - > shaped_text_get_dominant_direciton_in_range ( text . get_line_data ( r_end_line ) - > get_rid ( ) , ( r_end_line = = p_line ) ? cursor . column : 0 , r_end_column ) ;
if ( dir ! = TextServer : : DIRECTION_AUTO ) {
input_direction = ( TextDirection ) dir ;
}
2015-01-02 19:08:40 +01:00
if ( ! text_changed_dirty & & ! setting_text ) {
2020-05-14 16:41:43 +02:00
if ( is_inside_tree ( ) ) {
2017-03-05 16:44:50 +01:00
MessageQueue : : get_singleton ( ) - > push_call ( this , " _text_changed_emit " ) ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
text_changed_dirty = true ;
2015-01-02 19:08:40 +01:00
}
2020-07-28 15:33:46 +02:00
emit_signal ( " lines_edited_from " , p_line , r_end_line ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
String TextEdit : : _base_get_text ( int p_from_line , int p_from_column , int p_to_line , int p_to_column ) const {
ERR_FAIL_INDEX_V ( p_from_line , text . size ( ) , String ( ) ) ;
ERR_FAIL_INDEX_V ( p_from_column , text [ p_from_line ] . length ( ) + 1 , String ( ) ) ;
ERR_FAIL_INDEX_V ( p_to_line , text . size ( ) , String ( ) ) ;
ERR_FAIL_INDEX_V ( p_to_column , text [ p_to_line ] . length ( ) + 1 , String ( ) ) ;
2019-08-18 17:56:24 +02:00
ERR_FAIL_COND_V ( p_to_line < p_from_line , String ( ) ) ; // 'from > to'.
ERR_FAIL_COND_V ( p_to_line = = p_from_line & & p_to_column < p_from_column , String ( ) ) ; // 'from > to'.
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
String ret ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = p_from_line ; i < = p_to_line ; i + + ) {
int begin = ( i = = p_from_line ) ? p_from_column : 0 ;
int end = ( i = = p_to_line ) ? p_to_column : text [ i ] . length ( ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( i > p_from_line ) {
2017-03-05 16:44:50 +01:00
ret + = " \n " ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
ret + = text [ i ] . substr ( begin , end - begin ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
return ret ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : _base_remove_text ( int p_from_line , int p_from_column , int p_to_line , int p_to_column ) {
ERR_FAIL_INDEX ( p_from_line , text . size ( ) ) ;
ERR_FAIL_INDEX ( p_from_column , text [ p_from_line ] . length ( ) + 1 ) ;
ERR_FAIL_INDEX ( p_to_line , text . size ( ) ) ;
ERR_FAIL_INDEX ( p_to_column , text [ p_to_line ] . length ( ) + 1 ) ;
2019-08-18 17:56:24 +02:00
ERR_FAIL_COND ( p_to_line < p_from_line ) ; // 'from > to'.
ERR_FAIL_COND ( p_to_line = = p_from_line & & p_to_column < p_from_column ) ; // 'from > to'.
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
String pre_text = text [ p_from_line ] . substr ( 0 , p_from_column ) ;
String post_text = text [ p_to_line ] . substr ( p_to_column , text [ p_to_line ] . length ( ) ) ;
2016-03-09 00:00:52 +01:00
2018-05-30 14:02:51 +02:00
for ( int i = p_from_line ; i < p_to_line ; i + + ) {
text . remove ( p_from_line + 1 ) ;
}
2020-09-18 09:36:10 +02:00
text . set ( p_from_line , pre_text + post_text , structured_text_parser ( st_parser , st_args , pre_text + post_text ) ) ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
//text.set_line_wrap_amount(p_from_line, -1);
text . invalidate_cache ( p_from_line ) ;
2018-01-26 02:41:17 +01:00
2015-01-02 19:08:40 +01:00
if ( ! text_changed_dirty & & ! setting_text ) {
2020-05-14 16:41:43 +02:00
if ( is_inside_tree ( ) ) {
2017-03-05 16:44:50 +01:00
MessageQueue : : get_singleton ( ) - > push_call ( this , " _text_changed_emit " ) ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
text_changed_dirty = true ;
2015-01-02 19:08:40 +01:00
}
2020-07-28 15:33:46 +02:00
emit_signal ( " lines_edited_from " , p_to_line , p_from_line ) ;
2014-02-10 02:10:30 +01:00
}
2017-08-11 21:10:05 +02:00
void TextEdit : : _insert_text ( int p_line , int p_char , const String & p_text , int * r_end_line , int * r_end_char ) {
2020-05-14 16:41:43 +02:00
if ( ! setting_text & & idle_detect - > is_inside_tree ( ) ) {
2015-01-02 19:08:40 +01:00
idle_detect - > start ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
if ( undo_enabled ) {
_clear_redo ( ) ;
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
int retline , retchar ;
_base_insert_text ( p_line , p_char , p_text , retline , retchar ) ;
2020-05-14 16:41:43 +02:00
if ( r_end_line ) {
2017-03-05 16:44:50 +01:00
* r_end_line = retline ;
2020-05-14 16:41:43 +02:00
}
if ( r_end_char ) {
2017-08-11 21:10:05 +02:00
* r_end_char = retchar ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( ! undo_enabled ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
/* UNDO!! */
TextOperation op ;
2017-03-05 16:44:50 +01:00
op . type = TextOperation : : TYPE_INSERT ;
op . from_line = p_line ;
op . from_column = p_char ;
op . to_line = retline ;
op . to_column = retchar ;
op . text = p_text ;
op . version = + + version ;
op . chain_forward = false ;
op . chain_backward = false ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// See if it should just be set as current op.
2017-03-05 16:44:50 +01:00
if ( current_op . type ! = op . type ) {
2016-03-13 21:08:12 +01:00
op . prev_version = get_version ( ) ;
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2017-03-05 16:44:50 +01:00
current_op = op ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
return ; // Set as current op, return.
2015-01-02 19:08:40 +01:00
}
2019-08-18 17:56:24 +02:00
// See if it can be merged.
2017-03-05 16:44:50 +01:00
if ( current_op . to_line ! = p_line | | current_op . to_column ! = p_char ) {
2016-03-13 21:08:12 +01:00
op . prev_version = get_version ( ) ;
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2017-03-05 16:44:50 +01:00
current_op = op ;
2019-08-18 17:56:24 +02:00
return ; // Set as current op, return.
2015-01-02 19:08:40 +01:00
}
2019-08-18 17:56:24 +02:00
// Merge current op.
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
current_op . text + = p_text ;
current_op . to_column = retchar ;
current_op . to_line = retline ;
current_op . version = op . version ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : _remove_text ( int p_from_line , int p_from_column , int p_to_line , int p_to_column ) {
2020-05-14 16:41:43 +02:00
if ( ! setting_text & & idle_detect - > is_inside_tree ( ) ) {
2015-01-02 19:08:40 +01:00
idle_detect - > start ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
String text ;
if ( undo_enabled ) {
_clear_redo ( ) ;
2017-03-05 16:44:50 +01:00
text = _base_get_text ( p_from_line , p_from_column , p_to_line , p_to_column ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
_base_remove_text ( p_from_line , p_from_column , p_to_line , p_to_column ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( ! undo_enabled ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
/* UNDO! */
2015-01-02 19:08:40 +01:00
TextOperation op ;
2017-03-05 16:44:50 +01:00
op . type = TextOperation : : TYPE_REMOVE ;
op . from_line = p_from_line ;
op . from_column = p_from_column ;
op . to_line = p_to_line ;
op . to_column = p_to_column ;
op . text = text ;
op . version = + + version ;
op . chain_forward = false ;
op . chain_backward = false ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// See if it should just be set as current op.
2017-03-05 16:44:50 +01:00
if ( current_op . type ! = op . type ) {
2016-03-13 21:08:12 +01:00
op . prev_version = get_version ( ) ;
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2017-03-05 16:44:50 +01:00
current_op = op ;
2019-08-18 17:56:24 +02:00
return ; // Set as current op, return.
2015-01-02 19:08:40 +01:00
}
2019-08-18 17:56:24 +02:00
// See if it can be merged.
2017-03-05 16:44:50 +01:00
if ( current_op . from_line = = p_to_line & & current_op . from_column = = p_to_column ) {
2019-08-18 17:56:24 +02:00
// Backspace or similar.
2017-03-05 16:44:50 +01:00
current_op . text = text + current_op . text ;
current_op . from_line = p_from_line ;
current_op . from_column = p_from_column ;
2019-08-18 17:56:24 +02:00
return ; // Update current op.
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2016-03-13 21:08:12 +01:00
op . prev_version = get_version ( ) ;
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2017-03-05 16:44:50 +01:00
current_op = op ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : _insert_text_at_cursor ( const String & p_text ) {
int new_column , new_line ;
_insert_text ( cursor . line , cursor . column , p_text , & new_line , & new_column ) ;
2019-10-07 20:17:32 +02:00
_update_scrollbars ( ) ;
2015-01-02 19:08:40 +01:00
cursor_set_line ( new_line ) ;
cursor_set_column ( new_column ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
int TextEdit : : get_char_count ( ) {
2017-03-05 16:44:50 +01:00
int totalsize = 0 ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
2020-05-14 16:41:43 +02:00
if ( i > 0 ) {
2019-08-18 17:56:24 +02:00
totalsize + + ; // Include \n.
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
totalsize + = text [ i ] . length ( ) ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
return totalsize ; // Omit last \n.
2014-02-10 02:10:30 +01:00
}
2016-06-12 18:31:22 +02:00
Size2 TextEdit : : get_minimum_size ( ) const {
2015-01-02 19:08:40 +01:00
return cache . style_normal - > get_minimum_size ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-11-13 00:12:17 +01:00
2019-08-25 15:49:13 +02:00
int TextEdit : : _get_control_height ( ) const {
int control_height = get_size ( ) . height ;
control_height - = cache . style_normal - > get_minimum_size ( ) . height ;
if ( h_scroll - > is_visible_in_tree ( ) ) {
control_height - = h_scroll - > get_size ( ) . height ;
}
return control_height ;
}
2016-03-09 00:00:52 +01:00
2019-08-08 02:09:46 +02:00
void TextEdit : : _generate_context_menu ( ) {
// Reorganize context menu.
menu - > clear ( ) ;
2020-05-14 16:41:43 +02:00
if ( ! readonly ) {
2019-08-08 02:09:46 +02:00
menu - > add_item ( RTR ( " Cut " ) , MENU_CUT , is_shortcut_keys_enabled ( ) ? KEY_MASK_CMD | KEY_X : 0 ) ;
2020-05-14 16:41:43 +02:00
}
2019-08-08 02:09:46 +02:00
menu - > add_item ( RTR ( " Copy " ) , MENU_COPY , is_shortcut_keys_enabled ( ) ? KEY_MASK_CMD | KEY_C : 0 ) ;
2020-05-14 16:41:43 +02:00
if ( ! readonly ) {
2019-08-08 02:09:46 +02:00
menu - > add_item ( RTR ( " Paste " ) , MENU_PASTE , is_shortcut_keys_enabled ( ) ? KEY_MASK_CMD | KEY_V : 0 ) ;
2020-05-14 16:41:43 +02:00
}
2019-08-08 02:09:46 +02:00
menu - > add_separator ( ) ;
2020-05-14 16:41:43 +02:00
if ( is_selecting_enabled ( ) ) {
2019-08-08 02:09:46 +02:00
menu - > add_item ( RTR ( " Select All " ) , MENU_SELECT_ALL , is_shortcut_keys_enabled ( ) ? KEY_MASK_CMD | KEY_A : 0 ) ;
2020-05-14 16:41:43 +02:00
}
2019-08-08 02:09:46 +02:00
if ( ! readonly ) {
menu - > add_item ( RTR ( " Clear " ) , MENU_CLEAR ) ;
menu - > add_separator ( ) ;
menu - > add_item ( RTR ( " Undo " ) , MENU_UNDO , is_shortcut_keys_enabled ( ) ? KEY_MASK_CMD | KEY_Z : 0 ) ;
menu - > add_item ( RTR ( " Redo " ) , MENU_REDO , is_shortcut_keys_enabled ( ) ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0 ) ;
}
2020-09-18 09:36:10 +02:00
menu - > add_separator ( ) ;
menu - > add_submenu_item ( RTR ( " Text writing direction " ) , " DirMenu " ) ;
menu - > add_separator ( ) ;
menu - > add_check_item ( RTR ( " Display control characters " ) , MENU_DISPLAY_UCC ) ;
if ( ! readonly ) {
menu - > add_submenu_item ( RTR ( " Insert control character " ) , " CTLMenu " ) ;
}
2019-08-08 02:09:46 +02:00
}
2019-08-25 15:49:13 +02:00
int TextEdit : : get_visible_rows ( ) const {
return _get_control_height ( ) / get_row_height ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-11-13 00:12:17 +01:00
2019-08-11 20:31:19 +02:00
int TextEdit : : _get_minimap_visible_rows ( ) const {
2019-08-25 15:49:13 +02:00
return _get_control_height ( ) / ( minimap_char_size . y + minimap_line_spacing ) ;
2019-08-11 20:31:19 +02:00
}
2018-01-26 02:41:17 +01:00
int TextEdit : : get_total_visible_rows ( ) const {
2019-08-18 17:56:24 +02:00
// Returns the total amount of rows we need in the editor.
2018-01-26 02:41:17 +01:00
// This skips hidden lines and counts each wrapping of a line.
2020-05-14 16:41:43 +02:00
if ( ! is_hiding_enabled ( ) & & ! is_wrap_enabled ( ) ) {
2017-11-16 05:00:27 +01:00
return text . size ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-11-16 05:00:27 +01:00
2018-01-26 02:41:17 +01:00
int total_rows = 0 ;
2017-11-16 05:00:27 +01:00
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
2018-01-26 02:41:17 +01:00
if ( ! text . is_hidden ( i ) ) {
total_rows + + ;
total_rows + = times_line_wraps ( i ) ;
}
2017-11-16 05:00:27 +01:00
}
2018-01-26 02:41:17 +01:00
return total_rows ;
2017-11-16 05:00:27 +01:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : _update_wrap_at ( bool p_force ) {
int new_wrap_at = get_size ( ) . width - cache . style_normal - > get_minimum_size ( ) . width - gutters_width - gutter_padding ;
if ( draw_minimap ) {
new_wrap_at - = minimap_width ;
}
if ( v_scroll - > is_visible_in_tree ( ) ) {
new_wrap_at - = v_scroll - > get_combined_minimum_size ( ) . width ;
}
new_wrap_at - = wrap_right_offset ; // Give it a little more space.
2017-11-16 05:00:27 +01:00
2020-09-18 09:36:10 +02:00
if ( ( wrap_at ! = new_wrap_at ) | | p_force ) {
wrap_at = new_wrap_at ;
if ( wrap_enabled ) {
text . set_width ( wrap_at ) ;
} else {
text . set_width ( - 1 ) ;
2020-05-14 16:41:43 +02:00
}
2020-09-18 09:36:10 +02:00
text . invalidate_all_lines ( ) ;
2017-11-16 05:00:27 +01:00
}
2020-09-18 09:36:10 +02:00
update_cursor_wrap_offset ( ) ;
2017-11-16 05:00:27 +01:00
}
2018-01-26 02:41:17 +01:00
void TextEdit : : adjust_viewport_to_cursor ( ) {
2019-08-18 17:56:24 +02:00
// Make sure cursor is visible on the screen.
2018-01-26 02:41:17 +01:00
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
2017-11-16 05:00:27 +01:00
2018-01-26 02:41:17 +01:00
int cur_line = cursor . line ;
int cur_wrap = get_cursor_wrap_index ( ) ;
2017-11-16 05:00:27 +01:00
2018-01-26 02:41:17 +01:00
int first_vis_line = get_first_visible_line ( ) ;
int first_vis_wrap = cursor . wrap_ofs ;
int last_vis_line = get_last_visible_line ( ) ;
int last_vis_wrap = get_last_visible_line_wrap_index ( ) ;
2016-03-09 00:00:52 +01:00
2018-01-26 02:41:17 +01:00
if ( cur_line < first_vis_line | | ( cur_line = = first_vis_line & & cur_wrap < first_vis_wrap ) ) {
2019-08-18 17:56:24 +02:00
// Cursor is above screen.
2018-01-26 02:41:17 +01:00
set_line_as_first_visible ( cur_line , cur_wrap ) ;
} else if ( cur_line > last_vis_line | | ( cur_line = = last_vis_line & & cur_wrap > last_vis_wrap ) ) {
2019-08-18 17:56:24 +02:00
// Cursor is below screen.
2018-01-26 02:41:17 +01:00
set_line_as_last_visible ( cur_line , cur_wrap ) ;
2017-11-16 05:00:27 +01:00
}
2016-03-09 00:00:52 +01:00
2020-07-29 23:26:49 +02:00
int visible_width = get_size ( ) . width - cache . style_normal - > get_minimum_size ( ) . width - gutters_width - gutter_padding - cache . minimap_width ;
2020-05-14 16:41:43 +02:00
if ( v_scroll - > is_visible_in_tree ( ) ) {
2017-03-05 16:44:50 +01:00
visible_width - = v_scroll - > get_combined_minimum_size ( ) . width ;
2020-05-14 16:41:43 +02:00
}
2019-08-18 17:56:24 +02:00
visible_width - = 20 ; // Give it a little more space.
2016-03-09 00:00:52 +01:00
2018-05-15 21:32:09 +02:00
if ( ! is_wrap_enabled ( ) ) {
2019-08-18 17:56:24 +02:00
// Adjust x offset.
2020-09-18 09:36:10 +02:00
Vector2i cursor_pos ;
// Get position of the start of caret.
if ( ime_text . length ( ) ! = 0 & & ime_selection . x ! = 0 ) {
cursor_pos . x = get_column_x_offset_for_line ( cursor . column + ime_selection . x , cursor . line ) ;
} else {
cursor_pos . x = get_column_x_offset_for_line ( cursor . column , cursor . line ) ;
}
// Get position of the end of caret.
if ( ime_text . length ( ) ! = 0 ) {
if ( ime_selection . y ! = 0 ) {
cursor_pos . y = get_column_x_offset_for_line ( cursor . column + ime_selection . x + ime_selection . y , cursor . line ) ;
} else {
cursor_pos . y = get_column_x_offset_for_line ( cursor . column + ime_text . size ( ) , cursor . line ) ;
}
} else {
cursor_pos . y = cursor_pos . x ;
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
if ( MAX ( cursor_pos . x , cursor_pos . y ) > ( cursor . x_ofs + visible_width ) ) {
cursor . x_ofs = MAX ( cursor_pos . x , cursor_pos . y ) - visible_width + 1 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
if ( MIN ( cursor_pos . x , cursor_pos . y ) < cursor . x_ofs ) {
cursor . x_ofs = MIN ( cursor_pos . x , cursor_pos . y ) ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
} else {
cursor . x_ofs = 0 ;
2017-11-26 20:10:05 +01:00
}
2018-01-26 02:41:17 +01:00
h_scroll - > set_value ( cursor . x_ofs ) ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2016-07-21 03:40:08 +02:00
void TextEdit : : center_viewport_to_cursor ( ) {
2019-08-18 17:56:24 +02:00
// Move viewport so the cursor is in the center of the screen.
2018-01-26 02:41:17 +01:00
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
2016-07-21 03:40:08 +02:00
2020-05-14 16:41:43 +02:00
if ( is_line_hidden ( cursor . line ) ) {
2017-11-13 00:12:17 +01:00
unfold_line ( cursor . line ) ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
2018-01-26 02:41:17 +01:00
set_line_as_center_visible ( cursor . line , get_cursor_wrap_index ( ) ) ;
2020-07-29 23:26:49 +02:00
int visible_width = get_size ( ) . width - cache . style_normal - > get_minimum_size ( ) . width - gutters_width - gutter_padding - cache . minimap_width ;
2020-05-14 16:41:43 +02:00
if ( v_scroll - > is_visible_in_tree ( ) ) {
2017-03-05 16:44:50 +01:00
visible_width - = v_scroll - > get_combined_minimum_size ( ) . width ;
2020-05-14 16:41:43 +02:00
}
2019-08-18 17:56:24 +02:00
visible_width - = 20 ; // Give it a little more space.
2016-07-21 03:40:08 +02:00
2018-01-26 02:41:17 +01:00
if ( is_wrap_enabled ( ) ) {
2019-08-18 17:56:24 +02:00
// Center x offset.
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
Vector2i cursor_pos ;
// Get position of the start of caret.
if ( ime_text . length ( ) ! = 0 & & ime_selection . x ! = 0 ) {
cursor_pos . x = get_column_x_offset_for_line ( cursor . column + ime_selection . x , cursor . line ) ;
} else {
cursor_pos . x = get_column_x_offset_for_line ( cursor . column , cursor . line ) ;
}
// Get position of the end of caret.
if ( ime_text . length ( ) ! = 0 ) {
if ( ime_selection . y ! = 0 ) {
cursor_pos . y = get_column_x_offset_for_line ( cursor . column + ime_selection . x + ime_selection . y , cursor . line ) ;
} else {
cursor_pos . y = get_column_x_offset_for_line ( cursor . column + ime_text . size ( ) , cursor . line ) ;
}
} else {
cursor_pos . y = cursor_pos . x ;
}
if ( MAX ( cursor_pos . x , cursor_pos . y ) > ( cursor . x_ofs + visible_width ) ) {
cursor . x_ofs = MAX ( cursor_pos . x , cursor_pos . y ) - visible_width + 1 ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
if ( MIN ( cursor_pos . x , cursor_pos . y ) < cursor . x_ofs ) {
cursor . x_ofs = MIN ( cursor_pos . x , cursor_pos . y ) ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
} else {
cursor . x_ofs = 0 ;
2018-01-06 22:46:31 +01:00
}
2018-01-26 02:41:17 +01:00
h_scroll - > set_value ( cursor . x_ofs ) ;
2016-07-21 03:40:08 +02:00
2018-01-26 02:41:17 +01:00
update ( ) ;
}
2016-07-21 03:40:08 +02:00
2018-01-26 02:41:17 +01:00
void TextEdit : : update_cursor_wrap_offset ( ) {
int first_vis_line = get_first_visible_line ( ) ;
if ( line_wraps ( first_vis_line ) ) {
cursor . wrap_ofs = MIN ( cursor . wrap_ofs , times_line_wraps ( first_vis_line ) ) ;
} else {
cursor . wrap_ofs = 0 ;
}
set_line_as_first_visible ( cursor . line_ofs , cursor . wrap_ofs ) ;
}
2016-07-21 03:40:08 +02:00
2018-01-26 02:41:17 +01:00
bool TextEdit : : line_wraps ( int line ) const {
ERR_FAIL_INDEX_V ( line , text . size ( ) , 0 ) ;
2020-05-14 16:41:43 +02:00
if ( ! is_wrap_enabled ( ) ) {
2018-01-26 02:41:17 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
2020-09-18 09:36:10 +02:00
return text . get_line_wrap_amount ( line ) > 0 ;
2018-01-26 02:41:17 +01:00
}
int TextEdit : : times_line_wraps ( int line ) const {
ERR_FAIL_INDEX_V ( line , text . size ( ) , 0 ) ;
2020-09-18 09:36:10 +02:00
2020-05-14 16:41:43 +02:00
if ( ! line_wraps ( line ) ) {
2018-01-26 02:41:17 +01:00
return 0 ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
2020-09-18 09:36:10 +02:00
return text . get_line_wrap_amount ( line ) ;
2018-01-26 02:41:17 +01:00
}
Vector < String > TextEdit : : get_wrap_rows_text ( int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , Vector < String > ( ) ) ;
Vector < String > lines ;
if ( ! line_wraps ( p_line ) ) {
lines . push_back ( text [ p_line ] ) ;
return lines ;
}
2020-09-18 09:36:10 +02:00
const String & line_text = text [ p_line ] ;
Vector < Vector2i > line_ranges = text . get_line_wrap_ranges ( p_line ) ;
for ( int i = 0 ; i < line_ranges . size ( ) ; i + + ) {
lines . push_back ( line_text . substr ( line_ranges [ i ] . x , line_ranges [ i ] . y - line_ranges [ i ] . x ) ) ;
2018-01-26 02:41:17 +01:00
}
2018-12-18 14:01:15 +01:00
2018-01-26 02:41:17 +01:00
return lines ;
}
int TextEdit : : get_cursor_wrap_index ( ) const {
return get_line_wrap_index_at_col ( cursor . line , cursor . column ) ;
}
int TextEdit : : get_line_wrap_index_at_col ( int p_line , int p_column ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , 0 ) ;
2020-05-14 16:41:43 +02:00
if ( ! line_wraps ( p_line ) ) {
2018-01-26 02:41:17 +01:00
return 0 ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
2019-08-18 17:56:24 +02:00
// Loop through wraps in the line text until we get to the column.
2018-01-26 02:41:17 +01:00
int wrap_index = 0 ;
int col = 0 ;
Vector < String > rows = get_wrap_rows_text ( p_line ) ;
for ( int i = 0 ; i < rows . size ( ) ; i + + ) {
wrap_index = i ;
String s = rows [ wrap_index ] ;
col + = s . length ( ) ;
2020-05-14 16:41:43 +02:00
if ( col > p_column ) {
2018-01-26 02:41:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
}
return wrap_index ;
2016-07-21 03:40:08 +02:00
}
2020-09-18 09:36:10 +02:00
void TextEdit : : set_mid_grapheme_caret_enabled ( const bool p_enabled ) {
mid_grapheme_caret_enabled = p_enabled ;
}
bool TextEdit : : get_mid_grapheme_caret_enabled ( ) const {
return mid_grapheme_caret_enabled ;
}
2015-08-13 00:34:07 +02:00
void TextEdit : : cursor_set_column ( int p_col , bool p_adjust_viewport ) {
2020-05-14 16:41:43 +02:00
if ( p_col < 0 ) {
2017-03-05 16:44:50 +01:00
p_col = 0 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
cursor . column = p_col ;
2020-05-14 16:41:43 +02:00
if ( cursor . column > get_line ( cursor . line ) . length ( ) ) {
2017-03-05 16:44:50 +01:00
cursor . column = get_line ( cursor . line ) . length ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2018-01-26 02:41:17 +01:00
cursor . last_fit_x = get_column_x_offset_for_line ( cursor . column , cursor . line ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( p_adjust_viewport ) {
2015-08-13 00:34:07 +02:00
adjust_viewport_to_cursor ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
if ( ! cursor_changed_dirty ) {
2020-05-14 16:41:43 +02:00
if ( is_inside_tree ( ) ) {
2017-03-05 16:44:50 +01:00
MessageQueue : : get_singleton ( ) - > push_call ( this , " _cursor_changed_emit " ) ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
cursor_changed_dirty = true ;
2015-01-02 19:08:40 +01:00
}
2014-02-10 02:10:30 +01:00
}
2018-01-26 02:41:17 +01:00
void TextEdit : : cursor_set_line ( int p_row , bool p_adjust_viewport , bool p_can_be_hidden , int p_wrap_index ) {
2020-05-14 16:41:43 +02:00
if ( setting_row ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
setting_row = true ;
2020-05-14 16:41:43 +02:00
if ( p_row < 0 ) {
2017-03-05 16:44:50 +01:00
p_row = 0 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( p_row > = text . size ( ) ) {
2018-01-26 02:41:17 +01:00
p_row = text . size ( ) - 1 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-11-13 00:12:17 +01:00
if ( ! p_can_be_hidden ) {
2017-11-16 05:00:27 +01:00
if ( is_line_hidden ( CLAMP ( p_row , 0 , text . size ( ) - 1 ) ) ) {
int move_down = num_lines_from ( p_row , 1 ) - 1 ;
if ( p_row + move_down < = text . size ( ) - 1 & & ! is_line_hidden ( p_row + move_down ) ) {
p_row + = move_down ;
} else {
int move_up = num_lines_from ( p_row , - 1 ) - 1 ;
if ( p_row - move_up > 0 & & ! is_line_hidden ( p_row - move_up ) ) {
p_row - = move_up ;
} else {
2019-11-07 09:44:15 +01:00
WARN_PRINT ( ( " Cursor set to hidden line " + itos ( p_row ) + " and there are no nonhidden lines. " ) ) ;
2017-11-13 00:12:17 +01:00
}
}
}
}
2017-03-05 16:44:50 +01:00
cursor . line = p_row ;
2018-01-26 02:41:17 +01:00
int n_col = get_char_pos_for_line ( cursor . last_fit_x , p_row , p_wrap_index ) ;
2019-06-14 22:48:16 +02:00
if ( n_col ! = 0 & & is_wrap_enabled ( ) & & p_wrap_index < times_line_wraps ( p_row ) ) {
2018-01-26 02:41:17 +01:00
Vector < String > rows = get_wrap_rows_text ( p_row ) ;
int row_end_col = 0 ;
for ( int i = 0 ; i < p_wrap_index + 1 ; i + + ) {
row_end_col + = rows [ i ] . length ( ) ;
}
2020-05-14 16:41:43 +02:00
if ( n_col > = row_end_col ) {
2018-01-26 02:41:17 +01:00
n_col - = 1 ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
}
cursor . column = n_col ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( p_adjust_viewport ) {
2015-08-13 00:34:07 +02:00
adjust_viewport_to_cursor ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
setting_row = false ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
if ( ! cursor_changed_dirty ) {
2020-05-14 16:41:43 +02:00
if ( is_inside_tree ( ) ) {
2017-03-05 16:44:50 +01:00
MessageQueue : : get_singleton ( ) - > push_call ( this , " _cursor_changed_emit " ) ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
cursor_changed_dirty = true ;
2015-01-02 19:08:40 +01:00
}
2014-02-10 02:10:30 +01:00
}
int TextEdit : : cursor_get_column ( ) const {
2015-01-02 19:08:40 +01:00
return cursor . column ;
2014-02-10 02:10:30 +01:00
}
int TextEdit : : cursor_get_line ( ) const {
2015-01-02 19:08:40 +01:00
return cursor . line ;
2014-02-10 02:10:30 +01:00
}
2016-05-09 20:21:55 +02:00
bool TextEdit : : cursor_get_blink_enabled ( ) const {
return caret_blink_enabled ;
}
void TextEdit : : cursor_set_blink_enabled ( const bool p_enabled ) {
caret_blink_enabled = p_enabled ;
2019-09-04 13:06:15 +02:00
if ( has_focus ( ) ) {
if ( p_enabled ) {
caret_blink_timer - > start ( ) ;
} else {
caret_blink_timer - > stop ( ) ;
}
2016-05-09 20:21:55 +02:00
}
2019-09-04 13:06:15 +02:00
2016-05-09 20:21:55 +02:00
draw_caret = true ;
}
float TextEdit : : cursor_get_blink_speed ( ) const {
return caret_blink_timer - > get_wait_time ( ) ;
}
void TextEdit : : cursor_set_blink_speed ( const float p_speed ) {
ERR_FAIL_COND ( p_speed < = 0 ) ;
caret_blink_timer - > set_wait_time ( p_speed ) ;
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
void TextEdit : : cursor_set_block_mode ( const bool p_enable ) {
2016-07-12 17:07:17 +02:00
block_caret = p_enable ;
update ( ) ;
}
bool TextEdit : : cursor_is_block_mode ( ) const {
return block_caret ;
}
2017-12-20 02:36:47 +01:00
void TextEdit : : set_right_click_moves_caret ( bool p_enable ) {
right_click_moves_caret = p_enable ;
}
bool TextEdit : : is_right_click_moving_caret ( ) const {
return right_click_moves_caret ;
}
2020-10-11 19:44:53 +02:00
TextEdit : : SelectionMode TextEdit : : get_selection_mode ( ) const {
return selection . selecting_mode ;
}
void TextEdit : : set_selection_mode ( SelectionMode p_mode , int p_line , int p_column ) {
selection . selecting_mode = p_mode ;
if ( p_line > = 0 ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
selection . selecting_line = p_line ;
}
if ( p_column > = 0 ) {
2020-09-18 09:36:10 +02:00
ERR_FAIL_INDEX ( p_column , text [ selection . selecting_line ] . length ( ) ) ;
2020-10-11 19:44:53 +02:00
selection . selecting_column = p_column ;
}
}
int TextEdit : : get_selection_line ( ) const {
return selection . selecting_line ;
} ;
int TextEdit : : get_selection_column ( ) const {
return selection . selecting_column ;
} ;
2017-08-22 21:02:08 +02:00
void TextEdit : : _v_scroll_input ( ) {
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
2017-08-22 21:02:08 +02:00
}
2014-02-10 02:10:30 +01:00
void TextEdit : : _scroll_moved ( double p_to_val ) {
2020-05-14 16:41:43 +02:00
if ( updating_scrolls ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( h_scroll - > is_visible_in_tree ( ) ) {
2017-03-05 16:44:50 +01:00
cursor . x_ofs = h_scroll - > get_value ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-11-16 05:00:27 +01:00
if ( v_scroll - > is_visible_in_tree ( ) ) {
2019-08-18 17:56:24 +02:00
// Set line ofs and wrap ofs.
2018-01-26 02:41:17 +01:00
int v_scroll_i = floor ( get_v_scroll ( ) ) ;
int sc = 0 ;
int n_line ;
2019-11-09 17:45:44 +01:00
for ( n_line = 0 ; n_line < text . size ( ) ; n_line + + ) {
2018-01-26 02:41:17 +01:00
if ( ! is_line_hidden ( n_line ) ) {
sc + + ;
sc + = times_line_wraps ( n_line ) ;
2020-05-14 16:41:43 +02:00
if ( sc > v_scroll_i ) {
2018-01-26 02:41:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
}
}
2019-11-24 10:48:04 +01:00
n_line = MIN ( n_line , text . size ( ) - 1 ) ;
2018-01-26 02:41:17 +01:00
int line_wrap_amount = times_line_wraps ( n_line ) ;
int wi = line_wrap_amount - ( sc - v_scroll_i - 1 ) ;
wi = CLAMP ( wi , 0 , line_wrap_amount ) ;
cursor . line_ofs = n_line ;
cursor . wrap_ofs = wi ;
2017-11-16 05:00:27 +01:00
}
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
int TextEdit : : get_row_height ( ) const {
2020-09-18 09:36:10 +02:00
int height = cache . font - > get_height ( cache . font_size ) ;
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
for ( int j = 0 ; j < = text . get_line_wrap_amount ( i ) ; j + + ) {
height = MAX ( height , text . get_line_height ( i , j ) ) ;
}
}
return height + cache . line_spacing ;
2014-02-10 02:10:30 +01:00
}
2018-01-26 02:41:17 +01:00
int TextEdit : : get_char_pos_for_line ( int p_px , int p_line , int p_wrap_index ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , 0 ) ;
2020-09-18 09:36:10 +02:00
p_wrap_index = MIN ( p_wrap_index , text . get_line_data ( p_line ) - > get_line_count ( ) - 1 ) ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
RID text_rid = text . get_line_data ( p_line ) - > get_line_rid ( p_wrap_index ) ;
if ( is_layout_rtl ( ) ) {
p_px = TS - > shaped_text_get_size ( text_rid ) . x - p_px ;
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
return TS - > shaped_text_hit_test_position ( text_rid , p_px ) ;
2018-01-26 02:41:17 +01:00
}
2016-03-09 00:00:52 +01:00
2018-01-26 02:41:17 +01:00
int TextEdit : : get_column_x_offset_for_line ( int p_char , int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , 0 ) ;
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
int row = 0 ;
Vector < Vector2i > rows2 = text . get_line_wrap_ranges ( p_line ) ;
for ( int i = 0 ; i < rows2 . size ( ) ; i + + ) {
if ( ( p_char > = rows2 [ i ] . x ) & & ( p_char < rows2 [ i ] . y ) ) {
row = i ;
2015-01-02 19:08:40 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
Rect2 l_caret , t_caret ;
TextServer : : Direction l_dir , t_dir ;
RID text_rid = text . get_line_data ( p_line ) - > get_line_rid ( row ) ;
TS - > shaped_text_get_carets ( text_rid , cursor . column , l_caret , l_dir , t_caret , t_dir ) ;
if ( ( l_caret ! = Rect2 ( ) & & ( l_dir = = TextServer : : DIRECTION_AUTO | | l_dir = = ( TextServer : : Direction ) input_direction ) ) | | ( t_caret = = Rect2 ( ) ) ) {
return l_caret . position . x ;
} else {
return t_caret . position . x ;
2015-01-02 19:08:40 +01:00
}
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : insert_text_at_cursor ( const String & p_text ) {
2015-01-02 19:08:40 +01:00
if ( selection . active ) {
cursor_set_line ( selection . from_line ) ;
cursor_set_column ( selection . from_column ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
_remove_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
selection . active = false ;
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_NONE ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
_insert_text_at_cursor ( p_text ) ;
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
Control : : CursorShape TextEdit : : get_cursor_shape ( const Point2 & p_pos ) const {
2020-05-14 16:41:43 +02:00
if ( highlighted_word ! = String ( ) ) {
2016-09-12 15:52:29 +02:00
return CURSOR_POINTING_HAND ;
2020-05-14 16:41:43 +02:00
}
2016-09-12 15:52:29 +02:00
2017-11-16 05:00:27 +01:00
if ( ( completion_active & & completion_rect . has_point ( p_pos ) ) ) {
return CURSOR_ARROW ;
}
2020-07-27 14:54:12 +02:00
int row , col ;
_get_mouse_pos ( p_pos , row , col ) ;
int left_margin = cache . style_normal - > get_margin ( MARGIN_LEFT ) ;
2020-07-29 23:26:49 +02:00
int gutter = left_margin + gutters_width ;
2020-07-27 14:54:12 +02:00
if ( p_pos . x < gutter ) {
2020-07-25 02:15:23 +02:00
for ( int i = 0 ; i < gutters . size ( ) ; i + + ) {
if ( ! gutters [ i ] . draw ) {
continue ;
}
if ( p_pos . x > left_margin & & p_pos . x < = ( left_margin + gutters [ i ] . width ) - 3 ) {
if ( gutters [ i ] . clickable | | is_line_gutter_clickable ( row , i ) ) {
return CURSOR_POINTING_HAND ;
}
}
left_margin + = gutters [ i ] . width ;
}
2020-07-27 14:54:12 +02:00
return CURSOR_ARROW ;
}
2019-04-20 13:51:25 +02:00
2020-07-27 14:54:12 +02:00
int xmargin_end = get_size ( ) . width - cache . style_normal - > get_margin ( MARGIN_RIGHT ) ;
if ( draw_minimap & & p_pos . x > xmargin_end - minimap_width & & p_pos . x < = xmargin_end ) {
2015-01-02 19:08:40 +01:00
return CURSOR_ARROW ;
2020-07-27 14:54:12 +02:00
}
2019-08-11 20:31:19 +02:00
2020-07-27 14:54:12 +02:00
// EOL fold icon.
if ( is_folded ( row ) ) {
gutter + = gutter_padding + text . get_line_width ( row ) - cursor . x_ofs ;
if ( p_pos . x > gutter - 3 & & p_pos . x < = gutter + cache . folded_eol_icon - > get_width ( ) + 3 ) {
return CURSOR_POINTING_HAND ;
2017-11-16 05:00:27 +01:00
}
2015-01-02 19:08:40 +01:00
}
2017-11-16 05:00:27 +01:00
2018-09-11 18:25:40 +02:00
return get_default_cursor_shape ( ) ;
2014-04-17 13:30:40 +02:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : set_text ( String p_text ) {
setting_text = true ;
2019-03-09 01:26:49 +01:00
if ( ! undo_enabled ) {
_clear ( ) ;
_insert_text_at_cursor ( p_text ) ;
}
if ( undo_enabled ) {
cursor_set_line ( 0 ) ;
cursor_set_column ( 0 ) ;
begin_complex_operation ( ) ;
_remove_text ( 0 , 0 , MAX ( 0 , get_line_count ( ) - 1 ) , MAX ( get_line ( MAX ( get_line_count ( ) - 1 , 0 ) ) . size ( ) - 1 , 0 ) ) ;
_insert_text_at_cursor ( p_text ) ;
end_complex_operation ( ) ;
selection . active = false ;
}
2019-10-13 16:29:20 +02:00
cursor_set_line ( 0 ) ;
cursor_set_column ( 0 ) ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2017-03-05 16:44:50 +01:00
setting_text = false ;
2020-09-18 09:36:10 +02:00
}
2014-02-10 02:10:30 +01:00
String TextEdit : : get_text ( ) {
2015-01-02 19:08:40 +01:00
String longthing ;
int len = text . size ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
longthing + = text [ i ] ;
2020-05-14 16:41:43 +02:00
if ( i ! = len - 1 ) {
2017-03-05 16:44:50 +01:00
longthing + = " \n " ;
2020-05-14 16:41:43 +02:00
}
2014-12-17 02:31:57 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
return longthing ;
2020-09-18 09:36:10 +02:00
}
void TextEdit : : set_structured_text_bidi_override ( Control : : StructuredTextParser p_parser ) {
if ( st_parser ! = p_parser ) {
st_parser = p_parser ;
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
text . set ( i , text [ i ] , structured_text_parser ( st_parser , st_args , text [ i ] ) ) ;
}
update ( ) ;
}
}
Control : : StructuredTextParser TextEdit : : get_structured_text_bidi_override ( ) const {
return st_parser ;
}
void TextEdit : : set_structured_text_bidi_override_options ( Array p_args ) {
st_args = p_args ;
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
text . set ( i , text [ i ] , structured_text_parser ( st_parser , st_args , text [ i ] ) ) ;
}
update ( ) ;
}
Array TextEdit : : get_structured_text_bidi_override_options ( ) const {
return st_args ;
}
void TextEdit : : set_text_direction ( Control : : TextDirection p_text_direction ) {
ERR_FAIL_COND ( ( int ) p_text_direction < - 1 | | ( int ) p_text_direction > 3 ) ;
if ( text_direction ! = p_text_direction ) {
text_direction = p_text_direction ;
if ( text_direction ! = TEXT_DIRECTION_AUTO & & text_direction ! = TEXT_DIRECTION_INHERITED ) {
input_direction = text_direction ;
}
TextServer : : Direction dir ;
if ( text_direction = = Control : : TEXT_DIRECTION_INHERITED ) {
dir = is_layout_rtl ( ) ? TextServer : : DIRECTION_RTL : TextServer : : DIRECTION_LTR ;
} else {
dir = ( TextServer : : Direction ) text_direction ;
}
text . set_direction_and_language ( dir , ( language ! = " " ) ? language : TranslationServer : : get_singleton ( ) - > get_tool_locale ( ) ) ;
text . invalidate_all ( ) ;
menu_dir - > set_item_checked ( menu_dir - > get_item_index ( MENU_DIR_INHERITED ) , text_direction = = TEXT_DIRECTION_INHERITED ) ;
menu_dir - > set_item_checked ( menu_dir - > get_item_index ( MENU_DIR_AUTO ) , text_direction = = TEXT_DIRECTION_AUTO ) ;
menu_dir - > set_item_checked ( menu_dir - > get_item_index ( MENU_DIR_LTR ) , text_direction = = TEXT_DIRECTION_LTR ) ;
menu_dir - > set_item_checked ( menu_dir - > get_item_index ( MENU_DIR_RTL ) , text_direction = = TEXT_DIRECTION_RTL ) ;
update ( ) ;
}
}
Control : : TextDirection TextEdit : : get_text_direction ( ) const {
return text_direction ;
}
void TextEdit : : clear_opentype_features ( ) {
opentype_features . clear ( ) ;
text . set_font_features ( opentype_features ) ;
text . invalidate_all ( ) ;
update ( ) ;
}
void TextEdit : : set_opentype_feature ( const String & p_name , int p_value ) {
int32_t tag = TS - > name_to_tag ( p_name ) ;
if ( ! opentype_features . has ( tag ) | | ( int ) opentype_features [ tag ] ! = p_value ) {
opentype_features [ tag ] = p_value ;
text . set_font_features ( opentype_features ) ;
text . invalidate_all ( ) ;
update ( ) ;
}
}
int TextEdit : : get_opentype_feature ( const String & p_name ) const {
int32_t tag = TS - > name_to_tag ( p_name ) ;
if ( ! opentype_features . has ( tag ) ) {
return - 1 ;
}
return opentype_features [ tag ] ;
}
void TextEdit : : set_language ( const String & p_language ) {
if ( language ! = p_language ) {
language = p_language ;
TextServer : : Direction dir ;
if ( text_direction = = Control : : TEXT_DIRECTION_INHERITED ) {
dir = is_layout_rtl ( ) ? TextServer : : DIRECTION_RTL : TextServer : : DIRECTION_LTR ;
} else {
dir = ( TextServer : : Direction ) text_direction ;
}
text . set_direction_and_language ( dir , ( language ! = " " ) ? language : TranslationServer : : get_singleton ( ) - > get_tool_locale ( ) ) ;
text . invalidate_all ( ) ;
update ( ) ;
}
}
String TextEdit : : get_language ( ) const {
return language ;
}
void TextEdit : : set_draw_control_chars ( bool p_draw_control_chars ) {
if ( draw_control_chars ! = p_draw_control_chars ) {
draw_control_chars = p_draw_control_chars ;
menu - > set_item_checked ( menu - > get_item_index ( MENU_DISPLAY_UCC ) , draw_control_chars ) ;
text . set_draw_control_chars ( draw_control_chars ) ;
text . invalidate_all ( ) ;
update ( ) ;
}
}
bool TextEdit : : get_draw_control_chars ( ) const {
return draw_control_chars ;
}
2014-12-17 02:31:57 +01:00
2016-09-12 15:52:29 +02:00
String TextEdit : : get_text_for_lookup_completion ( ) {
2017-03-05 16:44:50 +01:00
int row , col ;
2020-09-18 09:36:10 +02:00
Point2i mp = _get_local_mouse_pos ( ) ;
_get_mouse_pos ( mp , row , col ) ;
2016-09-12 15:52:29 +02:00
String longthing ;
int len = text . size ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
if ( i = = row ) {
longthing + = text [ i ] . substr ( 0 , col ) ;
2019-08-18 17:56:24 +02:00
longthing + = String : : chr ( 0xFFFF ) ; // Not unicode, represents the cursor.
2017-03-05 16:44:50 +01:00
longthing + = text [ i ] . substr ( col , text [ i ] . size ( ) ) ;
2016-09-12 15:52:29 +02:00
} else {
2017-03-05 16:44:50 +01:00
longthing + = text [ i ] ;
2016-09-12 15:52:29 +02:00
}
2020-05-14 16:41:43 +02:00
if ( i ! = len - 1 ) {
2017-03-05 16:44:50 +01:00
longthing + = " \n " ;
2020-05-14 16:41:43 +02:00
}
2016-09-12 15:52:29 +02:00
}
return longthing ;
}
2015-01-02 19:08:40 +01:00
String TextEdit : : get_text_for_completion ( ) {
String longthing ;
int len = text . size ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
if ( i = = cursor . line ) {
longthing + = text [ i ] . substr ( 0 , cursor . column ) ;
2019-08-18 17:56:24 +02:00
longthing + = String : : chr ( 0xFFFF ) ; // Not unicode, represents the cursor.
2017-03-05 16:44:50 +01:00
longthing + = text [ i ] . substr ( cursor . column , text [ i ] . size ( ) ) ;
2015-01-02 19:08:40 +01:00
} else {
2017-03-05 16:44:50 +01:00
longthing + = text [ i ] ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( i ! = len - 1 ) {
2017-03-05 16:44:50 +01:00
longthing + = " \n " ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
return longthing ;
2014-12-17 02:31:57 +01:00
} ;
2014-02-10 02:10:30 +01:00
String TextEdit : : get_line ( int line ) const {
2020-05-14 16:41:43 +02:00
if ( line < 0 | | line > = text . size ( ) ) {
2015-01-02 19:08:40 +01:00
return " " ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
return text [ line ] ;
2014-02-10 02:10:30 +01:00
} ;
void TextEdit : : _clear ( ) {
2015-01-02 19:08:40 +01:00
clear_undo_history ( ) ;
text . clear ( ) ;
2017-03-05 16:44:50 +01:00
cursor . column = 0 ;
cursor . line = 0 ;
cursor . x_ofs = 0 ;
cursor . line_ofs = 0 ;
2018-01-26 02:41:17 +01:00
cursor . wrap_ofs = 0 ;
2017-03-05 16:44:50 +01:00
cursor . last_fit_x = 0 ;
2018-11-25 18:24:49 +01:00
selection . active = false ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : clear ( ) {
2017-03-05 16:44:50 +01:00
setting_text = true ;
2015-01-02 19:08:40 +01:00
_clear ( ) ;
2017-03-05 16:44:50 +01:00
setting_text = false ;
2014-02-10 02:10:30 +01:00
} ;
void TextEdit : : set_readonly ( bool p_readonly ) {
2020-05-14 16:41:43 +02:00
if ( readonly = = p_readonly ) {
2019-04-22 01:09:52 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2019-04-22 01:09:52 +02:00
2017-03-05 16:44:50 +01:00
readonly = p_readonly ;
2019-08-08 02:09:46 +02:00
_generate_context_menu ( ) ;
2019-04-22 01:09:52 +02:00
2017-11-30 04:53:15 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-11-11 19:07:17 +01:00
bool TextEdit : : is_readonly ( ) const {
return readonly ;
}
2018-01-26 02:41:17 +01:00
void TextEdit : : set_wrap_enabled ( bool p_wrap_enabled ) {
2020-09-18 09:36:10 +02:00
if ( wrap_enabled ! = p_wrap_enabled ) {
wrap_enabled = p_wrap_enabled ;
_update_wrap_at ( true ) ;
}
2014-02-10 02:10:30 +01:00
}
2018-01-26 02:41:17 +01:00
bool TextEdit : : is_wrap_enabled ( ) const {
return wrap_enabled ;
2018-01-11 23:35:12 +01:00
}
2014-02-10 02:10:30 +01:00
void TextEdit : : set_max_chars ( int p_max_chars ) {
2017-03-05 16:44:50 +01:00
max_chars = p_max_chars ;
2014-02-10 02:10:30 +01:00
}
2018-01-11 23:35:12 +01:00
int TextEdit : : get_max_chars ( ) const {
return max_chars ;
}
2016-05-09 20:21:55 +02:00
void TextEdit : : _reset_caret_blink_timer ( ) {
if ( caret_blink_enabled ) {
draw_caret = true ;
2019-09-04 13:06:15 +02:00
if ( has_focus ( ) ) {
caret_blink_timer - > stop ( ) ;
caret_blink_timer - > start ( ) ;
update ( ) ;
}
2016-05-09 20:21:55 +02:00
}
}
void TextEdit : : _toggle_draw_caret ( ) {
draw_caret = ! draw_caret ;
2017-01-13 14:45:50 +01:00
if ( is_visible_in_tree ( ) & & has_focus ( ) & & window_has_focus ) {
2016-06-08 15:02:39 +02:00
update ( ) ;
}
2016-05-09 20:21:55 +02:00
}
2014-02-10 02:10:30 +01:00
void TextEdit : : _update_caches ( ) {
2020-03-12 13:37:40 +01:00
cache . style_normal = get_theme_stylebox ( " normal " ) ;
cache . style_focus = get_theme_stylebox ( " focus " ) ;
cache . style_readonly = get_theme_stylebox ( " read_only " ) ;
cache . completion_background_color = get_theme_color ( " completion_background_color " ) ;
cache . completion_selected_color = get_theme_color ( " completion_selected_color " ) ;
cache . completion_existing_color = get_theme_color ( " completion_existing_color " ) ;
cache . completion_font_color = get_theme_color ( " completion_font_color " ) ;
cache . font = get_theme_font ( " font " ) ;
2020-09-03 13:22:16 +02:00
cache . font_size = get_theme_font_size ( " font_size " ) ;
2020-03-12 13:37:40 +01:00
cache . caret_color = get_theme_color ( " caret_color " ) ;
cache . caret_background_color = get_theme_color ( " caret_background_color " ) ;
cache . font_color = get_theme_color ( " font_color " ) ;
cache . font_color_selected = get_theme_color ( " font_color_selected " ) ;
cache . font_color_readonly = get_theme_color ( " font_color_readonly " ) ;
cache . selection_color = get_theme_color ( " selection_color " ) ;
cache . mark_color = get_theme_color ( " mark_color " ) ;
cache . current_line_color = get_theme_color ( " current_line_color " ) ;
cache . line_length_guideline_color = get_theme_color ( " line_length_guideline_color " ) ;
cache . code_folding_color = get_theme_color ( " code_folding_color " ) ;
cache . brace_mismatch_color = get_theme_color ( " brace_mismatch_color " ) ;
cache . word_highlighted_color = get_theme_color ( " word_highlighted_color " ) ;
cache . search_result_color = get_theme_color ( " search_result_color " ) ;
cache . search_result_border_color = get_theme_color ( " search_result_border_color " ) ;
cache . background_color = get_theme_color ( " background_color " ) ;
2018-07-26 23:50:16 +02:00
# ifdef TOOLS_ENABLED
2020-03-12 13:37:40 +01:00
cache . line_spacing = get_theme_constant ( " line_spacing " ) * EDSCALE ;
2018-07-26 23:50:16 +02:00
# else
2020-03-13 00:12:34 +01:00
cache . line_spacing = get_theme_constant ( " line_spacing " ) ;
2018-07-26 23:50:16 +02:00
# endif
2020-03-12 13:37:40 +01:00
cache . tab_icon = get_theme_icon ( " tab " ) ;
cache . space_icon = get_theme_icon ( " space " ) ;
cache . folded_eol_icon = get_theme_icon ( " GuiEllipsis " , " EditorIcons " ) ;
2020-09-18 09:36:10 +02:00
TextServer : : Direction dir ;
if ( text_direction = = Control : : TEXT_DIRECTION_INHERITED ) {
dir = is_layout_rtl ( ) ? TextServer : : DIRECTION_RTL : TextServer : : DIRECTION_LTR ;
} else {
dir = ( TextServer : : Direction ) text_direction ;
}
text . set_direction_and_language ( dir , ( language ! = " " ) ? language : TranslationServer : : get_singleton ( ) - > get_tool_locale ( ) ) ;
text . set_font_features ( opentype_features ) ;
text . set_draw_control_chars ( draw_control_chars ) ;
2015-01-02 19:08:40 +01:00
text . set_font ( cache . font ) ;
2020-09-18 09:36:10 +02:00
text . set_font_size ( cache . font_size ) ;
text . invalidate_all ( ) ;
2018-04-02 13:41:44 +02:00
2020-03-07 12:17:18 +01:00
if ( syntax_highlighter . is_valid ( ) ) {
2020-05-03 18:08:15 +02:00
syntax_highlighter - > set_text_edit ( this ) ;
2018-04-02 13:41:44 +02:00
}
}
2020-07-25 02:15:23 +02:00
/* Syntax Highlighting. */
2020-05-03 18:08:15 +02:00
Ref < SyntaxHighlighter > TextEdit : : get_syntax_highlighter ( ) {
2018-04-02 13:41:44 +02:00
return syntax_highlighter ;
}
2020-05-03 18:08:15 +02:00
void TextEdit : : set_syntax_highlighter ( Ref < SyntaxHighlighter > p_syntax_highlighter ) {
2018-04-02 13:41:44 +02:00
syntax_highlighter = p_syntax_highlighter ;
2020-03-07 12:17:18 +01:00
if ( syntax_highlighter . is_valid ( ) ) {
syntax_highlighter - > set_text_edit ( this ) ;
2018-04-02 13:41:44 +02:00
}
update ( ) ;
}
2020-07-25 02:15:23 +02:00
/* Gutters. */
void TextEdit : : _update_gutter_width ( ) {
gutters_width = 0 ;
for ( int i = 0 ; i < gutters . size ( ) ; i + + ) {
if ( gutters [ i ] . draw ) {
gutters_width + = gutters [ i ] . width ;
}
}
if ( gutters_width > 0 ) {
gutter_padding = 2 ;
}
update ( ) ;
}
void TextEdit : : add_gutter ( int p_at ) {
if ( p_at < 0 | | p_at > gutters . size ( ) ) {
gutters . push_back ( GutterInfo ( ) ) ;
} else {
gutters . insert ( p_at , GutterInfo ( ) ) ;
}
for ( int i = 0 ; i < text . size ( ) + 1 ; i + + ) {
text . add_gutter ( p_at ) ;
}
emit_signal ( " gutter_added " ) ;
update ( ) ;
}
void TextEdit : : remove_gutter ( int p_gutter ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
gutters . remove ( p_gutter ) ;
for ( int i = 0 ; i < text . size ( ) + 1 ; i + + ) {
text . remove_gutter ( p_gutter ) ;
}
emit_signal ( " gutter_removed " ) ;
update ( ) ;
}
int TextEdit : : get_gutter_count ( ) const {
return gutters . size ( ) ;
}
void TextEdit : : set_gutter_name ( int p_gutter , const String & p_name ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
gutters . write [ p_gutter ] . name = p_name ;
}
String TextEdit : : get_gutter_name ( int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , " " ) ;
return gutters [ p_gutter ] . name ;
}
void TextEdit : : set_gutter_type ( int p_gutter , GutterType p_type ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
gutters . write [ p_gutter ] . type = p_type ;
update ( ) ;
}
TextEdit : : GutterType TextEdit : : get_gutter_type ( int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , GUTTER_TYPE_STRING ) ;
return gutters [ p_gutter ] . type ;
}
void TextEdit : : set_gutter_width ( int p_gutter , int p_width ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
gutters . write [ p_gutter ] . width = p_width ;
_update_gutter_width ( ) ;
}
int TextEdit : : get_gutter_width ( int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , - 1 ) ;
return gutters [ p_gutter ] . width ;
}
void TextEdit : : set_gutter_draw ( int p_gutter , bool p_draw ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
gutters . write [ p_gutter ] . draw = p_draw ;
_update_gutter_width ( ) ;
}
bool TextEdit : : is_gutter_drawn ( int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , false ) ;
return gutters [ p_gutter ] . draw ;
}
void TextEdit : : set_gutter_clickable ( int p_gutter , bool p_clickable ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
gutters . write [ p_gutter ] . clickable = p_clickable ;
update ( ) ;
}
bool TextEdit : : is_gutter_clickable ( int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , false ) ;
return gutters [ p_gutter ] . clickable ;
}
void TextEdit : : set_gutter_overwritable ( int p_gutter , bool p_overwritable ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
gutters . write [ p_gutter ] . overwritable = p_overwritable ;
}
bool TextEdit : : is_gutter_overwritable ( int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , false ) ;
return gutters [ p_gutter ] . overwritable ;
}
void TextEdit : : set_gutter_custom_draw ( int p_gutter , Object * p_object , const StringName & p_callback ) {
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
ERR_FAIL_NULL ( p_object ) ;
gutters . write [ p_gutter ] . custom_draw_obj = p_object - > get_instance_id ( ) ;
gutters . write [ p_gutter ] . custom_draw_callback = p_callback ;
update ( ) ;
}
// Line gutters.
void TextEdit : : set_line_gutter_metadata ( int p_line , int p_gutter , const Variant & p_metadata ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
text . set_line_gutter_metadata ( p_line , p_gutter , p_metadata ) ;
}
Variant TextEdit : : get_line_gutter_metadata ( int p_line , int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , " " ) ;
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , " " ) ;
return text . get_line_gutter_metadata ( p_line , p_gutter ) ;
}
void TextEdit : : set_line_gutter_text ( int p_line , int p_gutter , const String & p_text ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
text . set_line_gutter_text ( p_line , p_gutter , p_text ) ;
update ( ) ;
}
String TextEdit : : get_line_gutter_text ( int p_line , int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , " " ) ;
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , " " ) ;
return text . get_line_gutter_text ( p_line , p_gutter ) ;
}
void TextEdit : : set_line_gutter_icon ( int p_line , int p_gutter , Ref < Texture2D > p_icon ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
text . set_line_gutter_icon ( p_line , p_gutter , p_icon ) ;
update ( ) ;
}
Ref < Texture2D > TextEdit : : get_line_gutter_icon ( int p_line , int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , Ref < Texture2D > ( ) ) ;
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , Ref < Texture2D > ( ) ) ;
return text . get_line_gutter_icon ( p_line , p_gutter ) ;
}
void TextEdit : : set_line_gutter_item_color ( int p_line , int p_gutter , const Color & p_color ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
text . set_line_gutter_item_color ( p_line , p_gutter , p_color ) ;
update ( ) ;
}
Color TextEdit : : get_line_gutter_item_color ( int p_line , int p_gutter ) {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , Color ( ) ) ;
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , Color ( ) ) ;
return text . get_line_gutter_item_color ( p_line , p_gutter ) ;
}
void TextEdit : : set_line_gutter_clickable ( int p_line , int p_gutter , bool p_clickable ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
ERR_FAIL_INDEX ( p_gutter , gutters . size ( ) ) ;
text . set_line_gutter_clickable ( p_line , p_gutter , p_clickable ) ;
}
bool TextEdit : : is_line_gutter_clickable ( int p_line , int p_gutter ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , false ) ;
ERR_FAIL_INDEX_V ( p_gutter , gutters . size ( ) , false ) ;
return text . is_line_gutter_clickable ( p_line , p_gutter ) ;
}
2020-05-03 18:08:15 +02:00
void TextEdit : : add_keyword ( const String & p_keyword ) {
keywords . insert ( p_keyword ) ;
2018-04-02 13:41:44 +02:00
}
2020-05-03 18:08:15 +02:00
void TextEdit : : clear_keywords ( ) {
2015-01-02 19:08:40 +01:00
keywords . clear ( ) ;
2014-02-10 02:10:30 +01:00
}
2015-11-29 17:02:35 +01:00
void TextEdit : : set_auto_indent ( bool p_auto_indent ) {
auto_indent = p_auto_indent ;
}
2014-02-10 02:10:30 +01:00
void TextEdit : : cut ( ) {
2016-03-15 13:02:38 +01:00
if ( ! selection . active ) {
String clipboard = text [ cursor . line ] ;
2020-03-03 14:36:29 +01:00
DisplayServer : : get_singleton ( ) - > clipboard_set ( clipboard ) ;
2016-03-15 13:02:38 +01:00
cursor_set_line ( cursor . line ) ;
cursor_set_column ( 0 ) ;
2016-03-09 00:00:52 +01:00
2019-12-12 20:17:41 +01:00
if ( cursor . line = = 0 & & get_line_count ( ) > 1 ) {
_remove_text ( cursor . line , 0 , cursor . line + 1 , 0 ) ;
} else {
_remove_text ( cursor . line , 0 , cursor . line , text [ cursor . line ] . length ( ) ) ;
backspace_at_cursor ( ) ;
cursor_set_line ( cursor . line + 1 ) ;
}
2016-03-15 13:02:38 +01:00
update ( ) ;
2017-12-17 16:24:23 +01:00
cut_copy_line = clipboard ;
2016-03-09 00:00:52 +01:00
2016-03-15 13:02:38 +01:00
} else {
2017-03-05 16:44:50 +01:00
String clipboard = _base_get_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
2020-03-03 14:36:29 +01:00
DisplayServer : : get_singleton ( ) - > clipboard_set ( clipboard ) ;
2016-03-15 13:02:38 +01:00
2017-08-27 19:07:28 +02:00
_remove_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
2019-08-18 17:56:24 +02:00
cursor_set_line ( selection . from_line ) ; // Set afterwards else it causes the view to be offset.
2016-03-15 13:02:38 +01:00
cursor_set_column ( selection . from_column ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
selection . active = false ;
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_NONE ;
2016-03-15 13:02:38 +01:00
update ( ) ;
2017-12-17 16:24:23 +01:00
cut_copy_line = " " ;
2016-03-15 13:02:38 +01:00
}
2014-02-10 02:10:30 +01:00
}
void TextEdit : : copy ( ) {
2016-03-15 13:02:38 +01:00
if ( ! selection . active ) {
2018-09-30 17:17:29 +02:00
if ( text [ cursor . line ] . length ( ) ! = 0 ) {
String clipboard = _base_get_text ( cursor . line , 0 , cursor . line , text [ cursor . line ] . length ( ) ) ;
2020-03-03 14:36:29 +01:00
DisplayServer : : get_singleton ( ) - > clipboard_set ( clipboard ) ;
2018-09-30 17:17:29 +02:00
cut_copy_line = clipboard ;
}
2016-03-15 13:02:38 +01:00
} else {
2017-03-05 16:44:50 +01:00
String clipboard = _base_get_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
2020-03-03 14:36:29 +01:00
DisplayServer : : get_singleton ( ) - > clipboard_set ( clipboard ) ;
2017-12-17 16:24:23 +01:00
cut_copy_line = " " ;
2016-03-15 13:02:38 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-15 13:02:38 +01:00
2014-02-10 02:10:30 +01:00
void TextEdit : : paste ( ) {
2020-03-03 14:36:29 +01:00
String clipboard = DisplayServer : : get_singleton ( ) - > clipboard_get ( ) ;
2016-03-15 13:02:38 +01:00
2018-05-09 14:07:06 +02:00
begin_complex_operation ( ) ;
2015-01-02 19:08:40 +01:00
if ( selection . active ) {
2017-03-05 16:44:50 +01:00
selection . active = false ;
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_NONE ;
2017-03-05 16:44:50 +01:00
_remove_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
2015-01-02 19:08:40 +01:00
cursor_set_line ( selection . from_line ) ;
cursor_set_column ( selection . from_column ) ;
2016-03-09 00:00:52 +01:00
2017-12-17 16:24:23 +01:00
} else if ( ! cut_copy_line . empty ( ) & & cut_copy_line = = clipboard ) {
2016-03-15 13:02:38 +01:00
cursor_set_column ( 0 ) ;
2017-03-05 16:44:50 +01:00
String ins = " \n " ;
2016-03-15 13:02:38 +01:00
clipboard + = ins ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
_insert_text_at_cursor ( clipboard ) ;
2018-05-09 14:07:06 +02:00
end_complex_operation ( ) ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : select_all ( ) {
2020-05-14 16:41:43 +02:00
if ( ! selecting_enabled ) {
2019-08-08 02:09:46 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( text . size ( ) = = 1 & & text [ 0 ] . length ( ) = = 0 ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
selection . active = true ;
selection . from_line = 0 ;
selection . from_column = 0 ;
selection . selecting_line = 0 ;
selection . selecting_column = 0 ;
selection . to_line = text . size ( ) - 1 ;
selection . to_column = text [ selection . to_line ] . length ( ) ;
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_SHIFT ;
2017-03-05 16:44:50 +01:00
selection . shiftclick_left = true ;
cursor_set_line ( selection . to_line , false ) ;
cursor_set_column ( selection . to_column , false ) ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : deselect ( ) {
2017-03-05 16:44:50 +01:00
selection . active = false ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : select ( int p_from_line , int p_from_column , int p_to_line , int p_to_column ) {
2020-05-14 16:41:43 +02:00
if ( ! selecting_enabled ) {
2019-08-08 02:09:46 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( p_from_line < 0 ) {
2019-06-20 17:13:16 +02:00
p_from_line = 0 ;
2020-05-14 16:41:43 +02:00
} else if ( p_from_line > = text . size ( ) ) {
2017-03-05 16:44:50 +01:00
p_from_line = text . size ( ) - 1 ;
2020-05-14 16:41:43 +02:00
}
if ( p_from_column > = text [ p_from_line ] . length ( ) ) {
2017-03-05 16:44:50 +01:00
p_from_column = text [ p_from_line ] . length ( ) ;
2020-05-14 16:41:43 +02:00
}
if ( p_from_column < 0 ) {
2017-12-26 21:13:17 +01:00
p_from_column = 0 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( p_to_line < 0 ) {
2019-06-20 17:13:16 +02:00
p_to_line = 0 ;
2020-05-14 16:41:43 +02:00
} else if ( p_to_line > = text . size ( ) ) {
2017-03-05 16:44:50 +01:00
p_to_line = text . size ( ) - 1 ;
2020-05-14 16:41:43 +02:00
}
if ( p_to_column > = text [ p_to_line ] . length ( ) ) {
2017-03-05 16:44:50 +01:00
p_to_column = text [ p_to_line ] . length ( ) ;
2020-05-14 16:41:43 +02:00
}
if ( p_to_column < 0 ) {
2017-12-26 21:13:17 +01:00
p_to_column = 0 ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
selection . from_line = p_from_line ;
selection . from_column = p_from_column ;
selection . to_line = p_to_line ;
selection . to_column = p_to_column ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
selection . active = true ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( selection . from_line = = selection . to_line ) {
if ( selection . from_column = = selection . to_column ) {
selection . active = false ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
} else if ( selection . from_column > selection . to_column ) {
2015-08-13 00:34:07 +02:00
selection . shiftclick_left = false ;
2017-03-05 16:44:50 +01:00
SWAP ( selection . from_column , selection . to_column ) ;
2015-08-13 00:34:07 +02:00
} else {
selection . shiftclick_left = true ;
2015-01-02 19:08:40 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( selection . from_line > selection . to_line ) {
2015-08-13 00:34:07 +02:00
selection . shiftclick_left = false ;
2017-03-05 16:44:50 +01:00
SWAP ( selection . from_line , selection . to_line ) ;
SWAP ( selection . from_column , selection . to_column ) ;
2015-08-13 00:34:07 +02:00
} else {
selection . shiftclick_left = true ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2017-11-11 05:07:41 +01:00
void TextEdit : : swap_lines ( int line1 , int line2 ) {
String tmp = get_line ( line1 ) ;
String tmp2 = get_line ( line2 ) ;
set_line ( line2 , tmp ) ;
set_line ( line1 , tmp2 ) ;
}
2020-05-14 14:29:06 +02:00
2014-02-10 02:10:30 +01:00
bool TextEdit : : is_selection_active ( ) const {
2015-01-02 19:08:40 +01:00
return selection . active ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2014-02-10 02:10:30 +01:00
int TextEdit : : get_selection_from_line ( ) const {
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! selection . active , - 1 ) ;
2015-01-02 19:08:40 +01:00
return selection . from_line ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2014-02-10 02:10:30 +01:00
int TextEdit : : get_selection_from_column ( ) const {
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! selection . active , - 1 ) ;
2015-01-02 19:08:40 +01:00
return selection . from_column ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2014-02-10 02:10:30 +01:00
int TextEdit : : get_selection_to_line ( ) const {
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! selection . active , - 1 ) ;
2015-01-02 19:08:40 +01:00
return selection . to_line ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2014-02-10 02:10:30 +01:00
int TextEdit : : get_selection_to_column ( ) const {
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! selection . active , - 1 ) ;
2015-01-02 19:08:40 +01:00
return selection . to_column ;
2014-02-10 02:10:30 +01:00
}
String TextEdit : : get_selection_text ( ) const {
2020-05-14 16:41:43 +02:00
if ( ! selection . active ) {
2015-01-02 19:08:40 +01:00
return " " ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
return _base_get_text ( selection . from_line , selection . from_column , selection . to_line , selection . to_column ) ;
2014-02-10 02:10:30 +01:00
}
2014-05-06 11:36:39 +02:00
String TextEdit : : get_word_under_cursor ( ) const {
2020-09-18 09:36:10 +02:00
Vector < Vector2i > words = TS - > shaped_text_get_word_breaks ( text . get_line_data ( cursor . line ) - > get_rid ( ) ) ;
for ( int i = 0 ; i < words . size ( ) ; i + + ) {
if ( words [ i ] . x < = cursor . column & & words [ i ] . y > cursor . column ) {
return text [ cursor . line ] . substr ( words [ i ] . x , words [ i ] . y - words [ i ] . x ) ;
2020-05-14 16:41:43 +02:00
}
}
2020-09-18 09:36:10 +02:00
return " " ;
2014-05-06 11:36:39 +02:00
}
2014-02-10 02:10:30 +01:00
2016-05-28 18:25:45 +02:00
void TextEdit : : set_search_text ( const String & p_search_text ) {
search_text = p_search_text ;
}
void TextEdit : : set_search_flags ( uint32_t p_flags ) {
search_flags = p_flags ;
}
void TextEdit : : set_current_search_result ( int line , int col ) {
search_result_line = line ;
search_result_col = col ;
2016-05-30 18:15:41 +02:00
update ( ) ;
2016-05-28 18:25:45 +02:00
}
2016-03-16 22:20:42 +01:00
void TextEdit : : set_highlight_all_occurrences ( const bool p_enabled ) {
highlight_all_occurrences = p_enabled ;
update ( ) ;
}
2016-07-11 16:20:01 +02:00
bool TextEdit : : is_highlight_all_occurrences_enabled ( ) const {
return highlight_all_occurrences ;
}
2016-05-28 18:25:45 +02:00
int TextEdit : : _get_column_pos_of_word ( const String & p_key , const String & p_search , uint32_t p_search_flags , int p_from_column ) {
2016-03-16 22:20:42 +01:00
int col = - 1 ;
if ( p_key . length ( ) > 0 & & p_search . length ( ) > 0 ) {
if ( p_from_column < 0 | | p_from_column > p_search . length ( ) ) {
p_from_column = 0 ;
}
2016-05-28 18:25:45 +02:00
while ( col = = - 1 & & p_from_column < = p_search . length ( ) ) {
2017-03-05 16:44:50 +01:00
if ( p_search_flags & SEARCH_MATCH_CASE ) {
col = p_search . find ( p_key , p_from_column ) ;
2016-05-28 18:25:45 +02:00
} else {
2017-03-05 16:44:50 +01:00
col = p_search . findn ( p_key , p_from_column ) ;
2016-05-28 18:25:45 +02:00
}
2016-03-17 21:35:04 +01:00
2019-08-18 17:56:24 +02:00
// Whole words only.
2017-03-05 16:44:50 +01:00
if ( col ! = - 1 & & p_search_flags & SEARCH_WHOLE_WORDS ) {
p_from_column = col ;
2016-03-17 21:35:04 +01:00
2017-03-05 16:44:50 +01:00
if ( col > 0 & & _is_text_char ( p_search [ col - 1 ] ) ) {
2016-03-17 21:35:04 +01:00
col = - 1 ;
2017-03-05 16:44:50 +01:00
} else if ( ( col + p_key . length ( ) ) < p_search . length ( ) & & _is_text_char ( p_search [ col + p_key . length ( ) ] ) ) {
2016-03-17 21:35:04 +01:00
col = - 1 ;
}
2016-03-16 22:20:42 +01:00
}
2016-03-17 21:35:04 +01:00
2017-03-05 16:44:50 +01:00
p_from_column + = 1 ;
2016-03-16 22:20:42 +01:00
}
}
return col ;
}
2020-05-16 23:23:46 +02:00
Dictionary TextEdit : : _search_bind ( const String & p_key , uint32_t p_search_flags , int p_from_line , int p_from_column ) const {
2017-03-05 16:44:50 +01:00
int col , line ;
2019-10-31 12:35:23 +01:00
if ( search ( p_key , p_search_flags , p_from_line , p_from_column , line , col ) ) {
2020-05-16 23:23:46 +02:00
Dictionary result ;
result [ " line " ] = line ;
result [ " column " ] = col ;
2015-01-02 19:08:40 +01:00
return result ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
} else {
2020-05-16 23:23:46 +02:00
return Dictionary ( ) ;
2015-01-02 19:08:40 +01:00
}
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
bool TextEdit : : search ( const String & p_key , uint32_t p_search_flags , int p_from_line , int p_from_column , int & r_line , int & r_column ) const {
2020-05-14 16:41:43 +02:00
if ( p_key . length ( ) = = 0 ) {
2015-01-02 19:08:40 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
ERR_FAIL_INDEX_V ( p_from_line , text . size ( ) , false ) ;
ERR_FAIL_INDEX_V ( p_from_column , text [ p_from_line ] . length ( ) + 1 , false ) ;
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Search through the whole document, but start by current line.
2016-03-09 00:00:52 +01:00
2017-08-21 21:15:36 +02:00
int line = p_from_line ;
2017-03-05 16:44:50 +01:00
int pos = - 1 ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < text . size ( ) + 1 ; i + + ) {
if ( line < 0 ) {
line = text . size ( ) - 1 ;
2015-01-02 19:08:40 +01:00
}
2017-03-05 16:44:50 +01:00
if ( line = = text . size ( ) ) {
line = 0 ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
String text_line = text [ line ] ;
2017-03-05 16:44:50 +01:00
int from_column = 0 ;
if ( line = = p_from_line ) {
if ( i = = text . size ( ) ) {
2019-08-18 17:56:24 +02:00
// Wrapped.
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( p_search_flags & SEARCH_BACKWARDS ) {
from_column = text_line . length ( ) ;
2015-01-02 19:08:40 +01:00
} else {
2017-03-05 16:44:50 +01:00
from_column = 0 ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
} else {
2017-03-05 16:44:50 +01:00
from_column = p_from_column ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
} else {
2020-05-14 16:41:43 +02:00
if ( p_search_flags & SEARCH_BACKWARDS ) {
2017-03-05 16:44:50 +01:00
from_column = text_line . length ( ) - 1 ;
2020-05-14 16:41:43 +02:00
} else {
2017-03-05 16:44:50 +01:00
from_column = 0 ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
pos = - 1 ;
2016-03-09 00:00:52 +01:00
2018-08-27 19:01:12 +02:00
int pos_from = ( p_search_flags & SEARCH_BACKWARDS ) ? text_line . length ( ) : 0 ;
2017-03-05 16:44:50 +01:00
int last_pos = - 1 ;
2016-03-09 00:00:52 +01:00
2018-01-08 22:50:33 +01:00
while ( true ) {
2018-08-27 19:01:12 +02:00
if ( p_search_flags & SEARCH_BACKWARDS ) {
while ( ( last_pos = ( p_search_flags & SEARCH_MATCH_CASE ) ? text_line . rfind ( p_key , pos_from ) : text_line . rfindn ( p_key , pos_from ) ) ! = - 1 ) {
if ( last_pos < = from_column ) {
pos = last_pos ;
2018-01-08 22:50:33 +01:00
break ;
2018-08-27 19:01:12 +02:00
}
pos_from = last_pos - p_key . length ( ) ;
2019-09-02 14:15:55 +02:00
if ( pos_from < 0 ) {
break ;
}
2018-08-27 19:01:12 +02:00
}
} else {
while ( ( last_pos = ( p_search_flags & SEARCH_MATCH_CASE ) ? text_line . find ( p_key , pos_from ) : text_line . findn ( p_key , pos_from ) ) ! = - 1 ) {
2018-01-08 22:50:33 +01:00
if ( last_pos > = from_column ) {
pos = last_pos ;
break ;
}
2018-08-27 19:01:12 +02:00
pos_from = last_pos + p_key . length ( ) ;
2016-05-28 18:23:49 +02:00
}
}
2016-03-09 00:00:52 +01:00
2018-01-08 22:50:33 +01:00
bool is_match = true ;
if ( pos ! = - 1 & & ( p_search_flags & SEARCH_WHOLE_WORDS ) ) {
2019-08-18 17:56:24 +02:00
// Validate for whole words.
2020-05-14 16:41:43 +02:00
if ( pos > 0 & & _is_text_char ( text_line [ pos - 1 ] ) ) {
2018-01-08 22:50:33 +01:00
is_match = false ;
2020-05-14 16:41:43 +02:00
} else if ( pos + p_key . length ( ) < text_line . length ( ) & & _is_text_char ( text_line [ pos + p_key . length ( ) ] ) ) {
2018-01-08 22:50:33 +01:00
is_match = false ;
2020-05-14 16:41:43 +02:00
}
2018-01-08 22:50:33 +01:00
}
2018-08-27 19:01:12 +02:00
if ( pos_from = = - 1 ) {
pos = - 1 ;
}
2018-01-08 22:50:33 +01:00
if ( is_match | | last_pos = = - 1 | | pos = = - 1 ) {
break ;
}
2016-03-09 00:00:52 +01:00
2018-08-27 19:01:12 +02:00
pos_from = ( p_search_flags & SEARCH_BACKWARDS ) ? pos - 1 : pos + 1 ;
2018-01-08 22:50:33 +01:00
pos = - 1 ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( pos ! = - 1 ) {
2015-01-02 19:08:40 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( p_search_flags & SEARCH_BACKWARDS ) {
2015-01-02 19:08:40 +01:00
line - - ;
2020-05-14 16:41:43 +02:00
} else {
2015-01-02 19:08:40 +01:00
line + + ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( pos = = - 1 ) {
r_line = - 1 ;
r_column = - 1 ;
2015-01-02 19:08:40 +01:00
return false ;
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
r_line = line ;
r_column = pos ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
return true ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : _cursor_changed_emit ( ) {
2015-01-02 19:08:40 +01:00
emit_signal ( " cursor_changed " ) ;
2017-03-05 16:44:50 +01:00
cursor_changed_dirty = false ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : _text_changed_emit ( ) {
2015-01-02 19:08:40 +01:00
emit_signal ( " text_changed " ) ;
2017-03-05 16:44:50 +01:00
text_changed_dirty = false ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : set_line_as_marked ( int p_line , bool p_marked ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
text . set_marked ( p_line , p_marked ) ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-11-13 00:12:17 +01:00
void TextEdit : : set_line_as_hidden ( int p_line , bool p_hidden ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
2020-05-14 16:41:43 +02:00
if ( is_hiding_enabled ( ) | | ! p_hidden ) {
2017-11-13 00:12:17 +01:00
text . set_hidden ( p_line , p_hidden ) ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
update ( ) ;
}
bool TextEdit : : is_line_hidden ( int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , false ) ;
return text . is_hidden ( p_line ) ;
}
2017-11-16 05:00:27 +01:00
void TextEdit : : fold_all_lines ( ) {
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
fold_line ( i ) ;
}
_update_scrollbars ( ) ;
update ( ) ;
}
2017-11-13 00:12:17 +01:00
void TextEdit : : unhide_all_lines ( ) {
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
text . set_hidden ( i , false ) ;
}
2017-11-16 05:00:27 +01:00
_update_scrollbars ( ) ;
2017-11-13 00:12:17 +01:00
update ( ) ;
}
2018-01-26 02:41:17 +01:00
int TextEdit : : num_lines_from ( int p_line_from , int visible_amount ) const {
2019-08-18 17:56:24 +02:00
// Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines).
2018-01-26 02:41:17 +01:00
ERR_FAIL_INDEX_V ( p_line_from , text . size ( ) , ABS ( visible_amount ) ) ;
2017-11-13 00:12:17 +01:00
2020-05-14 16:41:43 +02:00
if ( ! is_hiding_enabled ( ) ) {
2018-01-26 02:41:17 +01:00
return ABS ( visible_amount ) ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
2017-11-13 00:12:17 +01:00
int num_visible = 0 ;
int num_total = 0 ;
2018-01-26 02:41:17 +01:00
if ( visible_amount > = 0 ) {
2017-11-13 00:12:17 +01:00
for ( int i = p_line_from ; i < text . size ( ) ; i + + ) {
num_total + + ;
2018-01-26 02:41:17 +01:00
if ( ! is_line_hidden ( i ) ) {
2017-11-13 00:12:17 +01:00
num_visible + + ;
2018-01-26 02:41:17 +01:00
}
2020-05-14 16:41:43 +02:00
if ( num_visible > = visible_amount ) {
2017-11-13 00:12:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
}
} else {
2018-01-26 02:41:17 +01:00
visible_amount = ABS ( visible_amount ) ;
2017-11-13 00:12:17 +01:00
for ( int i = p_line_from ; i > = 0 ; i - - ) {
num_total + + ;
2018-01-26 02:41:17 +01:00
if ( ! is_line_hidden ( i ) ) {
2017-11-13 00:12:17 +01:00
num_visible + + ;
2018-01-26 02:41:17 +01:00
}
2020-05-14 16:41:43 +02:00
if ( num_visible > = visible_amount ) {
2017-11-13 00:12:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
}
}
return num_total ;
}
2018-01-26 02:41:17 +01:00
int TextEdit : : num_lines_from_rows ( int p_line_from , int p_wrap_index_from , int visible_amount , int & wrap_index ) const {
2019-08-18 17:56:24 +02:00
// Returns the number of lines (hidden and unhidden) from (p_line_from + p_wrap_index_from) row to (p_line_from + visible_amount of unhidden and wrapped rows).
// Wrap index is set to the wrap index of the last line.
2018-01-26 02:41:17 +01:00
wrap_index = 0 ;
ERR_FAIL_INDEX_V ( p_line_from , text . size ( ) , ABS ( visible_amount ) ) ;
2017-12-26 07:53:16 +01:00
2020-05-14 16:41:43 +02:00
if ( ! is_hiding_enabled ( ) & & ! is_wrap_enabled ( ) ) {
2018-01-26 02:41:17 +01:00
return ABS ( visible_amount ) ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
int num_visible = 0 ;
int num_total = 0 ;
if ( visible_amount = = 0 ) {
num_total = 0 ;
wrap_index = 0 ;
} else if ( visible_amount > 0 ) {
int i ;
num_visible - = p_wrap_index_from ;
for ( i = p_line_from ; i < text . size ( ) ; i + + ) {
num_total + + ;
if ( ! is_line_hidden ( i ) ) {
num_visible + + ;
num_visible + = times_line_wraps ( i ) ;
}
2020-05-14 16:41:43 +02:00
if ( num_visible > = visible_amount ) {
2018-01-26 02:41:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
wrap_index = times_line_wraps ( MIN ( i , text . size ( ) - 1 ) ) - MAX ( 0 , num_visible - visible_amount ) ;
2018-01-26 02:41:17 +01:00
} else {
visible_amount = ABS ( visible_amount ) ;
int i ;
num_visible - = times_line_wraps ( p_line_from ) - p_wrap_index_from ;
for ( i = p_line_from ; i > = 0 ; i - - ) {
num_total + + ;
if ( ! is_line_hidden ( i ) ) {
num_visible + + ;
num_visible + = times_line_wraps ( i ) ;
}
2020-05-14 16:41:43 +02:00
if ( num_visible > = visible_amount ) {
2018-01-26 02:41:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2018-01-26 02:41:17 +01:00
}
2020-09-18 09:36:10 +02:00
wrap_index = MAX ( 0 , num_visible - visible_amount ) ;
2018-01-26 02:41:17 +01:00
}
wrap_index = MAX ( wrap_index , 0 ) ;
return num_total ;
}
int TextEdit : : get_last_unhidden_line ( ) const {
2019-08-18 17:56:24 +02:00
// Returns the last line in the text that is not hidden.
2020-05-14 16:41:43 +02:00
if ( ! is_hiding_enabled ( ) ) {
2018-01-26 02:41:17 +01:00
return text . size ( ) - 1 ;
2020-05-14 16:41:43 +02:00
}
2017-12-26 07:53:16 +01:00
2018-01-26 02:41:17 +01:00
int last_line ;
for ( last_line = text . size ( ) - 1 ; last_line > 0 ; last_line - - ) {
if ( ! is_line_hidden ( last_line ) ) {
break ;
}
2017-12-26 07:53:16 +01:00
}
2018-01-26 02:41:17 +01:00
return last_line ;
2017-12-26 07:53:16 +01:00
}
2017-11-28 12:03:46 +01:00
int TextEdit : : get_indent_level ( int p_line ) const {
2017-11-13 00:12:17 +01:00
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , 0 ) ;
2019-08-18 17:56:24 +02:00
// Counts number of tabs and spaces before line starts.
2017-11-28 12:03:46 +01:00
int tab_count = 0 ;
2017-11-13 00:12:17 +01:00
int whitespace_count = 0 ;
int line_length = text [ p_line ] . size ( ) ;
for ( int i = 0 ; i < line_length - 1 ; i + + ) {
if ( text [ p_line ] [ i ] = = ' \t ' ) {
2017-11-28 12:03:46 +01:00
tab_count + + ;
2017-11-13 00:12:17 +01:00
} else if ( text [ p_line ] [ i ] = = ' ' ) {
whitespace_count + + ;
} else {
break ;
}
}
2018-01-26 02:41:17 +01:00
return tab_count * indent_size + whitespace_count ;
2017-11-13 00:12:17 +01:00
}
2018-01-12 06:25:04 +01:00
bool TextEdit : : is_line_comment ( int p_line ) const {
2019-08-18 17:56:24 +02:00
// Checks to see if this line is the start of a comment.
2018-01-12 06:25:04 +01:00
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , false ) ;
int line_length = text [ p_line ] . size ( ) ;
for ( int i = 0 ; i < line_length - 1 ; i + + ) {
2020-05-03 18:08:15 +02:00
if ( _is_whitespace ( text [ p_line ] [ i ] ) ) {
2018-01-12 06:25:04 +01:00
continue ;
}
2020-05-03 18:08:15 +02:00
if ( _is_symbol ( text [ p_line ] [ i ] ) ) {
if ( text [ p_line ] [ i ] = = ' \\ ' ) {
i + + ; // Skip quoted anything.
continue ;
}
return text [ p_line ] [ i ] = = ' # ' | | ( i + 1 < line_length & & text [ p_line ] [ i ] = = ' / ' & & text [ p_line ] [ i + 1 ] = = ' / ' ) ;
}
break ;
2018-01-12 06:25:04 +01:00
}
return false ;
}
2017-11-13 00:12:17 +01:00
bool TextEdit : : can_fold ( int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , false ) ;
2020-05-14 16:41:43 +02:00
if ( ! is_hiding_enabled ( ) ) {
2017-11-13 00:12:17 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
if ( p_line + 1 > = text . size ( ) ) {
2017-11-13 00:12:17 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
if ( text [ p_line ] . strip_edges ( ) . size ( ) = = 0 ) {
2017-11-13 00:12:17 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
if ( is_folded ( p_line ) ) {
2017-11-13 00:12:17 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
if ( is_line_hidden ( p_line ) ) {
2017-11-16 05:00:27 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
if ( is_line_comment ( p_line ) ) {
2018-01-12 06:25:04 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
2017-11-28 12:03:46 +01:00
int start_indent = get_indent_level ( p_line ) ;
2017-11-13 00:12:17 +01:00
for ( int i = p_line + 1 ; i < text . size ( ) ; i + + ) {
2020-05-14 16:41:43 +02:00
if ( text [ i ] . strip_edges ( ) . size ( ) = = 0 ) {
2017-11-13 00:12:17 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-11-28 12:03:46 +01:00
int next_indent = get_indent_level ( i ) ;
2018-01-12 06:25:04 +01:00
if ( is_line_comment ( i ) ) {
continue ;
} else if ( next_indent > start_indent ) {
2017-11-13 00:12:17 +01:00
return true ;
2018-01-12 06:25:04 +01:00
} else {
2017-11-13 00:12:17 +01:00
return false ;
2018-01-12 06:25:04 +01:00
}
2017-11-13 00:12:17 +01:00
}
return false ;
}
bool TextEdit : : is_folded ( int p_line ) const {
ERR_FAIL_INDEX_V ( p_line , text . size ( ) , false ) ;
2020-05-14 16:41:43 +02:00
if ( p_line + 1 > = text . size ( ) ) {
2017-11-13 00:12:17 +01:00
return false ;
2020-05-14 16:41:43 +02:00
}
2019-06-26 15:08:25 +02:00
return ! is_line_hidden ( p_line ) & & is_line_hidden ( p_line + 1 ) ;
2017-11-13 00:12:17 +01:00
}
2019-04-13 13:43:35 +02:00
Vector < int > TextEdit : : get_folded_lines ( ) const {
Vector < int > folded_lines ;
for ( int i = 0 ; i < text . size ( ) ; i + + ) {
if ( is_folded ( i ) ) {
folded_lines . push_back ( i ) ;
}
}
return folded_lines ;
}
2017-11-13 00:12:17 +01:00
void TextEdit : : fold_line ( int p_line ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
2020-05-14 16:41:43 +02:00
if ( ! is_hiding_enabled ( ) ) {
2017-11-13 00:12:17 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
if ( ! can_fold ( p_line ) ) {
2017-11-13 00:12:17 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
2019-08-18 17:56:24 +02:00
// Hide lines below this one.
2017-11-28 12:03:46 +01:00
int start_indent = get_indent_level ( p_line ) ;
int last_line = start_indent ;
2017-11-13 00:12:17 +01:00
for ( int i = p_line + 1 ; i < text . size ( ) ; i + + ) {
2017-11-28 12:03:46 +01:00
if ( text [ i ] . strip_edges ( ) . size ( ) ! = 0 ) {
2018-01-12 06:25:04 +01:00
if ( is_line_comment ( i ) ) {
continue ;
} else if ( get_indent_level ( i ) > start_indent ) {
2017-11-28 12:03:46 +01:00
last_line = i ;
} else {
break ;
2017-11-13 00:12:17 +01:00
}
}
}
2017-11-28 12:03:46 +01:00
for ( int i = p_line + 1 ; i < = last_line ; i + + ) {
set_line_as_hidden ( i , true ) ;
}
2017-11-16 05:00:27 +01:00
2019-08-18 17:56:24 +02:00
// Fix selection.
2017-11-16 05:00:27 +01:00
if ( is_selection_active ( ) ) {
if ( is_line_hidden ( selection . from_line ) & & is_line_hidden ( selection . to_line ) ) {
deselect ( ) ;
} else if ( is_line_hidden ( selection . from_line ) ) {
select ( p_line , 9999 , selection . to_line , selection . to_column ) ;
} else if ( is_line_hidden ( selection . to_line ) ) {
select ( selection . from_line , selection . from_column , p_line , 9999 ) ;
}
}
2019-08-18 17:56:24 +02:00
// Reset cursor.
2017-11-16 05:00:27 +01:00
if ( is_line_hidden ( cursor . line ) ) {
cursor_set_line ( p_line , false , false ) ;
cursor_set_column ( get_line ( p_line ) . length ( ) , false ) ;
}
_update_scrollbars ( ) ;
2017-11-13 00:12:17 +01:00
update ( ) ;
}
void TextEdit : : unfold_line ( int p_line ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
2020-05-14 16:41:43 +02:00
if ( ! is_folded ( p_line ) & & ! is_line_hidden ( p_line ) ) {
2017-11-16 05:00:27 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2019-10-14 11:40:55 +02:00
int fold_start ;
2017-11-13 00:12:17 +01:00
for ( fold_start = p_line ; fold_start > 0 ; fold_start - - ) {
2020-05-14 16:41:43 +02:00
if ( is_folded ( fold_start ) ) {
2017-11-13 00:12:17 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
}
fold_start = is_folded ( fold_start ) ? fold_start : p_line ;
for ( int i = fold_start + 1 ; i < text . size ( ) ; i + + ) {
if ( is_line_hidden ( i ) ) {
set_line_as_hidden ( i , false ) ;
} else {
break ;
}
}
2017-11-16 05:00:27 +01:00
_update_scrollbars ( ) ;
2017-11-13 00:12:17 +01:00
update ( ) ;
}
2017-12-08 19:17:10 +01:00
void TextEdit : : toggle_fold_line ( int p_line ) {
ERR_FAIL_INDEX ( p_line , text . size ( ) ) ;
2020-05-14 16:41:43 +02:00
if ( ! is_folded ( p_line ) ) {
2017-12-08 19:17:10 +01:00
fold_line ( p_line ) ;
2020-05-14 16:41:43 +02:00
} else {
2017-12-08 19:17:10 +01:00
unfold_line ( p_line ) ;
2020-05-14 16:41:43 +02:00
}
2017-12-08 19:17:10 +01:00
}
2014-02-10 02:10:30 +01:00
int TextEdit : : get_line_count ( ) const {
2015-01-02 19:08:40 +01:00
return text . size ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : _do_text_op ( const TextOperation & p_op , bool p_reverse ) {
ERR_FAIL_COND ( p_op . type = = TextOperation : : TYPE_NONE ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
bool insert = p_op . type = = TextOperation : : TYPE_INSERT ;
2020-05-14 16:41:43 +02:00
if ( p_reverse ) {
2017-03-05 16:44:50 +01:00
insert = ! insert ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
if ( insert ) {
int check_line ;
int check_column ;
2017-03-05 16:44:50 +01:00
_base_insert_text ( p_op . from_line , p_op . from_column , p_op . text , check_line , check_column ) ;
2019-08-18 17:56:24 +02:00
ERR_FAIL_COND ( check_line ! = p_op . to_line ) ; // BUG.
ERR_FAIL_COND ( check_column ! = p_op . to_column ) ; // BUG.
2015-01-02 19:08:40 +01:00
} else {
2017-03-05 16:44:50 +01:00
_base_remove_text ( p_op . from_line , p_op . from_column , p_op . to_line , p_op . to_column ) ;
2015-01-02 19:08:40 +01:00
}
2014-02-10 02:10:30 +01:00
}
void TextEdit : : _clear_redo ( ) {
2020-05-14 16:41:43 +02:00
if ( undo_stack_pos = = nullptr ) {
2019-08-18 17:56:24 +02:00
return ; // Nothing to clear.
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
while ( undo_stack_pos ) {
2015-01-02 19:08:40 +01:00
List < TextOperation > : : Element * elem = undo_stack_pos ;
2017-03-05 16:44:50 +01:00
undo_stack_pos = undo_stack_pos - > next ( ) ;
2015-01-02 19:08:40 +01:00
undo_stack . erase ( elem ) ;
}
2014-02-10 02:10:30 +01:00
}
void TextEdit : : undo ( ) {
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2016-03-09 00:00:52 +01:00
2020-04-02 01:20:12 +02:00
if ( undo_stack_pos = = nullptr ) {
2020-05-14 16:41:43 +02:00
if ( ! undo_stack . size ( ) ) {
2019-08-18 17:56:24 +02:00
return ; // Nothing to undo.
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
undo_stack_pos = undo_stack . back ( ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
} else if ( undo_stack_pos = = undo_stack . front ( ) ) {
2019-08-18 17:56:24 +02:00
return ; // At the bottom of the undo stack.
2020-05-14 16:41:43 +02:00
} else {
2017-03-05 16:44:50 +01:00
undo_stack_pos = undo_stack_pos - > prev ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2018-01-13 11:38:01 +01:00
deselect ( ) ;
2016-03-13 21:08:12 +01:00
TextOperation op = undo_stack_pos - > get ( ) ;
_do_text_op ( op , true ) ;
2020-05-14 16:41:43 +02:00
if ( op . type ! = TextOperation : : TYPE_INSERT & & ( op . from_line ! = op . to_line | | op . to_column ! = op . from_column + 1 ) ) {
2019-04-21 20:19:31 +02:00
select ( op . from_line , op . from_column , op . to_line , op . to_column ) ;
2020-05-14 16:41:43 +02:00
}
2019-04-21 20:19:31 +02:00
2017-03-05 16:44:50 +01:00
current_op . version = op . prev_version ;
if ( undo_stack_pos - > get ( ) . chain_backward ) {
while ( true ) {
2016-06-14 13:48:34 +02:00
ERR_BREAK ( ! undo_stack_pos - > prev ( ) ) ;
2015-01-02 19:08:40 +01:00
undo_stack_pos = undo_stack_pos - > prev ( ) ;
2016-03-13 21:08:12 +01:00
op = undo_stack_pos - > get ( ) ;
_do_text_op ( op , true ) ;
current_op . version = op . prev_version ;
2016-06-14 13:48:34 +02:00
if ( undo_stack_pos - > get ( ) . chain_forward ) {
break ;
}
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2019-10-13 16:34:28 +02:00
_update_scrollbars ( ) ;
2016-06-13 21:40:28 +02:00
if ( undo_stack_pos - > get ( ) . type = = TextOperation : : TYPE_REMOVE ) {
cursor_set_line ( undo_stack_pos - > get ( ) . to_line ) ;
cursor_set_column ( undo_stack_pos - > get ( ) . to_column ) ;
2016-06-13 21:55:26 +02:00
_cancel_code_hint ( ) ;
2016-06-13 21:40:28 +02:00
} else {
cursor_set_line ( undo_stack_pos - > get ( ) . from_line ) ;
cursor_set_column ( undo_stack_pos - > get ( ) . from_column ) ;
}
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : redo ( ) {
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2016-03-09 00:00:52 +01:00
2020-05-14 16:41:43 +02:00
if ( undo_stack_pos = = nullptr ) {
2019-08-18 17:56:24 +02:00
return ; // Nothing to do.
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2018-01-13 11:38:01 +01:00
deselect ( ) ;
2016-03-13 21:08:12 +01:00
TextOperation op = undo_stack_pos - > get ( ) ;
_do_text_op ( op , false ) ;
current_op . version = op . version ;
2017-03-05 16:44:50 +01:00
if ( undo_stack_pos - > get ( ) . chain_forward ) {
while ( true ) {
2016-06-14 13:48:34 +02:00
ERR_BREAK ( ! undo_stack_pos - > next ( ) ) ;
2017-03-05 16:44:50 +01:00
undo_stack_pos = undo_stack_pos - > next ( ) ;
2016-03-13 21:08:12 +01:00
op = undo_stack_pos - > get ( ) ;
_do_text_op ( op , false ) ;
current_op . version = op . version ;
2020-05-14 16:41:43 +02:00
if ( undo_stack_pos - > get ( ) . chain_backward ) {
2016-06-14 13:48:34 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2016-06-14 13:48:34 +02:00
}
2015-01-02 19:08:40 +01:00
}
2019-10-13 16:34:28 +02:00
_update_scrollbars ( ) ;
2016-03-13 21:08:12 +01:00
cursor_set_line ( undo_stack_pos - > get ( ) . to_line ) ;
cursor_set_column ( undo_stack_pos - > get ( ) . to_column ) ;
2017-03-05 16:44:50 +01:00
undo_stack_pos = undo_stack_pos - > next ( ) ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : clear_undo_history ( ) {
2017-03-05 16:44:50 +01:00
saved_version = 0 ;
current_op . type = TextOperation : : TYPE_NONE ;
2020-04-02 01:20:12 +02:00
undo_stack_pos = nullptr ;
2015-01-02 19:08:40 +01:00
undo_stack . clear ( ) ;
2014-02-10 02:10:30 +01:00
}
2016-04-06 08:36:29 +02:00
void TextEdit : : begin_complex_operation ( ) {
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
2017-03-05 16:44:50 +01:00
next_operation_is_complex = true ;
2014-04-26 14:42:19 +02:00
}
2014-02-10 02:10:30 +01:00
2016-04-06 08:36:29 +02:00
void TextEdit : : end_complex_operation ( ) {
2015-01-02 19:08:40 +01:00
_push_current_op ( ) ;
ERR_FAIL_COND ( undo_stack . size ( ) = = 0 ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( undo_stack . back ( ) - > get ( ) . chain_forward ) {
undo_stack . back ( ) - > get ( ) . chain_forward = false ;
2015-01-02 19:08:40 +01:00
return ;
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
undo_stack . back ( ) - > get ( ) . chain_backward = true ;
2014-04-26 14:42:19 +02:00
}
2014-02-10 02:10:30 +01:00
2014-04-26 14:42:19 +02:00
void TextEdit : : _push_current_op ( ) {
2020-05-14 16:41:43 +02:00
if ( current_op . type = = TextOperation : : TYPE_NONE ) {
2019-08-18 17:56:24 +02:00
return ; // Nothing to do.
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
if ( next_operation_is_complex ) {
current_op . chain_forward = true ;
next_operation_is_complex = false ;
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
undo_stack . push_back ( current_op ) ;
2017-03-05 16:44:50 +01:00
current_op . type = TextOperation : : TYPE_NONE ;
current_op . text = " " ;
current_op . chain_forward = false ;
2020-04-13 14:30:56 +02:00
if ( undo_stack . size ( ) > undo_stack_max_size ) {
undo_stack . pop_front ( ) ;
}
2014-02-10 02:10:30 +01:00
}
2017-04-17 15:24:30 +02:00
void TextEdit : : set_indent_using_spaces ( const bool p_use_spaces ) {
indent_using_spaces = p_use_spaces ;
}
bool TextEdit : : is_indent_using_spaces ( ) const {
return indent_using_spaces ;
}
void TextEdit : : set_indent_size ( const int p_size ) {
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_MSG ( p_size < = 0 , " Indend size must be greater than 0. " ) ;
2020-09-18 09:36:10 +02:00
if ( indent_size ! = p_size ) {
indent_size = p_size ;
text . set_indent_size ( p_size ) ;
text . invalidate_all_lines ( ) ;
}
2017-04-17 15:24:30 +02:00
space_indent = " " ;
for ( int i = 0 ; i < p_size ; i + + ) {
space_indent + = " " ;
}
2016-03-11 19:10:01 +01:00
update ( ) ;
}
2018-05-03 19:03:20 +02:00
int TextEdit : : get_indent_size ( ) {
return indent_size ;
}
2014-02-10 02:10:30 +01:00
void TextEdit : : set_draw_tabs ( bool p_draw ) {
2017-03-05 16:44:50 +01:00
draw_tabs = p_draw ;
2019-04-24 09:59:17 +02:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
bool TextEdit : : is_drawing_tabs ( ) const {
2015-01-02 19:08:40 +01:00
return draw_tabs ;
2014-02-10 02:10:30 +01:00
}
2019-04-24 01:33:20 +02:00
void TextEdit : : set_draw_spaces ( bool p_draw ) {
draw_spaces = p_draw ;
2020-09-18 09:36:10 +02:00
update ( ) ;
2019-04-24 01:33:20 +02:00
}
bool TextEdit : : is_drawing_spaces ( ) const {
return draw_spaces ;
}
2017-09-27 19:24:05 +02:00
void TextEdit : : set_override_selected_font_color ( bool p_override_selected_font_color ) {
override_selected_font_color = p_override_selected_font_color ;
}
2018-01-26 02:41:17 +01:00
2017-09-27 19:24:05 +02:00
bool TextEdit : : is_overriding_selected_font_color ( ) const {
return override_selected_font_color ;
}
2016-03-31 21:49:30 +02:00
void TextEdit : : set_insert_mode ( bool p_enabled ) {
insert_mode = p_enabled ;
update ( ) ;
}
bool TextEdit : : is_insert_mode ( ) const {
return insert_mode ;
}
2017-09-06 03:02:51 +02:00
bool TextEdit : : is_insert_text_operation ( ) {
return ( current_op . type = = TextOperation : : TYPE_INSERT ) ;
}
2014-02-10 02:10:30 +01:00
uint32_t TextEdit : : get_version ( ) const {
2015-01-02 19:08:40 +01:00
return current_op . version ;
2014-02-10 02:10:30 +01:00
}
2018-01-26 02:41:17 +01:00
2014-02-10 02:10:30 +01:00
uint32_t TextEdit : : get_saved_version ( ) const {
2015-01-02 19:08:40 +01:00
return saved_version ;
2014-02-10 02:10:30 +01:00
}
2018-01-26 02:41:17 +01:00
2014-02-10 02:10:30 +01:00
void TextEdit : : tag_saved_version ( ) {
2017-03-05 16:44:50 +01:00
saved_version = get_version ( ) ;
2014-02-10 02:10:30 +01:00
}
2018-01-26 02:41:17 +01:00
double TextEdit : : get_scroll_pos_for_line ( int p_line , int p_wrap_index ) const {
2020-05-14 16:41:43 +02:00
if ( ! is_wrap_enabled ( ) & & ! is_hiding_enabled ( ) ) {
2018-01-26 02:41:17 +01:00
return p_line ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// Count the number of visible lines up to this line.
2018-01-26 02:41:17 +01:00
double new_line_scroll_pos = 0 ;
int to = CLAMP ( p_line , 0 , text . size ( ) - 1 ) ;
for ( int i = 0 ; i < to ; i + + ) {
if ( ! text . is_hidden ( i ) ) {
new_line_scroll_pos + + ;
new_line_scroll_pos + = times_line_wraps ( i ) ;
2017-10-08 15:29:27 +02:00
}
}
2018-01-26 02:41:17 +01:00
new_line_scroll_pos + = p_wrap_index ;
return new_line_scroll_pos ;
}
void TextEdit : : set_line_as_first_visible ( int p_line , int p_wrap_index ) {
set_v_scroll ( get_scroll_pos_for_line ( p_line , p_wrap_index ) ) ;
}
void TextEdit : : set_line_as_center_visible ( int p_line , int p_wrap_index ) {
int visible_rows = get_visible_rows ( ) ;
int wi ;
int first_line = p_line - num_lines_from_rows ( p_line , p_wrap_index , - visible_rows / 2 , wi ) + 1 ;
set_v_scroll ( get_scroll_pos_for_line ( first_line , wi ) ) ;
}
void TextEdit : : set_line_as_last_visible ( int p_line , int p_wrap_index ) {
int wi ;
int first_line = p_line - num_lines_from_rows ( p_line , p_wrap_index , - get_visible_rows ( ) - 1 , wi ) + 1 ;
set_v_scroll ( get_scroll_pos_for_line ( first_line , wi ) + get_visible_rows_offset ( ) ) ;
}
int TextEdit : : get_first_visible_line ( ) const {
return CLAMP ( cursor . line_ofs , 0 , text . size ( ) - 1 ) ;
}
int TextEdit : : get_last_visible_line ( ) const {
int first_vis_line = get_first_visible_line ( ) ;
int last_vis_line = 0 ;
int wi ;
last_vis_line = first_vis_line + num_lines_from_rows ( first_vis_line , cursor . wrap_ofs , get_visible_rows ( ) + 1 , wi ) - 1 ;
last_vis_line = CLAMP ( last_vis_line , 0 , text . size ( ) - 1 ) ;
return last_vis_line ;
}
int TextEdit : : get_last_visible_line_wrap_index ( ) const {
int first_vis_line = get_first_visible_line ( ) ;
int wi ;
2018-03-26 01:36:34 +02:00
num_lines_from_rows ( first_vis_line , cursor . wrap_ofs , get_visible_rows ( ) + 1 , wi ) ;
2018-01-26 02:41:17 +01:00
return wi ;
}
double TextEdit : : get_visible_rows_offset ( ) const {
2019-08-25 15:49:13 +02:00
double total = _get_control_height ( ) ;
2018-01-26 02:41:17 +01:00
total / = ( double ) get_row_height ( ) ;
total = total - floor ( total ) ;
total = - CLAMP ( total , 0.001 , 1 ) + 1 ;
return total ;
}
double TextEdit : : get_v_scroll_offset ( ) const {
double val = get_v_scroll ( ) - floor ( get_v_scroll ( ) ) ;
return CLAMP ( val , 0 , 1 ) ;
}
double TextEdit : : get_v_scroll ( ) const {
return v_scroll - > get_value ( ) ;
}
void TextEdit : : set_v_scroll ( double p_scroll ) {
2017-01-04 05:16:14 +01:00
v_scroll - > set_value ( p_scroll ) ;
2018-01-26 02:41:17 +01:00
int max_v_scroll = v_scroll - > get_max ( ) - v_scroll - > get_page ( ) ;
2020-05-14 16:41:43 +02:00
if ( p_scroll > = max_v_scroll - 1.0 ) {
2018-01-26 02:41:17 +01:00
_scroll_moved ( v_scroll - > get_value ( ) ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
}
int TextEdit : : get_h_scroll ( ) const {
2017-01-04 05:16:14 +01:00
return h_scroll - > get_value ( ) ;
2014-02-10 02:10:30 +01:00
}
2018-01-26 02:41:17 +01:00
2014-02-10 02:10:30 +01:00
void TextEdit : : set_h_scroll ( int p_scroll ) {
2018-01-26 02:41:17 +01:00
if ( p_scroll < 0 ) {
p_scroll = 0 ;
}
2017-01-04 05:16:14 +01:00
h_scroll - > set_value ( p_scroll ) ;
2014-02-10 02:10:30 +01:00
}
2017-08-19 16:23:45 +02:00
void TextEdit : : set_smooth_scroll_enabled ( bool p_enable ) {
v_scroll - > set_smooth_scroll_enabled ( p_enable ) ;
smooth_scroll_enabled = p_enable ;
}
bool TextEdit : : is_smooth_scroll_enabled ( ) const {
return smooth_scroll_enabled ;
}
2017-08-21 20:13:24 +02:00
void TextEdit : : set_v_scroll_speed ( float p_speed ) {
v_scroll_speed = p_speed ;
}
float TextEdit : : get_v_scroll_speed ( ) const {
return v_scroll_speed ;
}
2017-03-05 16:44:50 +01:00
void TextEdit : : set_completion ( bool p_enabled , const Vector < String > & p_prefixes ) {
2015-01-02 19:08:40 +01:00
completion_prefixes . clear ( ) ;
2017-03-05 16:44:50 +01:00
completion_enabled = p_enabled ;
2020-05-14 16:41:43 +02:00
for ( int i = 0 ; i < p_prefixes . size ( ) ; i + + ) {
2015-01-02 19:08:40 +01:00
completion_prefixes . insert ( p_prefixes [ i ] ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
}
void TextEdit : : _confirm_completion ( ) {
2016-06-13 21:55:26 +02:00
begin_complex_operation ( ) ;
2016-03-09 00:00:52 +01:00
2016-06-13 21:55:26 +02:00
_remove_text ( cursor . line , cursor . column - completion_base . length ( ) , cursor . line , cursor . column ) ;
cursor_set_column ( cursor . column - completion_base . length ( ) , false ) ;
2019-06-13 11:32:03 +02:00
insert_text_at_cursor ( completion_current . insert_text ) ;
2016-06-13 21:55:26 +02:00
2019-04-05 04:06:44 +02:00
// When inserted into the middle of an existing string/method, don't add an unnecessary quote/bracket.
2018-05-26 23:13:42 +02:00
String line = text [ cursor . line ] ;
2020-07-27 12:43:20 +02:00
char32_t next_char = line [ cursor . column ] ;
char32_t last_completion_char = completion_current . insert_text [ completion_current . insert_text . length ( ) - 1 ] ;
char32_t last_completion_char_display = completion_current . display [ completion_current . display . length ( ) - 1 ] ;
2018-05-26 23:13:42 +02:00
2019-10-27 23:21:25 +01:00
if ( ( last_completion_char = = ' " ' | | last_completion_char = = ' \' ' ) & & ( last_completion_char = = next_char | | last_completion_char_display = = next_char ) ) {
2019-09-18 16:26:33 +02:00
_remove_text ( cursor . line , cursor . column , cursor . line , cursor . column + 1 ) ;
2018-05-26 23:13:42 +02:00
}
2019-04-05 04:06:44 +02:00
if ( last_completion_char = = ' ( ' ) {
if ( next_char = = last_completion_char ) {
_base_remove_text ( cursor . line , cursor . column - 1 , cursor . line , cursor . column ) ;
} else if ( auto_brace_completion_enabled ) {
insert_text_at_cursor ( " ) " ) ;
cursor . column - - ;
}
} else if ( last_completion_char = = ' ) ' & & next_char = = ' ( ' ) {
_base_remove_text ( cursor . line , cursor . column - 2 , cursor . line , cursor . column ) ;
if ( line [ cursor . column + 1 ] ! = ' ) ' ) {
cursor . column - - ;
}
2015-01-02 19:08:40 +01:00
}
2016-03-09 00:00:52 +01:00
2016-06-13 21:55:26 +02:00
end_complex_operation ( ) ;
2015-01-02 19:08:40 +01:00
_cancel_completion ( ) ;
2018-08-14 18:02:33 +02:00
if ( last_completion_char = = ' ( ' ) {
query_code_comple ( ) ;
}
2014-02-10 02:10:30 +01:00
}
2014-12-17 02:31:57 +01:00
void TextEdit : : _cancel_code_hint ( ) {
2017-03-05 16:44:50 +01:00
completion_hint = " " ;
2014-12-17 02:31:57 +01:00
update ( ) ;
}
2014-02-10 02:10:30 +01:00
void TextEdit : : _cancel_completion ( ) {
2020-05-14 16:41:43 +02:00
if ( ! completion_active ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
completion_active = false ;
2017-08-24 05:06:56 +02:00
completion_forced = false ;
2015-01-02 19:08:40 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2020-07-27 12:43:20 +02:00
static bool _is_completable ( char32_t c ) {
2017-03-05 16:44:50 +01:00
return ! _is_symbol ( c ) | | c = = ' " ' | | c = = ' \' ' ;
2014-12-17 02:31:57 +01:00
}
2014-02-10 02:10:30 +01:00
void TextEdit : : _update_completion_candidates ( ) {
2015-01-02 19:08:40 +01:00
String l = text [ cursor . line ] ;
2017-03-05 16:44:50 +01:00
int cofs = CLAMP ( cursor . column , 0 , l . length ( ) ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
String s ;
2015-01-03 17:03:13 +01:00
2019-08-18 17:56:24 +02:00
// Look for keywords first.
2015-01-03 17:03:13 +01:00
2017-03-05 16:44:50 +01:00
bool inquote = false ;
int first_quote = - 1 ;
2019-02-24 18:18:09 +01:00
int restore_quotes = - 1 ;
2015-06-26 06:14:31 +02:00
2017-03-05 16:44:50 +01:00
int c = cofs - 1 ;
while ( c > = 0 ) {
if ( l [ c ] = = ' " ' | | l [ c ] = = ' \' ' ) {
inquote = ! inquote ;
2020-05-14 16:41:43 +02:00
if ( first_quote = = - 1 ) {
2017-03-05 16:44:50 +01:00
first_quote = c ;
2020-05-14 16:41:43 +02:00
}
2019-02-24 18:18:09 +01:00
restore_quotes = 0 ;
} else if ( restore_quotes = = 0 & & l [ c ] = = ' $ ' ) {
restore_quotes = 1 ;
} else if ( restore_quotes = = 0 & & ! _is_whitespace ( l [ c ] ) ) {
restore_quotes = - 1 ;
2015-06-26 06:14:31 +02:00
}
c - - ;
}
2015-01-03 17:03:13 +01:00
2017-03-05 16:44:50 +01:00
bool pre_keyword = false ;
bool cancel = false ;
2015-06-26 06:14:31 +02:00
2017-03-05 16:44:50 +01:00
if ( ! inquote & & first_quote = = cofs - 1 ) {
2019-08-18 17:56:24 +02:00
// No completion here.
2017-03-05 16:44:50 +01:00
cancel = true ;
} else if ( inquote & & first_quote ! = - 1 ) {
s = l . substr ( first_quote , cofs - first_quote ) ;
} else if ( cofs > 0 & & l [ cofs - 1 ] = = ' ' ) {
int kofs = cofs - 1 ;
2015-01-03 17:03:13 +01:00
String kw ;
2020-05-14 16:41:43 +02:00
while ( kofs > = 0 & & l [ kofs ] = = ' ' ) {
2015-01-03 17:03:13 +01:00
kofs - - ;
2020-05-14 16:41:43 +02:00
}
2015-01-03 17:03:13 +01:00
2017-03-05 16:44:50 +01:00
while ( kofs > = 0 & & l [ kofs ] > 32 & & _is_completable ( l [ kofs ] ) ) {
kw = String : : chr ( l [ kofs ] ) + kw ;
2015-01-03 17:03:13 +01:00
kofs - - ;
}
2017-03-05 16:44:50 +01:00
pre_keyword = keywords . has ( kw ) ;
2015-01-03 17:03:13 +01:00
} else {
2018-03-06 19:25:14 +01:00
while ( cofs > 0 & & l [ cofs - 1 ] > 32 & & ( l [ cofs - 1 ] = = ' / ' | | _is_completable ( l [ cofs - 1 ] ) ) ) {
2017-03-05 16:44:50 +01:00
s = String : : chr ( l [ cofs - 1 ] ) + s ;
2020-05-14 16:41:43 +02:00
if ( l [ cofs - 1 ] = = ' \' ' | | l [ cofs - 1 ] = = ' " ' | | l [ cofs - 1 ] = = ' $ ' ) {
2015-01-03 17:03:13 +01:00
break ;
2020-05-14 16:41:43 +02:00
}
2015-01-03 17:03:13 +01:00
cofs - - ;
}
2015-01-02 19:08:40 +01:00
}
2015-01-03 17:03:13 +01:00
2017-08-24 05:06:56 +02:00
if ( cursor . column > 0 & & l [ cursor . column - 1 ] = = ' ( ' & & ! pre_keyword & & ! completion_forced ) {
2016-05-07 13:01:56 +02:00
cancel = true ;
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
update ( ) ;
2016-03-09 00:00:52 +01:00
2017-08-24 05:06:56 +02:00
bool prev_is_prefix = false ;
2020-05-14 16:41:43 +02:00
if ( cofs > 0 & & completion_prefixes . has ( String : : chr ( l [ cofs - 1 ] ) ) ) {
2017-08-24 05:06:56 +02:00
prev_is_prefix = true ;
2020-05-14 16:41:43 +02:00
}
2019-08-18 17:56:24 +02:00
// Check with one space before prefix, to allow indent.
2020-05-14 16:41:43 +02:00
if ( cofs > 1 & & l [ cofs - 1 ] = = ' ' & & completion_prefixes . has ( String : : chr ( l [ cofs - 2 ] ) ) ) {
2017-08-24 05:06:56 +02:00
prev_is_prefix = true ;
2020-05-14 16:41:43 +02:00
}
2017-08-24 05:06:56 +02:00
if ( cancel | | ( ! pre_keyword & & s = = " " & & ( cofs = = 0 | | ! prev_is_prefix ) ) ) {
2019-08-18 17:56:24 +02:00
// None to complete, cancel.
2015-01-02 19:08:40 +01:00
_cancel_completion ( ) ;
return ;
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
completion_options . clear ( ) ;
2017-03-05 16:44:50 +01:00
completion_index = 0 ;
completion_base = s ;
2016-06-13 21:55:26 +02:00
Vector < float > sim_cache ;
2017-10-28 23:46:20 +02:00
bool single_quote = s . begins_with ( " ' " ) ;
2019-06-13 11:32:03 +02:00
Vector < ScriptCodeCompletionOption > completion_options_casei ;
2019-12-12 00:25:44 +01:00
Vector < ScriptCodeCompletionOption > completion_options_subseq ;
Vector < ScriptCodeCompletionOption > completion_options_subseq_casei ;
String s_lower = s . to_lower ( ) ;
2019-06-13 11:32:03 +02:00
for ( List < ScriptCodeCompletionOption > : : Element * E = completion_sources . front ( ) ; E ; E = E - > next ( ) ) {
ScriptCodeCompletionOption & option = E - > get ( ) ;
2017-10-28 23:46:20 +02:00
2019-06-13 11:32:03 +02:00
if ( single_quote & & option . display . is_quoted ( ) ) {
option . display = option . display . unquote ( ) . quote ( " ' " ) ;
2017-10-28 23:46:20 +02:00
}
2019-06-13 11:32:03 +02:00
if ( inquote & & restore_quotes = = 1 & & ! option . display . is_quoted ( ) ) {
2019-02-24 18:18:09 +01:00
String quote = single_quote ? " ' " : " \" " ;
2019-06-13 11:32:03 +02:00
option . display = option . display . quote ( quote ) ;
2019-11-01 14:52:13 +01:00
option . insert_text = option . insert_text . quote ( quote ) ;
2019-02-24 18:18:09 +01:00
}
2019-12-12 00:25:44 +01:00
if ( option . display . length ( ) = = 0 ) {
continue ;
} else if ( s . length ( ) = = 0 ) {
2019-06-13 11:32:03 +02:00
completion_options . push_back ( option ) ;
2019-12-12 00:25:44 +01:00
} else {
// This code works the same as:
/*
if ( option . display . begins_with ( s ) ) {
completion_options . push_back ( option ) ;
} else if ( option . display . to_lower ( ) . begins_with ( s . to_lower ( ) ) ) {
completion_options_casei . push_back ( option ) ;
} else if ( s . is_subsequence_of ( option . display ) ) {
completion_options_subseq . push_back ( option ) ;
} else if ( s . is_subsequence_ofi ( option . display ) ) {
completion_options_subseq_casei . push_back ( option ) ;
}
*/
// But is more performant due to being inlined and looping over the characters only once
2018-11-26 16:36:46 +01:00
2019-12-12 00:25:44 +01:00
String display_lower = option . display . to_lower ( ) ;
2020-07-27 12:43:20 +02:00
const char32_t * ssq = & s [ 0 ] ;
const char32_t * ssq_lower = & s_lower [ 0 ] ;
2019-12-12 00:25:44 +01:00
2020-07-27 12:43:20 +02:00
const char32_t * tgt = & option . display [ 0 ] ;
const char32_t * tgt_lower = & display_lower [ 0 ] ;
2019-12-12 00:25:44 +01:00
2020-07-27 12:43:20 +02:00
const char32_t * ssq_last_tgt = nullptr ;
const char32_t * ssq_lower_last_tgt = nullptr ;
2019-12-12 00:25:44 +01:00
for ( ; * tgt ; tgt + + , tgt_lower + + ) {
if ( * ssq = = * tgt ) {
ssq + + ;
ssq_last_tgt = tgt ;
}
if ( * ssq_lower = = * tgt_lower ) {
ssq_lower + + ;
ssq_lower_last_tgt = tgt ;
}
2016-07-03 16:32:30 +02:00
}
2019-12-12 00:25:44 +01:00
if ( ! * ssq ) { // Matched the whole subsequence in s
if ( ssq_last_tgt = = & option . display [ s . length ( ) - 1 ] ) { // Finished matching in the first s.length() characters
completion_options . push_back ( option ) ;
} else {
completion_options_subseq . push_back ( option ) ;
}
} else if ( ! * ssq_lower ) { // Matched the whole subsequence in s_lower
if ( ssq_lower_last_tgt = = & option . display [ s . length ( ) - 1 ] ) { // Finished matching in the first s.length() characters
completion_options_casei . push_back ( option ) ;
} else {
completion_options_subseq_casei . push_back ( option ) ;
}
2015-01-02 19:08:40 +01:00
}
}
}
2016-03-09 00:00:52 +01:00
2019-12-12 00:25:44 +01:00
completion_options . append_array ( completion_options_casei ) ;
completion_options . append_array ( completion_options_subseq ) ;
completion_options . append_array ( completion_options_subseq_casei ) ;
2017-03-05 16:44:50 +01:00
if ( completion_options . size ( ) = = 0 ) {
2019-08-18 17:56:24 +02:00
// No options to complete, cancel.
2015-01-02 19:08:40 +01:00
_cancel_completion ( ) ;
2018-08-14 18:02:33 +02:00
return ;
}
2017-01-08 07:01:52 +01:00
2019-06-13 11:32:03 +02:00
if ( completion_options . size ( ) = = 1 & & s = = completion_options [ 0 ] . display ) {
2019-08-18 17:56:24 +02:00
// A perfect match, stop completion.
2018-08-14 18:02:33 +02:00
_cancel_completion ( ) ;
2015-01-02 19:08:40 +01:00
return ;
}
2016-03-09 00:00:52 +01:00
2019-08-18 17:56:24 +02:00
// The top of the list is the best match.
2017-03-05 16:44:50 +01:00
completion_current = completion_options [ 0 ] ;
completion_enabled = true ;
2014-02-10 02:10:30 +01:00
}
void TextEdit : : query_code_comple ( ) {
2014-12-17 02:31:57 +01:00
String l = text [ cursor . line ] ;
2017-03-05 16:44:50 +01:00
int ofs = CLAMP ( cursor . column , 0 , l . length ( ) ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
bool inquote = false ;
2015-06-26 06:14:31 +02:00
2017-03-05 16:44:50 +01:00
int c = ofs - 1 ;
while ( c > = 0 ) {
2020-05-14 16:41:43 +02:00
if ( l [ c ] = = ' " ' | | l [ c ] = = ' \' ' ) {
2017-03-05 16:44:50 +01:00
inquote = ! inquote ;
2020-05-14 16:41:43 +02:00
}
2015-06-26 06:14:31 +02:00
c - - ;
}
2019-08-18 07:36:42 +02:00
bool ignored = completion_active & & ! completion_options . empty ( ) ;
if ( ignored ) {
ScriptCodeCompletionOption : : Kind kind = ScriptCodeCompletionOption : : KIND_PLAIN_TEXT ;
2020-04-02 01:20:12 +02:00
const ScriptCodeCompletionOption * previous_option = nullptr ;
2019-08-18 07:36:42 +02:00
for ( int i = 0 ; i < completion_options . size ( ) ; i + + ) {
const ScriptCodeCompletionOption & current_option = completion_options [ i ] ;
if ( ! previous_option ) {
previous_option = & current_option ;
kind = current_option . kind ;
}
if ( previous_option - > kind ! = current_option . kind ) {
ignored = false ;
break ;
}
}
ignored = ignored & & ( kind = = ScriptCodeCompletionOption : : KIND_FILE_PATH | | kind = = ScriptCodeCompletionOption : : KIND_NODE_PATH | | kind = = ScriptCodeCompletionOption : : KIND_SIGNAL ) ;
}
if ( ! ignored ) {
2020-05-14 16:41:43 +02:00
if ( ofs > 0 & & ( inquote | | _is_completable ( l [ ofs - 1 ] ) | | completion_prefixes . has ( String : : chr ( l [ ofs - 1 ] ) ) ) ) {
2019-08-18 07:36:42 +02:00
emit_signal ( " request_completion " ) ;
2020-05-14 16:41:43 +02:00
} else if ( ofs > 1 & & l [ ofs - 1 ] = = ' ' & & completion_prefixes . has ( String : : chr ( l [ ofs - 2 ] ) ) ) { // Make it work with a space too, it's good enough.
2019-08-18 07:36:42 +02:00
emit_signal ( " request_completion " ) ;
2020-05-14 16:41:43 +02:00
}
2019-08-18 07:36:42 +02:00
}
2014-12-17 02:31:57 +01:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
void TextEdit : : set_code_hint ( const String & p_hint ) {
completion_hint = p_hint ;
completion_hint_offset = - 0xFFFF ;
2014-12-17 02:31:57 +01:00
update ( ) ;
2014-02-10 02:10:30 +01:00
}
2019-06-13 11:32:03 +02:00
void TextEdit : : code_complete ( const List < ScriptCodeCompletionOption > & p_strings , bool p_forced ) {
completion_sources = p_strings ;
2017-03-05 16:44:50 +01:00
completion_active = true ;
2017-08-24 05:06:56 +02:00
completion_forced = p_forced ;
2019-06-13 11:32:03 +02:00
completion_current = ScriptCodeCompletionOption ( ) ;
2017-03-05 16:44:50 +01:00
completion_index = 0 ;
2015-01-02 19:08:40 +01:00
_update_completion_candidates ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
String TextEdit : : get_word_at_pos ( const Vector2 & p_pos ) const {
int row , col ;
2016-09-12 15:52:29 +02:00
_get_mouse_pos ( p_pos , row , col ) ;
String s = text [ row ] ;
2020-05-14 16:41:43 +02:00
if ( s . length ( ) = = 0 ) {
2016-09-12 15:52:29 +02:00
return " " ;
2020-05-14 16:41:43 +02:00
}
2017-12-23 09:59:54 +01:00
int beg , end ;
if ( select_word ( s , col , beg , end ) ) {
2017-08-02 19:20:27 +02:00
bool inside_quotes = false ;
2020-07-27 12:43:20 +02:00
char32_t selected_quote = ' \0 ' ;
2017-09-01 22:33:39 +02:00
int qbegin = 0 , qend = 0 ;
2017-08-02 19:20:27 +02:00
for ( int i = 0 ; i < s . length ( ) ; i + + ) {
2018-03-11 13:59:48 +01:00
if ( s [ i ] = = ' " ' | | s [ i ] = = ' \' ' ) {
if ( i = = 0 | | s [ i - 1 ] ! = ' \\ ' ) {
if ( inside_quotes & & selected_quote = = s [ i ] ) {
qend = i ;
inside_quotes = false ;
selected_quote = ' \0 ' ;
if ( col > = qbegin & & col < = qend ) {
return s . substr ( qbegin , qend - qbegin ) ;
}
} else if ( ! inside_quotes ) {
qbegin = i + 1 ;
inside_quotes = true ;
selected_quote = s [ i ] ;
2017-08-02 19:20:27 +02:00
}
}
}
}
2017-03-05 16:44:50 +01:00
return s . substr ( beg , end - beg ) ;
2016-09-12 15:52:29 +02:00
}
return String ( ) ;
}
2017-03-05 16:44:50 +01:00
String TextEdit : : get_tooltip ( const Point2 & p_pos ) const {
2020-05-14 16:41:43 +02:00
if ( ! tooltip_obj ) {
2015-01-02 19:08:40 +01:00
return Control : : get_tooltip ( p_pos ) ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
int row , col ;
2015-11-07 13:39:03 +01:00
_get_mouse_pos ( p_pos , row , col ) ;
2015-01-02 19:08:40 +01:00
String s = text [ row ] ;
2020-05-14 16:41:43 +02:00
if ( s . length ( ) = = 0 ) {
2015-01-02 19:08:40 +01:00
return Control : : get_tooltip ( p_pos ) ;
2020-05-14 16:41:43 +02:00
}
2017-12-23 09:59:54 +01:00
int beg , end ;
if ( select_word ( s , col , beg , end ) ) {
2017-03-05 16:44:50 +01:00
String tt = tooltip_obj - > call ( tooltip_func , s . substr ( beg , end - beg ) , tooltip_ud ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
return tt ;
}
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
return Control : : get_tooltip ( p_pos ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : set_tooltip_request_func ( Object * p_obj , const StringName & p_function , const Variant & p_udata ) {
tooltip_obj = p_obj ;
tooltip_func = p_function ;
tooltip_ud = p_udata ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : set_line ( int line , String new_text ) {
2020-10-17 17:56:48 +02:00
if ( line < 0 | | line > = text . size ( ) ) {
2015-01-02 19:08:40 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2015-01-02 19:08:40 +01:00
_remove_text ( line , 0 , line , text [ line ] . length ( ) ) ;
_insert_text ( line , 0 , new_text ) ;
2017-03-05 16:44:50 +01:00
if ( cursor . line = = line ) {
cursor . column = MIN ( cursor . column , new_text . length ( ) ) ;
2015-05-05 05:17:22 +02:00
}
2018-09-08 14:20:45 +02:00
if ( is_selection_active ( ) & & line = = selection . to_line & & selection . to_column > text [ line ] . length ( ) ) {
selection . to_column = text [ line ] . length ( ) ;
}
2014-11-12 15:39:21 +01:00
}
2017-03-05 16:44:50 +01:00
void TextEdit : : insert_at ( const String & p_text , int at ) {
_insert_text ( at , 0 , p_text + " \n " ) ;
2019-08-20 09:38:57 +02:00
if ( cursor . line > = at ) {
// offset cursor when located after inserted line
+ + cursor . line ;
}
if ( is_selection_active ( ) ) {
if ( selection . from_line > = at ) {
// offset selection when located after inserted line
+ + selection . from_line ;
+ + selection . to_line ;
} else if ( selection . to_line > = at ) {
// extend selection that includes inserted line
+ + selection . to_line ;
}
}
2014-11-12 15:39:21 +01:00
}
2014-02-10 02:10:30 +01:00
2020-02-15 02:59:59 +01:00
void TextEdit : : set_show_line_length_guidelines ( bool p_show ) {
line_length_guidelines = p_show ;
update ( ) ;
}
void TextEdit : : set_line_length_guideline_soft_column ( int p_column ) {
line_length_guideline_soft_col = p_column ;
2016-10-10 10:34:51 +02:00
update ( ) ;
}
2020-02-15 02:59:59 +01:00
void TextEdit : : set_line_length_guideline_hard_column ( int p_column ) {
line_length_guideline_hard_col = p_column ;
2016-10-10 10:34:51 +02:00
update ( ) ;
}
2019-08-11 20:31:19 +02:00
void TextEdit : : set_draw_minimap ( bool p_draw ) {
draw_minimap = p_draw ;
2020-09-18 09:36:10 +02:00
if ( draw_minimap ! = p_draw ) {
_update_wrap_at ( ) ;
}
2019-08-11 20:31:19 +02:00
update ( ) ;
}
bool TextEdit : : is_drawing_minimap ( ) const {
return draw_minimap ;
}
void TextEdit : : set_minimap_width ( int p_minimap_width ) {
minimap_width = p_minimap_width ;
2020-09-18 09:36:10 +02:00
if ( minimap_width ! = p_minimap_width ) {
_update_wrap_at ( ) ;
}
2019-08-11 20:31:19 +02:00
update ( ) ;
}
int TextEdit : : get_minimap_width ( ) const {
return minimap_width ;
}
2019-06-08 13:37:57 +02:00
void TextEdit : : set_hiding_enabled ( bool p_enabled ) {
2020-05-14 16:41:43 +02:00
if ( ! p_enabled ) {
2017-11-16 05:00:27 +01:00
unhide_all_lines ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-11-13 00:12:17 +01:00
hiding_enabled = p_enabled ;
update ( ) ;
}
2019-06-08 13:37:57 +02:00
bool TextEdit : : is_hiding_enabled ( ) const {
2017-11-13 00:12:17 +01:00
return hiding_enabled ;
}
2017-10-22 14:38:00 +02:00
void TextEdit : : set_highlight_current_line ( bool p_enabled ) {
highlight_current_line = p_enabled ;
update ( ) ;
}
bool TextEdit : : is_highlight_current_line_enabled ( ) const {
return highlight_current_line ;
}
2015-10-17 15:29:54 +02:00
bool TextEdit : : is_text_field ( ) const {
2017-03-05 16:44:50 +01:00
return true ;
2015-10-17 15:29:54 +02:00
}
2016-05-17 01:25:17 +02:00
void TextEdit : : menu_option ( int p_option ) {
2017-03-05 16:44:50 +01:00
switch ( p_option ) {
2016-05-17 01:25:17 +02:00
case MENU_CUT : {
2016-09-19 14:17:48 +02:00
if ( ! readonly ) {
cut ( ) ;
}
2016-05-17 01:25:17 +02:00
} break ;
case MENU_COPY : {
copy ( ) ;
} break ;
case MENU_PASTE : {
2016-09-19 14:17:48 +02:00
if ( ! readonly ) {
paste ( ) ;
}
2016-05-17 01:25:17 +02:00
} break ;
case MENU_CLEAR : {
2016-09-19 14:17:48 +02:00
if ( ! readonly ) {
clear ( ) ;
}
2016-05-17 01:25:17 +02:00
} break ;
case MENU_SELECT_ALL : {
select_all ( ) ;
} break ;
case MENU_UNDO : {
undo ( ) ;
} break ;
2018-11-29 18:21:43 +01:00
case MENU_REDO : {
redo ( ) ;
2020-09-18 09:36:10 +02:00
} break ;
case MENU_DIR_INHERITED : {
set_text_direction ( TEXT_DIRECTION_INHERITED ) ;
} break ;
case MENU_DIR_AUTO : {
set_text_direction ( TEXT_DIRECTION_AUTO ) ;
} break ;
case MENU_DIR_LTR : {
set_text_direction ( TEXT_DIRECTION_LTR ) ;
} break ;
case MENU_DIR_RTL : {
set_text_direction ( TEXT_DIRECTION_RTL ) ;
} break ;
case MENU_DISPLAY_UCC : {
set_draw_control_chars ( ! get_draw_control_chars ( ) ) ;
} break ;
case MENU_INSERT_LRM : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x200E ) ) ;
}
} break ;
case MENU_INSERT_RLM : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x200F ) ) ;
}
} break ;
case MENU_INSERT_LRE : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x202A ) ) ;
}
} break ;
case MENU_INSERT_RLE : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x202B ) ) ;
}
} break ;
case MENU_INSERT_LRO : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x202D ) ) ;
}
} break ;
case MENU_INSERT_RLO : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x202E ) ) ;
}
} break ;
case MENU_INSERT_PDF : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x202C ) ) ;
}
} break ;
case MENU_INSERT_ALM : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x061C ) ) ;
}
} break ;
case MENU_INSERT_LRI : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x2066 ) ) ;
}
} break ;
case MENU_INSERT_RLI : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x2067 ) ) ;
}
} break ;
case MENU_INSERT_FSI : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x2068 ) ) ;
}
} break ;
case MENU_INSERT_PDI : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x2069 ) ) ;
}
} break ;
case MENU_INSERT_ZWJ : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x200D ) ) ;
}
} break ;
case MENU_INSERT_ZWNJ : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x200C ) ) ;
}
} break ;
case MENU_INSERT_WJ : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x2060 ) ) ;
}
} break ;
case MENU_INSERT_SHY : {
if ( ! readonly ) {
insert_text_at_cursor ( String : : chr ( 0x00AD ) ) ;
}
2018-11-29 18:21:43 +01:00
}
}
2016-05-17 01:25:17 +02:00
}
2020-03-01 09:13:41 +01:00
void TextEdit : : set_highlighted_word ( const String & new_word ) {
highlighted_word = new_word ;
update ( ) ;
}
2016-09-12 15:52:29 +02:00
void TextEdit : : set_select_identifiers_on_hover ( bool p_enable ) {
2017-03-05 16:44:50 +01:00
select_identifiers_enabled = p_enable ;
2016-09-12 15:52:29 +02:00
}
bool TextEdit : : is_selecting_identifiers_on_hover_enabled ( ) const {
return select_identifiers_enabled ;
}
2016-09-29 09:12:45 +02:00
void TextEdit : : set_context_menu_enabled ( bool p_enable ) {
context_menu_enabled = p_enable ;
}
2016-09-12 15:52:29 +02:00
2017-11-09 21:46:29 +01:00
bool TextEdit : : is_context_menu_enabled ( ) {
return context_menu_enabled ;
}
2019-08-08 02:09:46 +02:00
void TextEdit : : set_shortcut_keys_enabled ( bool p_enabled ) {
shortcut_keys_enabled = p_enabled ;
_generate_context_menu ( ) ;
}
2020-08-09 11:06:36 +02:00
void TextEdit : : set_virtual_keyboard_enabled ( bool p_enable ) {
virtual_keyboard_enabled = p_enable ;
}
2019-08-08 02:09:46 +02:00
void TextEdit : : set_selecting_enabled ( bool p_enabled ) {
selecting_enabled = p_enabled ;
2020-05-14 16:41:43 +02:00
if ( ! selecting_enabled ) {
2019-08-08 02:09:46 +02:00
deselect ( ) ;
2020-05-14 16:41:43 +02:00
}
2019-08-08 02:09:46 +02:00
_generate_context_menu ( ) ;
}
bool TextEdit : : is_selecting_enabled ( ) const {
return selecting_enabled ;
}
bool TextEdit : : is_shortcut_keys_enabled ( ) const {
return shortcut_keys_enabled ;
}
2020-08-09 11:06:36 +02:00
bool TextEdit : : is_virtual_keyboard_enabled ( ) const {
return virtual_keyboard_enabled ;
}
2016-05-17 01:25:17 +02:00
PopupMenu * TextEdit : : get_menu ( ) const {
return menu ;
}
2020-09-18 09:36:10 +02:00
bool TextEdit : : _set ( const StringName & p_name , const Variant & p_value ) {
String str = p_name ;
if ( str . begins_with ( " opentype_features/ " ) ) {
String name = str . get_slicec ( ' / ' , 1 ) ;
int32_t tag = TS - > name_to_tag ( name ) ;
double value = p_value ;
if ( value = = - 1 ) {
if ( opentype_features . has ( tag ) ) {
opentype_features . erase ( tag ) ;
text . set_font_features ( opentype_features ) ;
text . invalidate_all ( ) ;
update ( ) ;
}
} else {
if ( ( double ) opentype_features [ tag ] ! = value ) {
opentype_features [ tag ] = value ;
text . set_font_features ( opentype_features ) ;
text . invalidate_all ( ) ;
;
update ( ) ;
}
}
_change_notify ( ) ;
return true ;
}
return false ;
}
bool TextEdit : : _get ( const StringName & p_name , Variant & r_ret ) const {
String str = p_name ;
if ( str . begins_with ( " opentype_features/ " ) ) {
String name = str . get_slicec ( ' / ' , 1 ) ;
int32_t tag = TS - > name_to_tag ( name ) ;
if ( opentype_features . has ( tag ) ) {
r_ret = opentype_features [ tag ] ;
return true ;
} else {
r_ret = - 1 ;
return true ;
}
}
return false ;
}
void TextEdit : : _get_property_list ( List < PropertyInfo > * p_list ) const {
for ( const Variant * ftr = opentype_features . next ( nullptr ) ; ftr ! = nullptr ; ftr = opentype_features . next ( ftr ) ) {
String name = TS - > tag_to_name ( * ftr ) ;
p_list - > push_back ( PropertyInfo ( Variant : : FLOAT , " opentype_features/ " + name ) ) ;
}
p_list - > push_back ( PropertyInfo ( Variant : : NIL , " opentype_features/_new " , PROPERTY_HINT_NONE , " " , PROPERTY_USAGE_EDITOR ) ) ;
}
2014-02-10 02:10:30 +01:00
void TextEdit : : _bind_methods ( ) {
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " _gui_input " ) , & TextEdit : : _gui_input ) ;
ClassDB : : bind_method ( D_METHOD ( " _cursor_changed_emit " ) , & TextEdit : : _cursor_changed_emit ) ;
ClassDB : : bind_method ( D_METHOD ( " _text_changed_emit " ) , & TextEdit : : _text_changed_emit ) ;
2020-09-18 09:36:10 +02:00
ClassDB : : bind_method ( D_METHOD ( " _update_wrap_at " , " force " ) , & TextEdit : : _update_wrap_at , DEFVAL ( false ) ) ;
2016-03-09 00:00:52 +01:00
2017-08-20 17:45:01 +02:00
BIND_ENUM_CONSTANT ( SEARCH_MATCH_CASE ) ;
BIND_ENUM_CONSTANT ( SEARCH_WHOLE_WORDS ) ;
BIND_ENUM_CONSTANT ( SEARCH_BACKWARDS ) ;
2016-03-09 00:00:52 +01:00
2020-10-11 19:44:53 +02:00
BIND_ENUM_CONSTANT ( SELECTION_MODE_NONE ) ;
BIND_ENUM_CONSTANT ( SELECTION_MODE_SHIFT ) ;
BIND_ENUM_CONSTANT ( SELECTION_MODE_POINTER ) ;
BIND_ENUM_CONSTANT ( SELECTION_MODE_WORD ) ;
BIND_ENUM_CONSTANT ( SELECTION_MODE_LINE ) ;
2015-01-02 19:08:40 +01:00
/*
2017-11-05 16:54:00 +01:00
ClassDB : : bind_method ( D_METHOD ( " delete_char " ) , & TextEdit : : delete_char ) ;
ClassDB : : bind_method ( D_METHOD ( " delete_line " ) , & TextEdit : : delete_line ) ;
2014-02-10 02:10:30 +01:00
*/
2016-03-09 00:00:52 +01:00
2020-09-18 09:36:10 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_draw_control_chars " ) , & TextEdit : : get_draw_control_chars ) ;
ClassDB : : bind_method ( D_METHOD ( " set_draw_control_chars " , " enable " ) , & TextEdit : : set_draw_control_chars ) ;
ClassDB : : bind_method ( D_METHOD ( " set_text_direction " , " direction " ) , & TextEdit : : set_text_direction ) ;
ClassDB : : bind_method ( D_METHOD ( " get_text_direction " ) , & TextEdit : : get_text_direction ) ;
ClassDB : : bind_method ( D_METHOD ( " set_opentype_feature " , " tag " , " value " ) , & TextEdit : : set_opentype_feature ) ;
ClassDB : : bind_method ( D_METHOD ( " get_opentype_feature " , " tag " ) , & TextEdit : : get_opentype_feature ) ;
ClassDB : : bind_method ( D_METHOD ( " clear_opentype_features " ) , & TextEdit : : clear_opentype_features ) ;
ClassDB : : bind_method ( D_METHOD ( " set_language " , " language " ) , & TextEdit : : set_language ) ;
ClassDB : : bind_method ( D_METHOD ( " get_language " ) , & TextEdit : : get_language ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_text " , " text " ) , & TextEdit : : set_text ) ;
ClassDB : : bind_method ( D_METHOD ( " insert_text_at_cursor " , " text " ) , & TextEdit : : insert_text_at_cursor ) ;
2017-02-13 12:47:24 +01:00
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " get_line_count " ) , & TextEdit : : get_line_count ) ;
ClassDB : : bind_method ( D_METHOD ( " get_text " ) , & TextEdit : : get_text ) ;
ClassDB : : bind_method ( D_METHOD ( " get_line " , " line " ) , & TextEdit : : get_line ) ;
2020-05-01 14:24:49 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_line " , " line " , " new_text " ) , & TextEdit : : set_line ) ;
2017-02-13 12:47:24 +01:00
2020-09-18 09:36:10 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_structured_text_bidi_override " , " parser " ) , & TextEdit : : set_structured_text_bidi_override ) ;
ClassDB : : bind_method ( D_METHOD ( " get_structured_text_bidi_override " ) , & TextEdit : : get_structured_text_bidi_override ) ;
ClassDB : : bind_method ( D_METHOD ( " set_structured_text_bidi_override_options " , " args " ) , & TextEdit : : set_structured_text_bidi_override_options ) ;
ClassDB : : bind_method ( D_METHOD ( " get_structured_text_bidi_override_options " ) , & TextEdit : : get_structured_text_bidi_override_options ) ;
2019-01-04 13:09:01 +01:00
ClassDB : : bind_method ( D_METHOD ( " center_viewport_to_cursor " ) , & TextEdit : : center_viewport_to_cursor ) ;
2017-04-15 19:48:10 +02:00
ClassDB : : bind_method ( D_METHOD ( " cursor_set_column " , " column " , " adjust_viewport " ) , & TextEdit : : cursor_set_column , DEFVAL ( true ) ) ;
2018-05-15 21:53:42 +02:00
ClassDB : : bind_method ( D_METHOD ( " cursor_set_line " , " line " , " adjust_viewport " , " can_be_hidden " , " wrap_index " ) , & TextEdit : : cursor_set_line , DEFVAL ( true ) , DEFVAL ( true ) , DEFVAL ( 0 ) ) ;
2017-02-13 12:47:24 +01:00
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " cursor_get_column " ) , & TextEdit : : cursor_get_column ) ;
ClassDB : : bind_method ( D_METHOD ( " cursor_get_line " ) , & TextEdit : : cursor_get_line ) ;
ClassDB : : bind_method ( D_METHOD ( " cursor_set_blink_enabled " , " enable " ) , & TextEdit : : cursor_set_blink_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " cursor_get_blink_enabled " ) , & TextEdit : : cursor_get_blink_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " cursor_set_blink_speed " , " blink_speed " ) , & TextEdit : : cursor_set_blink_speed ) ;
ClassDB : : bind_method ( D_METHOD ( " cursor_get_blink_speed " ) , & TextEdit : : cursor_get_blink_speed ) ;
2017-02-13 12:47:24 +01:00
ClassDB : : bind_method ( D_METHOD ( " cursor_set_block_mode " , " enable " ) , & TextEdit : : cursor_set_block_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " cursor_is_block_mode " ) , & TextEdit : : cursor_is_block_mode ) ;
2020-09-18 09:36:10 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_mid_grapheme_caret_enabled " , " enabled " ) , & TextEdit : : set_mid_grapheme_caret_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " get_mid_grapheme_caret_enabled " ) , & TextEdit : : get_mid_grapheme_caret_enabled ) ;
2017-12-20 02:36:47 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_right_click_moves_caret " , " enable " ) , & TextEdit : : set_right_click_moves_caret ) ;
ClassDB : : bind_method ( D_METHOD ( " is_right_click_moving_caret " ) , & TextEdit : : is_right_click_moving_caret ) ;
2020-10-11 19:44:53 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_selection_mode " ) , & TextEdit : : get_selection_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " set_selection_mode " , " mode " , " line " , " column " ) , & TextEdit : : set_selection_mode , DEFVAL ( - 1 ) , DEFVAL ( - 1 ) ) ;
ClassDB : : bind_method ( D_METHOD ( " get_selection_line " ) , & TextEdit : : get_selection_line ) ;
ClassDB : : bind_method ( D_METHOD ( " get_selection_column " ) , & TextEdit : : get_selection_column ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_readonly " , " enable " ) , & TextEdit : : set_readonly ) ;
2017-11-11 19:07:17 +01:00
ClassDB : : bind_method ( D_METHOD ( " is_readonly " ) , & TextEdit : : is_readonly ) ;
2018-01-26 02:41:17 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_wrap_enabled " , " enable " ) , & TextEdit : : set_wrap_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_wrap_enabled " ) , & TextEdit : : is_wrap_enabled ) ;
2017-11-09 21:46:29 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_context_menu_enabled " , " enable " ) , & TextEdit : : set_context_menu_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_context_menu_enabled " ) , & TextEdit : : is_context_menu_enabled ) ;
2019-08-08 02:09:46 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_shortcut_keys_enabled " , " enable " ) , & TextEdit : : set_shortcut_keys_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_shortcut_keys_enabled " ) , & TextEdit : : is_shortcut_keys_enabled ) ;
2020-08-09 11:06:36 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_virtual_keyboard_enabled " , " enable " ) , & TextEdit : : set_virtual_keyboard_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_virtual_keyboard_enabled " ) , & TextEdit : : is_virtual_keyboard_enabled ) ;
2019-08-08 02:09:46 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_selecting_enabled " , " enable " ) , & TextEdit : : set_selecting_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_selecting_enabled " ) , & TextEdit : : is_selecting_enabled ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " cut " ) , & TextEdit : : cut ) ;
ClassDB : : bind_method ( D_METHOD ( " copy " ) , & TextEdit : : copy ) ;
ClassDB : : bind_method ( D_METHOD ( " paste " ) , & TextEdit : : paste ) ;
2017-12-16 20:34:16 +01:00
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " select " , " from_line " , " from_column " , " to_line " , " to_column " ) , & TextEdit : : select ) ;
2017-12-16 20:34:16 +01:00
ClassDB : : bind_method ( D_METHOD ( " select_all " ) , & TextEdit : : select_all ) ;
ClassDB : : bind_method ( D_METHOD ( " deselect " ) , & TextEdit : : deselect ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " is_selection_active " ) , & TextEdit : : is_selection_active ) ;
ClassDB : : bind_method ( D_METHOD ( " get_selection_from_line " ) , & TextEdit : : get_selection_from_line ) ;
ClassDB : : bind_method ( D_METHOD ( " get_selection_from_column " ) , & TextEdit : : get_selection_from_column ) ;
ClassDB : : bind_method ( D_METHOD ( " get_selection_to_line " ) , & TextEdit : : get_selection_to_line ) ;
ClassDB : : bind_method ( D_METHOD ( " get_selection_to_column " ) , & TextEdit : : get_selection_to_column ) ;
ClassDB : : bind_method ( D_METHOD ( " get_selection_text " ) , & TextEdit : : get_selection_text ) ;
ClassDB : : bind_method ( D_METHOD ( " get_word_under_cursor " ) , & TextEdit : : get_word_under_cursor ) ;
2017-08-09 13:54:55 +02:00
ClassDB : : bind_method ( D_METHOD ( " search " , " key " , " flags " , " from_line " , " from_column " ) , & TextEdit : : _search_bind ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " undo " ) , & TextEdit : : undo ) ;
ClassDB : : bind_method ( D_METHOD ( " redo " ) , & TextEdit : : redo ) ;
ClassDB : : bind_method ( D_METHOD ( " clear_undo_history " ) , & TextEdit : : clear_undo_history ) ;
2017-02-13 12:47:24 +01:00
2019-04-24 09:59:17 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_draw_tabs " ) , & TextEdit : : set_draw_tabs ) ;
ClassDB : : bind_method ( D_METHOD ( " is_drawing_tabs " ) , & TextEdit : : is_drawing_tabs ) ;
2019-05-01 11:31:10 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_draw_spaces " ) , & TextEdit : : set_draw_spaces ) ;
ClassDB : : bind_method ( D_METHOD ( " is_drawing_spaces " ) , & TextEdit : : is_drawing_spaces ) ;
2017-02-13 12:47:24 +01:00
2017-11-13 00:12:17 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_hiding_enabled " , " enable " ) , & TextEdit : : set_hiding_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_hiding_enabled " ) , & TextEdit : : is_hiding_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " set_line_as_hidden " , " line " , " enable " ) , & TextEdit : : set_line_as_hidden ) ;
2017-12-25 15:19:56 +01:00
ClassDB : : bind_method ( D_METHOD ( " is_line_hidden " , " line " ) , & TextEdit : : is_line_hidden ) ;
2017-11-16 05:00:27 +01:00
ClassDB : : bind_method ( D_METHOD ( " fold_all_lines " ) , & TextEdit : : fold_all_lines ) ;
2017-11-13 00:12:17 +01:00
ClassDB : : bind_method ( D_METHOD ( " unhide_all_lines " ) , & TextEdit : : unhide_all_lines ) ;
ClassDB : : bind_method ( D_METHOD ( " fold_line " , " line " ) , & TextEdit : : fold_line ) ;
ClassDB : : bind_method ( D_METHOD ( " unfold_line " , " line " ) , & TextEdit : : unfold_line ) ;
2017-12-08 19:17:10 +01:00
ClassDB : : bind_method ( D_METHOD ( " toggle_fold_line " , " line " ) , & TextEdit : : toggle_fold_line ) ;
2017-11-13 00:12:17 +01:00
ClassDB : : bind_method ( D_METHOD ( " can_fold " , " line " ) , & TextEdit : : can_fold ) ;
ClassDB : : bind_method ( D_METHOD ( " is_folded " , " line " ) , & TextEdit : : is_folded ) ;
2017-02-13 12:47:24 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_highlight_all_occurrences " , " enable " ) , & TextEdit : : set_highlight_all_occurrences ) ;
ClassDB : : bind_method ( D_METHOD ( " is_highlight_all_occurrences_enabled " ) , & TextEdit : : is_highlight_all_occurrences_enabled ) ;
2017-09-27 19:24:05 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_override_selected_font_color " , " override " ) , & TextEdit : : set_override_selected_font_color ) ;
ClassDB : : bind_method ( D_METHOD ( " is_overriding_selected_font_color " ) , & TextEdit : : is_overriding_selected_font_color ) ;
2020-05-03 18:08:15 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_syntax_highlighter " , " syntax_highlighter " ) , & TextEdit : : set_syntax_highlighter ) ;
ClassDB : : bind_method ( D_METHOD ( " get_syntax_highlighter " ) , & TextEdit : : get_syntax_highlighter ) ;
2020-03-07 12:17:18 +01:00
2020-07-25 02:15:23 +02:00
/* Gutters. */
BIND_ENUM_CONSTANT ( GUTTER_TYPE_STRING ) ;
BIND_ENUM_CONSTANT ( GUTTER_TPYE_ICON ) ;
BIND_ENUM_CONSTANT ( GUTTER_TPYE_CUSTOM ) ;
ClassDB : : bind_method ( D_METHOD ( " add_gutter " , " at " ) , & TextEdit : : add_gutter , DEFVAL ( - 1 ) ) ;
ClassDB : : bind_method ( D_METHOD ( " remove_gutter " , " gutter " ) , & TextEdit : : remove_gutter ) ;
ClassDB : : bind_method ( D_METHOD ( " get_gutter_count " ) , & TextEdit : : get_gutter_count ) ;
ClassDB : : bind_method ( D_METHOD ( " set_gutter_name " , " gutter " , " name " ) , & TextEdit : : set_gutter_name ) ;
ClassDB : : bind_method ( D_METHOD ( " get_gutter_name " , " gutter " ) , & TextEdit : : get_gutter_name ) ;
ClassDB : : bind_method ( D_METHOD ( " set_gutter_type " , " gutter " , " type " ) , & TextEdit : : set_gutter_type ) ;
ClassDB : : bind_method ( D_METHOD ( " get_gutter_type " , " gutter " ) , & TextEdit : : get_gutter_type ) ;
ClassDB : : bind_method ( D_METHOD ( " set_gutter_width " , " gutter " , " width " ) , & TextEdit : : set_gutter_width ) ;
ClassDB : : bind_method ( D_METHOD ( " get_gutter_width " , " gutter " ) , & TextEdit : : get_gutter_width ) ;
ClassDB : : bind_method ( D_METHOD ( " set_gutter_draw " , " gutter " , " draw " ) , & TextEdit : : set_gutter_draw ) ;
ClassDB : : bind_method ( D_METHOD ( " is_gutter_drawn " , " gutter " ) , & TextEdit : : is_gutter_drawn ) ;
ClassDB : : bind_method ( D_METHOD ( " set_gutter_clickable " , " gutter " , " clickable " ) , & TextEdit : : set_gutter_clickable ) ;
ClassDB : : bind_method ( D_METHOD ( " is_gutter_clickable " , " gutter " ) , & TextEdit : : is_gutter_clickable ) ;
ClassDB : : bind_method ( D_METHOD ( " set_gutter_overwritable " , " gutter " , " overwritable " ) , & TextEdit : : set_gutter_overwritable ) ;
ClassDB : : bind_method ( D_METHOD ( " is_gutter_overwritable " , " gutter " ) , & TextEdit : : is_gutter_overwritable ) ;
ClassDB : : bind_method ( D_METHOD ( " set_gutter_custom_draw " , " column " , " object " , " callback " ) , & TextEdit : : set_gutter_custom_draw ) ;
// Line gutters.
ClassDB : : bind_method ( D_METHOD ( " set_line_gutter_metadata " , " line " , " gutter " , " metadata " ) , & TextEdit : : set_line_gutter_metadata ) ;
ClassDB : : bind_method ( D_METHOD ( " get_line_gutter_metadata " , " line " , " gutter " ) , & TextEdit : : get_line_gutter_metadata ) ;
ClassDB : : bind_method ( D_METHOD ( " set_line_gutter_text " , " line " , " gutter " , " text " ) , & TextEdit : : set_line_gutter_text ) ;
ClassDB : : bind_method ( D_METHOD ( " get_line_gutter_text " , " line " , " gutter " ) , & TextEdit : : get_line_gutter_text ) ;
ClassDB : : bind_method ( D_METHOD ( " set_line_gutter_icon " , " line " , " gutter " , " icon " ) , & TextEdit : : set_line_gutter_icon ) ;
ClassDB : : bind_method ( D_METHOD ( " get_line_gutter_icon " , " line " , " gutter " ) , & TextEdit : : get_line_gutter_icon ) ;
ClassDB : : bind_method ( D_METHOD ( " set_line_gutter_item_color " , " line " , " gutter " , " color " ) , & TextEdit : : set_line_gutter_item_color ) ;
ClassDB : : bind_method ( D_METHOD ( " get_line_gutter_item_color " , " line " , " gutter " ) , & TextEdit : : get_line_gutter_item_color ) ;
ClassDB : : bind_method ( D_METHOD ( " set_line_gutter_clickable " , " line " , " gutter " , " clickable " ) , & TextEdit : : set_line_gutter_clickable ) ;
ClassDB : : bind_method ( D_METHOD ( " is_line_gutter_clickable " , " line " , " gutter " ) , & TextEdit : : is_line_gutter_clickable ) ;
2017-10-22 14:38:00 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_highlight_current_line " , " enabled " ) , & TextEdit : : set_highlight_current_line ) ;
ClassDB : : bind_method ( D_METHOD ( " is_highlight_current_line_enabled " ) , & TextEdit : : is_highlight_current_line_enabled ) ;
2017-08-19 16:23:45 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_smooth_scroll_enable " , " enable " ) , & TextEdit : : set_smooth_scroll_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_smooth_scroll_enabled " ) , & TextEdit : : is_smooth_scroll_enabled ) ;
2017-08-21 20:13:24 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_v_scroll_speed " , " speed " ) , & TextEdit : : set_v_scroll_speed ) ;
ClassDB : : bind_method ( D_METHOD ( " get_v_scroll_speed " ) , & TextEdit : : get_v_scroll_speed ) ;
2020-01-06 20:16:00 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_v_scroll " , " value " ) , & TextEdit : : set_v_scroll ) ;
ClassDB : : bind_method ( D_METHOD ( " get_v_scroll " ) , & TextEdit : : get_v_scroll ) ;
ClassDB : : bind_method ( D_METHOD ( " set_h_scroll " , " value " ) , & TextEdit : : set_h_scroll ) ;
ClassDB : : bind_method ( D_METHOD ( " get_h_scroll " ) , & TextEdit : : get_h_scroll ) ;
2017-08-19 16:23:45 +02:00
2017-07-22 12:11:42 +02:00
ClassDB : : bind_method ( D_METHOD ( " menu_option " , " option " ) , & TextEdit : : menu_option ) ;
2017-08-09 13:19:41 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_menu " ) , & TextEdit : : get_menu ) ;
2016-03-09 00:00:52 +01:00
2019-08-11 20:31:19 +02:00
ClassDB : : bind_method ( D_METHOD ( " draw_minimap " , " draw " ) , & TextEdit : : set_draw_minimap ) ;
ClassDB : : bind_method ( D_METHOD ( " is_drawing_minimap " ) , & TextEdit : : is_drawing_minimap ) ;
ClassDB : : bind_method ( D_METHOD ( " set_minimap_width " , " width " ) , & TextEdit : : set_minimap_width ) ;
ClassDB : : bind_method ( D_METHOD ( " get_minimap_width " ) , & TextEdit : : get_minimap_width ) ;
2017-11-12 16:10:26 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : STRING , " text " , PROPERTY_HINT_MULTILINE_TEXT ) , " set_text " , " get_text " ) ;
2020-09-18 09:36:10 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " text_direction " , PROPERTY_HINT_ENUM , " Auto,LTR,RTL,Inherited " ) , " set_text_direction " , " get_text_direction " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : STRING , " language " ) , " set_language " , " get_language " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " draw_control_chars " ) , " set_draw_control_chars " , " get_draw_control_chars " ) ;
2017-11-11 19:07:17 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " readonly " ) , " set_readonly " , " is_readonly " ) ;
2017-10-22 14:38:00 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " highlight_current_line " ) , " set_highlight_current_line " , " is_highlight_current_line_enabled " ) ;
2019-04-24 09:59:17 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " draw_tabs " ) , " set_draw_tabs " , " is_drawing_tabs " ) ;
2019-05-01 11:31:10 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " draw_spaces " ) , " set_draw_spaces " , " is_drawing_spaces " ) ;
2017-02-12 01:11:37 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " highlight_all_occurrences " ) , " set_highlight_all_occurrences " , " is_highlight_all_occurrences_enabled " ) ;
2017-09-27 19:24:05 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " override_selected_font_color " ) , " set_override_selected_font_color " , " is_overriding_selected_font_color " ) ;
2017-11-09 21:46:29 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " context_menu_enabled " ) , " set_context_menu_enabled " , " is_context_menu_enabled " ) ;
2019-08-08 02:09:46 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " shortcut_keys_enabled " ) , " set_shortcut_keys_enabled " , " is_shortcut_keys_enabled " ) ;
2020-08-09 11:06:36 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " virtual_keyboard_enabled " ) , " set_virtual_keyboard_enabled " , " is_virtual_keyboard_enabled " ) ;
2019-08-08 02:09:46 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " selecting_enabled " ) , " set_selecting_enabled " , " is_selecting_enabled " ) ;
2017-08-19 16:23:45 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " smooth_scrolling " ) , " set_smooth_scroll_enable " , " is_smooth_scroll_enabled " ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " v_scroll_speed " ) , " set_v_scroll_speed " , " get_v_scroll_speed " ) ;
2017-11-13 00:12:17 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " hiding_enabled " ) , " set_hiding_enabled " , " is_hiding_enabled " ) ;
2018-01-26 02:41:17 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " wrap_enabled " ) , " set_wrap_enabled " , " is_wrap_enabled " ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " scroll_vertical " ) , " set_v_scroll " , " get_v_scroll " ) ;
2020-01-06 20:16:00 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " scroll_horizontal " ) , " set_h_scroll " , " get_h_scroll " ) ;
2016-07-11 16:11:35 +02:00
2020-05-03 18:08:15 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : OBJECT , " syntax_highlighter " , PROPERTY_HINT_RESOURCE_TYPE , " SyntaxHighlighter " , PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE ) , " set_syntax_highlighter " , " get_syntax_highlighter " ) ;
2019-08-11 20:31:19 +02:00
ADD_GROUP ( " Minimap " , " minimap_ " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " minimap_draw " ) , " draw_minimap " , " is_drawing_minimap " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " minimap_width " ) , " set_minimap_width " , " get_minimap_width " ) ;
2017-03-05 16:44:50 +01:00
ADD_GROUP ( " Caret " , " caret_ " ) ;
2017-02-12 01:11:37 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " caret_block_mode " ) , " cursor_set_block_mode " , " cursor_is_block_mode " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " caret_blink " ) , " cursor_set_blink_enabled " , " cursor_get_blink_enabled " ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " caret_blink_speed " , PROPERTY_HINT_RANGE , " 0.1,10,0.01 " ) , " cursor_set_blink_speed " , " cursor_get_blink_speed " ) ;
2017-12-20 02:36:47 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " caret_moving_by_right_click " ) , " set_right_click_moves_caret " , " is_right_click_moving_caret " ) ;
2020-09-18 09:36:10 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " caret_mid_grapheme " ) , " set_mid_grapheme_caret_enabled " , " get_mid_grapheme_caret_enabled " ) ;
ADD_GROUP ( " Structured Text " , " structured_text_ " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " structured_text_bidi_override " , PROPERTY_HINT_ENUM , " Default,URI,File,Email,List,None,Custom " ) , " set_structured_text_bidi_override " , " get_structured_text_bidi_override " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : ARRAY , " structured_text_bidi_override_options " ) , " set_structured_text_bidi_override_options " , " get_structured_text_bidi_override_options " ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
ADD_SIGNAL ( MethodInfo ( " cursor_changed " ) ) ;
ADD_SIGNAL ( MethodInfo ( " text_changed " ) ) ;
2020-07-28 15:33:46 +02:00
ADD_SIGNAL ( MethodInfo ( " lines_edited_from " , PropertyInfo ( Variant : : INT , " from_line " ) , PropertyInfo ( Variant : : INT , " to_line " ) ) ) ;
2015-01-02 19:08:40 +01:00
ADD_SIGNAL ( MethodInfo ( " request_completion " ) ) ;
2020-07-25 02:15:23 +02:00
ADD_SIGNAL ( MethodInfo ( " gutter_clicked " , PropertyInfo ( Variant : : INT , " line " ) , PropertyInfo ( Variant : : INT , " gutter " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " gutter_added " ) ) ;
ADD_SIGNAL ( MethodInfo ( " gutter_removed " ) ) ;
2017-03-05 16:44:50 +01:00
ADD_SIGNAL ( MethodInfo ( " symbol_lookup " , PropertyInfo ( Variant : : STRING , " symbol " ) , PropertyInfo ( Variant : : INT , " row " ) , PropertyInfo ( Variant : : INT , " column " ) ) ) ;
2020-03-01 09:13:41 +01:00
ADD_SIGNAL ( MethodInfo ( " symbol_validate " , PropertyInfo ( Variant : : STRING , " symbol " ) ) ) ;
2016-05-17 01:25:17 +02:00
2017-08-20 17:45:01 +02:00
BIND_ENUM_CONSTANT ( MENU_CUT ) ;
BIND_ENUM_CONSTANT ( MENU_COPY ) ;
BIND_ENUM_CONSTANT ( MENU_PASTE ) ;
BIND_ENUM_CONSTANT ( MENU_CLEAR ) ;
BIND_ENUM_CONSTANT ( MENU_SELECT_ALL ) ;
BIND_ENUM_CONSTANT ( MENU_UNDO ) ;
2018-11-29 18:21:43 +01:00
BIND_ENUM_CONSTANT ( MENU_REDO ) ;
2020-09-18 09:36:10 +02:00
BIND_ENUM_CONSTANT ( MENU_DIR_INHERITED ) ;
BIND_ENUM_CONSTANT ( MENU_DIR_AUTO ) ;
BIND_ENUM_CONSTANT ( MENU_DIR_LTR ) ;
BIND_ENUM_CONSTANT ( MENU_DIR_RTL ) ;
BIND_ENUM_CONSTANT ( MENU_DISPLAY_UCC ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_LRM ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_RLM ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_LRE ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_RLE ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_LRO ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_RLO ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_PDF ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_ALM ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_LRI ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_RLI ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_FSI ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_PDI ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_ZWJ ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_ZWNJ ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_WJ ) ;
BIND_ENUM_CONSTANT ( MENU_INSERT_SHY ) ;
2017-08-20 17:45:01 +02:00
BIND_ENUM_CONSTANT ( MENU_MAX ) ;
2016-05-17 01:25:17 +02:00
2017-03-05 16:44:50 +01:00
GLOBAL_DEF ( " gui/timers/text_edit_idle_detect_sec " , 3 ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
ProjectSettings : : get_singleton ( ) - > set_custom_property_info ( " gui/timers/text_edit_idle_detect_sec " , PropertyInfo ( Variant : : FLOAT , " gui/timers/text_edit_idle_detect_sec " , PROPERTY_HINT_RANGE , " 0,10,0.01,or_greater " ) ) ; // No negative numbers.
2020-04-13 14:30:56 +02:00
GLOBAL_DEF ( " gui/common/text_edit_undo_stack_max_size " , 1024 ) ;
ProjectSettings : : get_singleton ( ) - > set_custom_property_info ( " gui/common/text_edit_undo_stack_max_size " , PropertyInfo ( Variant : : INT , " gui/common/text_edit_undo_stack_max_size " , PROPERTY_HINT_RANGE , " 0,10000,1,or_greater " ) ) ; // No negative numbers.
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
TextEdit : : TextEdit ( ) {
setting_row = false ;
draw_tabs = false ;
2019-04-24 01:33:20 +02:00
draw_spaces = false ;
2017-09-27 19:24:05 +02:00
override_selected_font_color = false ;
2017-03-05 16:44:50 +01:00
draw_caret = true ;
max_chars = 0 ;
2015-01-02 19:08:40 +01:00
clear ( ) ;
2018-01-26 02:41:17 +01:00
wrap_enabled = false ;
2019-12-10 05:13:02 +01:00
wrap_at = 0 ;
2018-01-26 02:41:17 +01:00
wrap_right_offset = 10 ;
2015-01-02 19:08:40 +01:00
set_focus_mode ( FOCUS_ALL ) ;
_update_caches ( ) ;
2017-03-05 16:44:50 +01:00
cache . line_spacing = 1 ;
2020-09-18 09:36:10 +02:00
cache . font_size = 16 ;
2018-09-11 18:25:40 +02:00
set_default_cursor_shape ( CURSOR_IBEAM ) ;
2015-06-07 03:06:58 +02:00
2017-04-17 15:24:30 +02:00
indent_size = 4 ;
text . set_indent_size ( indent_size ) ;
2015-01-02 19:08:40 +01:00
text . clear ( ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
h_scroll = memnew ( HScrollBar ) ;
v_scroll = memnew ( VScrollBar ) ;
2016-03-09 00:00:52 +01:00
2015-01-02 19:08:40 +01:00
add_child ( h_scroll ) ;
add_child ( v_scroll ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
updating_scrolls = false ;
selection . active = false ;
2016-03-09 00:00:52 +01:00
2020-02-21 18:28:45 +01:00
h_scroll - > connect ( " value_changed " , callable_mp ( this , & TextEdit : : _scroll_moved ) ) ;
v_scroll - > connect ( " value_changed " , callable_mp ( this , & TextEdit : : _scroll_moved ) ) ;
2016-03-09 00:00:52 +01:00
2020-02-21 18:28:45 +01:00
v_scroll - > connect ( " scrolling " , callable_mp ( this , & TextEdit : : _v_scroll_input ) ) ;
2017-08-22 21:02:08 +02:00
2017-03-05 16:44:50 +01:00
cursor_changed_dirty = false ;
text_changed_dirty = false ;
2016-03-09 00:00:52 +01:00
2020-10-11 19:44:53 +02:00
selection . selecting_mode = SelectionMode : : SELECTION_MODE_NONE ;
2017-03-05 16:44:50 +01:00
selection . selecting_line = 0 ;
selection . selecting_column = 0 ;
selection . selecting_text = false ;
selection . active = false ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
block_caret = false ;
caret_blink_enabled = false ;
2016-05-09 20:21:55 +02:00
caret_blink_timer = memnew ( Timer ) ;
add_child ( caret_blink_timer ) ;
caret_blink_timer - > set_wait_time ( 0.65 ) ;
2020-02-21 18:28:45 +01:00
caret_blink_timer - > connect ( " timeout " , callable_mp ( this , & TextEdit : : _toggle_draw_caret ) ) ;
2016-05-09 20:21:55 +02:00
cursor_set_blink_enabled ( false ) ;
2017-12-20 02:36:47 +01:00
right_click_moves_caret = true ;
2016-05-09 20:21:55 +02:00
2017-03-05 16:44:50 +01:00
idle_detect = memnew ( Timer ) ;
2015-01-02 19:08:40 +01:00
add_child ( idle_detect ) ;
idle_detect - > set_one_shot ( true ) ;
2017-01-05 13:16:00 +01:00
idle_detect - > set_wait_time ( GLOBAL_GET ( " gui/timers/text_edit_idle_detect_sec " ) ) ;
2020-02-21 18:28:45 +01:00
idle_detect - > connect ( " timeout " , callable_mp ( this , & TextEdit : : _push_current_op ) ) ;
2015-12-09 19:56:41 +01:00
2017-03-05 16:44:50 +01:00
click_select_held = memnew ( Timer ) ;
2015-12-09 19:56:41 +01:00
add_child ( click_select_held ) ;
click_select_held - > set_wait_time ( 0.05 ) ;
2020-02-21 18:28:45 +01:00
click_select_held - > connect ( " timeout " , callable_mp ( this , & TextEdit : : _click_selection_held ) ) ;
2016-03-09 00:00:52 +01:00
2017-03-05 16:44:50 +01:00
current_op . type = TextOperation : : TYPE_NONE ;
undo_enabled = true ;
2020-04-13 14:30:56 +02:00
undo_stack_max_size = GLOBAL_GET ( " gui/common/text_edit_undo_stack_max_size " ) ;
2020-04-02 01:20:12 +02:00
undo_stack_pos = nullptr ;
2017-03-05 16:44:50 +01:00
setting_text = false ;
last_dblclk = 0 ;
current_op . version = 0 ;
version = 0 ;
saved_version = 0 ;
completion_enabled = false ;
completion_active = false ;
completion_line_ofs = 0 ;
2020-04-02 01:20:12 +02:00
tooltip_obj = nullptr ;
2020-02-15 02:59:59 +01:00
line_length_guidelines = false ;
line_length_guideline_soft_col = 80 ;
line_length_guideline_hard_col = 100 ;
2017-11-13 00:12:17 +01:00
hiding_enabled = false ;
2017-03-05 16:44:50 +01:00
next_operation_is_complex = false ;
scroll_past_end_of_file_enabled = false ;
auto_brace_completion_enabled = false ;
brace_matching_enabled = false ;
highlight_all_occurrences = false ;
2017-10-22 14:38:00 +02:00
highlight_current_line = false ;
2017-04-17 15:24:30 +02:00
indent_using_spaces = false ;
space_indent = " " ;
2017-03-05 16:44:50 +01:00
auto_indent = false ;
2016-03-31 21:49:30 +02:00
insert_mode = false ;
2017-03-05 16:44:50 +01:00
window_has_focus = true ;
select_identifiers_enabled = false ;
2017-08-19 16:23:45 +02:00
smooth_scroll_enabled = false ;
scrolling = false ;
2019-08-11 20:31:19 +02:00
minimap_clicked = false ;
dragging_minimap = false ;
2019-08-25 15:38:14 +02:00
can_drag_minimap = false ;
minimap_scroll_ratio = 0 ;
minimap_scroll_click_pos = 0 ;
2019-08-11 20:31:19 +02:00
dragging_selection = false ;
2017-08-19 16:23:45 +02:00
target_v_scroll = 0 ;
2017-08-21 20:13:24 +02:00
v_scroll_speed = 80 ;
2019-08-11 20:31:19 +02:00
draw_minimap = false ;
minimap_width = 80 ;
minimap_char_size = Point2 ( 1 , 2 ) ;
minimap_line_spacing = 1 ;
2016-05-17 01:25:17 +02:00
2019-08-08 02:09:46 +02:00
selecting_enabled = true ;
2017-03-05 16:44:50 +01:00
context_menu_enabled = true ;
2019-08-08 02:09:46 +02:00
shortcut_keys_enabled = true ;
2017-03-05 16:44:50 +01:00
menu = memnew ( PopupMenu ) ;
2016-05-17 01:25:17 +02:00
add_child ( menu ) ;
2020-09-18 09:36:10 +02:00
menu_dir = memnew ( PopupMenu ) ;
menu_dir - > set_name ( " DirMenu " ) ;
menu_dir - > add_radio_check_item ( RTR ( " Same as layout direction " ) , MENU_DIR_INHERITED ) ;
menu_dir - > add_radio_check_item ( RTR ( " Auto-detect direction " ) , MENU_DIR_AUTO ) ;
menu_dir - > add_radio_check_item ( RTR ( " Left-to-right " ) , MENU_DIR_LTR ) ;
menu_dir - > add_radio_check_item ( RTR ( " Right-to-left " ) , MENU_DIR_RTL ) ;
menu_dir - > set_item_checked ( menu_dir - > get_item_index ( MENU_DIR_INHERITED ) , true ) ;
menu - > add_child ( menu_dir ) ;
menu_ctl = memnew ( PopupMenu ) ;
menu_ctl - > set_name ( " CTLMenu " ) ;
menu_ctl - > add_item ( RTR ( " Left-to-right mark (LRM) " ) , MENU_INSERT_LRM ) ;
menu_ctl - > add_item ( RTR ( " Right-to-left mark (RLM) " ) , MENU_INSERT_RLM ) ;
menu_ctl - > add_item ( RTR ( " Start of left-to-right embedding (LRE) " ) , MENU_INSERT_LRE ) ;
menu_ctl - > add_item ( RTR ( " Start of right-to-left embedding (RLE) " ) , MENU_INSERT_RLE ) ;
menu_ctl - > add_item ( RTR ( " Start of left-to-right override (LRO) " ) , MENU_INSERT_LRO ) ;
menu_ctl - > add_item ( RTR ( " Start of right-to-left override (RLO) " ) , MENU_INSERT_RLO ) ;
menu_ctl - > add_item ( RTR ( " Pop direction formatting (PDF) " ) , MENU_INSERT_PDF ) ;
menu_ctl - > add_separator ( ) ;
menu_ctl - > add_item ( RTR ( " Arabic letter mark (ALM) " ) , MENU_INSERT_ALM ) ;
menu_ctl - > add_item ( RTR ( " Left-to-right isolate (LRI) " ) , MENU_INSERT_LRI ) ;
menu_ctl - > add_item ( RTR ( " Right-to-left isolate (RLI) " ) , MENU_INSERT_RLI ) ;
menu_ctl - > add_item ( RTR ( " First strong isolate (FSI) " ) , MENU_INSERT_FSI ) ;
menu_ctl - > add_item ( RTR ( " Pop direction isolate (PDI) " ) , MENU_INSERT_PDI ) ;
menu_ctl - > add_separator ( ) ;
menu_ctl - > add_item ( RTR ( " Zero width joiner (ZWJ) " ) , MENU_INSERT_ZWJ ) ;
menu_ctl - > add_item ( RTR ( " Zero width non-joiner (ZWNJ) " ) , MENU_INSERT_ZWNJ ) ;
menu_ctl - > add_item ( RTR ( " Word joiner (WJ) " ) , MENU_INSERT_WJ ) ;
menu_ctl - > add_item ( RTR ( " Soft hyphen (SHY) " ) , MENU_INSERT_SHY ) ;
menu - > add_child ( menu_ctl ) ;
2019-08-18 17:56:24 +02:00
readonly = true ; // Initialise to opposite first, so we get past the early-out in set_readonly.
2019-04-22 01:09:52 +02:00
set_readonly ( false ) ;
2020-02-21 18:28:45 +01:00
menu - > connect ( " id_pressed " , callable_mp ( this , & TextEdit : : menu_option ) ) ;
2020-09-18 09:36:10 +02:00
menu_dir - > connect ( " id_pressed " , callable_mp ( this , & TextEdit : : menu_option ) ) ;
menu_ctl - > connect ( " id_pressed " , callable_mp ( this , & TextEdit : : menu_option ) ) ;
2019-04-12 04:21:48 +02:00
first_draw = true ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
TextEdit : : ~ TextEdit ( ) {
2017-12-16 20:34:16 +01:00
}
2018-04-02 13:41:44 +02:00
///////////////////////////////////////////////////////////////////////////////
2020-03-07 12:17:18 +01:00
Dictionary TextEdit : : _get_line_syntax_highlighting ( int p_line ) {
2020-05-03 18:08:15 +02:00
return syntax_highlighter . is_null ( ) & & ! setting_text ? Dictionary ( ) : syntax_highlighter - > get_line_syntax_highlighting ( p_line ) ;
2018-04-02 13:41:44 +02:00
}