### Godot version

v4.1.1-stable_mono_win64

### System information

Windows 11

### Issue description

I removed the node to replace it with another, but when I change the path in the code, the engine crashes for me.

### Steps to reproduce

I did a screen recording as it happens:

https://github.com/godotengine/godot/assets/116289408/b2fd2689-8d82-44e8-8e02-be01674842e1



### Minimal reproduction project

[MixApo.zip](https://github.com/godotengine/godot/files/12208403/MixApo.zip)

remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
				adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
			}
		} else {
			if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && last_char_matches) {
				remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
				adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
			} else if (auto_brace_completion_enabled && pre_brace_pair != -1) {
				insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
				set_caret_column(get_caret_column(i) - auto_brace_completion_pairs[pre_brace_pair].close_key.length(), i == 0, i);
			}
		}
		if (pre_brace_pair == -1 && post_brace_pair == -1 && get_caret_column(i) > 0 && get_caret_column(i) < get_line(caret_line).length()) {
			pre_brace_pair = _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i) + 1);
			if (pre_brace_pair != -1 && pre_brace_pair == _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1)) {
				remove_text(caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
				adjust_carets_after_edit(i, caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
				if (_get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1) != pre_brace_pair) {
					set_caret_column(get_caret_column(i) - 1, i == 0, i);
				}
			}
		}
	}
	end_complex_operation();
	cancel_code_completion();
	if (code_completion_prefixes.has(caret_last_completion_char)) {
		request_code_completion();
	}
}
void CodeEdit::cancel_code_completion() {