microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
144 stars 34 forks source link

Plugin destroys identation in "empty" lines. #490

Closed scriptbotprime closed 3 months ago

scriptbotprime commented 3 months ago

In a situation like this:

def my_func():
    print('first part')

    print('second part')

the plugin removes the identation of the "empty" line. Therefore the interpreter does not consider the second print statement as part of the function.

karthiknadig commented 3 months ago

We don't really do anything specific in the extension. All we do is return what black provides after formatting. Also, note that even with the empty line, from python point of view the second print is still part of the function block. The indentation on the print line matters not the empty line.

So here both prints are part of my_func (note that there is no indentation on the empty line): image

For second print to not be part of my_func it should look like this: image

karthiknadig commented 3 months ago

You can enable verbose logging by setting "black-formatter.trace.server": "verbose" in your user settings, and this will generate more detailed logs including the diff that is being applied to your code, and output from black itself. That should give more clues on what is going on.

karthiknadig commented 3 months ago

Got the logs my self:

2024-03-20 12:57:25.606 [info] file:///c%3A/GIT/demo/diff_test/format_Test.py :
****************************************************************************************************
def my_func():
    print("first part")

    print("second part")

****************************************************************************************************

Black it self is returning this exact string: Input: def my_func():\n print(\"first part\")\n \n print(\"second part\")\n Output: def my_func():\n print(\"first part\")\n\n print(\"second part\")\n

As you can see in the output black has removed the empty line.