### 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
_update_minimap_click();
					if (dragging_minimap) {
						return;
					}
				}
				int caret = carets.size() - 1;
				int prev_col = get_caret_column(caret);
				int prev_line = get_caret_line(caret);
				const int triple_click_timeout = 600;
				const int triple_click_tolerance = 5;
				bool is_triple_click = (!mb->is_double_click() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < triple_click_timeout && mb->get_position().distance_to(last_dblclk_pos) < triple_click_tolerance);
				if (!mb->is_double_click() && !is_triple_click) {
					if (mb->is_alt_pressed()) {
						prev_line = row;
						prev_col = col;
						
						if (carets.size() > 1) {
							for (int i = 0; i < carets.size(); i++) {
								
								if ((get_caret_column(i) == col && get_caret_line(i) == row) || is_mouse_over_selection(true, i)) {
									remove_caret(i);
									last_dblclk = 0;
									return;
								}
							}
						}
						if (is_mouse_over_selection()) {
							return;
						}
						caret = add_caret(row, col);