Extra newline added to cells after formatting with any formatter
See original GitHub issueThanks 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
- create a cell in a Jupyter notebook
- add any code in it, as long as it does not finish with a blank line
- format cell (e.g. with Shift+Alt+F)
Expected behaviour
- the number of lines in the cell stays unchanged, same as if you ran e.g. black on the notebook or autopep8 on the notebook.
Observed behaviour
- an empty new line is added at the end of the cell.
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 line https://github.com/microsoft/vscode-python/blob/3698950c97982f31bb9dbfc19c4cd8308acda284/src/client/formatters/baseFormatter.ts#L78 or after applying the pach in https://github.com/microsoft/vscode-python/blob/3698950c97982f31bb9dbfc19c4cd8308acda284/src/client/formatters/baseFormatter.ts#L84 insert a promise that calls isNotebookCell(document)
(like createTempFile
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
- VS Code version: 1.61.0
- Extension version (available under the Extensions sidebar): v2021.10.1317843341
- OS and version: Windows 10 20H2 with a WSL remote to an Ubuntu 20.04 (not that it matters for this bug 😃 )
- Python version (& distribution if applicable, e.g. Anaconda): All, but in this case 3.9.5 64bit
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): conda with micromamba
- Relevant/affected Python packages and their versions: All
- Relevant/affected Python-related VS Code extensions and their versions: vscode-python
- Value of the
python.languageServer
setting: Default (with pylance installed)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:7 (3 by maintainers)
Top GitHub Comments
@jucor Can you try the black formatter extension we have? https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
@karthiknadig mentions to use the black formatter https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter, it works for me.