### 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
remove_caret(second_caret);
				caret_edit_order = get_caret_index_edit_order();
				i--;
			}
			continue;
		}
		
		if (has_selection(second_caret)) {
			if (get_caret_line(first_caret) >= get_selection_from_line(second_caret) && get_caret_line(first_caret) <= get_selection_to_line(second_caret) && (get_caret_line(first_caret) > get_selection_from_line(second_caret) || get_caret_column(first_caret) >= get_selection_from_column(second_caret)) && (get_caret_line(first_caret) < get_selection_to_line(second_caret) || get_caret_column(first_caret) <= get_selection_to_column(second_caret))) {
				remove_caret(first_caret);
				caret_edit_order = get_caret_index_edit_order();
				i--;
			}
			continue;
		}
		
		if (get_caret_line(first_caret) == get_caret_line(second_caret) && get_caret_column(first_caret) == get_caret_column(second_caret)) {
			
			if (first_caret < second_caret) {
				remove_caret(first_caret);
			} else {
				remove_caret(second_caret);
			}
			i--;
			caret_edit_order = get_caret_index_edit_order();
			continue;
		}
	}
}
int TextEdit::get_caret_count() const {