Skip to content

Commit

Permalink
feat: support line ending
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Mar 14, 2024
1 parent 6e159e8 commit bd532a6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 70 deletions.
1 change: 1 addition & 0 deletions crates/ruff_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}"#;
Expand Down
101 changes: 40 additions & 61 deletions crates/ruff_fmt_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -48,74 +46,46 @@ impl From<IndentStyle> for RuffIndentStyle {
}
}

#[derive(Clone, Deserialize, Serialize)]
pub struct IndentWidth(NonZeroU8);

impl FromStr for IndentWidth {
type Err = ParseIntError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
NonZeroU8::from_str(s).map(Self)
}
}

impl From<IndentWidth> for RuffIndentWidth {
fn from(value: IndentWidth) -> Self {
Self::try_from(value.0.get()).unwrap()
}
}

impl From<RuffIndentWidth> 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<u8> for IndentWidth {
type Error = TryFromIntError;

fn try_from(value: u8) -> Result<Self, Self::Error> {
NonZeroU8::try_from(value).map(Self)
impl From<LineEnding> 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<Self, Self::Err> {
NonZeroU16::from_str(s).map(Self)
}
}

impl From<LineWidth> for RuffLineWidth {
fn from(value: LineWidth) -> Self {
Self::try_from(value.0.get()).unwrap()
}
}

impl From<RuffLineWidth> for LineWidth {
fn from(value: RuffLineWidth) -> Self {
Self(NonZeroU16::try_from(value.value()).unwrap())
}
}

impl TryFrom<u16> for LineWidth {
type Error = TryFromIntError;

fn try_from(value: u16) -> Result<Self, Self::Error> {
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<IndentStyle>,
pub indent_width: Option<IndentWidth>,
pub line_width: Option<LineWidth>,
#[serde(alias = "indentWidth")]
pub indent_width: Option<NonZeroU8>,
#[serde(alias = "lineWidth")]
pub line_width: Option<NonZeroU16>,
#[serde(alias = "lineEnding")]
pub line_ending: Option<LineEnding>,

pub quote_style: Option<QuoteStyle>,
pub magic_trailing_comma: Option<MagicTrailingComma>,

Expand All @@ -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
Expand Down Expand Up @@ -171,6 +146,10 @@ impl From<Config> 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);
}
Expand Down
8 changes: 8 additions & 0 deletions crates/ruff_fmt_dprint/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 14 additions & 9 deletions crates/ruff_fmt_dprint/src/configuration/configuration.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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())
}
4 changes: 4 additions & 0 deletions crates/ruff_fmt_dprint/src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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);

Expand Down

0 comments on commit bd532a6

Please sign in to comment.