### 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
Size2 size = get_size();
			bool rtl = is_layout_rtl();
			if ((!has_focus() && !(menu && menu->has_focus())) || !window_has_focus) {
				draw_caret = false;
			}
			_update_scrollbars();
			RID ci = get_canvas_item();
			RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
			int xmargin_beg = theme_cache.style_normal->get_margin(SIDE_LEFT) + gutters_width + gutter_padding;
			int xmargin_end = size.width - theme_cache.style_normal->get_margin(SIDE_RIGHT);
			if (draw_minimap) {
				xmargin_end -= minimap_width;
			}
			
			theme_cache.style_normal->draw(ci, Rect2(Point2(), size));
			if (!editable) {
				theme_cache.style_readonly->draw(ci, Rect2(Point2(), size));
				draw_caret = is_drawing_caret_when_editable_disabled();
			}
			if (has_focus()) {
				theme_cache.style_focus->draw(ci, Rect2(Point2(), size));
			}
			int visible_rows = get_visible_line_count() + 1;
			Color color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color;
			if (theme_cache.background_color.a > 0.01) {
				RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), theme_cache.background_color);
			}
			Vector<BraceMatchingData> brace_matching;
			if (highlight_matching_braces_enabled) {
				brace_matching.resize(carets.size());