Repository: godotengine/godot (language: cpp)

Issue:
Selecting text horizontally in TextEdit works strangely
### Tested versions

Godot 4.4 RC2

### System information

Godot v4.4.rc2 - Windows 10 (build 19045) - Multi-window, 1 monitor - Vulkan (Forward+) - dedicated Radeon RX 560 Series (Advanced Micro Devices, Inc.; 31.0.21912.14) - Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz (4 threads)

### Issue description

I encountered one inconvenience in the shader editor, when we select a line from its end to its beginning, and if we want to select a smaller section of text (moving the cursor towards the beginning of the selection), it will be very difficult, since the ScrollContainer will constantly move in the direction of the mouse movement.

It is not easy to explain, so I will attach a video of the problem:

https://github.com/user-attachments/assets/7fc4e4e1-5977-4cd7-a6c0-dde11fe5d233

In this video I try to highlight the code
`ALBEDO = vec3(texture(skin_texture, UV).r,texture(skin_texture, UV).g,texture(skin_texture, UV).b) * final_color;`
but only this part - `vec3(texture(skin_texture, UV).r,texture(skin_texture, UV).g,texture(skin_texture, UV).b) * final_color;`. It's not easy to do this - the code editor instantly moves to the right.

### Steps to reproduce

1. Write a long code in the shader editor or in the script editor.
2. Select the part of the code you need, starting from the end of the line.
3. The text editor scrolls instantly, which is why we do not have time to select the code to the part we need.

### Minimal reproduction project (MRP)

N/A


Discussion / hints:
This is most likely a regression that appeared in **4.4 beta2**, it does not happen in beta1.

4.4 beta1:

https://github.com/user-attachments/assets/98300f0d-f994-4b57-95b7-3f9011693488
Can confirm the regression, will bisect
Bisected to:
* https://github.com/godotengine/godot/pull/93792

CC @kitbdev @KoBeWi 

This was done to solve:
* https://github.com/godotengine/godot/issues/93762

So unintended consequence but if I am to guess this is because the way selection works relies on the broken behavior fixed by this and should be adjusted to not center in this way in these cases, but don't know the relevant code well enough to propose any fix

Fix patch:
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index ad2202315008..887d4eaf00f1 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -6081,7 +6081,7 @@ void TextEdit::adjust_viewport_to_caret(int p_caret) {
 		set_line_as_last_visible(cur_line, cur_wrap);
 	}
 
-	_adjust_viewport_to_caret_horizontally(p_caret);
+	_adjust_viewport_to_caret_horizontally(p_caret, false);
 }
 
 void TextEdit::center_viewport_to_caret(int p_caret) {
@@ -8186,7 +8186,7 @@ void TextEdit::_scroll_lines_down() {
 	merge_overlapping_carets();
 }
 
-void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret) {
+void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret, bool p_maximize_selection) {
 	if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
 		first_visible_col = 0;
 		h_scroll->set_value(first_visible_col);
@@ -8220,7 +8220,7 @@ void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret) {
 		int ime_end_column = get_caret_column(p_caret) + (ime_selection.y > 0 ? ime_selection.x + ime_selection.y : ime_text.length());
 		caret_end_pos = _get_column_x_offset_for_line(ime_end_column, get_caret_line(p_caret), ime_end_column);
 		prioritize_end = false;
-	} else if (has_selection(p_caret) && get_selection_from_line(p_caret) == get_selection_to_line(p_caret)) {
+	} else if (p_maximize_selection && has_selection(p_caret) && get_selection_from_line(p_caret) == get_selection_to_line(p_caret)) {
 		// Use selection if it is on one line.
 		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));
 		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));
diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h
index 21c8781e2648..b8d78283403d 100644
--- a/scene/gui/text_edit.h
+++ b/scene/gui/text_edit.h
@@ -539,7 +539,7 @@ class TextEdit : public Control {
 	void _scroll_lines_up();
 	void _scroll_lines_down();
 
-	void _adjust_viewport_to_caret_horizontally(int p_caret = 0);
+	void _adjust_viewport_to_caret_horizontally(int p_caret = 0, bool p_maximize_selection = true);
 
 	// Minimap.
 	bool draw_minimap = false;


Test patch: