Skip to content

Commit

Permalink
Printer: Remove unused state fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Aug 14, 2023
1 parent a84c023 commit 7f83eeb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 9 additions & 13 deletions crates/ruff_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -695,21 +696,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 +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<TextRange>,
group_modes: GroupModes,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -1369,7 +1365,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

0 comments on commit 7f83eeb

Please sign in to comment.