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

Gracefully handle black formatter with new "Format modified text" option in vscode 1.49 #13919

Closed
ZacharyJoswick opened this issue Sep 14, 2020 · 1 comment
Labels
feature-request Request for new features or functionality

Comments

@ZacharyJoswick
Copy link

VSCode version 1.49 introduces a new "Only format modified text" option which only formats code that has been modified as reported by the version control system. Details of this change can be found on the official vscode release page.

However, users of the python extension cannot utilize this feature if they choose black as their code formatter, and attempts to do so result in an annoying prompt popping up after every save which states Black does not support the "Format Selection" command. This appears to be a somewhat long running issue and was previously reported in microsoft/vscode-black-formatter#176 , but up to this point had not effected many people as it only pops up the message when using the format selection command directly.

However, with this new VSCode change this error message pops up on every save unless you disable the feature entirely, which means you loose it for all other file types where it does function correctly. Regardless of the validity of the arguments from the black developers (as found in 134, 142, 245, 370, 511, 830) this is a very annoying popup to have displayed repeatedly and could be handled better.

There are several things that could be done to better handle this in my opinion. The first and simplest method is to provide the ability to disable this popup as discussed in microsoft/vscode-black-formatter#176. However, there are some other options to fix the behavior entirely.

One option would be to use something like akaihola/darker which essentially does the same thing as the "Only format modified text" option in vscode, but is black specific and has some additional features. This tool seems to work quite well in my experience, and does achieve the desired behavior, but still prevents using the only format modified text option for all other file types, which makes the solution suboptimal.

Another alternative is wbolster/black-macchiato which does similar things to the darker project, but is a bit more focused on black specifically and does not contain the extra features. I do not have any specific experience with this project, but it seems to perform the desired task.

I understand that this is an odd use case, and that the intent of black is to be run against all files all the time, however, there are many cases where one may wish to leave unmodified portions of the code base alone and not re-format them. the linked black issues above include many examples of cases where this behavior is desired, and at the end of the day, this feature was important enough to enough people that it was included in the main VSCode editor.

I'm personally unsure of the best path forward, and am not confident in declaring this as a bug since the behavior is technically desired, however, I see it as more significant due to the 1.49 changes as the error message will now be printed on every single save command (and if you have save on focus change enabled, every time you switch away from a file). That being said, I see this as something many people would use if implemented (look no further than 830 to see this for yourself).

@ZacharyJoswick ZacharyJoswick added triage-needed Needs assignment to the proper sub-team feature-request Request for new features or functionality labels Sep 14, 2020
@brettcannon
Copy link
Member

You can turn this setting off just for Python so you can leave it on globally for those formatters that do have support:

"[python]": {
    "editor.formatOnSaveMode": "file"
}

Otherwise this is basically a duplicate of microsoft/vscode-black-formatter#176.

@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Sep 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

2 participants