### 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 (get_caret_column(caret_edit_order[j]) > column || (has_selection(caret_edit_order[j]) && get_selection_from_column(caret_edit_order[j]) > column)) {
					deselect(caret_edit_order[j]);
					carets_to_remove.push_back(caret_edit_order[j]);
					set_caret_column(0, caret_idx == 0, caret_idx);
					i = j;
				}
			}
			_remove_text(get_caret_line(caret_idx), column, get_caret_line(caret_idx), from_column);
			set_caret_line(get_caret_line(caret_idx), false, true, 0, caret_idx);
			set_caret_column(column, caret_idx == 0, caret_idx);
			adjust_carets_after_edit(caret_idx, get_caret_line(caret_idx), column, get_caret_line(caret_idx), from_column);
			
			if (overlapping_caret_index != -1) {
				backspace(overlapping_caret_index);
				i++;
				carets_to_remove.push_back(overlapping_caret_index);
				set_caret_column(get_caret_column(overlapping_caret_index), caret_idx == 0, caret_idx);
			}
			continue;
		}
	}
	
	carets_to_remove.sort();
	for (int i = carets_to_remove.size() - 1; i >= 0; i--) {
		remove_caret(carets_to_remove[i]);
	}
	end_action();
}
void TextEdit::_delete(bool p_word, bool p_all_to_right) {