Repository: godot
Issue: Code editor: deleting folder containing scripts does not auto-close scripts. Batch closing them later causes errors.
Labels: bug,topic:editor

### Godot version

v4.0.stable.official [92bee43ad]

### System information

Linux Ubuntu 22.04 with Unity desktop

### Issue description

Deleting a script open in the code editor auto-closes it, even if it has unsaved changes. This works as expected when directly selecting and deleting one or multiple scripts.

When deleting a *folder* containing open scripts, however, the auto-close does not work as expected. Instead, the deleted scripts remain open in the Code editor, and their name is changed to "Uns[unsaved]" with the dirty symbol "(*)".

![Godot 4 0 - Delete folder with opened scripts leaves scripts unsaved](https://user-images.githubusercontent.com/1067772/226112653-4dcf0922-326e-43ef-a3f5-e47784c3546f.gif)

Furthermore, if you try to Close All or Close Other Tabs to batch close such leftover files, then press either Discard, Cancel or Save, you can encounter one of the three following errors:

Error 1:

After the first Discard/Cancel/Save, you may see:

> Signal 'visibility_changed' is already connected to given callable 'ScriptEditor::_queue_close_tabs' in that object.

If you selected Discard or Save, the file is closed (it is never saved). The second file will be renamed just "[unsaved](*)".

You can then Discard/Cancel/Save again, but if whatever you choose, it will stop the batch operation there.

In other words, you can close at most 2 files.

Here is a GIF focused on the batch close stopping after two files:

![Godot 4 0 - Close other tabs on unsaved deleted scripts stops suddenly after visibility_changed and viewport errors](https://user-images.githubusercontent.com/1067772/226112162-b5916a0f-7e59-41cb-9d15-199dcc7d1a9a.gif)

Error 2

Sometimes, instead of Error 1, batch close works, but you'll see a repeated error (one per file). For 5 files (total, saved or unsaved deleted):

> scene/gui/tab_bar.cpp:597 - Index p_current = 4 is out of bounds (get_tab_count() = 4).
  scene/gui/tab_bar.cpp:597 - Index p_current = 3 is out of bounds (get_tab_count() = 3).
  scene/gui/tab_bar.cpp:597 - Index p_current = 2 is out of bounds (get_tab_count() = 2).
  scene/gui/tab_bar.cpp:597 - Index p_current = 1 is out of bounds (get_tab_count() = 1).
  scene/gui/tab_bar.cpp:597 - Index p_current = 0 is out of bounds (get_tab_count() = 0).

Error 3:

Often, after Error 1 or Error 2, you'll also see this:

>  Can't make active a Viewport that is already active.
  Can't make active a Viewport that is already active.

Of course, fixing the first issue should fix the later errors. However, if there are other ways to make unsaved deleted files appear, then the errors on batch close should be fixed too.

(one technique to have extra safety is to fix the later errors first, then fix the first bug at the end)

### Steps to reproduce

1. Open the Script editor and open new_script and all its numbered variants under new folder
2. Delete new folder

Main bug: the scripts deleted together with the new folder are not auto-closed. Instead, they remain open in the Code editor, and their name is changed to "Uns[unsaved]" with the dirty symbol "(*)"

Then to trigger side-effect errors:

3. Right-click or File > Close All or Close Other Tabs
4. In popup, select Discard/Cancel/Save (Discard shows better how files don't all close, and Save shows better how files don't save)
5. See Error 1 or Error 2 + optionally Error 3
6. If you see Error 1, you have a second popup. Select Discard/Cancel/Save again.
7. Note that there is no further popup, so we could not close all the files.

### Minimal reproduction project

[v4.0 - Close other tabs with deleted scripts fails.zip](https://github.com/godotengine/godot/files/11008793/v4.0.-.Close.other.tabs.with.deleted.scripts.fails.zip)