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

Trailing empty line added to end of file / notebook cell #52

Closed
j-kleemann-old opened this issue Oct 16, 2023 · 4 comments · Fixed by #56
Closed

Trailing empty line added to end of file / notebook cell #52

j-kleemann-old opened this issue Oct 16, 2023 · 4 comments · Fixed by #56
Labels
enhancement New feature or request

Comments

@j-kleemann-old
Copy link
Contributor

If a file or a notebook cell is formatted it will cause the last line to be an empty line.
While for files this might be desireable, in a Jupyter Notebook I personally find it irritating, as there, cells often are only a few lines long anyway and the additional empty lines at the end of the cells now add up fast (and cause a lot of changes for git).
When I used yapf previously with the built-in support via the "python.formatting.provider" : "yapf" setting this was not the case and I don't think this is a yapf setting either (couldn't find anything, and I had the same style setting before with the old built-in support, when I didn't have this issue, and now the same behaviour even if I use the default style...)?

Is there any way to make this configurable or supress adding a trailing empty line in notebook cells?
Thanks!

yapf extension version: 0.1.9
yapf python package version: 0.40.2

VS Code Version:
Version: 1.83.1 (user setup)
Commit: f1b07bd25dfad64b0167beb15359ae573aecd2cc
Date: 2023-10-10T23:48:05.904Z
Electron: 25.8.4
ElectronBuildId: 24154031
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Windows_NT x64 10.0.19045

@EeyoreLee
Copy link
Owner

@j-kleemann - yup, cause yapf will always add a empty line at then end and I just invoke it. The problem is yapf hasn't provide a release to format jupyter like black[jupyter], similar as jupyter magic function, so I do that on the extension side. Do you think it will be better if I always remove the empty line at the end in jupyter. Or do you have some situation that need the empty line at the end. There are already quite a few knobs here, so I want to try not to add extra new knob setting.

@EeyoreLee
Copy link
Owner

@j-kleemann - hi, I create a PR that if your cell end with a empty line, it will hold on it and if your cell didn't has a empty line at the end, I will remove the empty line at the end. If it can fix your situation, I'll merge it later.

@j-kleemann-old
Copy link
Contributor Author

@EeyoreLee - Wow that was quick! Thank you, I think your solution of keeping an existing blank line, while removing it if it hasn't been there before is ideal, way better and more flexible than any configuration switch!
While I am already happy with your solution, I still have one improvement idea:
Right now your code does not detect lines containing only whitespaces as blank, e.g, when formating

def f():
    pass # The next line seems empty, but it has four spaces.
    

it will remove the empty line at the end, which some people might not want (I actually realized that it can be quite useful to have the newline stay at the end, while one is still working on the cell 😅 and regularly you will have such whitespace-only lines because you hit enter on the previous line and vs code already intended the new line for you).
I added a rstrip(" \t") to your blank-line-detection code in PR #56, so it should now also consider lines containing only spaces and tabs as blank as well and keep a new line there (the tabs and spaces will be gone though, because yapf will remove them, but this should be fine and more often desired than not).

@EeyoreLee
Copy link
Owner

@j-kleemann - Thanks! The addition commit is great! I'll merge it but release maybe tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants