### 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
for (int j = 1; j < words.size(); j = j + 2) {
				if (words[j] > column) {
					column = words[j];
					break;
				}
			}
			next_line = line;
			next_column = column;
			
			for (int j = i - 1; j >= 0; j--) {
				if (get_caret_line(caret_edit_order[j]) != get_caret_line(caret_idx)) {
					break;
				}
				if (get_caret_column(caret_edit_order[j]) > column) {
					break;
				}
				carets_to_remove.push_back(caret_edit_order[j]);
			}
		} else {
			
			if (caret_mid_grapheme_enabled) {
				next_column = get_caret_column(caret_idx) < curline_len ? (get_caret_column(caret_idx) + 1) : 0;
			} else {
				next_column = get_caret_column(caret_idx) < curline_len ? TS->shaped_text_next_character_pos(text.get_line_data(get_caret_line(caret_idx))->get_rid(), (get_caret_column(caret_idx))) : 0;
			}
			
			if (i > 0) {
				int prev_caret_idx = caret_edit_order[i - 1];
				if (get_caret_line(prev_caret_idx) == next_line && get_caret_column(prev_caret_idx) == next_column) {
					carets_to_remove.push_back(prev_caret_idx);