### 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
}
		}
	}
	_update_scrollbars();
	bool dirty_carets = carets.size() != undo_stack_pos->get().end_carets.size();
	if (!dirty_carets) {
		for (int i = 0; i < carets.size(); i++) {
			if (carets[i].line != undo_stack_pos->get().end_carets[i].line || carets[i].column != undo_stack_pos->get().end_carets[i].column) {
				dirty_carets = true;
				break;
			}
		}
	}
	carets = undo_stack_pos->get().end_carets;
	undo_stack_pos = undo_stack_pos->next();
	if (dirty_carets && !caret_pos_dirty) {
		if (is_inside_tree()) {
			MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
		}
		caret_pos_dirty = true;
	}
	adjust_viewport_to_caret();
}
void TextEdit::clear_undo_history() {
	saved_version = 0;
	current_op.type = TextOperation::TYPE_NONE;
	undo_stack_pos = nullptr;
	undo_stack.clear();
}
bool TextEdit::is_insert_text_operation() const {