Repository: spyder-ide/spyder

Issue:
New Pyx file fails to save with "Code Formatting" enabled
### What steps will reproduce the problem?

<!--- You can use Markdown here --->

1. Go to Tools->Preferences->Completion and linting->Code style and formatting and enable "Autoformat files on save" with autopep8 (I'm not sure the provider matters)

2. Create a new file

3. Attempt to save the file with .pyx extension

The file appears to be saved, but is not written to disk. The name appears with an asterisk next to it as unsaved in the open file tab. No error message is shown.

This behavior did not exist on Spyder 4. I performed a clean install of Anaconda and did only 'conda update --all' and 'conda install spyder=5'.


Discussion:
# Versions
Spyder 5.0.5 | Python 3.8.11 64-bit | Qt 5.9.7 | PyQt5 5.9.2 | Windows 10 

# Dependencies
## Mandatory:
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  1.6.0 (OK)
cookiecutter >=1.6.0          :  1.7.2 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.6.0               :  7.26.0 (OK)
jedi =0.17.2                  :  0.17.2 (OK)
jsonschema >=3.2.0            :  3.2.0 (OK)
keyring >=17.0.0              :  23.0.1 (OK)
nbconvert >=4.0               :  6.1.0 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
paramiko >=2.4.0              :  2.7.2 (OK)
parso =0.7.0                  :  0.7.0 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.8.0 (OK)
pygments >=2.0                :  2.9.0 (OK)
pylint >=1.0                  :  2.9.6 (OK)
pyls >=0.36.2;<1.0.0          :  0.36.2 (OK)
pyls_black >=0.4.6            :  0.4.6 (OK)
pyls_spyder >=0.3.2;<0.4.0    :  0.3.2 (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.1.10 (OK)
qtawesome >=1.0.2             :  1.0.2 (OK)
qtconsole >=5.1.0             :  5.1.0 (OK)
qtpy >=1.5.0                  :  1.9.0 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=39.0.0           :  52.0.0.post20210125 (OK)
sphinx >=0.6.6                :  4.0.2 (OK)
spyder_kernels >=2.0.4;<2.1.0 :  2.0.5 (OK)
textdistance >=4.2.0          :  4.2.1 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  1.0.2 (OK)
zmq >=17                      :  20.0.0 (OK)

## Optional:
cython >=0.21                 :  0.29.24 (OK)
matplotlib >=2.0.0            :  3.4.2 (OK)
numpy >=1.7                   :  1.20.3 (OK)
pandas >=1.1.1                :  1.3.1 (OK)
scipy >=0.17.0                :  1.6.2 (OK)
sympy >=0.7.3                 :  1.8 (OK)

Hi @Meisterrichter. Thanks for reporting. I can successfully reproduce this behaviour in Spyder 6.0.0 dev0 and Spyder 5.0.5 as well. 
@ccordoba12 any thoughts on this? This is probably a bug we need to fix.
Yep, I totally agree. We need to fix this and we'll try to in our of our future releases.

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py
--- a/spyder/plugins/editor/widgets/editor.py
+++ b/spyder/plugins/editor/widgets/editor.py
@@ -1919,8 +1919,15 @@ def save(self, index=None, force=False, save_new_files=True):
             self.set_os_eol_chars(osname=osname)
 
         try:
-            if self.format_on_save and finfo.editor.formatting_enabled:
-                # Wait for document autoformat and then save
+            if (
+                self.format_on_save
+                and finfo.editor.formatting_enabled
+                and finfo.editor.is_python()
+            ):
+                # Wait for document autoformat in case it is a Python file
+                # and then save.
+                # Just trigger the autoformat for Python files.
+                # See spyder-ide/spyder#19344
 
                 # Waiting for the autoformat to complete is needed
                 # when the file is going to be closed after saving.


Test patch: