diff --git a/crates/ruff_formatter/src/printer/mod.rs b/crates/ruff_formatter/src/printer/mod.rs index e24ab80ea839e6..22e381a01b525d 100644 --- a/crates/ruff_formatter/src/printer/mod.rs +++ b/crates/ruff_formatter/src/printer/mod.rs @@ -684,6 +684,7 @@ impl<'a> Printer<'a> { } fn print_str(&mut self, content: &str) { + self.state.buffer.reserve(content.len()); for char in content.chars() { self.print_char(char); } @@ -695,8 +696,6 @@ impl<'a> Printer<'a> { .buffer .push_str(self.options.line_ending.as_str()); - self.state.generated_line += 1; - self.state.generated_column = 0; self.state.line_width = 0; // Fit's only tests if groups up to the first line break fit. @@ -704,12 +703,11 @@ impl<'a> Printer<'a> { self.state.measured_group_fits = false; } else { self.state.buffer.push(char); - self.state.generated_column += 1; let char_width = if char == '\t' { - self.options.tab_width as usize + self.options.tab_width as u32 } else { - char.width().unwrap_or(0) + char.width().unwrap_or(0) as u32 }; self.state.line_width += char_width; @@ -744,9 +742,7 @@ struct PrinterState<'a> { source_position: TextSize, pending_indent: Indention, measured_group_fits: bool, - generated_line: usize, - generated_column: usize, - line_width: usize, + line_width: u32, line_suffixes: LineSuffixes<'a>, verbatim_markers: Vec, group_modes: GroupModes, @@ -1256,12 +1252,12 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> { fn fits_text(&mut self, text: &str, args: PrintElementArgs) -> Fits { let indent = std::mem::take(&mut self.state.pending_indent); - self.state.line_width += indent.level() as usize * self.options().indent_width() as usize - + indent.align() as usize; + self.state.line_width += + indent.level() as u32 * self.options().indent_width() as u32 + indent.align() as u32; for c in text.chars() { let char_width = match c { - '\t' => self.options().tab_width as usize, + '\t' => self.options().tab_width as u32, '\n' => { if self.must_be_flat { return Fits::No; @@ -1275,7 +1271,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> { } }; } - c => c.width().unwrap_or(0), + c => c.width().unwrap_or(0) as u32, }; self.state.line_width += char_width; } @@ -1369,7 +1365,7 @@ impl From for Fits { struct FitsState { pending_indent: Indention, has_line_suffix: bool, - line_width: usize, + line_width: u32, } #[derive(Copy, Clone, Debug, Eq, PartialEq)] diff --git a/crates/ruff_formatter/src/printer/printer_options/mod.rs b/crates/ruff_formatter/src/printer/printer_options/mod.rs index 36b754fcccf5d8..291d02661f5e9e 100644 --- a/crates/ruff_formatter/src/printer/printer_options/mod.rs +++ b/crates/ruff_formatter/src/printer/printer_options/mod.rs @@ -43,6 +43,12 @@ impl From for usize { } } +impl From for u32 { + fn from(width: PrintWidth) -> Self { + width.0 + } +} + impl<'a, O> From<&'a O> for PrinterOptions where O: FormatOptions,