### 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_text(get_caret_line(caret_idx), 0, get_caret_line(caret_idx), caret_current_column);
			adjust_carets_after_edit(caret_idx, get_caret_line(caret_idx), caret_current_column, get_caret_line(caret_idx), get_caret_column(caret_idx));
			
			for (int j = i + 1; j < caret_edit_order.size(); j++) {
				
				if (has_selection(caret_edit_order[j]) && get_selection_from_line(caret_edit_order[j]) != get_caret_line(caret_idx) && get_selection_to_line(caret_edit_order[j]) == get_caret_line(caret_idx)) {
					carets.write[caret_edit_order[j]].selection.to_column = 0;
					break;
				}
				
				if (get_caret_line(caret_edit_order[j]) != get_caret_line(caret_idx) || (has_selection(caret_edit_order[j]) && get_selection_from_line(caret_edit_order[j]) != get_caret_line(caret_idx))) {
					break;
				}
				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;
			}
			continue;
		}
		if (p_word) {
			
			int from_column = get_caret_column(caret_idx);
			int column = get_caret_column(caret_idx);
			
			
			if (is_whitespace(text[get_caret_line(caret_idx)][get_caret_column(caret_idx) - 1])) {
				column -= 1;
			}