### 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 (!selecting_enabled || !is_multiple_carets_enabled()) {
		return;
	}
	if (text.size() == 1 && text[0].length() == 0) {
		return;
	}
	
	
	int caret = get_caret_count() - 1;
	if (!has_selection(caret)) {
		select_word_under_caret(caret);
		return;
	}
	const String &highlighted_text = get_selected_text(caret);
	int column = get_selection_from_column(caret) + 1;
	int line = get_caret_line(caret);
	const Point2i next_occurrence = search(highlighted_text, SEARCH_MATCH_CASE, line, column);
	if (next_occurrence.x == -1 || next_occurrence.y == -1) {
		return;
	}
	int to_column = get_selection_to_column(caret) + 1;
	int end = next_occurrence.x + (to_column - column);
	int new_caret = add_caret(next_occurrence.y, end);
	if (new_caret != -1) {
		select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret);
		adjust_viewport_to_caret(new_caret);
		merge_overlapping_carets();
	}
}
void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column, int p_caret) {