From bd532a6bcb34782bada739371d2149785dd904ce Mon Sep 17 00:00:00 2001 From: magic-akari Date: Thu, 14 Mar 2024 18:02:54 +0800 Subject: [PATCH] feat: support line ending --- crates/ruff_fmt/src/lib.rs | 1 + crates/ruff_fmt_config/src/lib.rs | 101 +++++++----------- crates/ruff_fmt_dprint/schema.json | 8 ++ .../src/configuration/configuration.rs | 23 ++-- .../src/configuration/resolve_config.rs | 4 + 5 files changed, 67 insertions(+), 70 deletions(-) diff --git a/crates/ruff_fmt/src/lib.rs b/crates/ruff_fmt/src/lib.rs index 20fabb9..557dfe2 100644 --- a/crates/ruff_fmt/src/lib.rs +++ b/crates/ruff_fmt/src/lib.rs @@ -29,6 +29,7 @@ export interface Config { indent_style?: "tab" | "space"; indent_width?: number; line_width?: number; + line_ending?: "lf" | "crlf"; quote_style?: "single" | "double"; magic_trailing_comma?: "respect" | "ignore"; }"#; diff --git a/crates/ruff_fmt_config/src/lib.rs b/crates/ruff_fmt_config/src/lib.rs index 62fc89c..d4a4458 100644 --- a/crates/ruff_fmt_config/src/lib.rs +++ b/crates/ruff_fmt_config/src/lib.rs @@ -1,12 +1,10 @@ use std::{ - num::{NonZeroU16, NonZeroU8, ParseIntError, TryFromIntError}, + num::{NonZeroU16, NonZeroU8}, path::Path, str::FromStr, }; -use ruff_formatter::{ - IndentStyle as RuffIndentStyle, IndentWidth as RuffIndentWidth, LineWidth as RuffLineWidth, -}; +use ruff_formatter::{printer::LineEnding as RuffLineEnding, IndentStyle as RuffIndentStyle}; use ruff_python_formatter::{MagicTrailingComma, PyFormatOptions, QuoteStyle}; use serde::{Deserialize, Serialize}; @@ -48,74 +46,46 @@ impl From for RuffIndentStyle { } } -#[derive(Clone, Deserialize, Serialize)] -pub struct IndentWidth(NonZeroU8); - -impl FromStr for IndentWidth { - type Err = ParseIntError; - - fn from_str(s: &str) -> Result { - NonZeroU8::from_str(s).map(Self) - } -} - -impl From for RuffIndentWidth { - fn from(value: IndentWidth) -> Self { - Self::try_from(value.0.get()).unwrap() - } -} - -impl From for IndentWidth { - fn from(value: RuffIndentWidth) -> Self { - Self::try_from(value.value() as u8).unwrap() - } +#[derive(Clone, Default, Deserialize, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum LineEnding { + #[default] + Lf, + CrLf, } -impl TryFrom for IndentWidth { - type Error = TryFromIntError; - - fn try_from(value: u8) -> Result { - NonZeroU8::try_from(value).map(Self) +impl From for RuffLineEnding { + fn from(value: LineEnding) -> Self { + match value { + LineEnding::Lf => Self::LineFeed, + LineEnding::CrLf => Self::CarriageReturnLineFeed, + } } } -#[derive(Clone, Deserialize, Serialize)] -pub struct LineWidth(NonZeroU16); - -impl FromStr for LineWidth { - type Err = ParseIntError; +impl FromStr for LineEnding { + type Err = &'static str; fn from_str(s: &str) -> Result { - NonZeroU16::from_str(s).map(Self) - } -} - -impl From for RuffLineWidth { - fn from(value: LineWidth) -> Self { - Self::try_from(value.0.get()).unwrap() - } -} - -impl From for LineWidth { - fn from(value: RuffLineWidth) -> Self { - Self(NonZeroU16::try_from(value.value()).unwrap()) - } -} - -impl TryFrom for LineWidth { - type Error = TryFromIntError; - - fn try_from(value: u16) -> Result { - NonZeroU16::try_from(value).map(Self) + match s { + "lf" => Ok(Self::Lf), + "crlf" => Ok(Self::CrLf), + _ => Err("Value not supported for LineEnding"), + } } } #[derive(Default, Clone, Deserialize, Serialize)] -#[serde(rename_all = "snake_case")] pub struct Config { + #[serde(alias = "indentStyle")] pub indent_style: Option, - pub indent_width: Option, - pub line_width: Option, + #[serde(alias = "indentWidth")] + pub indent_width: Option, + #[serde(alias = "lineWidth")] + pub line_width: Option, + #[serde(alias = "lineEnding")] + pub line_ending: Option, + pub quote_style: Option, pub magic_trailing_comma: Option, @@ -129,16 +99,21 @@ impl Config { self } - pub fn with_indent_width(mut self, indent_width: IndentWidth) -> Self { + pub fn with_indent_width(mut self, indent_width: NonZeroU8) -> Self { self.indent_width = Some(indent_width); self } - pub fn with_line_width(mut self, line_width: LineWidth) -> Self { + pub fn with_line_width(mut self, line_width: NonZeroU16) -> Self { self.line_width = Some(line_width); self } + pub fn with_line_ending(mut self, line_ending: LineEnding) -> Self { + self.line_ending = Some(line_ending); + self + } + pub fn with_quote_style(mut self, quote_style: QuoteStyle) -> Self { self.quote_style = Some(quote_style); self @@ -171,6 +146,10 @@ impl From for PyFormatOptions { config = config.with_line_width(line_width.into()); } + if let Some(line_ending) = value.line_ending { + config = config.with_line_ending(line_ending.into()); + } + if let Some(quote_style) = value.quote_style { config = config.with_quote_style(quote_style); } diff --git a/crates/ruff_fmt_dprint/schema.json b/crates/ruff_fmt_dprint/schema.json index 59969ff..7673ce1 100644 --- a/crates/ruff_fmt_dprint/schema.json +++ b/crates/ruff_fmt_dprint/schema.json @@ -25,6 +25,14 @@ "minimum": 1, "maximum": 320 }, + "lineEndings": { + "description": "The line endings to use.", + "default": "lf", + "enum": [ + "lf", + "crlf" + ] + }, "quoteStyle": { "description": "The preferred line width at which the formatter should wrap lines.", "default": "double", diff --git a/crates/ruff_fmt_dprint/src/configuration/configuration.rs b/crates/ruff_fmt_dprint/src/configuration/configuration.rs index ed1748d..a2c9c87 100644 --- a/crates/ruff_fmt_dprint/src/configuration/configuration.rs +++ b/crates/ruff_fmt_dprint/src/configuration/configuration.rs @@ -1,6 +1,8 @@ +use std::num::{NonZeroU16, NonZeroU8}; + use dprint_core::configuration::GlobalConfiguration; pub use ruff_fmt_config::Config as Configuration; -use ruff_fmt_config::{IndentStyle, IndentWidth, LineWidth}; +use ruff_fmt_config::IndentStyle; use ruff_formatter::FormatOptions; use ruff_python_formatter::PyFormatOptions; @@ -17,22 +19,25 @@ pub(crate) fn indent_style_from_global_config( pub(crate) fn indent_width_from_global_config( global_config: &GlobalConfiguration, default_ruff_config: &PyFormatOptions, -) -> IndentWidth { - global_config - .indent_width - .and_then(|indent_width| indent_width.try_into().ok()) - .unwrap_or_else(|| default_ruff_config.indent_width().into()) +) -> NonZeroU8 { + global_config.indent_width.and_then(|indent_width| indent_width.try_into().ok()).unwrap_or_else( + || { + let value = default_ruff_config.indent_width().value(); + + (value as u8).try_into().unwrap() + }, + ) } pub(crate) fn line_width_from_global_config( global_config: &GlobalConfiguration, default_ruff_config: &PyFormatOptions, -) -> LineWidth { +) -> NonZeroU16 { global_config .line_width .map(u16::try_from) .and_then(Result::ok) - .map(LineWidth::try_from) + .map(NonZeroU16::try_from) .and_then(Result::ok) - .unwrap_or_else(|| default_ruff_config.line_width().into()) + .unwrap_or_else(|| default_ruff_config.line_width().value().try_into().unwrap()) } diff --git a/crates/ruff_fmt_dprint/src/configuration/resolve_config.rs b/crates/ruff_fmt_dprint/src/configuration/resolve_config.rs index a56927d..cef44f9 100644 --- a/crates/ruff_fmt_dprint/src/configuration/resolve_config.rs +++ b/crates/ruff_fmt_dprint/src/configuration/resolve_config.rs @@ -3,6 +3,7 @@ use super::{ line_width_from_global_config, Configuration, }; use dprint_core::configuration::*; +use ruff_fmt_config::LineEnding; use ruff_python_formatter::{MagicTrailingComma, PyFormatOptions, QuoteStyle}; pub fn resolve_config( @@ -36,6 +37,8 @@ pub fn resolve_config( &mut diagnostics, ); + let line_ending = get_value(&mut config, "lineEnding", LineEnding::default(), &mut diagnostics); + let quote_style = get_value(&mut config, "quoteStyle", QuoteStyle::default(), &mut diagnostics); let magic_trailing_comma = get_value( @@ -51,6 +54,7 @@ pub fn resolve_config( .with_indent_style(indent_style) .with_indent_width(indent_width) .with_line_width(line_width) + .with_line_ending(line_ending) .with_quote_style(quote_style) .with_magic_trailing_comma(magic_trailing_comma);