### 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 caret = 0; caret < carets.size(); caret++) {
					if (get_caret_line(caret) < 0 || get_caret_line(caret) >= text.size() || get_caret_column(caret) < 0) {
						continue;
					}
					if (get_caret_column(caret) < text[get_caret_line(caret)].length()) {
						
						char32_t c = text[get_caret_line(caret)][get_caret_column(caret)];
						char32_t closec = 0;
						if (c == '[') {
							closec = ']';
						} else if (c == '{') {
							closec = '}';
						} else if (c == '(') {
							closec = ')';
						}
						if (closec != 0) {
							int stack = 1;
							for (int i = get_caret_line(caret); i < text.size(); i++) {
								int from = i == get_caret_line(caret) ? get_caret_column(caret) + 1 : 0;
								for (int j = from; j < text[i].length(); j++) {
									char32_t cc = text[i][j];
									
									if (cc == '"' || cc == '\'') {
										char32_t quotation = cc;
										do {
											j++;
											if (!(j < text[i].length())) {
												break;
											}
											cc = text[i][j];