### 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), get_caret_column(caret_idx), next_line, next_column);
		adjust_carets_after_edit(caret_idx, get_caret_line(caret_idx), get_caret_column(caret_idx), next_line, next_column);
	}
	
	carets_to_remove.sort();
	for (int i = carets_to_remove.size() - 1; i >= 0; i--) {
		remove_caret(carets_to_remove[i]);
	}
	
	merge_overlapping_carets();
	end_action();
	queue_redraw();
}
void TextEdit::_move_caret_document_start(bool p_select) {
	remove_secondary_carets();
	if (p_select) {
		_pre_shift_selection(0);
	} else {
		deselect();
	}
	set_caret_line(0, false);
	set_caret_column(0);
	if (p_select) {
		_post_shift_selection(0);
	}
}
void TextEdit::_move_caret_document_end(bool p_select) {