-
Notifications
You must be signed in to change notification settings - Fork 36
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
More gracefully handle the fact that Black does not support "Format Selection" #176
Comments
That's by design from Black itself, so you will have to convince the Black project to support partial formatting (which they have said they don't want to). |
I've re-oriented this to provide some feedback somehow that Black purposefully doesn't support "Format Selection". |
@brettcannon Thanks for your answer but, please note that my original issue was not what this ticket has become now. My issue is with the Python Extension popping up an error window every single time I do a paste. Also, the issue ticket is currently closed, so the re-orientation won't lead to anything. |
@egabrum the closure was from before I thought about redirecting this issue (IOW my bad 😄 ). I have also made the title more generic to also address how to handle |
I should also mention that to turn off any feature in VS Code just for Python, you can do: "[python]": {
"editor.formatOnPaste": false
} |
Thanks. Setting |
I got the same spammy notification from black even though I'm using "python.formatting.provider": "autopep8". |
This needs to be verified.. |
@zephyrite I can't reproduce the error message when autopep8 is the chosen formatter: "python.formatting.provider": "autopep8",
"editor.formatOnPaste": true Did you perhaps try out Black and then flip your setting back to autopep8 without saving initially? |
Maybe we can detect when people turn on Black but have |
Why don't we modify the warning notification to provide an option to disable |
Why don't we just have the logic that respects |
@d3r3kk I don't quite follow. What "logic that respects |
Ah gotcha, yes, that makes sense. Since VSCode handles this flow, we cannot subvert this and add our own 'special logic' to simply not do |
@brettcannon @d3r3kk
|
My bad, I've just realised I had black selected in the workspace settings overriding autopep8 in the main settings. It's probably from the time when a notification asked me what to use and I clicked black by mistake, and never figured out where that setting went. |
@DonJayamanne Your idea SGTM! |
This might be a different discussion, but @brettcannon & @DonJayamanne what do you think about allowing the configuration of a different formatter for |
@nnja it's definitely possible, but I don't if that is a better solution as that's extra configuration and installing versus saying "the tool you chose doesn't let you do that". |
@brettcannon It's kind of a tricky-hot-fix, but better than nothing, I guess? |
For people looking for a way to use a different formatter than Black on paste, until VSCode supports a better solution, you can at least get indentation formatting on paste by installing the Paste and Indent extension and using it's suggested keybindings. |
There's actually a way to do that:
Surely it won't work in all cases, but should do the trick for many. There should be a global option IMHO that tells vscode what to do when a formatter doesn't support partial formatting: no format, format whole file, smartypants-format (above process) or no-format if fails, smartypants-format or format-whole-file if fails. |
The latest VS Code allows the users to configure the formatting for the entire file or line of code changed, so many sure to enable the {
"python.formatting.provider": "black",
"[python]": {
"editor.formatOnPaste": false,
"editor.formatOnSaveMode": "file"
}
}
|
You could use Darker to only do Black reformatting for those lines which have been modified since the last commit (or e.g. relative to a given branch). The documentation has instructions for integration with VS Code. See this blog post for a detailed example of what Darker does. |
Open the settings.json file in the .vscode folder wnich is in the project directory and change the value of the That should fix the problem. |
Since Darker can already be used as a drop-in replacement for Black as a Python formatter for VSCode, what if we added the capability for Darker to format a given line range (instead of lines it detects as modified according to Git history)? How does VSCode pass the selection range to the reformatting tool? |
@codingbrowny people specifically want Black in this issue, so changing the formatter to autopep8 won't fix the issue. @akaihola we have some ideas on how to at least make it easier for people to create their own formatting extension, so hopefully that will allow someone to create a Darker extension. But unfortunately Darker isn't popular enough right now for us to want to rely on it for something as critical as formatting support. |
@brettcannon, we are considering adding this to Black to decrease the need to use alternative formatters in editor integration. How does VScode request a code region to be formatted? How is Black supposed to return the formatted code? It would be easiest if we'd still get the entire file with a line range which would enable us to fare better with partially selected expressions. Then we'd just emit the entire file back as usual. |
@ambv there are two ways: the API way and the LSP way, both covered by https://code.visualstudio.com/api/language-extensions/programmatic-language-features#format-the-selected-lines-in-an-editor. We have medium/long-term plans to move more tool integrations behind LSP, so for our needs that means https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rangeFormatting is how we would implement it. |
Since we already have blackd, we'll teach it the LSP protocol. |
Here's a first stab: psf/black#2512 |
Thanks a lot man! This fixed my issue! 😄 |
I have this: "editor.formatOnSave": true,
"editor.formatOnSaveMode": "modificationsIfAvailable", But still the extension yields about black not having the modifications mode available. Shouldn't it switch automatically to file mode, given I configured |
FYI we have a new, separate Black extension (code at https://github.com/microsoft/vscode-black-formatter ). Please try it out and if there are issues as that is where we plan to focus our efforts around Black going forward. |
Hi, an extension of this issue now happens with the move to a separate extension. If I try to format a selection or to format modified lines I get the following popup: I think this message is very confusing and that the previous message saying that black does not support "Format selection" should be shown instead. |
@luabud Hey I'd like to work on this as part of GHC OSD. For this issue, I need to change the message to the previous message saying that black does not support "Format selection". Is that correct? |
Environment data
Actual behavior
Every single time a paste a piece of code, I get the error message:
"Black does not support the "Format Selection" command"
Expected behavior
No error message pop-ups.
The text was updated successfully, but these errors were encountered: