@@ -6004,7 +6004,7 @@ int TextEdit::get_total_visible_line_count() const {
60046004void TextEdit::adjust_viewport_to_caret (int p_caret) {
60056005 ERR_FAIL_INDEX (p_caret, carets.size ());
60066006
6007- // Make sure Caret is visible on the screen.
6007+ // Move viewport so the caret is visible on the screen vertically .
60086008 scrolling = false ;
60096009 minimap_clicked = false ;
60106010
@@ -6024,105 +6024,19 @@ void TextEdit::adjust_viewport_to_caret(int p_caret) {
60246024 set_line_as_last_visible (cur_line, cur_wrap);
60256025 }
60266026
6027- int visible_width = get_size ().width - theme_cache.style_normal ->get_minimum_size ().width - gutters_width - gutter_padding;
6028- if (draw_minimap) {
6029- visible_width -= minimap_width;
6030- }
6031- if (v_scroll->is_visible_in_tree ()) {
6032- visible_width -= v_scroll->get_combined_minimum_size ().width ;
6033- }
6034- visible_width -= 20 ; // Give it a little more space.
6035-
6036- if (visible_width <= 0 ) {
6037- // Not resized yet.
6038- return ;
6039- }
6040-
6041- Vector2i caret_pos;
6042-
6043- // Get position of the start of caret.
6044- if (has_ime_text () && ime_selection.x != 0 ) {
6045- caret_pos.x = _get_column_x_offset_for_line (get_caret_column (p_caret) + ime_selection.x , get_caret_line (p_caret), get_caret_column (p_caret) + ime_selection.x );
6046- } else {
6047- caret_pos.x = _get_column_x_offset_for_line (get_caret_column (p_caret), get_caret_line (p_caret), get_caret_column (p_caret));
6048- }
6049-
6050- // Get position of the end of caret.
6051- if (has_ime_text ()) {
6052- if (ime_selection.y > 0 ) {
6053- caret_pos.y = _get_column_x_offset_for_line (get_caret_column (p_caret) + ime_selection.x + ime_selection.y , get_caret_line (p_caret), get_caret_column (p_caret) + ime_selection.x + ime_selection.y );
6054- } else {
6055- caret_pos.y = _get_column_x_offset_for_line (get_caret_column (p_caret) + ime_text.length (), get_caret_line (p_caret), get_caret_column (p_caret) + ime_text.length ());
6056- }
6057- } else {
6058- caret_pos.y = caret_pos.x ;
6059- }
6060-
6061- if (MAX (caret_pos.x , caret_pos.y ) > (first_visible_col + visible_width)) {
6062- first_visible_col = MAX (caret_pos.x , caret_pos.y ) - visible_width + 1 ;
6063- }
6064-
6065- if (MIN (caret_pos.x , caret_pos.y ) < first_visible_col) {
6066- first_visible_col = MIN (caret_pos.x , caret_pos.y );
6067- }
6068- h_scroll->set_value (first_visible_col);
6069-
6070- queue_redraw ();
6027+ _adjust_viewport_to_caret_horizontally (p_caret);
60716028}
60726029
60736030void TextEdit::center_viewport_to_caret (int p_caret) {
60746031 ERR_FAIL_INDEX (p_caret, carets.size ());
60756032
6076- // Move viewport so the caret is in the center of the screen.
6033+ // Move viewport so the caret is in the center of the screen vertically .
60776034 scrolling = false ;
60786035 minimap_clicked = false ;
60796036
60806037 set_line_as_center_visible (get_caret_line (p_caret), get_caret_wrap_index (p_caret));
6081- int visible_width = get_size ().width - theme_cache.style_normal ->get_minimum_size ().width - gutters_width - gutter_padding;
6082- if (draw_minimap) {
6083- visible_width -= minimap_width;
6084- }
6085- if (v_scroll->is_visible_in_tree ()) {
6086- visible_width -= v_scroll->get_combined_minimum_size ().width ;
6087- }
6088- visible_width -= 20 ; // Give it a little more space.
6089-
6090- if (get_line_wrapping_mode () != LineWrappingMode::LINE_WRAPPING_NONE) {
6091- // Center x offset.
6092-
6093- Vector2i caret_pos;
60946038
6095- // Get position of the start of caret.
6096- if (has_ime_text () && ime_selection.x != 0 ) {
6097- caret_pos.x = _get_column_x_offset_for_line (get_caret_column (p_caret) + ime_selection.x , get_caret_line (p_caret), get_caret_column (p_caret) + ime_selection.x );
6098- } else {
6099- caret_pos.x = _get_column_x_offset_for_line (get_caret_column (p_caret), get_caret_line (p_caret), get_caret_column (p_caret));
6100- }
6101-
6102- // Get position of the end of caret.
6103- if (has_ime_text ()) {
6104- if (ime_selection.y > 0 ) {
6105- caret_pos.y = _get_column_x_offset_for_line (get_caret_column (p_caret) + ime_selection.x + ime_selection.y , get_caret_line (p_caret), get_caret_column (p_caret) + ime_selection.x + ime_selection.y );
6106- } else {
6107- caret_pos.y = _get_column_x_offset_for_line (get_caret_column (p_caret) + ime_text.length (), get_caret_line (p_caret), get_caret_column (p_caret) + ime_text.length ());
6108- }
6109- } else {
6110- caret_pos.y = caret_pos.x ;
6111- }
6112-
6113- if (MAX (caret_pos.x , caret_pos.y ) > (first_visible_col + visible_width)) {
6114- first_visible_col = MAX (caret_pos.x , caret_pos.y ) - visible_width + 1 ;
6115- }
6116-
6117- if (MIN (caret_pos.x , caret_pos.y ) < first_visible_col) {
6118- first_visible_col = MIN (caret_pos.x , caret_pos.y );
6119- }
6120- } else {
6121- first_visible_col = 0 ;
6122- }
6123- h_scroll->set_value (first_visible_col);
6124-
6125- queue_redraw ();
6039+ _adjust_viewport_to_caret_horizontally (p_caret);
61266040}
61276041
61286042/* Minimap */
@@ -8212,6 +8126,70 @@ void TextEdit::_scroll_lines_down() {
82128126 merge_overlapping_carets ();
82138127}
82148128
8129+ void TextEdit::_adjust_viewport_to_caret_horizontally (int p_caret) {
8130+ if (get_line_wrapping_mode () != LineWrappingMode::LINE_WRAPPING_NONE) {
8131+ first_visible_col = 0 ;
8132+ h_scroll->set_value (first_visible_col);
8133+ queue_redraw ();
8134+ return ;
8135+ }
8136+
8137+ int visible_width = get_size ().width - theme_cache.style_normal ->get_minimum_size ().width - gutters_width - gutter_padding;
8138+ if (draw_minimap) {
8139+ visible_width -= minimap_width;
8140+ }
8141+ if (v_scroll->is_visible_in_tree ()) {
8142+ visible_width -= v_scroll->get_combined_minimum_size ().width ;
8143+ }
8144+ visible_width -= 20 ; // Give it a little more space.
8145+
8146+ if (visible_width <= 0 ) {
8147+ // Not resized yet.
8148+ return ;
8149+ }
8150+
8151+ int caret_start_pos;
8152+ int caret_end_pos;
8153+ bool prioritize_end = true ;
8154+
8155+ // Get start and end position of the caret.
8156+ if (has_ime_text ()) {
8157+ // Use the size of the IME.
8158+ int ime_start_column = get_caret_column (p_caret) + ime_selection.x ;
8159+ caret_start_pos = _get_column_x_offset_for_line (ime_start_column, get_caret_line (p_caret), ime_start_column);
8160+ int ime_end_column = get_caret_column (p_caret) + (ime_selection.y > 0 ? ime_selection.x + ime_selection.y : ime_text.length ());
8161+ caret_end_pos = _get_column_x_offset_for_line (ime_end_column, get_caret_line (p_caret), ime_end_column);
8162+ prioritize_end = false ;
8163+ } else if (has_selection (p_caret) && get_selection_from_line (p_caret) == get_selection_to_line (p_caret)) {
8164+ // Use selection if it is on one line.
8165+ caret_start_pos = _get_column_x_offset_for_line (get_selection_from_column (p_caret), get_caret_line (p_caret), get_selection_from_column (p_caret));
8166+ caret_end_pos = _get_column_x_offset_for_line (get_selection_to_column (p_caret), get_caret_line (p_caret), get_selection_to_column (p_caret));
8167+ prioritize_end = is_caret_after_selection_origin ();
8168+ } else {
8169+ caret_start_pos = _get_column_x_offset_for_line (get_caret_column (p_caret), get_caret_line (p_caret), get_caret_column (p_caret));
8170+ caret_end_pos = caret_start_pos;
8171+ }
8172+
8173+ if (caret_start_pos > caret_end_pos) {
8174+ // For RTL text.
8175+ SWAP (caret_start_pos, caret_end_pos);
8176+ prioritize_end = !prioritize_end;
8177+ }
8178+
8179+ if (!prioritize_end && caret_end_pos > first_visible_col + visible_width) {
8180+ first_visible_col = caret_end_pos - visible_width + 1 ;
8181+ }
8182+ if (caret_start_pos < first_visible_col) {
8183+ first_visible_col = caret_start_pos;
8184+ }
8185+ if (prioritize_end && caret_end_pos > first_visible_col + visible_width) {
8186+ first_visible_col = caret_end_pos - visible_width + 1 ;
8187+ }
8188+
8189+ h_scroll->set_value (first_visible_col);
8190+ queue_redraw ();
8191+ }
8192+
82158193// Minimap
82168194
82178195void TextEdit::_update_minimap_hover () {
0 commit comments