ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Formatter trims empty lines after `;`

Open MichaReiser opened this issue 1 year ago • 1 comments

b = """
    This looks like a docstring but is not in a notebook because notebooks can't be imported as a module.
    Ruff should leave it as is
""";


a = "another normal string"

The formatter should preserve the empty lines between b and a but it does not today.

Playground

MichaReiser avatar Feb 12 '24 18:02 MichaReiser

Giving a quick 10 second look, the issue is probably on this line:

https://github.com/astral-sh/ruff/blob/fe79798c12b4771cee0b0c59964ad7bd751c3779/crates/ruff_python_formatter/src/statement/suite.rs#L419-L420

As lines_after doesn't take into account the ; token which results in the value being 0 and then the hard line break is added instead.

Maybe we should use lines_after_ignoring_end_of_line_trivia but that doesn't ignore ; either.

dhruvmanila avatar Feb 15 '24 05:02 dhruvmanila

@MichaReiser, followed what @dhruvmanila suggested. Worked out nicely. Adapted some tests as well. If you find the time, please review the fix. Thanks!

robincaloudis avatar Mar 27 '24 22:03 robincaloudis