mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 23:25:14 +00:00
Remove collapsing space
behaviour from Printer
(#4782)
This commit is contained in:
parent
5f4bce6d2b
commit
28aad95414
2 changed files with 12 additions and 31 deletions
|
@ -91,12 +91,7 @@ impl<'a> Printer<'a> {
|
|||
let args = stack.top();
|
||||
|
||||
match element {
|
||||
FormatElement::Space => {
|
||||
if self.state.line_width > 0 {
|
||||
self.state.pending_space = true;
|
||||
}
|
||||
}
|
||||
|
||||
FormatElement::Space => self.print_text(" ", None),
|
||||
FormatElement::StaticText { text } => self.print_text(text, None),
|
||||
FormatElement::DynamicText { text } => self.print_text(text, None),
|
||||
FormatElement::SourceCodeSlice { slice, .. } => {
|
||||
|
@ -107,8 +102,8 @@ impl<'a> Printer<'a> {
|
|||
if args.mode().is_flat()
|
||||
&& matches!(line_mode, LineMode::Soft | LineMode::SoftOrSpace)
|
||||
{
|
||||
if line_mode == &LineMode::SoftOrSpace && self.state.line_width > 0 {
|
||||
self.state.pending_space = true;
|
||||
if line_mode == &LineMode::SoftOrSpace {
|
||||
self.print_text(" ", None);
|
||||
}
|
||||
} else if self.state.line_suffixes.has_pending() {
|
||||
self.flush_line_suffixes(queue, stack, Some(element));
|
||||
|
@ -304,12 +299,6 @@ impl<'a> Printer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
// Print pending spaces
|
||||
if self.state.pending_space {
|
||||
self.print_str(" ");
|
||||
self.state.pending_space = false;
|
||||
}
|
||||
|
||||
// Insert source map markers before and after the token
|
||||
//
|
||||
// If the token has source position information the start marker
|
||||
|
@ -879,7 +868,6 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
|
||||
let fits_state = FitsState {
|
||||
pending_indent: printer.state.pending_indent,
|
||||
pending_space: printer.state.pending_space,
|
||||
line_width: printer.state.line_width,
|
||||
has_line_suffix: printer.state.line_suffixes.has_pending(),
|
||||
};
|
||||
|
@ -968,18 +956,12 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
let args = self.stack.top();
|
||||
|
||||
match element {
|
||||
FormatElement::Space => {
|
||||
if self.state.line_width > 0 {
|
||||
self.state.pending_space = true;
|
||||
}
|
||||
}
|
||||
FormatElement::Space => return Ok(self.fits_text(" ")),
|
||||
|
||||
FormatElement::Line(line_mode) => {
|
||||
if args.mode().is_flat() {
|
||||
match line_mode {
|
||||
LineMode::SoftOrSpace => {
|
||||
self.state.pending_space = true;
|
||||
}
|
||||
LineMode::SoftOrSpace => return Ok(self.fits_text(" ")),
|
||||
LineMode::Soft => {}
|
||||
LineMode::Hard | LineMode::Empty => {
|
||||
return Ok(if self.must_be_flat {
|
||||
|
@ -1145,10 +1127,6 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
self.state.line_width += indent.level() as usize * self.options().indent_width() as usize
|
||||
+ indent.align() as usize;
|
||||
|
||||
if self.state.pending_space {
|
||||
self.state.line_width += 1;
|
||||
}
|
||||
|
||||
for c in text.chars() {
|
||||
let char_width = match c {
|
||||
'\t' => self.options().tab_width as usize,
|
||||
|
@ -1168,8 +1146,6 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
return Fits::No;
|
||||
}
|
||||
|
||||
self.state.pending_space = false;
|
||||
|
||||
Fits::Maybe
|
||||
}
|
||||
|
||||
|
@ -1254,7 +1230,6 @@ impl From<bool> for Fits {
|
|||
#[derive(Debug)]
|
||||
struct FitsState {
|
||||
pending_indent: Indention,
|
||||
pending_space: bool,
|
||||
has_line_suffix: bool,
|
||||
line_width: usize,
|
||||
}
|
||||
|
@ -1555,7 +1530,7 @@ two lines`,
|
|||
text("]")
|
||||
]),
|
||||
text(";"),
|
||||
&line_suffix(&format_args![space(), text("// trailing"), space()])
|
||||
&line_suffix(&format_args![space(), text("// trailing")])
|
||||
]);
|
||||
|
||||
assert_eq!(printed.as_code(), "[1, 2, 3]; // trailing")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue