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

More gracefully handle the fact that Black does not support "Format Selection" #176

Closed
egabrum opened this issue Jun 6, 2018 · 45 comments · Fixed by #342
Closed

More gracefully handle the fact that Black does not support "Format Selection" #176

egabrum opened this issue Jun 6, 2018 · 45 comments · Fixed by #342
Assignees
Labels
feature-request Request for new features or functionality good first issue Good for newcomers needs PR

Comments

@egabrum
Copy link

egabrum commented Jun 6, 2018

Environment data

  • VS Code version: 1.23.1
  • Extension version (available under the Extensions sidebar): 2018.5.0
  • OS and version: Windows 10 x64
  • Python version (& distribution if applicable, e.g. Anaconda): python 3.6.3 x64
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): n/a
  • Relevant/affected Python packages and their versions: black 18.6b1

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.

    "editor.formatOnPaste": true,
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "editor.tabSize": 3,
    "python.formatting.autopep8Args": [
        "--indent_size=3"
    ],
    //"python.formatting.blackArgs": [
    //    "--indent_size=3"
    //],
    "python.formatting.provider": "black",
    "python.linting.pep8Enabled": false,
    "python.linting.pep8Args": [
        "--ignore=E111,E114,E121,E126,E127,E128,E265,E231,E303,E501,W291,W293,W503",
        "--max-line-length=89"
    ],
    //"python.linting.pylintArgs" If you don't use the pylintrc config file:
    //"python.linting.pylintArgs": ["--disable=C0303,W0311,E1101"],
    "python.linting.pylintEnabled": true,
    "python.linting.pydocstyleEnabled": false,
@brettcannon
Copy link
Member

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

@brettcannon brettcannon changed the title "Black does not support the "Format Selection" command" pop-up on paste Somehow provide feedback that Black does not support "Format Selection" Jun 6, 2018
@brettcannon brettcannon added the feature-request Request for new features or functionality label Jun 6, 2018
@brettcannon
Copy link
Member

I've re-oriented this to provide some feedback somehow that Black purposefully doesn't support "Format Selection".

@egabrum
Copy link
Author

egabrum commented Jun 6, 2018

@brettcannon Thanks for your answer but, please note that my original issue was not what this ticket has become now.
I can see why Black does not want to do partial formatting. It would be nice if it allowed the option, but I can live with it.

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.
Maybe either one of us should open a new ticket to address our respective problem.

@brettcannon brettcannon reopened this Jun 6, 2018
@brettcannon brettcannon changed the title Somehow provide feedback that Black does not support "Format Selection" More gracefully handle the fact that Black does not support "Format Selection" Jun 6, 2018
@brettcannon
Copy link
Member

brettcannon commented Jun 6, 2018

@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 "editor.formatOnPaste": true.

@brettcannon
Copy link
Member

I should also mention that to turn off any feature in VS Code just for Python, you can do:

"[python]": {
    "editor.formatOnPaste": false
}

@egabrum
Copy link
Author

egabrum commented Jun 6, 2018

Thanks. Setting "editor.formatOnPaste": false will do the trick for me for now. I missed that.

@zephyrite
Copy link

I got the same spammy notification from black even though I'm using "python.formatting.provider": "autopep8".
Setting "editor.formatOnPaste": false for python seems to have fixed it, and "formatOnType" is still working so I'm happy for now.

@DonJayamanne
Copy link

I got the same spammy notification from black even though I'm using "python.formatting.provider": "autopep8".

This needs to be verified..

@brettcannon
Copy link
Member

@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?

@brettcannon
Copy link
Member

Maybe we can detect when people turn on Black but have editor.formatOnPaste turned on and give a notification that they should consider turning it off for Python (e.g. link to the docs once they are updated with how to do this)?

@d3r3kk
Copy link

d3r3kk commented Jun 11, 2018

I cannot reproduce this behaviour:

I got the same spammy notification from black even though I'm using "python.formatting.provider": "autopep8".

verify_autopep8_doesnt_show_black_warning_line_format

Removing needs verification label.

@d3r3kk d3r3kk closed this as completed Jun 11, 2018
@d3r3kk d3r3kk reopened this Jun 11, 2018
@brettcannon
Copy link
Member

Why don't we modify the warning notification to provide an option to disable formatOnPaste just for Python when they happen to have the setting turned on.

@d3r3kk
Copy link

d3r3kk commented Jun 13, 2018

Why don't we just have the logic that respects formatOnPaste also recognize python.formatting.provider: "black"? I know that might seem heavy-handed, but I believe it would make sense in this scenario...

@brettcannon
Copy link
Member

brettcannon commented Jun 13, 2018

@d3r3kk I don't quite follow. What "logic that respects formatOnPaste" are you thinking of? Do note that the way VS Code implements formatOnPaste is it does the paste and then make a format() call with a range object, just like on a Format Selection call (IOW from our perspective there is no difference at the code call site.)

@d3r3kk
Copy link

d3r3kk commented Jun 13, 2018

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 formatOnPaste in the way I was thinking.

@DonJayamanne
Copy link

@brettcannon @d3r3kk
Suggestions

  • Change the message from Black does not support the "Format Selection" command to Black does not support the "Format Selection" command and "Format on Paste" feature
  • Add a button Disable format on paste to the above message.
  • Add a button Do not show this again to the above message.

@zephyrite
Copy link

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.
Sorry for the red herring.

@brettcannon
Copy link
Member

@DonJayamanne Your idea SGTM!

@DonJayamanne DonJayamanne self-assigned this Jun 19, 2018
@nnja
Copy link

nnja commented Sep 19, 2018

This might be a different discussion, but @brettcannon & @DonJayamanne what do you think about allowing the configuration of a different formatter for formatOnPaste? That way black can be the main formatter used for the file, but you could still use one of the formatters that does format selections just for paste.

@brettcannon
Copy link
Member

@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".

@DonJayamanne DonJayamanne removed their assignment Sep 20, 2018
@tomy0000000
Copy link

@brettcannon
My suggestion above should likely be implement in VS Code rather than here,
but we can certainly link the "format selection" function from VS Code to a "format file" function in black temporally, so VS Code will "think" that black only formats the paste section.

It's kind of a tricky-hot-fix, but better than nothing, I guess?

@elliotwaite
Copy link

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.

@yajo
Copy link

yajo commented Sep 15, 2020

There's actually a way to do that:

  1. Save $theFile prior to changes in /tmp/a.
  2. Store diff between $theFile and /tmp/a.
  3. black $theFile
  4. Unpatch diff to $theFile

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.

@mysticaltech
Copy link

mysticaltech commented Sep 19, 2020

@ambv @zsol @psf I would really love to see this change in Black. Really tired of this notification!
Screen Shot 2020-09-19 at 8 42 59 AM

Edit: Setting the below param in Settings.json disabled the problem, but still that would be a good feature to have in the future FYI.

"[python]": {
    "editor.formatOnPaste": false
}

@yordis
Copy link

yordis commented Sep 27, 2020

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 editor.formatOnSaveMode as file in the settings

{
  "python.formatting.provider": "black",
  "[python]": {
    "editor.formatOnPaste": false,
    "editor.formatOnSaveMode": "file"
  }
}

@akaihola
Copy link

akaihola commented Nov 2, 2020

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.

@codingbrowny
Copy link

Open the settings.json file in the .vscode folder wnich is in the project directory and change the value of the
python.formatter.provider to autopep8.

That should fix the problem.

@akaihola
Copy link

akaihola commented Sep 9, 2021

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?

@brettcannon
Copy link
Member

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

@ambv
Copy link

ambv commented Sep 12, 2021

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

@brettcannon
Copy link
Member

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

@ambv
Copy link

ambv commented Sep 13, 2021

Since we already have blackd, we'll teach it the LSP protocol.

@zsol
Copy link

zsol commented Sep 26, 2021

Here's a first stab: psf/black#2512
Note: this currently just implements textDocument/formatting, but I'd appreciate a review as I'm not intimate with the LSP spec. textDocument/rangeFormatting TBD after this.

@edchelstephens
Copy link

edchelstephens commented Nov 9, 2021

@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?

Thanks a lot man! This fixed my issue! 😄

@yajo
Copy link

yajo commented Sep 20, 2022

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 modificationsIfAvailable?

@brettcannon
Copy link
Member

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.

@cwebster-99 cwebster-99 transferred this issue from microsoft/vscode-python Jan 12, 2023
@jmigual
Copy link

jmigual commented Jun 28, 2023

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:

image

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 luabud added good first issue Good for newcomers ghc-osd labels Sep 8, 2023
@krystal2710
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality good first issue Good for newcomers needs PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.