### 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
if (!is_dirty) {
		return;
	}
	max_width = -1;
	line_height = -1;
	if (font.is_valid() && font_size > 0) {
		font_height = font->get_height(font_size);
	}
	for (int i = 0; i < text.size(); i++) {
		invalidate_cache(i, -1, true);
	}
	is_dirty = false;
}
void TextEdit::Text::clear() {
	text.clear();
	max_width = -1;
	line_height = -1;
	Line line;
	line.gutters.resize(gutter_count);
	line.data = "";
	text.insert(0, line);
	invalidate_cache(0, -1, true);
}
int TextEdit::Text::get_max_width() const {
	return max_width;
}
void TextEdit::Text::set(int p_line, const String &p_text, const Array &p_bidi_override) {