From fdd131b695ef76946c2b468c08728452d8188a40 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sat, 26 Aug 2023 17:53:58 +0200 Subject: [PATCH] Discard changes to crates/ruff_formatter/src/macros.rs --- Cargo.lock | 5 --- crates/ruff/Cargo.toml | 2 +- crates/ruff_formatter/src/builders.rs | 1 - .../src/format_element/document.rs | 4 +- crates/ruff_formatter/src/lib.rs | 9 +++- crates/ruff_formatter/src/macros.rs | 6 +-- crates/ruff_formatter/src/printer/mod.rs | 12 +++--- .../src/printer/printer_options/mod.rs | 43 +++---------------- crates/ruff_python_formatter/src/options.rs | 2 +- .../src/statement/suite.rs | 2 +- 10 files changed, 26 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12e925bbcd5a0f..07b6c43c4fc2a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2275,7 +2275,6 @@ version = "0.0.0" dependencies = [ "drop_bomb", "insta", - "ruff_python_trivia", "ruff_text_size", "rustc-hash", "schemars", @@ -2451,13 +2450,10 @@ version = "0.0.0" dependencies = [ "insta", "memchr", - "ruff_cache", "ruff_python_ast", "ruff_python_parser", "ruff_source_file", "ruff_text_size", - "schemars", - "serde", "smallvec", "unic-ucd-ident", ] @@ -2514,7 +2510,6 @@ dependencies = [ "ruff_python_formatter", "ruff_python_index", "ruff_python_parser", - "ruff_python_trivia", "ruff_source_file", "serde", "serde-wasm-bindgen", diff --git a/crates/ruff/Cargo.toml b/crates/ruff/Cargo.toml index 2409b654caa286..e5164a61695adf 100644 --- a/crates/ruff/Cargo.toml +++ b/crates/ruff/Cargo.toml @@ -25,7 +25,7 @@ ruff_python_index = { path = "../ruff_python_index" } ruff_python_literal = { path = "../ruff_python_literal" } ruff_python_semantic = { path = "../ruff_python_semantic" } ruff_python_stdlib = { path = "../ruff_python_stdlib" } -ruff_python_trivia = { path = "../ruff_python_trivia", features = ["schemars"] } +ruff_python_trivia = { path = "../ruff_python_trivia" } ruff_python_parser = { path = "../ruff_python_parser" } ruff_source_file = { path = "../ruff_source_file", features = ["serde"] } ruff_text_size = { path = "../ruff_text_size" } diff --git a/crates/ruff_formatter/src/builders.rs b/crates/ruff_formatter/src/builders.rs index 881a80e557cd6a..642d24458032d8 100644 --- a/crates/ruff_formatter/src/builders.rs +++ b/crates/ruff_formatter/src/builders.rs @@ -1670,7 +1670,6 @@ impl Format for ExpandParent { /// ``` /// use ruff_formatter::{format_args, format, LineWidth, SimpleFormatOptions}; /// use ruff_formatter::prelude::*; -/// use ruff_formatter::printer::PrintWidth; /// /// fn main() -> FormatResult<()> { /// let context = SimpleFormatContext::new(SimpleFormatOptions { diff --git a/crates/ruff_formatter/src/format_element/document.rs b/crates/ruff_formatter/src/format_element/document.rs index 42bc2b1f39961f..1894084219231a 100644 --- a/crates/ruff_formatter/src/format_element/document.rs +++ b/crates/ruff_formatter/src/format_element/document.rs @@ -216,13 +216,13 @@ impl FormatOptions for IrFormatOptions { } fn line_width(&self) -> LineWidth { - LineWidth(80) + LineWidth::try_from(80).unwrap() } fn as_print_options(&self) -> PrinterOptions { PrinterOptions { tab_width: 2, - print_width: self.line_width().into(), + line_width: self.line_width(), line_ending: LineEnding::LineFeed, indent_style: IndentStyle::Space(2), } diff --git a/crates/ruff_formatter/src/lib.rs b/crates/ruff_formatter/src/lib.rs index 5b4ed9b4d6785e..3da23cf6c6ec9f 100644 --- a/crates/ruff_formatter/src/lib.rs +++ b/crates/ruff_formatter/src/lib.rs @@ -141,6 +141,12 @@ impl From for u16 { } } +impl From for u32 { + fn from(value: LineWidth) -> Self { + u32::from(value.0.get()) + } +} + impl From for LineWidth { fn from(value: NonZeroU16) -> Self { Self(value) @@ -221,7 +227,7 @@ impl FormatOptions for SimpleFormatOptions { fn as_print_options(&self) -> PrinterOptions { PrinterOptions::default() .with_indent(self.indent_style) - .with_print_width(self.line_width.into()) + .with_line_width(self.line_width) } } @@ -394,7 +400,6 @@ pub type FormatResult = Result; /// Implementing `Format` for a custom struct /// /// ``` -/// /// use ruff_formatter::{format, write, IndentStyle}; /// use ruff_formatter::prelude::*; /// use ruff_text_size::TextSize; diff --git a/crates/ruff_formatter/src/macros.rs b/crates/ruff_formatter/src/macros.rs index abec61b820a720..2e6b28bf235452 100644 --- a/crates/ruff_formatter/src/macros.rs +++ b/crates/ruff_formatter/src/macros.rs @@ -153,8 +153,7 @@ macro_rules! format { /// ## Examples /// /// ``` -/// -/// use ruff_formatter::{Formatted, format, format_args, SimpleFormatOptions}; +/// use ruff_formatter::{Formatted, LineWidth, format, format_args, SimpleFormatOptions}; /// use ruff_formatter::prelude::*; /// /// # fn main() -> FormatResult<()> { @@ -242,8 +241,7 @@ macro_rules! format { /// ### Enclosing group with `should_expand: true` /// /// ``` -/// -/// use ruff_formatter::{Formatted, format, format_args, SimpleFormatOptions}; +/// use ruff_formatter::{Formatted, LineWidth, format, format_args, SimpleFormatOptions}; /// use ruff_formatter::prelude::*; /// /// # fn main() -> FormatResult<()> { diff --git a/crates/ruff_formatter/src/printer/mod.rs b/crates/ruff_formatter/src/printer/mod.rs index 57e6d1a010ba42..77485354543feb 100644 --- a/crates/ruff_formatter/src/printer/mod.rs +++ b/crates/ruff_formatter/src/printer/mod.rs @@ -1309,7 +1309,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> { self.state.line_width += char_width; } - if self.state.line_width > self.options().print_width.into() { + if self.state.line_width > self.options().line_width.into() { return Fits::No; } @@ -1426,9 +1426,11 @@ impl From for MeasureMode { #[cfg(test)] mod tests { use crate::prelude::*; - use crate::printer::{LineEnding, PrintWidth, Printer, PrinterOptions}; + use crate::printer::{LineEnding, Printer, PrinterOptions}; use crate::source_code::SourceCode; - use crate::{format_args, write, Document, FormatState, IndentStyle, Printed, VecBuffer}; + use crate::{ + format_args, write, Document, FormatState, IndentStyle, LineWidth, Printed, VecBuffer, + }; fn format(root: &dyn Format) -> Printed { format_with_options( @@ -1579,7 +1581,7 @@ two lines`, let options = PrinterOptions { indent_style: IndentStyle::Tab, tab_width: 4, - print_width: PrintWidth::new(19), + line_width: LineWidth::try_from(19).unwrap(), ..PrinterOptions::default() }; @@ -1684,7 +1686,7 @@ two lines`, let printed = Printer::new( SourceCode::default(), - PrinterOptions::default().with_print_width(PrintWidth::new(10)), + PrinterOptions::default().with_line_width(LineWidth::try_from(10).unwrap()), ) .print(&document) .unwrap(); diff --git a/crates/ruff_formatter/src/printer/printer_options/mod.rs b/crates/ruff_formatter/src/printer/printer_options/mod.rs index 763afc00f164db..b51ed7bdef6aca 100644 --- a/crates/ruff_formatter/src/printer/printer_options/mod.rs +++ b/crates/ruff_formatter/src/printer/printer_options/mod.rs @@ -7,7 +7,7 @@ pub struct PrinterOptions { pub tab_width: u8, /// What's the max width of a line. Defaults to 80 - pub print_width: PrintWidth, + pub line_width: LineWidth, /// The type of line ending to apply to the printed input pub line_ending: LineEnding, @@ -16,39 +16,6 @@ pub struct PrinterOptions { pub indent_style: IndentStyle, } -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct PrintWidth(u16); - -impl PrintWidth { - pub fn new(width: u16) -> Self { - Self(width) - } -} - -impl Default for PrintWidth { - fn default() -> Self { - LineWidth::default().into() - } -} - -impl From for PrintWidth { - fn from(width: LineWidth) -> Self { - Self(u16::from(width)) - } -} - -impl From for u32 { - fn from(width: PrintWidth) -> Self { - u32::from(width.0) - } -} - -impl From for u16 { - fn from(width: PrintWidth) -> Self { - width.0 - } -} - impl<'a, O> From<&'a O> for PrinterOptions where O: FormatOptions, @@ -56,14 +23,14 @@ where fn from(options: &'a O) -> Self { PrinterOptions::default() .with_indent(options.indent_style()) - .with_print_width(options.line_width().into()) + .with_line_width(options.line_width()) } } impl PrinterOptions { #[must_use] - pub fn with_print_width(mut self, width: PrintWidth) -> Self { - self.print_width = width; + pub fn with_line_width(mut self, width: LineWidth) -> Self { + self.line_width = width; self } @@ -115,7 +82,7 @@ impl Default for PrinterOptions { fn default() -> Self { PrinterOptions { tab_width: 2, - print_width: PrintWidth::default(), + line_width: LineWidth::default(), indent_style: IndentStyle::default(), line_ending: LineEnding::LineFeed, } diff --git a/crates/ruff_python_formatter/src/options.rs b/crates/ruff_python_formatter/src/options.rs index ee427fd3128290..a18000976d5051 100644 --- a/crates/ruff_python_formatter/src/options.rs +++ b/crates/ruff_python_formatter/src/options.rs @@ -113,7 +113,7 @@ impl FormatOptions for PyFormatOptions { fn as_print_options(&self) -> PrinterOptions { PrinterOptions { tab_width: 4, - print_width: self.line_width.into(), + line_width: self.line_width, line_ending: LineEnding::LineFeed, indent_style: self.indent_style, } diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index 24dca3ffc2a170..def039de90c37f 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -197,7 +197,7 @@ impl FormatRule> for FormatSuite { match self.kind { SuiteKind::TopLevel => { match lines_after_ignoring_trivia(preceding.end(), source) { - 0 | 1 | 2 => empty_line().fmt(f)?, + 0..=2 => empty_line().fmt(f)?, _ => write!(f, [empty_line(), empty_line()])?, } }