New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format selection with Black does not work on indented code #21460
Comments
Hi @remisalmon thank you for the feedback! I was able to reproduce this, although I'm not sure if this is an issue with Spyder or a Maybe we should update the action text to |
It's a black issue. Their maintainers decided to not offer a way to format portions of code. However, there's a way to support this through the Python language server, but I haven't found the time to do it, sorry.
I think we should try to support range formatting with Black because it's really useful. I'll try to make that happen before the end of the year. |
Looks like the black issue linked above would unblock this. Thanks for looking into it! |
That issue was fixed in black 23.11.0: https://github.com/psf/black/releases/tag/23.11.0 The way it works is by passing a list of line ranges to https://black.readthedocs.io/en/stable/contributing/reference/reference_functions.html#black.format_file_contents that parses the entire file and only format the selected lines. It does return the entire file as well so the fix is not trivial. |
I open a pull request that may fix this at python-lsp/python-lsp-black#52. I have not been able to test it in Spyder yet (if anyone knows how to install a forked dependency of a submodule with conda...). |
Thanks a lot @remisalmon!
You need to run the command:
in your local fork of |
Did not know I could use pip with conda, the fix does work in Spyder!
Screencast.from.2023-12-04.19-01-56.webm |
Yep, you can if you use And things look pretty cool! Thanks again for your work on this. |
Issue Report Checklist
conda update spyder
(orpip
, if not using Anaconda)jupyter qtconsole
(if console-related)spyder --reset
Problem Description
The "Format file or selection with Black" option does not work when selecting code that is indented.
What steps reproduce the problem?
What is the expected output? What do you see instead?
With
selecting the second line and clicking "Format file or selection with Black" works and shows:
With
selecting the second line and clicking "Format file or selection with Black" does not works and shows:
Paste Traceback/Error Below (if applicable)
None.
Versions
Dependencies
The text was updated successfully, but these errors were encountered: