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
Extra newline added to cells after formatting with any formatter #17681
Comments
Tagging @DonJayamanne who kindly authored the support for formatting notebooks in #12199, and @rchiodo and @IanMatthewHuff who reviewed so might be familiar with the topic. Thanks for your attention and your time :) |
@jucor Thanks for investigating this issue. We have marked this as needs PR, so it is ready to be worked on. We are happy to review any PR fixing this issue. |
@jucor Can you try the black formatter extension we have? https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter |
This shouldn't change behavior, but it is a little less confusing. Also format the ipynb correctly. VSCode was formatting it incorrectly due to a bug: microsoft/vscode-python#17681
Jupyter so it's easier to edit notebooks. black-formatter because of this bug microsoft/vscode-python#17681
This shouldn't change behavior, but it is a little less confusing. Also format the ipynb correctly. VSCode was formatting it incorrectly due to a bug: microsoft/vscode-python#17681
Jupyter so it's easier to edit notebooks. black-formatter because of this bug microsoft/vscode-python#17681
Any temporary fix? |
@karthiknadig mentions to use the black formatter https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter, it works for me. |
works for me too! |
Thank for try the black formatter extension. Going forward that will be the way we provide formatting feature. We have started the process of migrating users over from current experience to the new extensions. |
Thanks so much for the native notebook experience: it's awesome!
One niggle that keeps biting me and my team: formatting cells (with autopep8, or yapf, or black) adds an empty line after each cell.
Steps to reproduce
Expected behaviour
Observed behaviour
Cause
This is because VSCode formats cells by, quite cleverly, saving their content to a tempfile then running the formatter on that tempfile. All good formatters ensure the last line of the file finishes with a new line
\n
char: this is entirely expected for Python files, but not for notebook cells.Indeed adding such a newline at the end of the last line of a Jupyter notebook cell adds it to the cell's JSON and is then interpreted as an actual new blank line displayed in the cell. In every single cell :/
This is why for example black for notebooks removes that newline when called on notebooks and so do autopep8 callers on notebooks.
See e.g. discussion in Black issue with @MarcoGorelli.
Suggested minimum fix
I think there's a simple way to keep the common formatting-a-tempfile logic while still fixing this bug: modifying
BaseFormatter.provideDocumentFormattingEdits()
to, at linevscode-python/src/client/formatters/baseFormatter.ts
Line 78 in 3698950
vscode-python/src/client/formatters/baseFormatter.ts
Line 84 in 3698950
isNotebookCell(document)
(likecreateTempFile
does ) then if true removes the final newline.I'm looking at making a PR to help, but never coded in TS, so filing this as an issue first.
Environment data
python.languageServer
setting: Default (with pylance installed)The text was updated successfully, but these errors were encountered: