### 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
DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
				Point2 pos = get_global_position() + get_caret_draw_pos();
				if (get_window()->get_embedder()) {
					pos += get_viewport()->get_popup_base_transform().get_origin();
				}
				DisplayServer::get_singleton()->window_set_ime_position(pos, get_viewport()->get_window_id());
			}
			if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
				int caret_start = -1;
				int caret_end = -1;
				if (!has_selection(0)) {
					String full_text = _base_get_text(0, 0, get_caret_line(), get_caret_column());
					caret_start = full_text.length();
				} else {
					String pre_text = _base_get_text(0, 0, get_selection_from_line(), get_selection_from_column());
					String post_text = get_selected_text(0);
					caret_start = pre_text.length();
					caret_end = caret_start + post_text.length();
				}
				DisplayServer::get_singleton()->virtual_keyboard_show(get_text(), get_global_rect(), DisplayServer::KEYBOARD_TYPE_MULTILINE, -1, caret_start, caret_end);
			}
		} break;
		case NOTIFICATION_FOCUS_EXIT: {
			if (caret_blink_enabled) {
				caret_blink_timer->stop();
			}
			if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
				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());
			}