### 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
} else {
								RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), theme_cache.current_line_color);
							}
						}
					}
					if (line_wrap_index == 0) {
						
						cache_entry.y_offset = ofs_y;
						int gutter_offset = theme_cache.style_normal->get_margin(SIDE_LEFT);
						for (int g = 0; g < gutters.size(); g++) {
							const GutterInfo gutter = gutters[g];
							if (!gutter.draw || gutter.width <= 0) {
								continue;
							}
							switch (gutter.type) {
								case GUTTER_TYPE_STRING: {
									const String &txt = get_line_gutter_text(line, g);
									if (txt.is_empty()) {
										break;
									}
									Ref<TextLine> tl;
									tl.instantiate();
									tl->add_string(txt, theme_cache.font, theme_cache.font_size);
									int yofs = ofs_y + (row_height - tl->get_size().y) / 2;
									if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) {
										tl->draw_outline(ci, Point2(gutter_offset + ofs_x, yofs), theme_cache.outline_size, theme_cache.outline_color);
									}
									tl->draw(ci, Point2(gutter_offset + ofs_x, yofs), get_line_gutter_item_color(line, g));
								} break;
								case GUTTER_TYPE_ICON: {