Skip to content
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

Closed
6 of 10 tasks
remisalmon opened this issue Oct 26, 2023 · 8 comments
Closed
6 of 10 tasks

Format selection with Black does not work on indented code #21460

remisalmon opened this issue Oct 26, 2023 · 8 comments

Comments

@remisalmon
Copy link
Contributor

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

The "Format file or selection with Black" option does not work when selecting code that is indented.

What steps reproduce the problem?

  1. Indent a line of code that can be formatted with black
  2. Select the line and click "Format file or selection with Black"
  3. The code is not formatted

What is the expected output? What do you see instead?

With

True
test=True

selecting the second line and clicking "Format file or selection with Black" works and shows:

True
test = True

With

if True:
    test=True

selecting the second line and clicking "Format file or selection with Black" does not works and shows:

if True:
    test=True

Paste Traceback/Error Below (if applicable)

None.

Versions

  • Spyder version: 5.4.5 (conda)
  • Python version: 3.11.6 64-bit
  • Qt version: 5.15.8
  • PyQt5 version: 5.15.9
  • Operating System: macOS-13.6-arm64-arm-64bit

Dependencies

# Mandatory:
applaunchservices >=0.3.0                                                   :  0.3.0 (OK)
atomicwrites >=1.2.0                                                        :  1.4.1 (OK)
chardet >=2.0.0                                                             :  5.2.0 (OK)
cloudpickle >=0.5.0                                                         :  3.0.0 (OK)
cookiecutter >=1.6.0                                                        :  2.4.0 (OK)
diff_match_patch >=20181111                                                 :  20230430 (OK)
intervaltree >=3.0.2                                                        :  3.1.0 (OK)
IPython >=7.31.1,<9.0.0,!=8.8.0,!=8.9.0,!=8.10.0,!=8.11.0,!=8.12.0,!=8.12.1 :  8.16.1 (OK)
jedi >=0.17.2,<0.19.0                                                       :  0.18.2 (OK)
jellyfish >=0.7                                                             :  1.0.1 (OK)
jsonschema >=3.2.0                                                          :  4.19.1 (OK)
keyring >=17.0.0                                                            :  24.2.0 (OK)
nbconvert >=4.0                                                             :  7.9.2 (OK)
numpydoc >=0.6.0                                                            :  1.5.0 (OK)
parso >=0.7.0,<0.9.0                                                        :  0.8.3 (OK)
pexpect >=4.4.0                                                             :  4.8.0 (OK)
pickleshare >=0.4                                                           :  0.7.5 (OK)
psutil >=5.3                                                                :  5.9.5 (OK)
pygments >=2.0                                                              :  2.16.1 (OK)
pylint >=2.5.0,<3.0                                                         :  2.17.7 (OK)
pylint_venv >=3.0.2                                                         :  3.0.3 (OK)
pyls_spyder >=0.4.0                                                         :  0.4.0 (OK)
pylsp >=1.7.4,<1.8.0                                                        :  1.7.4 (OK)
pylsp_black >=1.2.0,<3.0.0                                                  :  1.3.0 (OK)
qdarkstyle >=3.0.2,<3.2.0                                                   :  3.1 (OK)
qstylizer >=0.2.2                                                           :  0.2.2 (OK)
qtawesome >=1.2.1                                                           :  1.2.3 (OK)
qtconsole >=5.4.2,<5.5.0                                                    :  5.4.4 (OK)
qtpy >=2.1.0                                                                :  2.4.1 (OK)
rtree >=0.9.7                                                               :  1.1.0 (OK)
setuptools >=49.6.0                                                         :  68.2.2 (OK)
sphinx >=0.6.6                                                              :  7.2.6 (OK)
spyder_kernels >=2.4.4,<2.5.0                                               :  2.4.4 (OK)
textdistance >=4.2.0                                                        :  4.5.0 (OK)
three_merge >=0.1.1                                                         :  0.1.1 (OK)
watchdog >=0.10.3                                                           :  3.0.0 (OK)
zmq >=22.1.0                                                                :  25.1.1 (OK)

# Optional:
cython >=0.21                                                               :  None (NOK)
matplotlib >=3.0.0                                                          :  None (NOK)
numpy >=1.7                                                                 :  1.26.0 (OK)
pandas >=1.1.1                                                              :  2.1.1 (OK)
scipy >=0.17.0                                                              :  None (NOK)
sympy >=0.7.3                                                               :  None (NOK)
@dalthviz
Copy link
Member

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 black limitation. Checking around I found this issue: psf/black#1352 and using autopep8 as formatter provider seems like enables formating an indented code selection properly:

format_selection

Maybe we should update the action text to Format file or selection with Autopep8 when using autopep8 but leave it just as Format file with Black when using black and make it work with or without selection in the case black is selected 🤔 What do you think @ccordoba12 ?

@ccordoba12
Copy link
Member

ccordoba12 commented Oct 31, 2023

although I'm not sure if this is an issue with Spyder or a black limitation

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.

What do you think @ccordoba12 ?

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.

@remisalmon
Copy link
Contributor Author

Looks like the black issue linked above would unblock this. Thanks for looking into it!

@ccordoba12 ccordoba12 modified the milestones: v6.0alphaX, v6.0.1 Nov 16, 2023
@remisalmon
Copy link
Contributor Author

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.

@remisalmon
Copy link
Contributor Author

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...).

@ccordoba12
Copy link
Member

ccordoba12 commented Dec 3, 2023

I open a pull request that may fix this at python-lsp/python-lsp-black#52.

Thanks a lot @remisalmon!

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...).

You need to run the command:

pip install --no-deps -e .

in your local fork of python-lsp-black.

@remisalmon
Copy link
Contributor Author

remisalmon commented Dec 5, 2023

Did not know I could use pip with conda, the fix does work in Spyder!

> conda list | grep black
black                     23.11.0          py39hf3d152e_0    conda-forge
python-lsp-black          1.4.0.dev0               pypi_0    pypi
Screencast.from.2023-12-04.19-01-56.webm

@ccordoba12
Copy link
Member

ccordoba12 commented Dec 5, 2023

Did not know I could use pip with conda, the fix does work in Spyder!

Yep, you can if you use --no-deps to not bring any other pip dependency.

And things look pretty cool! Thanks again for your work on this.

@ccordoba12 ccordoba12 modified the milestones: v6.0.1, v5.5.1 Dec 5, 2023
@ccordoba12 ccordoba12 changed the title Format selection with black does not work on indented code Format selection with Black does not work on indented code Dec 5, 2023
@ccordoba12 ccordoba12 assigned remisalmon and unassigned ccordoba12 Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants