### 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 (!editable) {
									current_color.a = theme_cache.font_readonly_color.a;
								}
							}
							color = current_color;
							if (j == 0) {
								previous_color = color;
							}
							int xpos = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * j)) + tabs;
							bool out_of_bounds = (xpos >= xmargin_end + minimap_width);
							bool whitespace = is_whitespace(str[j]);
							if (!whitespace) {
								characters++;
								if (j < str.length() - 1 && color == previous_color && !out_of_bounds) {
									continue;
								}
								
								
								if (color != previous_color) {
									characters--;
									j--;
									if (str[j] == '\t') {
										tabs -= minimap_tab_size;
									}
								}
							}
							if (characters > 0) {
								previous_color.a *= 0.6;
								
								int chars = MAX(0, (j - (characters - 1)) - (whitespace ? 1 : 0)) + 1;