Skip to content

Commit

Permalink
Fix handling of newlines in empty files (#7473)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Sep 18, 2023
1 parent b66bfa6 commit 0346e78
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 15 deletions.
3 changes: 1 addition & 2 deletions crates/ruff_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
/// ])
Expand All @@ -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(())
/// # }
/// ```
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@



Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

30 changes: 21 additions & 9 deletions crates/ruff_python_formatter/src/module/mod_module.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -11,16 +12,27 @@ 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;

write!(
f,
[
body.format().with_options(SuiteKind::TopLevel),
// Trailing newline at the end of the file
hard_line_break()
]
)
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,
[
body.format().with_options(SuiteKind::TopLevel),
// Trailing newline at the end of the file
hard_line_break()
]
)
}
}

fn fmt_dangling_comments(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
```



Original file line number Diff line number Diff line change
@@ -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
```



Original file line number Diff line number Diff line change
@@ -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
```



Original file line number Diff line number Diff line change
@@ -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
```



Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ magic-trailing-comma = Respect
```

```py
```


Expand All @@ -31,7 +30,6 @@ magic-trailing-comma = Respect
```

```py
```


Expand All @@ -45,7 +43,6 @@ magic-trailing-comma = Respect
```

```py
```


Expand Down

0 comments on commit 0346e78

Please sign in to comment.