From 674d4dfbd5f7d56ce2f4d57ef9dc4f2f806de10c Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 5 Jun 2024 09:44:31 +0200 Subject: [PATCH] Fix: Printer must print a newline if a line only contains characters with a 0 width --- crates/ruff_formatter/src/printer/mod.rs | 20 +++++++++++++++- crates/ruff_python_formatter/src/verbatim.rs | 14 +---------- ...t@docstring_non_visible_characters.py.snap | 23 +++++++++++++++++++ 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 crates/ruff_python_formatter/tests/snapshots/format@docstring_non_visible_characters.py.snap diff --git a/crates/ruff_formatter/src/printer/mod.rs b/crates/ruff_formatter/src/printer/mod.rs index 8f4edd4e1ccb8..916dc5cad522c 100644 --- a/crates/ruff_formatter/src/printer/mod.rs +++ b/crates/ruff_formatter/src/printer/mod.rs @@ -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 { + if !self.state.buffer[self.state.line_start..].is_empty() { self.push_marker(); self.print_char('\n'); } @@ -830,6 +830,7 @@ impl<'a> Printer<'a> { .push_str(self.options.line_ending.as_str()); self.state.line_width = 0; + self.state.line_start = self.state.buffer.len(); // Fit's only tests if groups up to the first line break fit. // The next group must re-measure if it still fits. @@ -872,12 +873,29 @@ enum FillPairLayout { /// position the printer currently is. #[derive(Default, Debug)] struct PrinterState<'a> { + /// The formatted output. buffer: String, + + /// The source markers that map source positions to formatted positions. source_markers: Vec, + + /// The next source position that should be flushed when writing the next text. pending_source_position: Option, + + /// The current indentation that should be written before the next text. pending_indent: Indention, + + /// Caches if the code up to the next newline has been measured to fit on a single line. + /// This is used to avoid re-measuring the same content multiple times. measured_group_fits: bool, + + /// The offset at which the current line in `buffer` starts. + line_start: usize, + + /// The accumulated unicode-width of all characters on the current line. line_width: u32, + + /// The line suffixes that should be printed at the end of the line. line_suffixes: LineSuffixes<'a>, verbatim_markers: Vec, group_modes: GroupModes, diff --git a/crates/ruff_python_formatter/src/verbatim.rs b/crates/ruff_python_formatter/src/verbatim.rs index 587f2d0690383..df70e6e2986be 100644 --- a/crates/ruff_python_formatter/src/verbatim.rs +++ b/crates/ruff_python_formatter/src/verbatim.rs @@ -2,8 +2,6 @@ use std::borrow::Cow; use std::iter::FusedIterator; use std::slice::Iter; -use unicode_width::UnicodeWidthStr; - use ruff_formatter::{write, FormatError}; use ruff_python_ast::AnyNodeRef; use ruff_python_ast::Stmt; @@ -760,17 +758,7 @@ impl Format> for FormatVerbatimStatementRange { // Write the line separator that terminates the line, except if it is the last line (that isn't separated by a hard line break). if logical_line.has_trailing_newline { - // Insert an empty line if the text is non-empty but all characters have a width of zero. - // This is necessary to work around the fact that the Printer omits hard line breaks if the line width is 0. - // The alternative is to "fix" the printer and explicitly track the width and whether the line is empty. - // There's currently no use case for zero-width content outside of the verbatim context (and, form feeds are a Python specific speciality). - // It, therefore, feels wrong to add additional complexity to the very hot `Printer::print_char` function, - // to work around this special case. Therefore, work around the Printer behavior here, in the cold verbatim-formatting. - if f.context().source()[trimmed_line_range].width() == 0 { - empty_line().fmt(f)?; - } else { - hard_line_break().fmt(f)?; - } + hard_line_break().fmt(f)?; } } diff --git a/crates/ruff_python_formatter/tests/snapshots/format@docstring_non_visible_characters.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@docstring_non_visible_characters.py.snap new file mode 100644 index 0000000000000..9029655d5a8ed --- /dev/null +++ b/crates/ruff_python_formatter/tests/snapshots/format@docstring_non_visible_characters.py.snap @@ -0,0 +1,23 @@ +--- +source: crates/ruff_python_formatter/tests/fixtures.rs +input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_non_visible_characters.py +--- +## Input +```python +# Regresssion test for https://github.com/astral-sh/ruff/issues/11724 + +''' + + +''' +``` + +## Output +```python +# Regresssion test for https://github.com/astral-sh/ruff/issues/11724 + +""" + + +""" +```