### 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_index_edit_dirty) {
		return caret_index_edit_order;
	}
	caret_index_edit_order.clear();
	caret_index_edit_order.push_back(0);
	for (int i = 1; i < carets.size(); i++) {
		int j = 0;
		int line = has_selection(i) ? get_selection_to_line(i) : carets[i].line;
		int col = has_selection(i) ? get_selection_to_column(i) : carets[i].column;
		for (; j < caret_index_edit_order.size(); j++) {
			int idx = caret_index_edit_order[j];
			int other_line = has_selection(idx) ? get_selection_to_line(idx) : carets[idx].line;
			int other_col = has_selection(idx) ? get_selection_to_column(idx) : carets[idx].column;
			if (line > other_line || (line == other_line && col > other_col)) {
				break;
			}
		}
		caret_index_edit_order.insert(j, i);
	}
	caret_index_edit_dirty = false;
	return caret_index_edit_order;
}
void TextEdit::adjust_carets_after_edit(int p_caret, int p_from_line, int p_from_col, int p_to_line, int p_to_col) {