mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-15 16:10:17 +00:00
Fix handling of newlines in empty files (#7473)
This commit is contained in:
parent
b66bfa6570
commit
0346e781d4
12 changed files with 90 additions and 15 deletions
|
@ -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(())
|
||||
/// # }
|
||||
/// ```
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
|
||||
|
0
crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_now_newline.py
vendored
Normal file
0
crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_now_newline.py
vendored
Normal file
1
crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_trailing_newline.py
vendored
Normal file
1
crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_trailing_newline.py
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
|
1
crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_whitespace.py
vendored
Normal file
1
crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_whitespace.py
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -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
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -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
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -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
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -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
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue