### 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
deselect();
				}
				remove_secondary_carets();
				set_caret_line(caret_row_tmp, true, false);
				set_caret_column(caret_column_tmp);
				insert_text_at_caret(p_data);
				end_complex_operation();
			}
		} else if (is_mouse_over_selection()) {
			remove_secondary_carets();
			caret_row_tmp = get_selection_from_line();
			caret_column_tmp = get_selection_from_column();
			set_caret_line(caret_row_tmp, true, false);
			set_caret_column(caret_column_tmp);
			insert_text_at_caret(p_data);
			grab_focus();
		} else {
			remove_secondary_carets();
			deselect();
			set_caret_line(caret_row_tmp, true, false);
			set_caret_column(caret_column_tmp);
			insert_text_at_caret(p_data);
			grab_focus();
		}
		if (caret_row_tmp != get_caret_line() || caret_column_tmp != get_caret_column()) {
			select(caret_row_tmp, caret_column_tmp, get_caret_line(), get_caret_column());
		}
	}
}
Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {