### 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
setting_text = true;
	if (!undo_enabled) {
		_clear();
		insert_text_at_caret(p_text);
	}
	if (undo_enabled) {
		remove_secondary_carets();
		set_caret_line(0);
		set_caret_column(0);
		begin_complex_operation();
		deselect();
		_remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
		insert_text_at_caret(p_text);
		end_complex_operation();
	}
	set_caret_line(0);
	set_caret_column(0);
	queue_redraw();
	setting_text = false;
	emit_signal(SNAME("text_set"));
}
String TextEdit::get_text() const {