Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions crates/ruff_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,21 +695,18 @@ 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.
// The next group must re-measure if it still fits.
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;
Expand Down Expand Up @@ -744,9 +741,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<TextRange>,
group_modes: GroupModes,
Expand Down Expand Up @@ -1256,12 +1251,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;
Expand All @@ -1275,7 +1270,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;
}
Expand Down Expand Up @@ -1369,7 +1364,7 @@ impl From<bool> for Fits {
struct FitsState {
pending_indent: Indention,
has_line_suffix: bool,
line_width: usize,
line_width: u32,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down
6 changes: 6 additions & 0 deletions crates/ruff_formatter/src/printer/printer_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ impl From<PrintWidth> for usize {
}
}

impl From<PrintWidth> for u32 {
fn from(width: PrintWidth) -> Self {
width.0
}
}

impl<'a, O> From<&'a O> for PrinterOptions
where
O: FormatOptions,
Expand Down