### 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
text.write[p_line].data_buf->clear();
	}
	text.write[p_line].data_buf->set_width(width);
	text.write[p_line].data_buf->set_direction((TextServer::Direction)direction);
	text.write[p_line].data_buf->set_break_flags(brk_flags);
	text.write[p_line].data_buf->set_preserve_control(draw_control_chars);
	if (p_ime_text.length() > 0) {
		if (p_text_changed) {
			text.write[p_line].data_buf->add_string(p_ime_text, font, font_size, language);
		}
		if (!p_bidi_override.is_empty()) {
			TS->shaped_text_set_bidi_override(text.write[p_line].data_buf->get_rid(), p_bidi_override);
		}
	} else {
		if (p_text_changed) {
			text.write[p_line].data_buf->add_string(text[p_line].data, font, font_size, language);
		}
		if (!text[p_line].bidi_override.is_empty()) {
			TS->shaped_text_set_bidi_override(text.write[p_line].data_buf->get_rid(), text[p_line].bidi_override);
		}
	}
	if (!p_text_changed) {
		RID r = text.write[p_line].data_buf->get_rid();
		int spans = TS->shaped_get_span_count(r);
		for (int i = 0; i < spans; i++) {
			TS->shaped_set_span_update_font(r, i, font->get_rids(), font_size, font->get_opentype_features());
		}
	}
	
	if (tab_size > 0) {