question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extra newline added to cells after formatting with any formatter

See original GitHub issue

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

  • 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

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:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
karthiknadigcommented, Apr 20, 2022
2reactions
hyiyang2022commented, Jun 10, 2022

Any temporary fix?

@karthiknadig mentions to use the black formatter https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter, it works for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Find and Replace Line Breaks in Excel - Contextures
Select the cell · In the formula bar, click where you want the line break. Add a line break · On the keyboard,...
Read more >
Start new line in Excel cell - 3 ways to add carriage return
As an example, let's add a carriage return after each comma in a text string: Select all the cells in which you want...
Read more >
Add or Remove Line Breaks in a Cell in Excel - YouTube
In Excel, to create a line break in a cell, click where you want the line break, then press Alt + EnterLater, if...
Read more >
CSV in Python adding an extra carriage return, on Windows
In most cases, however, I believe that behavior is preferable and more natural than treating all CSV as a binary format. I provide...
Read more >
Add Line Break in Excel Formula - Contextures Blog
you need to make sure that “wrap text” is enabled on that cell. Without this, the new line character is ignored. Reply. Joe ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found