### 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
if (caret == -1) {
							return;
						}
						carets.write[caret].selection.selecting_line = row;
						carets.write[caret].selection.selecting_column = col;
						last_dblclk = 0;
					} else if (!mb->is_shift_pressed() && !is_mouse_over_selection()) {
						caret = 0;
						remove_secondary_carets();
					}
				}
				set_caret_line(row, false, true, 0, caret);
				set_caret_column(col, false, caret);
				selection_drag_attempt = false;
				if (selecting_enabled && mb->is_shift_pressed() && (get_caret_column(caret) != prev_col || get_caret_line(caret) != prev_line)) {
					if (!has_selection(caret)) {
						carets.write[caret].selection.active = true;
						selecting_mode = SelectionMode::SELECTION_MODE_POINTER;
						carets.write[caret].selection.from_column = prev_col;
						carets.write[caret].selection.from_line = prev_line;
						carets.write[caret].selection.to_column = carets[caret].column;
						carets.write[caret].selection.to_line = carets[caret].line;
						if (get_selection_from_line(caret) > get_selection_to_line(caret) || (get_selection_from_line(caret) == get_selection_to_line(caret) && get_selection_from_column(caret) > get_selection_to_column(caret))) {
							SWAP(carets.write[caret].selection.from_column, carets.write[caret].selection.to_column);
							SWAP(carets.write[caret].selection.from_line, carets.write[caret].selection.to_line);
							carets.write[caret].selection.shiftclick_left = false;
						} else {
							carets.write[caret].selection.shiftclick_left = true;
						}
						carets.write[caret].selection.selecting_line = prev_line;