### 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
return multi_carets_enabled;
}
int TextEdit::add_caret(int p_line, int p_col) {
	if (!multi_carets_enabled) {
		return -1;
	}
	p_line = CLAMP(p_line, 0, text.size() - 1);
	p_col = CLAMP(p_col, 0, get_line(p_line).length());
	for (int i = 0; i < carets.size(); i++) {
		if (get_caret_line(i) == p_line && get_caret_column(i) == p_col) {
			return -1;
		}
		if (has_selection(i)) {
			if (p_line >= get_selection_from_line(i) && p_line <= get_selection_to_line(i) && (p_line > get_selection_from_line(i) || p_col >= get_selection_from_column(i)) && (p_line < get_selection_to_line(i) || p_col <= get_selection_to_column(i))) {
				return -1;
			}
		}
	}
	carets.push_back(Caret());
	set_caret_line(p_line, false, false, 0, carets.size() - 1);
	set_caret_column(p_col, false, carets.size() - 1);
	caret_index_edit_dirty = true;
	return carets.size() - 1;
}
void TextEdit::remove_caret(int p_caret) {