Unify line size settings between ruff and the formatter (#6873)

This commit is contained in:
konsti 2023-08-28 08:44:56 +02:00 committed by GitHub
parent a6aa16630d
commit e615870659
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 227 additions and 190 deletions

View file

@ -1194,7 +1194,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
FormatElement::Tag(StartLineSuffix { reserved_width }) => {
self.state.line_width += reserved_width;
if self.state.line_width > self.options().print_width.into() {
if self.state.line_width > self.options().line_width.into() {
return Ok(Fits::No);
}
self.queue.skip_content(TagKind::LineSuffix);
@ -1320,7 +1320,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;
}
@ -1437,10 +1437,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, TabWidth, VecBuffer,
format_args, write, Document, FormatState, IndentStyle, LineWidth, Printed, TabWidth,
VecBuffer,
};
fn format(root: &dyn Format<SimpleFormatContext>) -> Printed {
@ -1592,7 +1593,7 @@ two lines`,
let options = PrinterOptions {
indent_style: IndentStyle::Tab,
tab_width: TabWidth::try_from(4).unwrap(),
print_width: PrintWidth::new(19),
line_width: LineWidth::try_from(19).unwrap(),
..PrinterOptions::default()
};
@ -1697,7 +1698,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();

View file

@ -7,7 +7,7 @@ pub struct PrinterOptions {
pub tab_width: TabWidth,
/// 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,
@ -27,14 +27,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
}
@ -66,10 +66,10 @@ impl PrinterOptions {
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PrintWidth(u32);
pub struct PrintWidth(u16);
impl PrintWidth {
pub fn new(width: u32) -> Self {
pub fn new(width: u16) -> Self {
Self(width)
}
}
@ -82,17 +82,17 @@ impl Default for PrintWidth {
impl From<LineWidth> for PrintWidth {
fn from(width: LineWidth) -> Self {
Self(u32::from(u16::from(width)))
}
}
impl From<PrintWidth> for usize {
fn from(width: PrintWidth) -> Self {
width.0 as usize
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
}