### 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 xofs = char_ofs + char_margin + ofs_x + (_get_folded_eol_icon()->get_width() / 2);
						if (xofs >= xmargin_beg && xofs < xmargin_end) {
							int yofs = (text_height - _get_folded_eol_icon()->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
							Color eol_color = _get_code_folding_color();
							eol_color.a = 1;
							_get_folded_eol_icon()->draw(ci, Point2(xofs, ofs_y + yofs), eol_color);
						}
					}
					
					
					const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
					for (int c = 0; c < carets.size(); c++) {
						if (!clipped && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index) {
							carets.write[c].draw_pos.y = ofs_y + ldata->get_line_descent(line_wrap_index);
							if (ime_text.is_empty() || ime_selection.y == 0) {
								
								CaretInfo ts_caret;
								if (!str.is_empty() || !ime_text.is_empty()) {
									
									ts_caret = TS->shaped_text_get_carets(rid, ime_text.is_empty() ? get_caret_column(c) : get_caret_column(c) + ime_selection.x);
								} else {
									
									int h = theme_cache.font->get_height(theme_cache.font_size);
									if (rtl) {
										ts_caret.l_dir = TextServer::DIRECTION_RTL;
										ts_caret.l_caret = Rect2(Vector2(xmargin_end - char_margin + ofs_x, -h / 2), Size2(caret_width * 4, h));
									} else {
										ts_caret.l_dir = TextServer::DIRECTION_LTR;
										ts_caret.l_caret = Rect2(Vector2(char_ofs, -h / 2), Size2(caret_width * 4, h));
									}