🐞 `.h` extension as a default when saving files
Description
When I try to rename the file from Untitled to something like main.swift, it generates a header file named main.swift.h
To Reproduce
- Create an empty file
- Click on file name
- Rename it something like
main.swiftormain.cppetc etc .. - Click enter or save
Expected behavior
To generate a file with the extension entered.
Version information
CodeEdit: [e.g. 1.0] macOS: [e.g. 12.3.0] Xcode: [e.g. 13.3]
Additional context
Was your last opened file a .h file? normally it uses the extension of the last opened file.
Hello, I found out why this is happening. It uses the ".h" extension when the file is first opened, but when the extension is changed, it uses that extension. CodeEdit enforces this extension resulting in extensions like '.py.h' or '.swift.py'. This is clearly visible in the video I recorded.
Edit: updated from iCloud link to google photos link (I think that will work this time)
Great find @lucek8123! We need to add logic to not enforce the extension when changing the extension to something other than what it currently is. Would you be able to take this on?
I started poking at this and now understand what the problem is. When the user selects "Create a new file", WelcomeWindowViewController calls CodeEditDocumentController.shared.newDocument(nil). By default, that calls NSDocumentController#openUntitledDocumentAndDisplay() which uses NSDocumentController.defaultType to determine the type of file to create.
The documentation for defaultType explains that it "returns the first Editor type declared by the CFBundleDocumentTypes array in the application's Info.plist". For CodeEdit, that is "C header file".
You can actually reproduce this bug using MacOS TextEdit which always uses an extension of "rtf" for new files and similarly won't respect the user-typed extension if you try to rename it from the title bar. Instead it always appends ".rtf" even if a different extension is provided.
When I get a chance to investigate more, I'll see whether it's possible to easily change the popover that NSDocumentController displays to one that lets the user see and change the document type when renaming the document. It would be nice if it could display the same dialog that is shown if the user saves the untitled document. That dialog shows the file type and lets the user change it.
Another approach would be to override CodeEditDocumentController#newDocument() and display a custom dialog before creating the document. This could let the user select the type of document they are creating, similar to how VSCode handles this case.
Actually the easiest thing is probably to just create a new document named "Untitled" in the user's home or documents directory and open that. Do you all have a preference between letting the user select the document type before opening a new "Untitled" document or just creating a new file named "Untitled" and opening that?
This should be addressed with #1030