### 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
int char_x_ofs = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * chars)) + tabs;
								if (rtl) {
									RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(size.width - char_x_ofs - minimap_char_size.x * characters, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), previous_color);
								} else {
									RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_x_ofs, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), previous_color);
								}
							}
							if (out_of_bounds) {
								break;
							}
							if (str[j] == '\t') {
								tabs += minimap_tab_size;
							}
							previous_color = color;
							characters = 0;
						}
					}
				}
			}
			int top_limit_y = 0;
			int bottom_limit_y = get_size().height;
			if (!editable) {
				top_limit_y += theme_cache.style_readonly->get_margin(SIDE_TOP);
				bottom_limit_y -= theme_cache.style_readonly->get_margin(SIDE_BOTTOM);
			} else {
				top_limit_y += theme_cache.style_normal->get_margin(SIDE_TOP);
				bottom_limit_y -= theme_cache.style_normal->get_margin(SIDE_BOTTOM);
			}
			
			line_drawing_cache.clear();