Skip to content

Commit

Permalink
Discard changes to crates/ruff_formatter/src/macros.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Aug 26, 2023
1 parent 8331e25 commit a4583e8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 58 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_formatter/src/format_element/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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().into(),
line_ending: LineEnding::LineFeed,
indent_style: IndentStyle::Space(2),
}
Expand Down
9 changes: 7 additions & 2 deletions crates/ruff_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ impl From<LineWidth> for u16 {
}
}

impl From<LineWidth> for u32 {
fn from(value: LineWidth) -> Self {
value.0.get() as u32
}
}

impl From<NonZeroU16> for LineWidth {
fn from(value: NonZeroU16) -> Self {
Self(value)
Expand Down Expand Up @@ -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.into())
}
}

Expand Down Expand Up @@ -394,7 +400,6 @@ pub type FormatResult<F> = Result<F, FormatError>;
/// Implementing `Format` for a custom struct
///
/// ```
///
/// use ruff_formatter::{format, write, IndentStyle};
/// use ruff_formatter::prelude::*;
/// use ruff_text_size::TextSize;
Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_formatter/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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<()> {
Expand Down
12 changes: 7 additions & 5 deletions crates/ruff_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -1426,9 +1426,11 @@ impl From<BestFittingMode> 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<SimpleFormatContext>) -> Printed {
format_with_options(
Expand Down Expand Up @@ -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()
};

Expand Down Expand Up @@ -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();
Expand Down
43 changes: 5 additions & 38 deletions crates/ruff_formatter/src/printer/printer_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,54 +16,21 @@ 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<LineWidth> for PrintWidth {
fn from(width: LineWidth) -> Self {
Self(u16::from(width))
}
}

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

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

impl<'a, O> From<&'a O> for PrinterOptions
where
O: FormatOptions,
{
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().into())
}
}

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
}

Expand Down Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.into(),
line_ending: LineEnding::LineFeed,
indent_style: self.indent_style,
}
Expand Down

0 comments on commit a4583e8

Please sign in to comment.