### 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
continue;
		}
		if (has_selection(c)) {
			
			
			
			deselect(c);
			continue;
		}
		int begin = 0;
		int end = 0;
		const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid());
		for (int i = 0; i < words.size(); i = i + 2) {
			if ((words[i] <= get_caret_column(c) && words[i + 1] >= get_caret_column(c)) || (i == words.size() - 2 && get_caret_column(c) == words[i + 1])) {
				begin = words[i];
				end = words[i + 1];
				break;
			}
		}
		
		if (begin == 0 && end == 0) {
			continue;
		}
		select(get_caret_line(c), begin, get_caret_line(c), end, c);
		
		set_caret_column(end, false, c);
	}
	merge_overlapping_carets();
}
void TextEdit::add_selection_for_next_occurrence() {