### 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
brace_matching.write[caret].open_mismatch = true;
							}
						}
					}
					if (get_caret_column(caret) > 0) {
						char32_t c = text[get_caret_line(caret)][get_caret_column(caret) - 1];
						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 >= 0; i--) {
								int from = i == get_caret_line(caret) ? get_caret_column(caret) - 2 : text[i].length() - 1;
								for (int j = from; j >= 0; j--) {
									char32_t cc = text[i][j];
									
									if (cc == '"' || cc == '\'') {
										char32_t quotation = cc;
										do {
											j--;
											if (!(j >= 0)) {
												break;
											}
											cc = text[i][j];