Fix handling of newlines in empty files (#7473)

This commit is contained in:
Micha Reiser 2023-09-18 08:08:10 +02:00 committed by GitHub
parent b66bfa6570
commit 0346e781d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 90 additions and 15 deletions

View file

@ -429,7 +429,6 @@ pub type FormatResult<F> = Result<F, FormatError>;
/// impl Format<SimpleFormatContext> for Paragraph {
/// fn fmt(&self, f: &mut Formatter<SimpleFormatContext>) -> FormatResult<()> {
/// write!(f, [
/// hard_line_break(),
/// text(&self.0, None),
/// hard_line_break(),
/// ])
@ -440,7 +439,7 @@ pub type FormatResult<F> = Result<F, FormatError>;
/// let paragraph = Paragraph(String::from("test"));
/// let formatted = format!(SimpleFormatContext::default(), [paragraph])?;
///
/// assert_eq!("\ntest\n", formatted.print()?.as_code());
/// assert_eq!("test\n", formatted.print()?.as_code());
/// # Ok(())
/// # }
/// ```

View file

@ -124,7 +124,7 @@ impl<'a> Printer<'a> {
self.flush_line_suffixes(queue, stack, Some(element));
} else {
// Only print a newline if the current line isn't already empty
if self.state.line_width > 0 || self.state.buffer.is_empty() {
if self.state.line_width > 0 {
self.print_char('\n');
}

View file

@ -1,5 +1,6 @@
use ruff_formatter::write;
use ruff_python_ast::ModModule;
use ruff_python_trivia::lines_after;
use crate::comments::SourceComment;
use crate::prelude::*;
@ -11,8 +12,18 @@ pub struct FormatModModule;
impl FormatNodeRule<ModModule> for FormatModModule {
fn fmt_fields(&self, item: &ModModule, f: &mut PyFormatter) -> FormatResult<()> {
let ModModule { range: _, body } = item;
let ModModule { range, body } = item;
if body.is_empty() {
// Only preserve an empty line if the source contains an empty line too.
if !f.context().comments().has_leading(item)
&& lines_after(range.end(), f.context().source()) != 0
{
empty_line().fmt(f)
} else {
Ok(())
}
} else {
write!(
f,
[
@ -22,6 +33,7 @@ impl FormatNodeRule<ModModule> for FormatModModule {
]
)
}
}
fn fmt_dangling_comments(
&self,

View file

@ -0,0 +1,18 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_multiple_trailing_newlines.py
---
## Input
```py
```
## Output
```py
```

View file

@ -0,0 +1,14 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_now_newline.py
---
## Input
```py
```
## Output
```py
```

View file

@ -0,0 +1,16 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_trailing_newline.py
---
## Input
```py
```
## Output
```py
```

View file

@ -0,0 +1,14 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_whitespace.py
---
## Input
```py
```
## Output
```py
```

View file

@ -17,7 +17,6 @@ magic-trailing-comma = Respect
```
```py
```
@ -31,7 +30,6 @@ magic-trailing-comma = Respect
```
```py
```
@ -45,7 +43,6 @@ magic-trailing-comma = Respect
```
```py
```