### 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
visible_width -= v_scroll->get_combined_minimum_size().width;
	}
	visible_width -= 20; 
	Vector2i caret_pos;
	
	if (ime_text.length() != 0 && ime_selection.x != 0) {
		caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x, get_caret_line(p_caret), get_caret_column(p_caret));
	} else {
		caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
	}
	
	if (ime_text.length() != 0) {
		if (ime_selection.y != 0) {
			caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x + ime_selection.y, get_caret_line(p_caret), get_caret_column(p_caret));
		} else {
			caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_text.size(), get_caret_line(p_caret), get_caret_column(p_caret));
		}
	} else {
		caret_pos.y = caret_pos.x;
	}
	if (MAX(caret_pos.x, caret_pos.y) > (first_visible_col + visible_width)) {
		first_visible_col = MAX(caret_pos.x, caret_pos.y) - visible_width + 1;
	}
	if (MIN(caret_pos.x, caret_pos.y) < first_visible_col) {
		first_visible_col = MIN(caret_pos.x, caret_pos.y);
	}
	h_scroll->set_value(first_visible_col);
	queue_redraw();
}
void TextEdit::center_viewport_to_caret(int p_caret) {