### 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
ERR_FAIL_INDEX(p_caret, carets.size());
	if (!selecting_enabled) {
		return;
	}
	p_from_line = CLAMP(p_from_line, 0, text.size() - 1);
	p_from_column = CLAMP(p_from_column, 0, text[p_from_line].length());
	p_to_line = CLAMP(p_to_line, 0, text.size() - 1);
	p_to_column = CLAMP(p_to_column, 0, text[p_to_line].length());
	carets.write[p_caret].selection.from_line = p_from_line;
	carets.write[p_caret].selection.from_column = p_from_column;
	carets.write[p_caret].selection.to_line = p_to_line;
	carets.write[p_caret].selection.to_column = p_to_column;
	carets.write[p_caret].selection.active = true;
	if (get_selection_from_line(p_caret) == get_selection_to_line(p_caret)) {
		if (get_selection_from_column(p_caret) == get_selection_to_column(p_caret)) {