godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.05k stars 65 forks source link

Apply find/replace in files to open files in script editor #9654

Open lostminds opened 2 weeks ago

lostminds commented 2 weeks ago

Describe the project you are working on

A project with many interconnected classes

Describe the problem or limitation you are having in your project

While doing refactoring using the find/replace in files feature is something that always gives me a little pause and I can see it tripping up other users up as well.

The issue is that the changes you make (replacing/renaming variables for example) are always applied to the files on disk. Screenshot 2024-05-02 at 10 50 03

While this is natural for files that you don't have open in the editor it causes issues when you have some files open in the script editor. Because this will result in the file having new changes compared to the version open in the editor you'll immediately get a dialogue like this after the replace:

Screenshot 2024-05-02 at 10 50 19

In this you will need to click Reload to keep the Replace in files change you just made. This could be confusing to the user as they might think they've made a change with the replace operation and now they are asked if this should be saved. If you instead press Resave it will instead save over the change with the version open in the script editor of the file, undoing the replace change you just made, but probably without you realizing this. The change will still be applied to files you didn't have open, so you'll end up with a partial replace when you think you've replaced all instances.

If you have files open with unsaved changes these issues can potentially lead to other issues due the the find/replace in files only looking at the saved files. If you have added an occurrence of the string you're looking for without saving the change yet, Find in files will not find it (so it will also not replace this instance). This is I think unexpected to most users since the the string is there in the editor and can be found if you use the regular single file Find/Replace, so it should be found when looking in all files as well.

Additionally, if you've made other changes to a file and not saved those, but then get the dialogue above to Reload/Resave the file due to the change Replace in files has made pressing Reload will discard all other unsaved changes without any warning, so you might not realize they've been lost.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

To alleviate the issues above I'd suggest to change the way Find in files and Replace in files works to operate on the version open in the script editor if there is one, instead of always looking at the version of the file on disk.

In other words, for files that are open Find/Replace in files will just execute a regular find/replace in this open file (without saving it) while files that are not open will be changed immediately like they are now on replace. While this might seem inconsistent (and technically is) as not all changes are saved immediately, the user experience will I believe be greatly improved as it will remove the need for the confusing Reload/Resave dialogue and avoid possible missed replacements or lost unsaved changes. And as the unsaved changes to open files will be saved when the project is run or next time the user saves anyway the changes will be applied to all files as expected when it matters.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

I imagine the find in files / replace in files code iterates over all the script files in the project. When doing this, before doing the find/replace on each file it should check if the file has an open script editor. If so, apply the change to that open editor text (perhaps reusing the single file editor find/replace functions) instead of using the file on disk.

If this enhancement will not be used often, can it be worked around with a few lines of script?

No

Is there a reason why this should be core and not an add-on in the asset library?

It's a commonly used part of the script editor