### Godot version

v4.0.beta.custom_build [13f0158]

### System information

Windows 10

### Issue description

When doing operations (`begin_complex_operation` and `end_complex_operation`) with multiple selections in TextEdit, some of these selections don't revert upon undo. This behavior is affects some of script editor's operations, like duplicate selection and delete lines.

### Steps to reproduce

Open script editor, make multiple selections. Keep in mind what you have selected. Do duplicate selection (ctrl+shift+D). Undo (ctrl+Z). Compare the result to what you remember.

### Minimal reproduction project

N/A
if (is_move_caret_on_right_click_enabled()) {
					if (has_selection()) {
						for (int i = 0; i < get_caret_count(); i++) {
							int from_line = get_selection_from_line(i);
							int to_line = get_selection_to_line(i);
							int from_column = get_selection_from_column(i);
							int to_column = get_selection_to_column(i);
							if (row >= from_line && row <= to_line && (row != from_line || col >= from_column) && (row != to_line || col <= to_column)) {
								
								selection_clicked = true;
								break;
							}
						}
					}
					if (!selection_clicked) {
						deselect();
						remove_secondary_carets();
						set_caret_line(row, false, false);
						set_caret_column(col);
					}
					merge_overlapping_carets();
				}
				if (context_menu_enabled) {
					_update_context_menu();
					menu->set_position(get_screen_position() + mpos);
					menu->reset_size();
					menu->popup();
					grab_focus();
				}
			}