### 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
int row_height = draw_placeholder ? placeholder_line_height + theme_cache.line_spacing : get_line_height();
			int line = first_vis_line;
			for (int i = 0; i < draw_amount; i++) {
				line++;
				if (line < 0 || line >= (int)text.size()) {
					continue;
				}
				while (_is_line_hidden(line)) {
					line++;
					if (line < 0 || line >= (int)text.size()) {
						break;
					}
				}
				if (line < 0 || line >= (int)text.size()) {
					continue;
				}
				LineDrawingCache cache_entry;
				Dictionary color_map = _get_line_syntax_highlighting(line);
				
				Color current_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color;
				if (draw_placeholder) {
					current_color = theme_cache.font_placeholder_color;
				}
				const Ref<TextParagraph> ldata = draw_placeholder ? placeholder_data_buf : text.get_line_data(line);
				Vector<String> wrap_rows = draw_placeholder ? placeholder_wraped_rows : get_line_wrapped_text(line);
				int line_wrap_amount = draw_placeholder ? placeholder_wraped_rows.size() - 1 : get_line_wrap_count(line);
				for (int line_wrap_index = 0; line_wrap_index <= line_wrap_amount; line_wrap_index++) {
					if (line_wrap_index != 0) {
						i++;
						if (i >= draw_amount) {